From 734d16aa81304cebcc583c3017fe2611ee3d8377 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Mon, 27 Oct 2025 13:06:57 +0000 Subject: [PATCH] v4 --- analysis.php | 18 +- dashboard.php | 2 +- db/migrations/002_create_users_table.sql | 7 + .../003_add_user_id_to_uploaded_files.sql | 3 + edit_user.php | 119 +++++++++++++ footer.php | 2 +- header.php | 9 +- index.php | 10 +- login.php | 22 ++- upload.php | 6 +- users.php | 159 ++++++++++++++++++ 11 files changed, 342 insertions(+), 15 deletions(-) create mode 100644 db/migrations/002_create_users_table.sql create mode 100644 db/migrations/003_add_user_id_to_uploaded_files.sql create mode 100644 edit_user.php create mode 100644 users.php diff --git a/analysis.php b/analysis.php index 4882666..a51f463 100644 --- a/analysis.php +++ b/analysis.php @@ -104,7 +104,23 @@ if (!isset($_SESSION['user_email'])) { query('SELECT id, original_filename, upload_time, status, uploaded_by FROM uploaded_files ORDER BY upload_time DESC'); + + $sql = 'SELECT id, original_filename, upload_time, status, uploaded_by FROM uploaded_files'; + + if ($_SESSION['user_role'] !== 'admin') { + $sql .= ' WHERE user_id = ?'; + } + + $sql .= ' ORDER BY upload_time DESC'; + + $stmt = $pdo->prepare($sql); + + if ($_SESSION['user_role'] !== 'admin') { + $stmt->execute([$_SESSION['user_id']]); + } else { + $stmt->execute(); + } + $files = $stmt->fetchAll(); if (empty($files)): diff --git a/dashboard.php b/dashboard.php index b18c5bf..0bcc158 100644 --- a/dashboard.php +++ b/dashboard.php @@ -13,7 +13,7 @@ if (!isset($_SESSION['user_email'])) {
- Welcome, ! You are now logged in. + Welcome, ! You are now logged in.
diff --git a/db/migrations/002_create_users_table.sql b/db/migrations/002_create_users_table.sql new file mode 100644 index 0000000..82906e4 --- /dev/null +++ b/db/migrations/002_create_users_table.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS `users` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `email` VARCHAR(255) NOT NULL UNIQUE, + `password` VARCHAR(255) NOT NULL, + `role` VARCHAR(50) NOT NULL DEFAULT 'user', + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); diff --git a/db/migrations/003_add_user_id_to_uploaded_files.sql b/db/migrations/003_add_user_id_to_uploaded_files.sql new file mode 100644 index 0000000..e07e790 --- /dev/null +++ b/db/migrations/003_add_user_id_to_uploaded_files.sql @@ -0,0 +1,3 @@ +ALTER TABLE `uploaded_files` +ADD COLUMN `user_id` INT, +ADD CONSTRAINT `fk_user_id` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL; \ No newline at end of file diff --git a/edit_user.php b/edit_user.php new file mode 100644 index 0000000..fb7de91 --- /dev/null +++ b/edit_user.php @@ -0,0 +1,119 @@ + 'danger', 'message' => 'Invalid input. Please check all fields.']; + } else { + // Prevent admin from changing their own role if they are the last admin + if ($userId == $_SESSION['user_id'] && $role !== 'admin') { + $stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE role = 'admin'"); + $stmt->execute(); + $adminCount = $stmt->fetchColumn(); + if ($adminCount <= 1) { + $feedback = ['type' => 'danger', 'message' => 'You cannot change your role as you are the only admin.']; + } + } + + if (empty($feedback)) { + $sql = "UPDATE users SET email = ?, role = ?"; + $params = [$email, $role]; + + if (!empty($password)) { + $sql .= ", password = ?"; + $params[] = password_hash($password, PASSWORD_DEFAULT); + } + + $sql .= " WHERE id = ?"; + $params[] = $userId; + + $stmt = $pdo->prepare($sql); + if ($stmt->execute($params)) { + header('Location: users.php?update=success'); + exit; + } else { + $feedback = ['type' => 'danger', 'message' => 'Failed to update user.']; + } + } + } +} + +// Fetch user data +$stmt = $pdo->prepare("SELECT id, email, role FROM users WHERE id = ?"); +$stmt->execute([$userId]); +$user = $stmt->fetch(); + +if (!$user) { + header('Location: users.php'); + exit; +} + +include 'header.php'; +?> + +
+
+

Edit User

+ Back to User List +
+ + + + + +
+
+
Editing User:
+
+
+
+ + +
+ + +
+
+ + +
+
+ + +
+ +
+
+
+
+ + diff --git a/footer.php b/footer.php index dfa5f5d..5dee881 100644 --- a/footer.php +++ b/footer.php @@ -1,6 +1,6 @@
- © E-PRTR+LCP Integrated Data Reporting. All rights reserved. + © IEPR Integrated Data Reporting. All rights reserved.
diff --git a/header.php b/header.php index cf624d6..0e4f89f 100644 --- a/header.php +++ b/header.php @@ -6,7 +6,7 @@ Integrated Environmental Management System - + @@ -24,7 +24,7 @@
- E-PRTR+LCP + IEPR +
+ + + +
+
+
Create New User
+
+
+
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
+
+
+ +
+
+
All Users
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
IDEmailRoleRegistered AtActions
No users found.
+ Edit +
+ + + +
+
+
+
+
+
+ + \ No newline at end of file