diff --git a/assets/js/edit_user.js b/assets/js/edit_user.js index 79d8ed1..dde9563 100644 --- a/assets/js/edit_user.js +++ b/assets/js/edit_user.js @@ -6,8 +6,8 @@ document.addEventListener('DOMContentLoaded', function () { const initialDepartmentId = departmentSelect.dataset.initial; const initialRoleId = roleSelect.dataset.initial; - function fetchDepartments(divisionId, callback) { - fetch(`api.php?action=get_departments&division_id=${divisionId}`) + function fetchDepartments(divisionId, selectedDepartmentId) { + return fetch(`api.php?action=get_departments&division_id=${divisionId}`) .then(response => response.json()) .then(data => { departmentSelect.innerHTML = ''; @@ -15,17 +15,22 @@ document.addEventListener('DOMContentLoaded', function () { const option = document.createElement('option'); option.value = department.id; option.textContent = department.name; - if (department.id == initialDepartmentId) { + if (department.id == selectedDepartmentId) { option.selected = true; } departmentSelect.appendChild(option); }); - if (callback) callback(); }); } - function fetchRoles(departmentId, callback) { - fetch(`api.php?action=get_roles&department_id=${departmentId}`) + function fetchRoles(departmentId, selectedRoleId) { + // Only fetch if a department is selected + if (!departmentId) { + roleSelect.innerHTML = ''; + return Promise.resolve(); // Return a resolved promise + } + + return fetch(`api.php?action=get_roles&department_id=${departmentId}`) .then(response => response.json()) .then(data => { roleSelect.innerHTML = ''; @@ -33,39 +38,33 @@ document.addEventListener('DOMContentLoaded', function () { const option = document.createElement('option'); option.value = role.id; option.textContent = role.name; - if (role.id == initialRoleId) { + if (role.id == selectedRoleId) { option.selected = true; } roleSelect.appendChild(option); }); - if (callback) callback(); }); } divisionSelect.addEventListener('change', function () { const divisionId = this.value; - departmentSelect.dataset.initial = ''; // Clear initial value on change - roleSelect.dataset.initial = ''; // Clear initial value on change - fetchDepartments(divisionId, () => { - // After loading departments, if there's a selected one, load its roles - if (departmentSelect.value) { - fetchRoles(departmentSelect.value); - } + fetchDepartments(divisionId, null).then(() => { + // After departments are loaded, fetch roles for the (now cleared) department selection + fetchRoles(departmentSelect.value, null); }); }); departmentSelect.addEventListener('change', function () { const departmentId = this.value; - roleSelect.dataset.initial = ''; // Clear initial value on change - fetchRoles(departmentId); + fetchRoles(departmentId, null); }); // Initial load if (divisionSelect.value) { - fetchDepartments(divisionSelect.value, () => { - if (initialDepartmentId) { - fetchRoles(initialDepartmentId); - } + fetchDepartments(divisionSelect.value, initialDepartmentId).then(() => { + // After initial departments are loaded and correct one is selected, + // fetch the roles for that department. + fetchRoles(departmentSelect.value, initialRoleId); }); } }); diff --git a/db/migrations/007_create_super_admin.sql b/db/migrations/007_create_super_admin.sql new file mode 100644 index 0000000..4699dd7 --- /dev/null +++ b/db/migrations/007_create_super_admin.sql @@ -0,0 +1,16 @@ +-- Create Super Admin Division, Department and Role if they do not exist +INSERT INTO divisions (name) SELECT 'Super Admin' WHERE NOT EXISTS (SELECT 1 FROM divisions WHERE name = 'Super Admin'); + +SET @division_id = (SELECT id FROM divisions WHERE name = 'Super Admin'); +INSERT INTO departments (name, division_id) SELECT 'Super Admin', @division_id WHERE NOT EXISTS (SELECT 1 FROM departments WHERE name = 'Super Admin'); + +SET @department_id = (SELECT id FROM departments WHERE name = 'Super Admin'); +INSERT INTO roles (name, department_id) SELECT 'Admin', @department_id WHERE NOT EXISTS (SELECT 1 FROM roles WHERE name = 'Admin'); + +-- Create Super Admin User +SET @role_id = (SELECT id FROM roles WHERE name = 'Admin' AND department_id = @department_id); +SET @hashed_password = '$2y$10$9.p6A8v.17O7.Q1yJgqjC.H9aJz/O1aB3F2aC6bF.p8aEwS2mS2W'; -- "password" + +INSERT INTO users (username, password, email, division_id, department_id, role_id) +SELECT 'superadmin', @hashed_password, 'superadmin@example.com', @division_id, @department_id, @role_id +WHERE NOT EXISTS (SELECT 1 FROM users WHERE username = 'superadmin'); \ No newline at end of file diff --git a/db/migrations/008_add_missing_columns_to_users.sql b/db/migrations/008_add_missing_columns_to_users.sql new file mode 100644 index 0000000..bedd9e0 --- /dev/null +++ b/db/migrations/008_add_missing_columns_to_users.sql @@ -0,0 +1,3 @@ +ALTER TABLE users +ADD COLUMN division_id INT, +ADD COLUMN department_id INT; diff --git a/edit_user.php b/edit_user.php index 6651308..af04cb1 100644 --- a/edit_user.php +++ b/edit_user.php @@ -38,7 +38,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { // Fetch user data along with their division, department, and role IDs - $stmt = $pdo->prepare("SELECT u.id, u.username, u.email, u.role_id, r.department_id, d.division_id FROM users u LEFT JOIN roles r ON u.role_id = r.id LEFT JOIN departments d ON r.department_id = d.id WHERE u.id = ?"); + $stmt = $pdo->prepare("SELECT id, username, email, role_id, department_id, division_id FROM users WHERE id = ?"); $stmt->execute([$user_id]); $user = $stmt->fetch(); if (!$user) { diff --git a/login.php b/login.php index d5e6bd1..caa0332 100644 --- a/login.php +++ b/login.php @@ -12,15 +12,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $error = 'Please fill in both fields.'; } else { try { - $stmt = db()->prepare("SELECT * FROM users WHERE username = ?"); + $stmt = db()->prepare("SELECT users.*, roles.name as role_name FROM users JOIN roles ON users.role_id = roles.id WHERE users.username = ?"); $stmt->execute([$username]); $user = $stmt->fetch(); if ($user && password_verify($password, $user['password'])) { $_SESSION['user_id'] = $user['id']; $_SESSION['username'] = $user['username']; - $_SESSION['role'] = $user['role']; - $_SESSION['department'] = $user['department']; + $_SESSION['role'] = $user['role_name']; header("Location: request_dashboard.php"); exit; } else { @@ -70,9 +69,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { -
Don't have an account? Register here
-