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

ISTWCMS-3921: updating permissions list in content access form

parent 59528b05
No related branches found
No related tags found
No related merge requests found
<?php <?php
/** /**
* @file * @file
* Contains \Drupal\uw_cfg_common\Form\UwContactAccessForm. * Contains \Drupal\uw_cfg_common\Form\UwContactAccessForm.
*/ */
namespace Drupal\uw_cfg_common\Form; namespace Drupal\uw_cfg_common\Form;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\user\PermissionHandlerInterface; use Drupal\user\PermissionHandlerInterface;
use Drupal\user\RoleStorageInterface; use Drupal\user\RoleStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
class UwContentAccessForm extends FormBase { class UwContentAccessForm extends FormBase {
/** /**
* The permission handler. * The permission handler.
* *
* @var \Drupal\user\PermissionHandlerInterface * @var \Drupal\user\PermissionHandlerInterface
*/ */
protected $permissionHandler; protected $permissionHandler;
/** /**
* The role storage. * The role storage.
* *
* @var \Drupal\user\RoleStorageInterface * @var \Drupal\user\RoleStorageInterface
*/ */
protected $roleStorage; protected $roleStorage;
/** /**
* The module handler. * The module handler.
* *
* @var \Drupal\Core\Extension\ModuleHandlerInterface * @var \Drupal\Core\Extension\ModuleHandlerInterface
*/ */
protected $moduleHandler; protected $moduleHandler;
/** /**
* Constructs a new UserPermissionsForm. * Constructs a new UserPermissionsForm.
* *
* @param \Drupal\user\PermissionHandlerInterface $permission_handler * @param \Drupal\user\PermissionHandlerInterface $permission_handler
* The permission handler. * The permission handler.
* @param \Drupal\user\RoleStorageInterface $role_storage * @param \Drupal\user\RoleStorageInterface $role_storage
* The role storage. * The role storage.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler. * The module handler.
*/ */
public function __construct(PermissionHandlerInterface $permission_handler, RoleStorageInterface $role_storage, ModuleHandlerInterface $module_handler) { public function __construct(PermissionHandlerInterface $permission_handler, RoleStorageInterface $role_storage, ModuleHandlerInterface $module_handler) {
$this->permissionHandler = $permission_handler; $this->permissionHandler = $permission_handler;
$this->roleStorage = $role_storage; $this->roleStorage = $role_storage;
$this->moduleHandler = $module_handler; $this->moduleHandler = $module_handler;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('user.permissions'), $container->get('user.permissions'),
$container->get('entity_type.manager')->getStorage('user_role'), $container->get('entity_type.manager')->getStorage('user_role'),
$container->get('module_handler') $container->get('module_handler')
); );
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getFormId() { public function getFormId() {
return 'uw_contact_access_form'; return 'uw_contact_access_form';
} }
/** /**
* Gets the roles to display in this form. * Gets the roles to display in this form.
* *
* @return \Drupal\user\RoleInterface[] * @return \Drupal\user\RoleInterface[]
* An array of role objects. * An array of role objects.
*/ */
protected function getRoles() { protected function getRoles() {
return $this->roleStorage->loadMultiple(); return $this->roleStorage->loadMultiple();
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state) {
// The roles to be used in this form. // The roles to be used in this form.
$roles_to_be_used = [ $roles_to_be_used = [
'Site manager', 'Site manager',
'Content editor', 'Content editor',
'Content author', 'Content author',
]; ];
// Role names array to be used. // Role names array to be used.
$role_names = []; $role_names = [];
// The role permissions array to be used. // The role permissions array to be used.
$role_permissions = []; $role_permissions = [];
// The admin roles to be used. // The admin roles to be used.
$admin_roles = []; $admin_roles = [];
// Step through each role and setup variables. // Step through each role and setup variables.
foreach ($this->getRoles() as $role_name => $role) { foreach ($this->getRoles() as $role_name => $role) {
// If the role is in the roles to be used, setup three variables for table. // If the role is in the roles to be used, setup three variables for table.
if (in_array($role->label(), $roles_to_be_used)) { if (in_array($role->label(), $roles_to_be_used)) {
// Retrieve role names for columns. // Retrieve role names for columns.
$role_names[$role_name] = $role->label(); $role_names[$role_name] = $role->label();
// Fetch permissions for the roles. // Fetch permissions for the roles.
$role_permissions[$role_name] = $role->getPermissions(); $role_permissions[$role_name] = $role->getPermissions();
// Check if admin role. // Check if admin role.
$admin_roles[$role_name] = $role->isAdmin(); $admin_roles[$role_name] = $role->isAdmin();
} }
} }
// Store $role_names for use when saving the data. // Store $role_names for use when saving the data.
$form['role_names'] = [ $form['role_names'] = [
'#type' => 'value', '#type' => 'value',
'#value' => $role_names, '#value' => $role_names,
]; ];
$form['permissions'] = [ $form['permissions'] = [
'#type' => 'table', '#type' => 'table',
'#header' => [$this->t('Content type/feature')], '#header' => [$this->t('Content type/feature')],
'#id' => 'permissions', '#id' => 'permissions',
'#attributes' => ['class' => ['permissions', 'js-permissions']], '#attributes' => ['class' => ['permissions', 'js-permissions']],
'#sticky' => TRUE, '#sticky' => TRUE,
]; ];
$form['permissions']['#header'][] = [ $form['permissions']['#header'][] = [
'data' => 'Functionality', 'data' => 'Functionality',
]; ];
foreach ($role_names as $name) { foreach ($role_names as $name) {
$form['permissions']['#header'][] = [ $form['permissions']['#header'][] = [
'data' => $name, 'data' => $name,
'class' => ['checkbox'], 'class' => ['checkbox'],
]; ];
} }
$permissions_to_be_used = $this->uw_get_permissions_array(); $permissions = $this->uw_get_permissions_array();
$permissions_list = $this->permissionHandler->getPermissions(); // Variable to store previous permission name.
$prev_perm_name = '';
foreach ($permissions_to_be_used as $perm_key => $permission_to_be_used) {
if (array_key_exists($perm_key, $permissions_list)) { /*
$permissions[$perm_key] = $permissions_list[$perm_key]; foreach ($permissions_to_be_used as $ptbu_key => $permission_to_be_used) {
}
} if ($permissions_to_be_used[$ptbu_key]['rowspan'] >= 1) {
$form['permissions'][$ptbu_key]['description'] = [
// Variable to store previous permission name. '#wrapper_attributes' => [
$prev_perm_name = ''; 'rowspan' => $permissions_to_be_used[$ptbu_key]['rowspan'],
'style' => 'vertical-align: top',
foreach ($permissions_to_be_used as $ptbu_key => $permission_to_be_used) { ],
$form['permissions'][$ptbu_key]['description'] = [ '#markup' => $permissions_to_be_used[$ptbu_key]['name'],
'#markup' => $prev_perm_name == $permissions_to_be_used[$ptbu_key]['name'] ? '' : $permissions_to_be_used[$ptbu_key]['name'], ];
]; }
$prev_perm_name = $permissions_to_be_used[$ptbu_key]['name']; $form['permissions'][$ptbu_key]['functionality'] = [
'#markup' => $permissions_to_be_used[$ptbu_key]['description'],
$form['permissions'][$ptbu_key]['functionality'] = [ ];
'#markup' => $permissions_to_be_used[$ptbu_key]['description'],
]; foreach ($role_names as $rid => $name) {
$form['permissions'][$ptbu_key][$rid] = [
foreach ($role_names as $rid => $name) { '#title' => $name . ': ' . $permissions[$ptbu_key]['title'],
$form['permissions'][$ptbu_key][$rid] = [ '#title_display' => 'invisible',
'#title' => $name . ': ' . $permissions[$ptbu_key]['title'], '#wrapper_attributes' => [
'#title_display' => 'invisible', 'class' => ['checkbox'],
'#wrapper_attributes' => [ ],
'class' => ['checkbox'], '#type' => 'checkbox',
], '#default_value' => in_array($ptbu_key, $role_permissions[$rid]) ? 1 : 0,
'#type' => 'checkbox', '#attributes' => ['class' => ['rid-' . $rid, 'js-rid-' . $rid]],
'#default_value' => in_array($ptbu_key, $role_permissions[$rid]) ? 1 : 0, '#parents' => [$rid, $ptbu_key],
'#attributes' => ['class' => ['rid-' . $rid, 'js-rid-' . $rid]], ];
'#parents' => [$rid, $ptbu_key],
]; // Show a column of disabled but checked checkboxes.
if ($admin_roles[$rid]) {
// Show a column of disabled but checked checkboxes. $form['permissions'][$ptbu_key][$rid]['#disabled'] = TRUE;
if ($admin_roles[$rid]) { $form['permissions'][$ptbu_key][$rid]['#default_value'] = TRUE;
$form['permissions'][$ptbu_key][$rid]['#disabled'] = TRUE; }
$form['permissions'][$ptbu_key][$rid]['#default_value'] = TRUE; }
} }
} */
}
return $form;
return $form; }
}
/**
/** * Build uw role permissions list for content types.
* Build permission list to be used in the content access form. *
*/ * @param string $ct_name
private function uw_build_content_permissions(array $permissions) { * The machine name of the content type.
dpm('JERE'); * @return array
} * An array of the uw permissions.
*/
/** private function uw_build_role_permissions_list_content_type(string $ct_name): array {
* Get Uw content permissions array.
*/ // Build the permissions list for the content type.
private function uw_get_permissions_array(): array { $content_type_permissions_list = [
'Site manager' => [
$permissions_to_be_used = [ 'create ' . $ct_name . ' content',
// Blog permissions. 'delete any ' . $ct_name . ' content',
'create uw_ct_blog content' => [ 'delete own ' . $ct_name . ' content',
'name' => 'Blog', 'edit any ' . $ct_name . ' content',
'description' => 'Use content type', 'edit own ' . $ct_name . ' content',
], 'revert ' . $ct_name . ' revisions',
// Event permissions. 'view ' . $ct_name . ' revisions',
'create uw_ct_event content' => [ ],
'name' => 'Event', 'Content editor' => [
'description' => 'Use content type', 'create ' . $ct_name . ' content',
], 'edit any ' . $ct_name . ' content',
'create terms in uw_tax_event_tags' => [ 'edit own ' . $ct_name . ' content',
'name' => 'Event', 'revert ' . $ct_name . ' revisions',
'description' => 'Create tags', 'view ' . $ct_name . ' revisions',
], ],
'edit terms in uw_tax_event_tags' => [ 'Content author' => [
'name' => 'Event', 'create ' . $ct_name . ' content',
'description' => 'Edit tags', 'edit any ' . $ct_name . ' content',
], 'edit own ' . $ct_name . ' content',
'delete terms in uw_tax_event_tags' => [ 'revert ' . $ct_name . ' revisions',
'name' => 'Event', 'view ' . $ct_name . ' revisions',
'description' => 'Delete tags', ],
], ];
'create terms in uw_tax_event_type' => [
'name' => 'Event', return $content_type_permissions_list;
'description' => 'Create types', }
],
'edit terms in uw_tax_event_type' => [ /**
'name' => 'Event', * Build role permissions list for taxonomy terms.
'description' => 'Edit types', *
], * @param string $tax_name
'delete terms in uw_tax_event_type' => [ * The machine name of the taxonomy term.
'name' => 'Event', * @param array $permission_types
'description' => 'Delete types', * The list of permissions for the taxonomy term (create, edit and/or delete).
], * @return array
// News permissions. * An array of the uw permissions.
'create uw_ct_news_item content' => [ */
'name' => 'News', private function uw_build_role_permissions_list_taxonomy_term(string $tax_name, array $permission_types): array {
'description' => 'Use content type',
], // The roles used for the uw permissions.
// Site footer permissions. $uw_roles = [
'create uw_ct_site_footer content' => [ 'Site manager',
'name' => 'Site footer', 'Content editor',
'description' => 'Use content type', 'Content author',
], ];
// Special alert.
'create uw_ct_special_alert content' => [ // Step through each of the uw roles and setup list of permissions.
'name' => 'Special alert', foreach ($uw_roles as $uw_role) {
'description' => 'Use content type',
], // Step through each permission types and setup list of permissions.
// Web page permissions. foreach ($permission_types as $permission_type) {
'create uw_ct_web_page content' => [
'name' => 'Web page', // Set the permission.
'description' => 'Use content type', $uw_permissions[$uw_role][] = 'create terms in ' . $tax_name;
], }
]; }
return $permissions_to_be_used; return $uw_permissions;
} }
/** /**
* {@inheritdoc} * Get Uw content permissions array.
*/ *
public function submitForm(array &$form, FormStateInterface $form_state) { * @return array
* The array of all permissions for uw content access form.
// drupal_set_message($this->t('@can_name ,Your application is being submitted!', array('@can_name' => $form_state->getValue('candidate_name')))); */
foreach ($form_state->getValues() as $key => $value) { private function uw_get_permissions_array(): array {
drupal_set_message($key . ': ' . $value);
} $uw_permissions = [
}
} // Blog permissions.
'Blog' => [
'Use content type' =>
$this->uw_build_role_permissions_list_content_type('uw_ct_blog'),
],
// Event permissions.
'Event' => [
'Use content type' => $this->uw_build_role_permissions_list_content_type('uw_ct_event'),
'Create/Edit tags' => $this->uw_build_role_permissions_list_taxonomy_term('uw_tax_event_tags', ['create', 'edit']),
'Delete tags' => $this->uw_build_role_permissions_list_taxonomy_term('uw_tax_event_tags', ['delete']),
'Create/Edit types' => $this->uw_build_role_permissions_list_taxonomy_term('uw_tax_event_type', ['create', 'edit']),
'Delete types' => $this->uw_build_role_permissions_list_taxonomy_term('uw_tax_event_type', ['delete']),
],
// News permissions.
'News' => [
'Use content type' =>
$this->uw_build_role_permissions_list_content_type('uw_ct_news_item'),
],
// Site footer permissions.
'Site footer' => [
'Use content type' =>
$this->uw_build_role_permissions_list_content_type('uw_ct_site_footer'),
],
// Special alert permissions.
'Special alert' => [
'Use content type' =>
$this->uw_build_role_permissions_list_content_type('uw_ct_special_alert'),
],
// Web page permissions.
'Web page' => [
'Use content type' =>
$this->uw_build_role_permissions_list_content_type('uw_ct_web_page'),
],
];
return $uw_permissions;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// drupal_set_message($this->t('@can_name ,Your application is being submitted!', array('@can_name' => $form_state->getValue('candidate_name'))));
foreach ($form_state->getValues() as $key => $value) {
drupal_set_message($key . ': ' . $value);
}
}
}
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