Update: 2026-05-03 23:57:27
This commit is contained in:
@@ -17,7 +17,10 @@ $data = input();
|
||||
|
||||
$errors = Validator::validate($data, [
|
||||
'name' => 'required',
|
||||
'email' => 'required|email'
|
||||
'email' => 'required|email',
|
||||
'manager_name' => 'required',
|
||||
'manager_email' => 'required|email',
|
||||
'manager_password' => 'required'
|
||||
]);
|
||||
|
||||
if ($errors) {
|
||||
@@ -27,14 +30,51 @@ if ($errors) {
|
||||
$db = Database::getInstance();
|
||||
|
||||
try {
|
||||
$stmt = $db->prepare("INSERT INTO tenants (name, email, phone, status, created_at) VALUES (?, ?, ?, 'active', NOW())");
|
||||
$db->beginTransaction();
|
||||
|
||||
// Generate Tenant UUID in PHP so we can use it immediately
|
||||
$tenantId = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000,
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
||||
);
|
||||
|
||||
// 1. Create Tenant
|
||||
$stmt = $db->prepare("INSERT INTO tenants (id, name, email, phone, status, created_at) VALUES (?, ?, ?, ?, 'active', NOW())");
|
||||
$stmt->execute([
|
||||
$tenantId,
|
||||
$data['name'],
|
||||
$data['email'],
|
||||
$data['phone'] ?? null
|
||||
]);
|
||||
|
||||
json_success(null, 'تم إنشاء المكتب بنجاح');
|
||||
// Generate User UUID
|
||||
$userId = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000,
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
||||
);
|
||||
|
||||
// Encrypt sensitive user data
|
||||
$encryptedName = \App\Core\Encryption::encrypt($data['manager_name']);
|
||||
$encryptedEmail = \App\Core\Encryption::encrypt($data['manager_email']);
|
||||
$emailHash = hash('sha256', strtolower($data['manager_email']));
|
||||
|
||||
// 2. Create Initial Manager (Admin) for this Tenant
|
||||
$stmtUser = $db->prepare("INSERT INTO users (id, tenant_id, name, email, email_hash, password_hash, role, created_at) VALUES (?, ?, ?, ?, ?, ?, 'admin', NOW())");
|
||||
$stmtUser->execute([
|
||||
$userId,
|
||||
$tenantId,
|
||||
$encryptedName,
|
||||
$encryptedEmail,
|
||||
$emailHash,
|
||||
password_hash($data['manager_password'], PASSWORD_DEFAULT)
|
||||
]);
|
||||
|
||||
$db->commit();
|
||||
json_success(null, 'تم إنشاء المكتب ومدير المكتب بنجاح');
|
||||
} catch (\Exception $e) {
|
||||
json_error('حدث خطأ أثناء حفظ البيانات', 500);
|
||||
$db->rollBack();
|
||||
json_error('حدث خطأ أثناء حفظ البيانات: ' . $e->getMessage(), 500);
|
||||
}
|
||||
|
||||
|
||||
@@ -46,11 +46,22 @@ $encryptedName = Encryption::encrypt($data['name']);
|
||||
$encryptedEmail = Encryption::encrypt($data['email']);
|
||||
$emailHash = hash('sha256', strtolower($data['email'])); // For fast lookup during login
|
||||
|
||||
// 3. Determine Tenant ID
|
||||
$tenantId = null;
|
||||
if ($decoded['role'] === 'super_admin') {
|
||||
if (empty($data['tenant_id'])) {
|
||||
json_error('يجب اختيار المكتب المحاسبي', 422);
|
||||
}
|
||||
$tenantId = $data['tenant_id'];
|
||||
} else {
|
||||
$tenantId = $decoded['tenant_id'];
|
||||
}
|
||||
|
||||
// 4. Save to Database
|
||||
try {
|
||||
$stmt = $db->prepare("INSERT INTO users (tenant_id, name, email, email_hash, password_hash, role, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
||||
$stmt->execute([
|
||||
$decoded['tenant_id'],
|
||||
$tenantId,
|
||||
$encryptedName,
|
||||
$encryptedEmail,
|
||||
$emailHash,
|
||||
|
||||
Reference in New Issue
Block a user