Files
Siro/backend/transit/admin/login_verify.php
T
Hamza-AyedandClaude Opus 5 8d7e3118b5 Migrate remaining encrypted-column lookups to the blind index
Completes the set of queries that matched a freshly encrypted value against a
stored one, which only works while encryption is deterministic. Each keeps its
original comparison and adds an index comparison in the same WHERE, so nothing
changes today.

- passenger sign-in by email, service-staff sign-in, Firebase token lookup
- driver lookup by phone and by national number
- admin ride lookup and ride monitor (both tables)
- nabeh: driver status, user resolution, ride history, complaint submission

transit_org_admins lives in the transit database and has no index column, so
login there falls back to decrypting the small set of active admins and
comparing normalised numbers.

Schema: adds users.email_bidx/phone_bidx and driver.national_bidx with their
indexes.

Verified that every :*_bidx placeholder introduced is actually bound — an
unbound one is a fatal error at request time, not a silent miss.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-25 16:36:32 +03:00

74 lines
2.6 KiB
PHP

<?php
// transit/admin/login_verify.php — الخطوة 2: OTP → session token
// POST: phone, otp
require_once __DIR__ . '/../../core/bootstrap.php';
require_once __DIR__ . '/../functions.php';
// Rate limiting
$limiter = new RateLimiter($redis);
$limiter->enforce(RateLimiter::identifier(), 'api');
try { $transit_con = Database::get('transit'); }
catch (Exception $e) { jsonError('Transit service unavailable', 503); }
requireTransitFields(['phone', 'otp']);
$phone = normalizePhone(filterRequest('phone'));
$otp = preg_replace('/\D/', '', filterRequest('otp'));
if (!transitVerifyAdminOtp($phone, $otp)) jsonError('Invalid or expired OTP', 401);
$phoneEnc = $encryptionHelper->encryptData($phone);
// transit_org_admins يعيش في قاعدة المواصلات ولا يملك عمود فهرس بعد،
// فتتم المطابقة على الرقم الأصلي بعد فك التشفير عند فشل المقارنة المباشرة.
$phoneNorm = normalizePhone($phone);
$st = $transit_con->prepare(
"SELECT a.id, a.org_id, a.name, a.role, a.permissions,
o.name_ar, o.name_en, o.type, o.contract_status, o.logo_url
FROM transit_org_admins a
JOIN transit_orgs o ON o.id = a.org_id
WHERE a.phone = ? AND a.is_active = 1 LIMIT 1"
);
$st->execute([$phoneEnc]);
$admin = $st->fetch();
if (!$admin) {
// تحت التشفير العشوائي لا تتطابق النصوص المشفّرة، فنقارن الأرقام الأصلية.
$all = $transit_con->query(
"SELECT a.id, a.phone, o.contract_status
FROM transit_org_admins a
JOIN transit_orgs o ON o.id = a.org_id
WHERE a.is_active = 1"
)->fetchAll(PDO::FETCH_ASSOC);
foreach ($all as $row) {
$plain = $encryptionHelper->decryptData($row['phone'] ?? null);
if ($plain && normalizePhone($plain) === $phoneNorm) {
$admin = $row;
break;
}
}
}
if (!$admin) jsonError('Admin not found', 401);
$token = transitCreateSession((int)$admin['id'], (int)$admin['org_id']);
jsonSuccess([
'token' => $token,
'expires_in' => 86400,
'admin' => [
'id' => (int)$admin['id'],
'name' => $admin['name'],
'role' => $admin['role'],
'permissions' => json_decode($admin['permissions'] ?? '{}', true),
],
'org' => [
'id' => (int)$admin['org_id'],
'name_ar' => $admin['name_ar'],
'name_en' => $admin['name_en'],
'type' => $admin['type'],
'contract_status' => $admin['contract_status'],
'logo_url' => $admin['logo_url'],
],
], 'Login successful');