From 19ec351a0250712af96434ce0219dc77e87a6e59 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Tue, 7 Apr 2026 18:42:59 +0000 Subject: [PATCH] update migrations --- admin.php | 16 ++++++++++++++++ admin_roles.php | 10 ++++++++++ db/migrations/schema.sql | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/admin.php b/admin.php index 8806896..4567310 100644 --- a/admin.php +++ b/admin.php @@ -4,6 +4,22 @@ require_once __DIR__ . '/includes/app.php'; require_once __DIR__ . '/includes/auth.php'; require_login(); +// Auto-migrate roles if needed +try { + db()->query('SELECT 1 FROM roles LIMIT 1'); +} catch (Exception $e) { + if (!isset($_GET['migrated'])) { + ob_start(); + require_once __DIR__ . '/db/migrations/migrate_roles.php'; + ob_end_clean(); + $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], '?') !== false ? '&' : '?') . 'migrated=1'; + header('Location: ' . $url); + exit; + } else { + die('Auto-migration failed. Please run /db/migrations/migrate_roles.php manually or contact support.'); + } +} + $page = $_GET['page'] ?? 'dashboard'; $action = $_GET['action'] ?? ''; diff --git a/admin_roles.php b/admin_roles.php index 81c3387..a7575df 100644 --- a/admin_roles.php +++ b/admin_roles.php @@ -3,6 +3,16 @@ require_once __DIR__ . '/includes/app.php'; require_once __DIR__ . '/includes/auth.php'; +try { + db()->query('SELECT 1 FROM roles LIMIT 1'); +} catch (Exception $e) { + ob_start(); + require_once __DIR__ . '/db/migrations/migrate_roles.php'; + ob_end_clean(); + header('Location: ' . app_url('admin.php', ['page' => 'roles'])); + exit; +} + require_permission('roles', 'view'); $pages = [ diff --git a/db/migrations/schema.sql b/db/migrations/schema.sql index d117422..85bf8c7 100644 --- a/db/migrations/schema.sql +++ b/db/migrations/schema.sql @@ -345,6 +345,7 @@ CREATE TABLE `users` ( `reset_token` varchar(255) DEFAULT NULL, `reset_expires` datetime DEFAULT NULL, `role` enum('admin','user') DEFAULT 'admin', + `role_id` int(11) DEFAULT NULL, `created_at` timestamp NULL DEFAULT current_timestamp(), `phone` varchar(50) DEFAULT NULL, `profile_picture` varchar(255) DEFAULT NULL, @@ -362,4 +363,39 @@ CREATE TABLE `users` ( /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + + +-- +-- Table structure for table `roles` +-- + +DROP TABLE IF EXISTS `roles`; +CREATE TABLE `roles` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) NOT NULL, + `description` text DEFAULT NULL, + `is_system` tinyint(1) DEFAULT 0, + `created_at` timestamp NULL DEFAULT current_timestamp(), + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- +-- Table structure for table `role_permissions` +-- + +DROP TABLE IF EXISTS `role_permissions`; +CREATE TABLE `role_permissions` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `role_id` int(11) NOT NULL, + `page` varchar(50) NOT NULL, + `can_view` tinyint(1) DEFAULT 0, + `can_add` tinyint(1) DEFAULT 0, + `can_edit` tinyint(1) DEFAULT 0, + `can_delete` tinyint(1) DEFAULT 0, + PRIMARY KEY (`id`), + UNIQUE KEY `role_page` (`role_id`,`page`), + KEY `role_id` (`role_id`), + CONSTRAINT `role_permissions_ibfk_1` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + -- Dump completed on 2026-04-07 13:32:08