From 0551faf0f2807fcf27fedf173d2cb79ee417e6fd Mon Sep 17 00:00:00 2001 From: ebremner <ebremner@uwaterloo.ca> Date: Tue, 14 Jul 2020 12:30:20 -0400 Subject: [PATCH] ISTWCMS-3921: updating cntent access form to use new UW permissions class --- src/Form/UwContentAccessForm.php | 214 ++----------------------------- 1 file changed, 8 insertions(+), 206 deletions(-) diff --git a/src/Form/UwContentAccessForm.php b/src/Form/UwContentAccessForm.php index 63500269..0a8034a4 100644 --- a/src/Form/UwContentAccessForm.php +++ b/src/Form/UwContentAccessForm.php @@ -14,6 +14,7 @@ use Drupal\user\PermissionHandlerInterface; use Drupal\user\RoleStorageInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Messenger\MessengerTrait; +use Drupal\uw_cfg_common\UwPermissions\UwPermissions; class UwContentAccessForm extends FormBase { @@ -30,7 +31,7 @@ class UwContentAccessForm extends FormBase { public function buildForm(array $form, FormStateInterface $form_state) { // 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. $form['permissions'] = [ @@ -54,9 +55,6 @@ class UwContentAccessForm extends FormBase { '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']); } // Get the permissions array for this form. @@ -111,7 +109,7 @@ class UwContentAccessForm extends FormBase { 'class' => ['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 { $submitted_permissions = $form_state->getValue('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. - $uw_roles = $this->uw_get_uw_roles_content_access_form(); - - // 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']); - } + $uw_roles = UwPermissions::uw_get_roles(); // 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). @@ -172,7 +163,7 @@ class UwContentAccessForm extends FormBase { foreach ($uw_perms as $uw_perm) { // 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 @@ -183,203 +174,14 @@ class UwContentAccessForm extends FormBase { foreach ($uw_perms as $uw_perm) { // 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, - // so that the permissions get saved. - foreach ($uw_role_objects as $uw_role_object) { - - // Save the role object. - $uw_role_object->save(); - } + UwPermissions::uw_save_permissions($uw_roles); $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; - } } -- GitLab