first commit
This commit is contained in:
91
backend/Admin/Staff/add.php
Normal file
91
backend/Admin/Staff/add.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* Admin/Staff/add.php
|
||||
* إضافة موظف جديد (أدمن أو خدمة عملاء) مع تشفير البيانات وحفظ بصمة الجهاز
|
||||
*/
|
||||
require_once __DIR__ . '/../../core/bootstrap.php';
|
||||
|
||||
$con = Database::get('main');
|
||||
|
||||
// التحقق من الصلاحيات: فقط المشرفين يمكنهم الإضافة
|
||||
// إذا لم يكن هناك أي مدير في النظام، نسمح// تم تعطيل التحقق للسماح بإعادة التهيئة في مرحلة التطوير
|
||||
// $count = $con->query("SELECT COUNT(*) FROM adminUser")->fetchColumn();
|
||||
// if ($count > 0) die("Access Denied: Admin already initialized.");
|
||||
// $auth = JwtService::authenticate($redis);
|
||||
// if ($auth['role'] !== 'super_admin' && $auth['role'] !== 'admin') {
|
||||
// jsonError("Unauthorized. Only Admins can add staff.");
|
||||
// exit;
|
||||
// }
|
||||
|
||||
$name = filterRequest("name");
|
||||
$phone = filterRequest("phone");
|
||||
$email = filterRequest("email");
|
||||
$password = filterRequest("password");
|
||||
$role = filterRequest("role"); // 'admin' or 'service'
|
||||
$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') {
|
||||
// الإضافة لجدول المديرين
|
||||
$sql = "INSERT INTO adminUser (id, fingerprint, fingerprint_hash, name, password, role, created_at)
|
||||
VALUES (:id, :fp, :fp_hash, :name, :pass, :role, NOW())";
|
||||
$stmt = $con->prepare($sql);
|
||||
$stmt->execute([
|
||||
':id' => $uniqueId,
|
||||
':fp' => $encFp,
|
||||
':fp_hash' => $fpHash,
|
||||
':name' => $encName,
|
||||
':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("Server error: " . $e->getMessage());
|
||||
}
|
||||
Reference in New Issue
Block a user