Skip to content

Commit 9b6ea85

Browse files
committed
Removed the prepended user_ from the fields in the user_roles table, moved user_role_id from user_settings directly to users table, rename table user_permissions to user_client_permissions, removed unused Sessions vars in login. This upedate will require to update using update_cli.php --db_update
1 parent 3804e18 commit 9b6ea85

21 files changed

+156
-132
lines changed

admin_role.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
// Default Column Sortby Filter
4-
$sort = "user_role_is_admin";
4+
$sort = "role_is_admin";
55
$order = "DESC";
66

77
require_once "includes/inc_all_admin.php";
@@ -13,8 +13,8 @@
1313
$sql = mysqli_query(
1414
$mysqli,
1515
"SELECT SQL_CALC_FOUND_ROWS * FROM user_roles
16-
WHERE (user_roles.user_role_name LIKE '%$q%' OR user_roles.user_role_description LIKE '%$q%')
17-
AND user_roles.user_role_archived_at IS NULL
16+
WHERE (role_name LIKE '%$q%' OR role_description LIKE '%$q%')
17+
AND role_archived_at IS NULL
1818
ORDER BY $sort $order LIMIT $record_from, $record_to"
1919
);
2020

@@ -53,14 +53,14 @@
5353
<thead class="text-dark <?php if ($num_rows[0] == 0) { echo "d-none"; } ?>">
5454
<tr>
5555
<th>
56-
<a class="text-dark" href="?<?php echo $url_query_strings_sort; ?>&sort=user_role_name&order=<?php echo $disp; ?>">
57-
Role <?php if ($sort == 'user_role_name') { echo $order_icon; } ?>
56+
<a class="text-dark" href="?<?php echo $url_query_strings_sort; ?>&sort=role_name&order=<?php echo $disp; ?>">
57+
Role <?php if ($sort == 'role_name') { echo $order_icon; } ?>
5858
</a>
5959
</th>
6060
<th>Members</th>
6161
<th>
62-
<a class="text-dark" href="?<?php echo $url_query_strings_sort; ?>&sort=user_role_is_admin&order=<?php echo $disp; ?>">
63-
Admin <?php if ($sort == 'user_role_is_admin') { echo $order_icon; } ?>
62+
<a class="text-dark" href="?<?php echo $url_query_strings_sort; ?>&sort=role_is_admin&order=<?php echo $disp; ?>">
63+
Admin <?php if ($sort == 'role_is_admin') { echo $order_icon; } ?>
6464
</a>
6565
</th>
6666
<th class="text-center">Action</th>
@@ -70,17 +70,17 @@
7070
<?php
7171

7272
while ($row = mysqli_fetch_array($sql)) {
73-
$role_id = intval($row['user_role_id']);
74-
$role_name = nullable_htmlentities($row['user_role_name']);
75-
$role_description = nullable_htmlentities($row['user_role_description']);
76-
$role_admin = intval($row['user_role_is_admin']);
77-
$role_archived_at = nullable_htmlentities($row['user_role_archived_at']);
73+
$role_id = intval($row['role_id']);
74+
$role_name = nullable_htmlentities($row['role_name']);
75+
$role_description = nullable_htmlentities($row['role_description']);
76+
$role_admin = intval($row['role_is_admin']);
77+
$role_archived_at = nullable_htmlentities($row['role_archived_at']);
7878

7979
// Count number of users that have each role
80-
$sql_role_user_count = mysqli_query($mysqli, "SELECT COUNT(users.user_id) FROM users LEFT JOIN user_settings on users.user_id = user_settings.user_id WHERE user_role = $role_id AND user_archived_at IS NULL");
80+
$sql_role_user_count = mysqli_query($mysqli, "SELECT COUNT(user_id) FROM users WHERE user_role_id = $role_id AND user_archived_at IS NULL");
8181
$role_user_count = mysqli_fetch_row($sql_role_user_count)[0];
8282

83-
$sql_users = mysqli_query($mysqli, "SELECT * FROM users LEFT JOIN user_settings on users.user_id = user_settings.user_id WHERE user_role = $role_id AND user_archived_at IS NULL");
83+
$sql_users = mysqli_query($mysqli, "SELECT * FROM users WHERE user_role_id = $role_id AND user_archived_at IS NULL");
8484
// Initialize an empty array to hold user names
8585
$user_names = [];
8686

@@ -90,7 +90,7 @@
9090
}
9191

9292
// Convert the array of user names to a comma-separated string
93-
$user_names_string = implode(",", $user_names) ;
93+
$user_names_string = implode(",", $user_names);
9494

9595
if (empty($user_names_string)) {
9696
$user_names_string = "-";

admin_user.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
$mysqli,
1515
"SELECT SQL_CALC_FOUND_ROWS * FROM users, user_settings, user_roles
1616
WHERE users.user_id = user_settings.user_id
17-
AND user_settings.user_role = user_roles.user_role_id
17+
AND user_role_id = role_id
1818
AND (user_name LIKE '%$q%' OR user_email LIKE '%$q%')
1919
AND user_archived_at IS NULL
2020
ORDER BY $sort $order LIMIT $record_from, $record_to"
@@ -77,8 +77,8 @@
7777
</a>
7878
</th>
7979
<th>
80-
<a class="text-dark" href="?<?php echo $url_query_strings_sort; ?>&sort=user_role&order=<?php echo $disp; ?>">
81-
Role <?php if ($sort == 'user_role') { echo $order_icon; } ?>
80+
<a class="text-dark" href="?<?php echo $url_query_strings_sort; ?>&sort=role_name&order=<?php echo $disp; ?>">
81+
Role <?php if ($sort == 'role_name') { echo $order_icon; } ?>
8282
</a>
8383
</th>
8484
<th>
@@ -116,8 +116,8 @@
116116
$mfa_status_display = "<i class='fas fa-fw fa-lock text-success'></i>";
117117
}
118118
$user_config_force_mfa = intval($row['user_config_force_mfa']);
119-
$user_role = $row['user_role'];
120-
$user_role_display = nullable_htmlentities($row['user_role_name']);
119+
$user_role = $row['user_role_id'];
120+
$user_role_display = nullable_htmlentities($row['role_name']);
121121
$user_initials = nullable_htmlentities(initials($user_name));
122122

123123
$sql_last_login = mysqli_query(
@@ -139,7 +139,7 @@
139139
}
140140

141141
// Get User Client Access Permissions
142-
$user_client_access_sql = mysqli_query($mysqli,"SELECT client_id FROM user_permissions WHERE user_id = $user_id");
142+
$user_client_access_sql = mysqli_query($mysqli,"SELECT client_id FROM user_client_permissions WHERE user_id = $user_id");
143143
$client_access_array = [];
144144
while ($row = mysqli_fetch_assoc($user_client_access_sql)) {
145145
$client_access_array[] = intval($row['client_id']);

ajax/ajax_project_edit.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@
7474
<?php
7575
$sql_project_managers_select = mysqli_query(
7676
$mysqli,
77-
"SELECT users.user_id, user_name FROM users
78-
LEFT JOIN user_settings on users.user_id = user_settings.user_id
79-
WHERE user_role > 1 AND user_status = 1 AND user_archived_at IS NULL ORDER BY user_name ASC"
77+
"SELECT user_id, user_name FROM users
78+
WHERE user_role_id > 1 AND user_status = 1 AND user_archived_at IS NULL ORDER BY user_name ASC"
8079
);
8180
while ($row = mysqli_fetch_array($sql_project_managers_select)) {
8281
$user_id_select = intval($row['user_id']);

ajax/ajax_role_edit.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
$role_id = intval($_GET['id']);
66

7-
$sql = mysqli_query($mysqli, "SELECT * FROM user_roles WHERE user_role_id = $role_id LIMIT 1");
7+
$sql = mysqli_query($mysqli, "SELECT * FROM user_roles WHERE role_id = $role_id LIMIT 1");
88

99
$row = mysqli_fetch_array($sql);
10-
$role_name = nullable_htmlentities($row['user_role_name']);
11-
$role_description = nullable_htmlentities($row['user_role_description']);
12-
$role_admin = intval($row['user_role_is_admin']);
10+
$role_name = nullable_htmlentities($row['role_name']);
11+
$role_description = nullable_htmlentities($row['role_description']);
12+
$role_admin = intval($row['role_is_admin']);
1313

1414
// Count number of users that have each role
15-
$sql_role_user_count = mysqli_query($mysqli, "SELECT COUNT(users.user_id) FROM users LEFT JOIN user_settings on users.user_id = user_settings.user_id WHERE user_role = $role_id AND user_archived_at IS NULL");
15+
$sql_role_user_count = mysqli_query($mysqli, "SELECT COUNT(user_id) FROM users WHERE user_role_id = $role_id AND user_archived_at IS NULL");
1616
$role_user_count = mysqli_fetch_row($sql_role_user_count)[0];
1717

18-
$sql_users = mysqli_query($mysqli, "SELECT * FROM users LEFT JOIN user_settings on users.user_id = user_settings.user_id WHERE user_role = $role_id AND user_archived_at IS NULL");
18+
$sql_users = mysqli_query($mysqli, "SELECT * FROM users WHERE user_role_id = $role_id AND user_archived_at IS NULL");
1919
// Initialize an empty array to hold user names
2020
$user_names = [];
2121

ajax/ajax_user_edit.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
$user_avatar = nullable_htmlentities($row['user_avatar']);
1313
$user_token = nullable_htmlentities($row['user_token']);
1414
$user_config_force_mfa = intval($row['user_config_force_mfa']);
15-
$user_role = intval($row['user_role']);
15+
$user_role_id = intval($row['user_role_id']);
1616
$user_initials = nullable_htmlentities(initials($user_name));
1717

1818
// Get User Client Access Permissions
19-
$user_client_access_sql = mysqli_query($mysqli,"SELECT client_id FROM user_permissions WHERE user_id = $user_id");
19+
$user_client_access_sql = mysqli_query($mysqli,"SELECT client_id FROM user_client_permissions WHERE user_id = $user_id");
2020
$client_access_array = [];
2121
while ($row = mysqli_fetch_assoc($user_client_access_sql)) {
2222
$client_access_array[] = intval($row['client_id']);
@@ -107,13 +107,13 @@
107107
</div>
108108
<select class="form-control select2" name="role" required>
109109
<?php
110-
$sql_user_roles = mysqli_query($mysqli, "SELECT * FROM user_roles WHERE user_role_archived_at IS NULL");
110+
$sql_user_roles = mysqli_query($mysqli, "SELECT * FROM user_roles WHERE role_archived_at IS NULL");
111111
while ($row = mysqli_fetch_array($sql_user_roles)) {
112-
$user_role_id = intval($row['user_role_id']);
113-
$user_role_name = nullable_htmlentities($row['user_role_name']);
112+
$role_id = intval($row['role_id']);
113+
$role_name = nullable_htmlentities($row['role_name']);
114114

115115
?>
116-
<option <?php if ($user_role == $user_role_id) {echo "selected";} ?> value="<?php echo $user_role_id; ?>"><?php echo $user_role_name; ?></option>
116+
<option <?php if ($role_id == $user_role_id) {echo "selected";} ?> value="<?php echo $role_id; ?>"><?php echo $role_name; ?></option>
117117
<?php } ?>
118118

119119
</select>

check_login.php

+16-12
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,11 @@
2727
exit;
2828
}
2929

30-
// Check user type
31-
if ($_SESSION['user_type'] !== 1) {
32-
header("Location: login.php");
33-
exit();
34-
}
35-
3630
// Set Timezone
3731
require_once "inc_set_timezone.php";
3832

3933

40-
// User IP & UA
34+
// User Vars and User Settings
4135
$session_ip = sanitizeInput(getIP());
4236
$session_user_agent = sanitizeInput($_SERVER['HTTP_USER_AGENT']);
4337

@@ -47,24 +41,34 @@
4741
$mysqli,
4842
"SELECT * FROM users
4943
LEFT JOIN user_settings ON users.user_id = user_settings.user_id
50-
LEFT JOIN user_roles ON user_settings.user_role = user_roles.user_role_id
44+
LEFT JOIN user_roles ON user_role_id = role_id
5145
WHERE users.user_id = $session_user_id");
5246

5347
$row = mysqli_fetch_array($sql);
5448
$session_name = sanitizeInput($row['user_name']);
5549
$session_email = $row['user_email'];
5650
$session_avatar = $row['user_avatar'];
5751
$session_token = $row['user_token']; // MFA Token
58-
$session_user_role = intval($row['user_role']);
59-
$session_user_role_display = sanitizeInput($row['user_role_name']);
60-
if (isset($row['user_role_is_admin']) && $row['user_role_is_admin'] == 1) {
52+
$session_user_type = intval($row['user_type']);
53+
$session_user_role = intval($row['user_role_id']);
54+
$session_user_role_display = sanitizeInput($row['role_name']);
55+
if (isset($row['role_is_admin']) && $row['role_is_admin'] == 1) {
6156
$session_is_admin = true;
6257
} else {
6358
$session_is_admin = false;
6459
}
6560
$session_user_config_force_mfa = intval($row['user_config_force_mfa']);
6661
$user_config_records_per_page = intval($row['user_config_records_per_page']);
6762

63+
// Check user type
64+
if ($session_user_type !== 1) {
65+
session_unset();
66+
session_destroy();
67+
header("Location: login.php");
68+
exit();
69+
}
70+
71+
// Company Vars and Company Settings
6872
$sql = mysqli_query($mysqli, "SELECT * FROM companies, settings WHERE settings.company_id = companies.company_id AND companies.company_id = 1");
6973
$row = mysqli_fetch_array($sql);
7074

@@ -78,7 +82,7 @@
7882
$currency_format = numfmt_create($session_company_locale, NumberFormatter::CURRENCY);
7983

8084
// Get User Client Access Permissions
81-
$user_client_access_sql = "SELECT client_id FROM user_permissions WHERE user_id = $session_user_id";
85+
$user_client_access_sql = "SELECT client_id FROM user_client_permissions WHERE user_id = $session_user_id";
8286
$user_client_access_result = mysqli_query($mysqli, $user_client_access_sql);
8387

8488
$client_access_array = [];

dashboard.php

-5
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@
7373
<?php
7474
if ($user_config_dashboard_financial_enable == 1) {
7575

76-
// Ensure the user has the appropriate role to view the financial dashboard
77-
if ($_SESSION['user_role'] != 3 && $_SESSION['user_role'] != 1) {
78-
exit('<script type="text/javascript">window.location.href = \'dashboard_technical.php\';</script>');
79-
}
80-
8176
// Fetch financial data for the dashboard
8277
// Define variables to avoid errors in logs
8378
$largest_income_month = 0;

database_updates.php

+46-3
Original file line numberDiff line numberDiff line change
@@ -2435,10 +2435,53 @@ function processFile($file_path, $file_name, $mysqli) {
24352435
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.8.9'");
24362436
}
24372437

2438-
// if (CURRENT_DATABASE_VERSION == '1.8.9') {
2439-
// // Insert queries here required to update to DB version 1.9.0
2438+
if (CURRENT_DATABASE_VERSION == '1.8.9') {
2439+
mysqli_query($mysqli, "ALTER TABLE `users` ADD `user_role_id` INT(11) DEFAULT 0 AFTER `user_archived_at`");
2440+
2441+
// Copy user role from user settings table to the users table
2442+
mysqli_query($mysqli,"
2443+
UPDATE `users`
2444+
JOIN `user_settings` ON users.user_id = user_settings.user_id
2445+
SET users.user_role_id = user_settings.user_role
2446+
");
2447+
2448+
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.9.0'");
2449+
}
2450+
2451+
if (CURRENT_DATABASE_VERSION == '1.9.0') {
2452+
mysqli_query($mysqli, "ALTER TABLE `user_settings` DROP `user_role`");
2453+
2454+
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.9.1'");
2455+
}
2456+
2457+
if (CURRENT_DATABASE_VERSION == '1.9.1') {
2458+
2459+
mysqli_query($mysqli,
2460+
"ALTER TABLE `user_roles`
2461+
CHANGE COLUMN `user_role_id` `role_id` INT(11) NOT NULL AUTO_INCREMENT,
2462+
CHANGE COLUMN `user_role_name` `role_name` VARCHAR(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
2463+
CHANGE COLUMN `user_role_description` `role_description` VARCHAR(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
2464+
CHANGE COLUMN `user_role_type` `role_type` TINYINT(1) NOT NULL DEFAULT 1,
2465+
CHANGE COLUMN `user_role_is_admin` `role_is_admin` TINYINT(1) NOT NULL DEFAULT 0,
2466+
CHANGE COLUMN `user_role_created_at` `role_created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(),
2467+
CHANGE COLUMN `user_role_updated_at` `role_updated_at` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP(),
2468+
CHANGE COLUMN `user_role_archived_at` `role_archived_at` DATETIME NULL DEFAULT NULL
2469+
");
2470+
2471+
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.9.2'");
2472+
}
2473+
2474+
if (CURRENT_DATABASE_VERSION == '1.9.2') {
2475+
2476+
mysqli_query($mysqli, "RENAME TABLE `user_permissions` TO `user_client_permissions`");
2477+
2478+
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.9.3'");
2479+
}
2480+
2481+
// if (CURRENT_DATABASE_VERSION == '1.9.3') {
2482+
// // Insert queries here required to update to DB version 1.9.4
24402483
// // Then, update the database to the next sequential version
2441-
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.9.0'");
2484+
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.9.4'");
24422485
// }
24432486

24442487
} else {

db.sql

+14-14
Original file line numberDiff line numberDiff line change
@@ -2205,13 +2205,13 @@ CREATE TABLE `trips` (
22052205
/*!40101 SET character_set_client = @saved_cs_client */;
22062206

22072207
--
2208-
-- Table structure for table `user_permissions`
2208+
-- Table structure for table `user_client_permissions`
22092209
--
22102210

2211-
DROP TABLE IF EXISTS `user_permissions`;
2211+
DROP TABLE IF EXISTS `user_client_permissions`;
22122212
/*!40101 SET @saved_cs_client = @@character_set_client */;
22132213
/*!40101 SET character_set_client = utf8 */;
2214-
CREATE TABLE `user_permissions` (
2214+
CREATE TABLE `user_client_permissions` (
22152215
`user_id` int(11) NOT NULL,
22162216
`client_id` int(11) NOT NULL,
22172217
PRIMARY KEY (`user_id`,`client_id`)
@@ -2240,15 +2240,15 @@ DROP TABLE IF EXISTS `user_roles`;
22402240
/*!40101 SET @saved_cs_client = @@character_set_client */;
22412241
/*!40101 SET character_set_client = utf8 */;
22422242
CREATE TABLE `user_roles` (
2243-
`user_role_id` int(11) NOT NULL AUTO_INCREMENT,
2244-
`user_role_name` varchar(200) NOT NULL,
2245-
`user_role_description` varchar(200) DEFAULT NULL,
2246-
`user_role_type` tinyint(1) NOT NULL DEFAULT 1,
2247-
`user_role_is_admin` tinyint(1) NOT NULL DEFAULT 0,
2248-
`user_role_created_at` datetime NOT NULL DEFAULT current_timestamp(),
2249-
`user_role_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
2250-
`user_role_archived_at` datetime DEFAULT NULL,
2251-
PRIMARY KEY (`user_role_id`)
2243+
`role_id` int(11) NOT NULL AUTO_INCREMENT,
2244+
`role_name` varchar(200) NOT NULL,
2245+
`role_description` varchar(200) DEFAULT NULL,
2246+
`role_type` tinyint(1) NOT NULL DEFAULT 1,
2247+
`role_is_admin` tinyint(1) NOT NULL DEFAULT 0,
2248+
`role_created_at` datetime NOT NULL DEFAULT current_timestamp(),
2249+
`role_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
2250+
`role_archived_at` datetime DEFAULT NULL,
2251+
PRIMARY KEY (`role_id`)
22522252
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
22532253
/*!40101 SET character_set_client = @saved_cs_client */;
22542254

@@ -2261,7 +2261,6 @@ DROP TABLE IF EXISTS `user_settings`;
22612261
/*!40101 SET character_set_client = utf8 */;
22622262
CREATE TABLE `user_settings` (
22632263
`user_id` int(11) NOT NULL,
2264-
`user_role` int(11) NOT NULL,
22652264
`user_config_force_mfa` tinyint(1) NOT NULL DEFAULT 0,
22662265
`user_config_records_per_page` int(11) NOT NULL DEFAULT 10,
22672266
`user_config_dashboard_financial_enable` tinyint(1) NOT NULL DEFAULT 0,
@@ -2296,6 +2295,7 @@ CREATE TABLE `users` (
22962295
`user_created_at` datetime NOT NULL DEFAULT current_timestamp(),
22972296
`user_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
22982297
`user_archived_at` datetime DEFAULT NULL,
2298+
`user_role_id` int(11) DEFAULT 0,
22992299
PRIMARY KEY (`user_id`)
23002300
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
23012301
/*!40101 SET character_set_client = @saved_cs_client */;
@@ -2383,4 +2383,4 @@ CREATE TABLE `vendors` (
23832383
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
23842384
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
23852385

2386-
-- Dump completed on 2025-03-10 12:25:19
2386+
-- Dump completed on 2025-03-10 15:52:38

0 commit comments

Comments
 (0)