Update Saqel Platform: 2026-08-28 15:13:58
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -28,17 +28,12 @@ class ChatController
|
||||
|
||||
$userId = (int)$request->user_id;
|
||||
|
||||
// 1. Fetch all other users in the system
|
||||
// 1. Fetch available teachers and students
|
||||
$users = [];
|
||||
try {
|
||||
$users = Database::select("
|
||||
SELECT u.id as user_id, u.uuid as user_uuid, u.full_name as encrypted_name, u.role as user_role, tp.specialization
|
||||
FROM users u
|
||||
LEFT JOIN teacher_profiles tp ON tp.user_id = u.id
|
||||
WHERE u.id != ?
|
||||
ORDER BY u.id DESC
|
||||
LIMIT 100
|
||||
", [$userId]);
|
||||
$teachers = Database::select("SELECT id as user_id, uuid as user_uuid, full_name as encrypted_name, 'teacher' as user_role, specialization FROM teachers WHERE id != ? LIMIT 50", [$userId]);
|
||||
$students = Database::select("SELECT id as user_id, uuid as user_uuid, full_name as encrypted_name, 'student' as user_role, 'طالب' as specialization FROM students WHERE id != ? LIMIT 50", [$userId]);
|
||||
$users = array_merge($teachers, $students);
|
||||
} catch (\Throwable $e) {
|
||||
error_log("Users select in getConversations error: " . $e->getMessage());
|
||||
}
|
||||
|
||||
@@ -17,46 +17,43 @@ class TeacherController
|
||||
*/
|
||||
public function profileStatus(Request $request, Response $response): void
|
||||
{
|
||||
$userId = $request->user_id;
|
||||
$teacherId = (int)$request->user_id;
|
||||
|
||||
$user = Database::selectOne("SELECT id, uuid, full_name, role, status FROM users WHERE id = ? LIMIT 1", [$userId]);
|
||||
if (!$user) {
|
||||
$response->status(404)->json(['status' => 'error', 'message' => 'المعلم غير موجود']);
|
||||
return;
|
||||
$teacher = Database::selectOne("SELECT id, uuid, full_name, specialization, bio, is_marketplace_public FROM teachers WHERE id = ? LIMIT 1", [$teacherId]);
|
||||
if (!$teacher) {
|
||||
// Auto create teacher record if not exists
|
||||
$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, full_name, specialization, bio, is_marketplace_public) VALUES (?, 'الأستاذ المعتمد', 'الرياضيات العلمي', 'معلم معتمد في منصة صَقِل', 1)",
|
||||
[$tUuid]
|
||||
);
|
||||
$teacher = [
|
||||
'id' => $tId,
|
||||
'uuid' => $tUuid,
|
||||
'full_name' => 'الأستاذ المعتمد',
|
||||
'specialization' => 'الرياضيات العلمي',
|
||||
'bio' => 'معلم معتمد في منصة صَقِل'
|
||||
];
|
||||
}
|
||||
|
||||
$profile = Database::selectOne("SELECT * FROM teacher_profiles WHERE user_id = ? LIMIT 1", [$userId]);
|
||||
if (!$profile) {
|
||||
try {
|
||||
Database::query(
|
||||
"INSERT INTO teacher_profiles (user_id, specialization, bio, is_verified) VALUES (?, 'المعلم المعتمد', 'معلم معتمد في منصة صَقِل', 1)
|
||||
ON DUPLICATE KEY UPDATE is_verified = 1",
|
||||
[$userId]
|
||||
);
|
||||
$profile = Database::selectOne("SELECT * FROM teacher_profiles WHERE user_id = ? LIMIT 1", [$userId]);
|
||||
} catch (\Exception $e) {
|
||||
error_log("Auto create teacher profile notice: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$rawName = (string)($user['full_name'] ?? '');
|
||||
$rawName = (string)($teacher['full_name'] ?? '');
|
||||
$decryptedName = Security::decrypt($rawName) ?: $rawName;
|
||||
if (empty($decryptedName)) {
|
||||
$decryptedName = 'معلم صَقِل المعتمد';
|
||||
$decryptedName = 'الأستاذ المعتمد';
|
||||
}
|
||||
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'is_completed' => true,
|
||||
'user' => [
|
||||
'uuid' => $user['uuid'],
|
||||
'uuid' => $teacher['uuid'],
|
||||
'full_name' => $decryptedName,
|
||||
'role' => $user['role'],
|
||||
'status' => $user['status'],
|
||||
'role' => 'teacher',
|
||||
'status' => 'active',
|
||||
],
|
||||
'profile' => $profile ?: [
|
||||
'specialization' => 'المعلم المعتمد',
|
||||
'bio' => 'معلم معتمد في منصة صَقِل'
|
||||
'profile' => [
|
||||
'specialization' => $teacher['specialization'] ?? 'الرياضيات العلمي',
|
||||
'bio' => $teacher['bio'] ?? 'معلم معتمد في منصة صَقِل'
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user