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

ISTWCMS-3921: updating cntent access form to use new UW permissions class

parent c2dd9789
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,7 @@ use Drupal\user\PermissionHandlerInterface; ...@@ -14,6 +14,7 @@ use Drupal\user\PermissionHandlerInterface;
use Drupal\user\RoleStorageInterface; use Drupal\user\RoleStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Messenger\MessengerTrait; use Drupal\Core\Messenger\MessengerTrait;
use Drupal\uw_cfg_common\UwPermissions\UwPermissions;
class UwContentAccessForm extends FormBase { class UwContentAccessForm extends FormBase {
...@@ -30,7 +31,7 @@ class UwContentAccessForm extends FormBase { ...@@ -30,7 +31,7 @@ class UwContentAccessForm extends FormBase {
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.
$uw_roles = $this->uw_get_uw_roles_content_access_form(); $uw_roles = UwPermissions::uw_get_roles();
// The permissions table to be used on this form. // The permissions table to be used on this form.
$form['permissions'] = [ $form['permissions'] = [
...@@ -54,9 +55,6 @@ class UwContentAccessForm extends FormBase { ...@@ -54,9 +55,6 @@ class UwContentAccessForm extends FormBase {
'data' => $uw_role['name'], 'data' => $uw_role['name'],
'class' => ['checkbox'], 'class' => ['checkbox'],
]; ];
// Store the role object for the UW role.
$role_objects[$uw_role['name']] = \Drupal\user\Entity\Role::load($uw_role['id']);
} }
// Get the permissions array for this form. // Get the permissions array for this form.
...@@ -111,7 +109,7 @@ class UwContentAccessForm extends FormBase { ...@@ -111,7 +109,7 @@ class UwContentAccessForm extends FormBase {
'class' => ['checkbox'], 'class' => ['checkbox'],
], ],
'#type' => 'checkbox', '#type' => 'checkbox',
'#default_value' => $role_objects[$uw_role_name]->hasPermission($uw_permission_role[0]) ? 1 : 0, '#default_value' => $uw_roles[$uw_role_name]['object']->hasPermission($uw_permission_role[0]) ? 1 : 0,
]; ];
} }
} }
...@@ -137,17 +135,10 @@ class UwContentAccessForm extends FormBase { ...@@ -137,17 +135,10 @@ class UwContentAccessForm extends FormBase {
$submitted_permissions = $form_state->getValue('permissions'); $submitted_permissions = $form_state->getValue('permissions');
// The array of uw permissions. // The array of uw permissions.
$uw_permissions = $this->uw_get_permissions_array(); $uw_permissions = UwPermissions::uw_get_permissions_array();
// The array of uw roles. // The array of uw roles.
$uw_roles = $this->uw_get_uw_roles_content_access_form(); $uw_roles = UwPermissions::uw_get_roles();
// Step through each role and store the role object.
foreach ($uw_roles as $uw_role) {
// Store the role object.
$uw_role_objects[$uw_role['name']] = \Drupal\user\Entity\Role::load($uw_role['id']);
}
// Step through each of the submitted permissions to grant or revoke it for the role. // Step through each of the submitted permissions to grant or revoke it for the role.
// Step through each of the permissions array to get the row name (role-permission). // Step through each of the permissions array to get the row name (role-permission).
...@@ -172,7 +163,7 @@ class UwContentAccessForm extends FormBase { ...@@ -172,7 +163,7 @@ class UwContentAccessForm extends FormBase {
foreach ($uw_perms as $uw_perm) { foreach ($uw_perms as $uw_perm) {
// Grant the permission for the specified role. // Grant the permission for the specified role.
$uw_role_objects[$uw_role_name]->grantPermission($uw_perm); $uw_roles[$uw_role_name]['object']->grantPermission($uw_perm);
} }
} }
// If the checkbox was not selected on the form revoke the permissions that is // If the checkbox was not selected on the form revoke the permissions that is
...@@ -183,203 +174,14 @@ class UwContentAccessForm extends FormBase { ...@@ -183,203 +174,14 @@ class UwContentAccessForm extends FormBase {
foreach ($uw_perms as $uw_perm) { foreach ($uw_perms as $uw_perm) {
// Revoke the permission for the specified role. // Revoke the permission for the specified role.
$uw_role_objects[$uw_role_name]->revokePermission($uw_perm); $uw_roles[$uw_role_name]['object']->revokePermission($uw_perm);
} }
} }
} }
} }
// Step through each of the roles and save the role object, UwPermissions::uw_save_permissions($uw_roles);
// so that the permissions get saved.
foreach ($uw_role_objects as $uw_role_object) {
// Save the role object.
$uw_role_object->save();
}
$this->messenger()->addStatus($this->t('The changes have been saved.')); $this->messenger()->addStatus($this->t('The changes have been saved.'));
} }
/**
* 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.
*
* @param string $ct_name
* The machine name of the content type.
* @return array
* An array of the uw permissions.
*/
private function uw_build_role_permissions_list_content_type(string $ct_name): array {
// Build the permissions list for the content type.
$content_type_permissions_list = [
'Site manager' => [
'create ' . $ct_name . ' content',
'delete any ' . $ct_name . ' content',
'delete own ' . $ct_name . ' content',
'edit any ' . $ct_name . ' content',
'edit own ' . $ct_name . ' content',
'revert ' . $ct_name . ' revisions',
'view ' . $ct_name . ' revisions',
],
'Content editor' => [
'create ' . $ct_name . ' content',
'edit any ' . $ct_name . ' content',
'edit own ' . $ct_name . ' content',
'revert ' . $ct_name . ' revisions',
'view ' . $ct_name . ' revisions',
],
'Content author' => [
'create ' . $ct_name . ' content',
'edit any ' . $ct_name . ' content',
'edit own ' . $ct_name . ' content',
'revert ' . $ct_name . ' revisions',
'view ' . $ct_name . ' revisions',
],
];
return $content_type_permissions_list;
}
/**
* Build role permissions list for taxonomy terms.
*
* @param string $tax_name
* The machine name of the taxonomy term.
* @param array $permission_types
* The list of permissions for the taxonomy term (create, edit and/or delete).
* @return array
* An array of the uw permissions.
*/
private function uw_build_role_permissions_list_taxonomy_term(string $tax_name, array $permission_types): array {
// The roles used for the uw permissions.
$uw_roles = [
'Site manager',
'Content editor',
'Content author',
];
// Step through each of the uw roles and setup list of permissions.
foreach ($uw_roles as $uw_role) {
// Step through each permission types and setup list of permissions.
foreach ($permission_types as $permission_type) {
// Set the permission.
$uw_permissions[$uw_role][] = $permission_type . ' terms in ' . $tax_name;
}
}
return $uw_permissions;
}
/**
* Build role permissions list for a custom permission.
*
* @param string $permission_name
* The machine name of the taxonomy term.
* @return array
* An array of the uw permissions.
*/
private function uw_build_role_permissions_list_custom(string $permission_name): array {
// The roles used for the uw permissions.
$uw_roles = [
'Site manager',
'Content editor',
'Content author',
];
// Step through each role and add permission.
foreach ($uw_roles as $uw_role) {
// Set the permission.
$uw_permissions[$uw_role][] = [
$permission_name,
];
}
return $uw_permissions;
}
/**
* Get Uw content permissions array.
*
* @return array
* The array of all permissions for uw content access form.
*/
private function uw_get_permissions_array(): array {
$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_custom('administer 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;
}
} }
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