Skip to content
Snippets Groups Projects
Commit d89c1826 authored by Eric Bremner's avatar Eric Bremner
Browse files

ISTWCMS-3921: updating code to use better algorithms for getting permissions...

ISTWCMS-3921: updating code to use better algorithms for getting permissions and roles for content access form
parent 72a47b28
No related branches found
No related tags found
No related merge requests found
......@@ -87,44 +87,9 @@ class UwContentAccessForm extends FormBase {
public function buildForm(array $form, FormStateInterface $form_state) {
// The roles to be used in this form.
$roles_to_be_used = [
'Site manager',
'Content editor',
'Content author',
];
// Role names array to be used.
$role_names = [];
// The role permissions array to be used.
$role_permissions = [];
// The admin roles to be used.
$admin_roles = [];
// Step through each role and setup variables.
foreach ($this->getRoles() as $role_name => $role) {
// If the role is in the roles to be used, setup three variables for table.
if (in_array($role->label(), $roles_to_be_used)) {
// Retrieve role names for columns.
$role_names[$role_name] = $role->label();
// Fetch permissions for the roles.
$role_permissions[$role_name] = $role->getPermissions();
// Check if admin role.
$admin_roles[$role_name] = $role->isAdmin();
}
}
// Store $role_names for use when saving the data.
$form['role_names'] = [
'#type' => 'value',
'#value' => $role_names,
];
$uw_roles = $this->uw_get_uw_roles_content_access_form();
// The permissions table to be used on this form.
$form['permissions'] = [
'#type' => 'table',
'#header' => [$this->t('Content type/feature')],
......@@ -133,64 +98,114 @@ class UwContentAccessForm extends FormBase {
'#sticky' => TRUE,
];
// Add to the header the functionality column.
$form['permissions']['#header'][] = [
'data' => 'Functionality',
];
foreach ($role_names as $name) {
// Step through each of the uw roles and add to header as well as store role object.
foreach ($uw_roles as $uw_role) {
// Add role to header of table for this form.
$form['permissions']['#header'][] = [
'data' => $name,
'data' => $uw_role['name'],
'class' => ['checkbox'],
];
// Store the role object for the UW role.
$role_objects[$uw_role['name']] = \Drupal\user\Entity\Role::load($uw_role['id']);
}
$permissions = $this->uw_get_permissions_array();
// Get the permissions array for this form.
$uw_permissions = $this->uw_get_permissions_array();
// Variable to store previous permission name.
$prev_perm_name = '';
// Step through each permission and setup table.
foreach ($uw_permissions as $feature => $uw_permission) {
/*
foreach ($permissions_to_be_used as $ptbu_key => $permission_to_be_used) {
// Row count to tell us if we are on the same row.
$row_count = 0;
if ($permissions_to_be_used[$ptbu_key]['rowspan'] >= 1) {
$form['permissions'][$ptbu_key]['description'] = [
'#wrapper_attributes' => [
'rowspan' => $permissions_to_be_used[$ptbu_key]['rowspan'],
'style' => 'vertical-align: top',
],
'#markup' => $permissions_to_be_used[$ptbu_key]['name'],
];
}
// Step through each of the permission types and setup table.
foreach ($uw_permission as $perm => $uw_permission_roles) {
$form['permissions'][$ptbu_key]['functionality'] = [
'#markup' => $permissions_to_be_used[$ptbu_key]['description'],
];
// Set the row name to be use for this table (role-permission_name).
$row_name = strtolower(str_replace(' ', '_', $feature));
$row_name .= '-' . strtolower(str_replace(' ', '_', $perm));
// If we are on the first of the row, setup the description.
if ($row_count == 0) {
// The markup for the description.
$form['permissions'][$row_name]['description']['#markup'] = $feature;
// If we have more than one permission setup the rowspan.
if (count($uw_permission) >= 1) {
foreach ($role_names as $rid => $name) {
$form['permissions'][$ptbu_key][$rid] = [
'#title' => $name . ': ' . $permissions[$ptbu_key]['title'],
'#title_display' => 'invisible',
'#wrapper_attributes' => [
'class' => ['checkbox'],
],
'#type' => 'checkbox',
'#default_value' => in_array($ptbu_key, $role_permissions[$rid]) ? 1 : 0,
'#attributes' => ['class' => ['rid-' . $rid, 'js-rid-' . $rid]],
'#parents' => [$rid, $ptbu_key],
// The rowspan settings.
$form['permissions'][$row_name]['description']['#wrapper_attributes'] = [
'rowspan' => count($uw_permission),
'style' => 'vertical-align: top',
];
}
}
// Increment the row counter.
$row_count++;
// Set the functionality column.
$form['permissions'][$row_name]['functionality'] = [
'#markup' => $perm,
];
// Show a column of disabled but checked checkboxes.
if ($admin_roles[$rid]) {
$form['permissions'][$ptbu_key][$rid]['#disabled'] = TRUE;
$form['permissions'][$ptbu_key][$rid]['#default_value'] = TRUE;
// Step through each of the uw permissions and setup checkbox if
// permission is set.
foreach ($uw_permission_roles as $uw_role_name => $uw_permission_role) {
// The checkbox for the role/permission set.
$form['permissions'][$row_name][$uw_role_name] = [
'#title' => $perm,
'#title_display' => 'invisible',
'#wrapper_attributes' => [
'class' => ['checkbox'],
],
'#type' => 'checkbox',
'#default_value' => $role_objects[$uw_role_name]->hasPermission($uw_permission_role[0]) ? 1 : 0,
];
}
}
}
*/
return $form;
}
/**
* Get UW roles that are going to be used for the form.
*
* @return array
* An array of the UW roles to be used on this form.
*/
private function uw_get_uw_roles_content_access_form(): array {
// UW site manager role.
$uw_roles[] = [
'name' => 'Site manager',
'id' => 'uw_role_site_manager',
];
// UW content editor role.
$uw_roles[] = [
'name' => 'Content editor',
'id' => 'uw_role_content_editor',
];
// UW content author role.
$uw_roles[] = [
'name' => 'Content author',
'id' => 'uw_role_content_author',
];
return $uw_roles;
}
/**
* Build uw role permissions list for content types.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment