Update Saqel Platform: 2026-08-28 15:13:58

This commit is contained in:
Hamza-Ayed
2026-08-28 15:13:58 +03:00
parent 1c39d4c9cb
commit 06461635bf
5 changed files with 116 additions and 88 deletions
+78 -40
View File
@@ -187,59 +187,97 @@ class AuthController
return;
}
// 2. Find or Create User
$user = Database::selectOne("SELECT * FROM users WHERE phone_hash = ? LIMIT 1", [$phoneHash]);
// 2. Find or Create Auth Identity (Central Multi-Persona Auth)
$identity = Database::selectOne("SELECT * FROM auth_identities WHERE phone_hash = ? LIMIT 1", [$phoneHash]);
if (!$user) {
// New user registration
$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
if (!$identity) {
$identityUuid = 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)
);
$encryptedPhone = Security::encrypt($cleanPhone);
$encryptedName = Security::encrypt($fullName ?: ($role === 'teacher' ? 'معلم جديد' : 'طالب جديد'));
$randomPassword = password_hash(bin2hex(random_bytes(16)), PASSWORD_BCRYPT);
$userId = Database::insert(
"INSERT INTO users (uuid, full_name, phone_number, phone_hash, password_hash, role, status, token_version) VALUES (?, ?, ?, ?, ?, ?, 'active', 1)",
[$uuid, $encryptedName, $encryptedPhone, $phoneHash, $randomPassword, $role]
$identityId = Database::insert(
"INSERT INTO auth_identities (uuid, phone_number, phone_hash, status, token_version) VALUES (?, ?, ?, 'active', 1)",
[$identityUuid, $encryptedPhone, $phoneHash]
);
$user = [
'id' => $userId,
'uuid' => $uuid,
'full_name' => $encryptedName,
'role' => $role,
$identity = [
'id' => $identityId,
'uuid' => $identityUuid,
'phone_number' => $encryptedPhone,
'phone_hash' => $phoneHash,
'status' => 'active',
'token_version' => 1,
'school_id' => null,
'token_version' => 1
];
}
$identityId = (int)$identity['id'];
$resolvedName = $fullName ?: ($role === 'teacher' ? 'الأستاذ المعتمد' : 'الطالب المتميز');
// Resolve or create specific Persona entity (Teacher, Student, Guardian)
if ($role === 'teacher') {
$teacher = Database::selectOne("SELECT * FROM teachers WHERE identity_id = ? LIMIT 1", [$identityId]);
if (!$teacher) {
$tUuid = 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));
$tId = Database::insert(
"INSERT INTO teachers (uuid, identity_id, full_name, specialization, bio, is_school_exclusive, is_marketplace_public)
VALUES (?, ?, ?, 'الرياضيات العلمي', 'معلم معتمد في منصة صَقِل', 0, 1)",
[$tUuid, $identityId, $resolvedName]
);
$teacher = ['id' => $tId, 'uuid' => $tUuid, 'full_name' => $resolvedName];
}
$user = [
'id' => $teacher['id'],
'uuid' => $teacher['uuid'],
'full_name' => $teacher['full_name'],
'role' => 'teacher',
'status' => 'active',
'token_version' => $identity['token_version'],
'school_id' => $teacher['school_id'] ?? null
];
} elseif ($role === 'guardian') {
$guardian = Database::selectOne("SELECT * FROM guardians WHERE identity_id = ? LIMIT 1", [$identityId]);
if (!$guardian) {
$gUuid = 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));
$gId = Database::insert(
"INSERT INTO guardians (uuid, identity_id, full_name) VALUES (?, ?, ?)",
[$gUuid, $identityId, $resolvedName]
);
$guardian = ['id' => $gId, 'uuid' => $gUuid, 'full_name' => $resolvedName];
}
$user = [
'id' => $guardian['id'],
'uuid' => $guardian['uuid'],
'full_name' => $guardian['full_name'],
'role' => 'guardian',
'status' => 'active',
'token_version' => $identity['token_version'],
'school_id' => null
];
} else {
// Verify role access if logging into specific portal
if ($role === 'teacher' && $user['role'] !== 'teacher' && $user['role'] !== 'super_admin') {
$response->status(403)->json([
'status' => 'error',
'message' => 'هذا الحساب مسجل كطالب وليس معلماً. يرجى الدخول من بوابة الطالب.'
]);
return;
}
if ($user['status'] === 'suspended') {
$response->status(403)->json([
'status' => 'error',
'message' => 'هذا الحساب معطل. يرجى مراجعة إدارة المنصة.'
]);
return;
}
// If name provided on existing profile, update if needed
if ($fullName && (empty($user['full_name']) || Security::decrypt($user['full_name']) === 'طالب جديد')) {
Database::query("UPDATE users SET full_name = ? WHERE id = ?", [Security::encrypt($fullName), $user['id']]);
// Student Role
$student = Database::selectOne("SELECT * FROM students WHERE identity_id = ? LIMIT 1", [$identityId]);
if (!$student) {
$sUuid = 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));
$natId = 'NAT' . substr($cleanPhone, -8);
$sId = Database::insert(
"INSERT INTO students (uuid, identity_id, national_id, full_name, grade_level, stream, is_school_sponsored)
VALUES (?, ?, ?, ?, 'tawjihi_2008', 'scientific', 0)",
[$sUuid, $identityId, $natId, $resolvedName]
);
$student = ['id' => $sId, 'uuid' => $sUuid, 'full_name' => $resolvedName, 'school_id' => null];
}
$user = [
'id' => $student['id'],
'uuid' => $student['uuid'],
'full_name' => $student['full_name'],
'role' => 'student',
'status' => 'active',
'token_version' => $identity['token_version'],
'school_id' => $student['school_id'] ?? null
];
}
// 3. Register / Update Device Fingerprint in user_devices