Add user role management functionality with CRUD operations and permissions handling
- Created user_role.php for viewing and editing user roles and their permissions. - Implemented inline editing for role details and permissions. - Added user_role_manage.php for creating and managing user roles. - Introduced user_roles.php for listing all user roles with pagination and filtering options. - Integrated API calls for fetching and updating role data and permissions. - Enhanced user interface with success messages and navigation controls.
This commit is contained in:
123
api/v2/post/user_roles.php
Normal file
123
api/v2/post/user_roles.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
defined($security_key) or exit;
|
||||
|
||||
//------------------------------------------
|
||||
// User Roles
|
||||
//------------------------------------------
|
||||
//Connect to DB
|
||||
$pdo = dbConnect($dbname);
|
||||
|
||||
//CONTENT FROM API (POST)
|
||||
$post_content = json_decode($input,true);
|
||||
|
||||
//SET PARAMETERS FOR QUERY
|
||||
$id = $post_content['rowID'] ?? '';
|
||||
$command = ($id == '')? 'insert' : 'update';
|
||||
if (isset($post_content['delete'])){$command = 'delete';}
|
||||
$date = date('Y-m-d H:i:s');
|
||||
|
||||
//CREATE EMPTY STRINGS
|
||||
$clause = '';
|
||||
$clause_insert ='';
|
||||
$input_insert = '';
|
||||
$execute_input = [];
|
||||
$criterias = [];
|
||||
|
||||
//ADD STANDARD PARAMETERS TO ARRAY BASED ON INSERT OR UPDATE
|
||||
if ($command == 'update'){
|
||||
$post_content['updatedby'] = $username;
|
||||
$post_content['updated'] = $date;
|
||||
}
|
||||
elseif ($command == 'insert'){
|
||||
$post_content['created'] = $date;
|
||||
$post_content['createdby'] = $username;
|
||||
}
|
||||
|
||||
//CREAT NEW ARRAY AND MAP TO CLAUSE
|
||||
if(isset($post_content) && $post_content!=''){
|
||||
foreach ($post_content as $key => $var){
|
||||
if ($key == 'submit' || $key == 'rowID' || $key == 'permissions' || str_contains($key, 'old_')){
|
||||
//do nothing
|
||||
}
|
||||
else {
|
||||
$criterias[$key] = $var;
|
||||
$clause .= ' , '.$key.' = ?';
|
||||
$clause_insert .= ' , '.$key.'';
|
||||
$input_insert .= ', ?';
|
||||
$execute_input[]= $var;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//CLEAN UP INPUT
|
||||
$clause = substr($clause, 2);
|
||||
$clause_insert = substr($clause_insert, 2);
|
||||
$input_insert = substr($input_insert, 1);
|
||||
|
||||
//QUERY AND VERIFY ALLOWED
|
||||
if ($command == 'update' && isAllowed('user_role_manage',$profile,$permission,'U') === 1){
|
||||
$sql = 'UPDATE user_roles SET '.$clause.' WHERE rowID = ?';
|
||||
$execute_input[] = $id;
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($execute_input);
|
||||
|
||||
//Handle permissions update
|
||||
if (isset($post_content['permissions'])){
|
||||
//First delete all existing permissions for this role
|
||||
$stmt = $pdo->prepare('DELETE FROM role_access_permissions WHERE role_id = ?');
|
||||
$stmt->execute([$id]);
|
||||
|
||||
//Insert new permissions
|
||||
foreach ($post_content['permissions'] as $access_id => $perms){
|
||||
$can_create = isset($perms['can_create']) ? 1 : 0;
|
||||
$can_read = isset($perms['can_read']) ? 1 : 0;
|
||||
$can_update = isset($perms['can_update']) ? 1 : 0;
|
||||
$can_delete = isset($perms['can_delete']) ? 1 : 0;
|
||||
|
||||
//Only insert if at least one permission is set
|
||||
if ($can_create || $can_read || $can_update || $can_delete){
|
||||
$stmt = $pdo->prepare('INSERT INTO role_access_permissions (role_id, access_id, can_create, can_read, can_update, can_delete, created, createdby) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
$stmt->execute([$id, $access_id, $can_create, $can_read, $can_update, $can_delete, $date, $userkey]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif ($command == 'insert' && isAllowed('user_role_manage',$profile,$permission,'C') === 1){
|
||||
$sql = 'INSERT INTO user_roles ('.$clause_insert.') VALUES ('.$input_insert.')';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($execute_input);
|
||||
|
||||
//Get the new role ID
|
||||
$new_role_id = $pdo->lastInsertId();
|
||||
|
||||
//Handle permissions for new role
|
||||
if (isset($post_content['permissions'])){
|
||||
foreach ($post_content['permissions'] as $access_id => $perms){
|
||||
$can_create = isset($perms['can_create']) ? 1 : 0;
|
||||
$can_read = isset($perms['can_read']) ? 1 : 0;
|
||||
$can_update = isset($perms['can_update']) ? 1 : 0;
|
||||
$can_delete = isset($perms['can_delete']) ? 1 : 0;
|
||||
|
||||
//Only insert if at least one permission is set
|
||||
if ($can_create || $can_read || $can_update || $can_delete){
|
||||
$stmt = $pdo->prepare('INSERT INTO role_access_permissions (role_id, access_id, can_create, can_read, can_update, can_delete, created, createdby) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
$stmt->execute([$new_role_id, $access_id, $can_create, $can_read, $can_update, $can_delete, $date, $userkey]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif ($command == 'delete' && isAllowed('user_role_manage',$profile,$permission,'D') === 1){
|
||||
//Delete role permissions first (foreign key constraint)
|
||||
$stmt = $pdo->prepare('DELETE FROM role_access_permissions WHERE role_id = ?');
|
||||
$stmt->execute([$id]);
|
||||
|
||||
//Delete user role assignments
|
||||
$stmt = $pdo->prepare('DELETE FROM user_role_assignments WHERE role_id = ?');
|
||||
$stmt->execute([$id]);
|
||||
|
||||
//Delete role
|
||||
$stmt = $pdo->prepare('DELETE FROM user_roles WHERE rowID = ?');
|
||||
$stmt->execute([$id]);
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user