- Replaced all client-facing $e->getMessage() with generic error messages - Added error_log() with filename prefix to all catch blocks - Covered jsonError(), echo, and json_encode() response patterns - Also fixed 2 remaining display_errors=1 and add_invoice.php leak - Script-assisted fix for 75 files, manual fix for 12 remaining edge cases
101 lines
4.1 KiB
PHP
101 lines
4.1 KiB
PHP
<?php
|
|
/**
|
|
* Admin/Staff/add.php
|
|
* إضافة موظف جديد (أدمن أو خدمة عملاء) مع تشفير البيانات وحفظ بصمة الجهاز
|
|
*/
|
|
require_once __DIR__ . '/../../core/bootstrap.php';
|
|
|
|
$con = Database::get('main');
|
|
|
|
// التحقق من الصلاحيات: فقط المشرف العام يمكنه إضافة مشرفين جدد
|
|
$jwtService = new JwtService($redis);
|
|
$auth = $jwtService->authenticate();
|
|
$authRole = $auth->role ?? '';
|
|
|
|
$name = filterRequest("name");
|
|
$phone = filterRequest("phone");
|
|
$email = filterRequest("email");
|
|
$password = filterRequest("password");
|
|
$role = filterRequest("role"); // 'admin' or 'service'
|
|
|
|
// ✅ FIX H-01: تقييد إضافة المشرفين لـ super_admin فقط
|
|
if ($role === 'admin' && $authRole !== 'super_admin') {
|
|
jsonError("غير مصرح لك. فقط المشرف العام يمكنه إضافة مشرفين جدد.");
|
|
exit;
|
|
}
|
|
|
|
if ($authRole !== 'super_admin' && $authRole !== 'admin') {
|
|
jsonError("غير مصرح لك. فقط المشرفون يمكنهم إضافة موظفين.");
|
|
exit;
|
|
}
|
|
$fingerprint = filterRequest("fingerprint") ?: '';
|
|
$gender = filterRequest("gender") ?? 'Male';
|
|
$birthdate = filterRequest("birthdate") ?? date('Y-m-d');
|
|
$site = filterRequest("site") ?? 'main';
|
|
|
|
if (empty($name) || empty($password) || empty($role)) {
|
|
jsonError("Missing required fields (name, password, role).");
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
// تشفير البيانات الحساسة
|
|
$encName = $encryptionHelper->encryptData($name);
|
|
$encPhone = $encryptionHelper->encryptData($phone);
|
|
$encEmail = $encryptionHelper->encryptData($email);
|
|
|
|
// تشفير البصمة وهش البصمة (إذا تم إرسالها)
|
|
$encFp = $fingerprint ? $encryptionHelper->encryptData($fingerprint) : '';
|
|
$fpHash = $fingerprint ? hash('sha256', $fingerprint) : '';
|
|
$uniqueId = bin2hex(random_bytes(16));
|
|
|
|
if ($role === 'admin') {
|
|
// ✅ FIX R4: إزالة ALTER TABLE من كود الإنتاج — يجب تشغيل migration منفصل
|
|
// قبل استخدام هذا الكود، تأكد من تشغيل:
|
|
// ALTER TABLE adminUser ADD COLUMN IF NOT EXISTS phone VARCHAR(255) NULL AFTER name;
|
|
$sql = "INSERT INTO adminUser (id, fingerprint, fingerprint_hash, name, phone, password, role, created_at)
|
|
VALUES (:id, :fp, :fp_hash, :name, :phone, :pass, :role, NOW())";
|
|
$stmt = $con->prepare($sql);
|
|
$stmt->execute([
|
|
':id' => $uniqueId,
|
|
':fp' => $encFp,
|
|
':fp_hash' => $fpHash,
|
|
':name' => $encName,
|
|
':phone' => $encPhone,
|
|
':pass' => $hashedPassword,
|
|
':role' => $role
|
|
]);
|
|
} else {
|
|
// الإضافة لجدول المستخدمين (خدمة العملاء)
|
|
// أضفنا site و last_name (كقيمة افتراضية فارغة إذا لم تتوفر)
|
|
$sql = "INSERT INTO users (id, fingerprint, fingerprint_hash, phone, email, gender, password, birthdate, user_type, first_name, last_name, site, created_at)
|
|
VALUES (:id, :fp, :fp_hash, :phone, :email, :gender, :pass, :bdate, 'service', :fname, :lname, :site, NOW())";
|
|
$stmt = $con->prepare($sql);
|
|
$stmt->execute([
|
|
':id' => $uniqueId,
|
|
':fp' => $encFp,
|
|
':fp_hash' => $fpHash,
|
|
':phone' => $encPhone,
|
|
':email' => $encEmail,
|
|
':gender' => $gender,
|
|
':pass' => $hashedPassword,
|
|
':bdate' => $birthdate,
|
|
':fname' => $encName,
|
|
':lname' => '', // last_name is empty for now
|
|
':site' => $site
|
|
]);
|
|
}
|
|
|
|
if ($stmt->rowCount() > 0) {
|
|
jsonSuccess("Staff member added successfully.");
|
|
} else {
|
|
jsonError("Failed to add staff member.");
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
error_log("[Staff Add Error] " . $e->getMessage());
|
|
jsonError("An internal error occurred. Please try again later.");
|
|
}
|