I posted a topic on Stack but still seeking an answer so I am trying here too...
Link to Post
Using Codeigniter 4.3.3, Shield 1.0.0-beta.5 and MySQL
I am sure I am over complicating this... I would like to build an html table using the auth_permissions_users table and 2 left nav tables sidebar_modules and sidebar_modules_sub.
By default, Shield likes to use Group.Permission stored in the auth_permissions_users table IE: admin.useredit, admin.useradd etc... I will be using a traditional method of controller.permission thus the reason for the other tables.
For the mast part I have this working correctly though it seems there should be an easier way... I built a function inside array_helper.php in the helpers folder. the function will split the controller.permission, group them by controller, then build the table based on what it finds. Though I said it is working, I have 2 issues:
Using this table as column order reference:
#1 - I would like to have 5 rows as shown in the table
IE: | Controller | Create | Read | Update | Delete |
This means we have to fill in any blanks if the permission is not provided to the user.
IE: If the user only has read permissions - shown in image below
#2 - Need to figure out how to sort them in a way they will come out exactly correct in the table since they are not always entered into the database table in the same order.
IE: | Controller | Create | Read | Update | Delete |
Data from Shield using $user->getPermissions();
PHP Code:
Array
(
[0] => admin.read
[1] => usergroups.read
[2] => usergroups.create
[3] => usergroups.update
[4] => usergroups.delete
[5] => users.read
[6] => users.update
[7] => users.create
[8] => activitylogs.read
[9] => authgroups.read
[10] => authgroups.create
[11] => authgroups.update
[12] => authgroups.delete
)
*********************************************************
What I have done so far
*********************************************************
If I use
asort() and
ksort(), I can
order the array which will sort the permissions alphabetically, however if I only have one of the 4 permissions, then I have to inject the missing permission. That's when the array once again is out of order... example data before injecting missing values into the array. You can see in
activitylogs I am missing Create, Update, Delete. Same with
admin permission
PHP Code:
Array
(
[activitylogs] => Array
(
[0] => Array
(
[controller] => activitylogs
[permission] => read
)
)
[admin] => Array
(
[0] => Array
(
[controller] => admin
[permission] => read
)
)
[authgroups] => Array
(
[0] => Array
(
[controller] => authgroups
[permission] => create
)
[1] => Array
(
[controller] => authgroups
[permission] => delete
)
[2] => Array
(
[controller] => authgroups
[permission] => read
)
[3] => Array
(
[controller] => authgroups
[permission] => update
)
)
[usergroups] => Array
(
[0] => Array
(
[controller] => usergroups
[permission] => create
)
[1] => Array
(
[controller] => usergroups
[permission] => delete
)
[2] => Array
(
[controller] => usergroups
[permission] => read
)
[3] => Array
(
[controller] => usergroups
[permission] => update
)
)
[users] => Array
(
[0] => Array
(
[controller] => users
[permission] => create
)
[1] => Array
(
[controller] => users
[permission] => read
)
[2] => Array
(
[controller] => users
[permission] => update
)
)
)
I created a function in an array_helper.php file to create a table
"buildPermissionsTable()". I tried to comment the code as much as possible to explain what I am doing.
array_helper.php
PHP Code:
if (!function_exists('buildPermissionsTable')) {
/**
* Assumes Codeigniter - Shield - Creates a table row(s) to populate the Permissions Table.
* This builds from the Shield auth_permissions_users DB Table
* @param $user->getPermissions() should be stored like: "group.value" or "controller.permission"
*
* This function will split from the "." and populate the table with:
* Group - Permission - Permission - Permission - Permission
* Controller - Permission - Permission - Permission - Permission
* ** Currently set to Create - Read - Update - Delete
*
* @param $array = $user->getPermissions();
* @param $permissionTable = buildPermissionsTable($array);
* @param echo $permissionTable = build out HTML Table.
*
* @return array|msg Returns a HTML Table array or `No Data` if `$array` is invalid or empty.
*
*
*/
function buildPermissionsTable(array $array)
{
$pTableHeader = PHP_EOL .'<table class="table table-sm table-bordered table-striped table-hover" style="width: 100%;">'. PHP_EOL .'<thead>'. PHP_EOL .'<tr>'. PHP_EOL .'<th><strong>'. lang('App.controller') .'</strong>:</th>'. PHP_EOL .'<th>Create</th>'. PHP_EOL .'<th>Read</th>'. PHP_EOL .'<th>Update</th>'. PHP_EOL .'<th>Delete</th>'. PHP_EOL .'</thead>'.PHP_EOL .'<tbody>'. PHP_EOL;
$pTableFooter = PHP_EOL .'</tbody>'.PHP_EOL .'</table>';
if (!$array) {
$pTableBody = PHP_EOL .'<tr>'. PHP_EOL .'<td colspan="5" class="text-center">No Data Provided...</td>'. PHP_EOL .'</tr>'. PHP_EOL;
return $pTableHeader . $pTableBody . $pTableFooter;
}
// This foreach loop will split the data based on the period into the first array
// Example: array([controller] => activitylogs [permission] => read )
$permData = [];
foreach ($array as $key) {
list($a, $b) = explode('.', $key);
array_push($permData, [
'controller' => $a,
'permission' => $b,
]);
}
// Sorting the keys and values alphabetically
asort($permData);
// grouping to make the controller the index
// Example: Array ( [activitylogs] => Array ( [0] => Array ( [controller] => activitylogs [permission] => read ) )
$grouped = array_reduce(
$permData,
function ($carry, $item) {
$carry[$item['controller']][] = $item;
return $carry;
},
[]
);
// Sorting again alphabetically - might not be needed but its free
ksort($grouped);
// Now that we have the array, lets build the HTML table
foreach ($grouped as $key => $value):
$pTableBody .= PHP_EOL .'<tr>'. PHP_EOL;
$pTableBody .= '<td><strong>'. strtoupper($key) .'</strong></td>'. PHP_EOL;
$pTableBody .= '<td class="text-center '. ($value[0]['permission'] =='' ? 'text-danger' : 'text-success').'"><i class="fas '. ($value[0]['permission'] =='' ? 'fa-remove' : 'fa-check').' fa-xl"></i><br>'. $key .'.'. ($value[0]['permission'] =='' ? 'none!' : $value[0]['permission']).'</td>'. PHP_EOL;
$pTableBody .= '<td class="text-center '. ($value[1]['permission'] =='' ? 'text-danger' : 'text-success').'"><i class="fas '. ($value[1]['permission'] =='' ? 'fa-remove' : 'fa-check').' fa-xl"></i><br>'. $key .'.'. ($value[1]['permission'] =='' ? 'none!' : $value[1]['permission']).'</td>'. PHP_EOL;
$pTableBody .= '<td class="text-center '. ($value[2]['permission'] =='' ? 'text-danger' : 'text-success').'"><i class="fas '. ($value[2]['permission'] =='' ? 'fa-remove' : 'fa-check').' fa-xl"></i><br>'. $key .'.'. ($value[2]['permission'] =='' ? 'none!' : $value[2]['permission']).'</td>'. PHP_EOL;
$pTableBody .= '<td class="text-center '. ($value[3]['permission'] =='' ? 'text-danger' : 'text-success').'"><i class="fas '. ($value[3]['permission'] =='' ? 'fa-remove' : 'fa-check').' fa-xl"></i><br>'. $key .'.'. ($value[3]['permission'] =='' ? 'none!' : $value[3]['permission']).'</td>'. PHP_EOL;
$pTableBody .= '</tr>'. PHP_EOL;
endforeach;
return $pTableHeader . $pTableBody . $pTableFooter;
}
}
This is how the table looks now, notice they are not in the correct order as Create-Read-Update-Delete. it should properly align with the column header:
| Controller | Create | Read | Update | Delete |