'البريد الوارد', 'outbound' => 'البريد الصادر', 'internal' => 'البريد الداخلي', 'users' => 'إدارة المستخدمين', 'settings' => 'الإعدادات', 'reports' => 'التقارير' ]; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $action = $_POST['action'] ?? ''; $username = $_POST['username'] ?? ''; $full_name = $_POST['full_name'] ?? ''; $role = $_POST['role'] ?? 'staff'; $password = $_POST['password'] ?? ''; $id = $_POST['id'] ?? 0; // Global permissions (legacy/fallback) $can_view = isset($_POST['can_view_global']) ? 1 : 0; $can_add = isset($_POST['can_add_global']) ? 1 : 0; $can_edit = isset($_POST['can_edit_global']) ? 1 : 0; $can_delete = isset($_POST['can_delete_global']) ? 1 : 0; if ($action === 'add') { if (!canAdd('users')) redirect('users.php'); if ($username && $password && $full_name) { $hashed_password = password_hash($password, PASSWORD_DEFAULT); try { $pdo = db(); $pdo->beginTransaction(); $stmt = $pdo->prepare("INSERT INTO users (username, password, full_name, role, can_view, can_add, can_edit, can_delete) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$username, $hashed_password, $full_name, $role, $can_view, $can_add, $can_edit, $can_delete]); $user_id = $pdo->lastInsertId(); // Save page permissions $perm_stmt = $pdo->prepare("INSERT INTO user_permissions (user_id, page, can_view, can_add, can_edit, can_delete) VALUES (?, ?, ?, ?, ?, ?)"); foreach ($modules as $module => $label) { $m_view = isset($_POST["perm_{$module}_view"]) ? 1 : 0; $m_add = isset($_POST["perm_{$module}_add"]) ? 1 : 0; $m_edit = isset($_POST["perm_{$module}_edit"]) ? 1 : 0; $m_delete = isset($_POST["perm_{$module}_delete"]) ? 1 : 0; $perm_stmt->execute([$user_id, $module, $m_view, $m_add, $m_edit, $m_delete]); } $pdo->commit(); $_SESSION['success'] = 'تم إضافة المستخدم بنجاح'; redirect('users.php'); } catch (PDOException $e) { if (isset($pdo)) $pdo->rollBack(); if ($e->getCode() == 23000) { $error = 'اسم المستخدم موجود مسبقاً'; } else { $error = 'حدث خطأ: ' . $e->getMessage(); } } } else { $error = 'يرجى ملء جميع الحقول المطلوبة'; } } elseif ($action === 'edit') { if (!canEdit('users')) redirect('users.php'); if ($username && $full_name && $id) { try { $pdo = db(); $pdo->beginTransaction(); if ($password) { $hashed_password = password_hash($password, PASSWORD_DEFAULT); $stmt = $pdo->prepare("UPDATE users SET username = ?, full_name = ?, role = ?, password = ?, can_view = ?, can_add = ?, can_edit = ?, can_delete = ? WHERE id = ?"); $stmt->execute([$username, $full_name, $role, $hashed_password, $can_view, $can_add, $can_edit, $can_delete, $id]); } else { $stmt = $pdo->prepare("UPDATE users SET username = ?, full_name = ?, role = ?, can_view = ?, can_add = ?, can_edit = ?, can_delete = ? WHERE id = ?"); $stmt->execute([$username, $full_name, $role, $can_view, $can_add, $can_edit, $can_delete, $id]); } // Update page permissions $pdo->prepare("DELETE FROM user_permissions WHERE user_id = ?")->execute([$id]); $perm_stmt = $pdo->prepare("INSERT INTO user_permissions (user_id, page, can_view, can_add, can_edit, can_delete) VALUES (?, ?, ?, ?, ?, ?)"); foreach ($modules as $module => $label) { $m_view = isset($_POST["perm_{$module}_view"]) ? 1 : 0; $m_add = isset($_POST["perm_{$module}_add"]) ? 1 : 0; $m_edit = isset($_POST["perm_{$module}_edit"]) ? 1 : 0; $m_delete = isset($_POST["perm_{$module}_delete"]) ? 1 : 0; $perm_stmt->execute([$id, $module, $m_view, $m_add, $m_edit, $m_delete]); } $pdo->commit(); // Refresh own session if editing self if ($id == $_SESSION['user_id']) { unset($_SESSION['permissions']); } $_SESSION['success'] = 'تم تحديث بيانات المستخدم بنجاح'; redirect('users.php'); } catch (PDOException $e) { if (isset($pdo)) $pdo->rollBack(); $error = 'حدث خطأ: ' . $e->getMessage(); } } else { $error = 'يرجى ملء جميع الحقول المطلوبة'; } } } if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) { if (!canDelete('users')) redirect('users.php'); if ($_GET['id'] != $_SESSION['user_id']) { $stmt = db()->prepare("DELETE FROM users WHERE id = ?"); $stmt->execute([$_GET['id']]); $_SESSION['success'] = 'تم حذف المستخدم بنجاح'; redirect('users.php'); } else { $error = 'لا يمكنك حذف حسابك الحالي'; } } // Get session messages if (isset($_SESSION['success'])) { $success = $_SESSION['success']; unset($_SESSION['success']); } if (isset($_SESSION['error'])) { $error = $_SESSION['error']; unset($_SESSION['error']); } $stmt = db()->query("SELECT * FROM users ORDER BY created_at DESC"); $users = $stmt->fetchAll(); // Fetch permissions for all users $user_perms = []; $perm_stmt = db()->query("SELECT * FROM user_permissions"); while ($row = $perm_stmt->fetch()) { $user_perms[$row['user_id']][$row['page']] = $row; } // Handle Deep Link for Edit $deepLinkData = null; if (isset($_GET['action']) && $_GET['action'] === 'edit' && isset($_GET['id'])) { $stmt = db()->prepare("SELECT * FROM users WHERE id = ?"); $stmt->execute([$_GET['id']]); $deepLinkData = $stmt->fetch(); if ($deepLinkData) { $deepLinkData['page_permissions'] = $user_perms[$deepLinkData['id']] ?? []; } } ?>
| الاسم الكامل | اسم المستخدم | الدور | تاريخ الإنشاء | الإجراءات |
|---|---|---|---|---|
| = htmlspecialchars($user['full_name']) ?> | = htmlspecialchars($user['username']) ?> | مدير كاتب موظف | = $user['created_at'] ?> | حذف |