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

ISTWCMS-3921: adding class to handle UW permissions

parent 8ccc7cac
No related branches found
No related tags found
No related merge requests found
<?php
namespace Drupal\uw_cfg_common\UwPermissions;
/**
* Class UwPermissions.
*/
class UwPermissions {
/**
* Get UW roles.
*
* @return array
* An array of the UW roles to be used on this form.
*/
public static function uw_get_roles(): array {
// UW site manager role.
$uw_roles['Site manager'] = [
'name' => 'Site manager',
'id' => 'uw_role_site_manager',
'object' => \Drupal\user\Entity\Role::load('uw_role_site_manager'),
];
// UW content editor role.
$uw_roles['Content editor'] = [
'name' => 'Content editor',
'id' => 'uw_role_content_editor',
'object' => \Drupal\user\Entity\Role::load('uw_role_content_editor'),
];
// UW content author role.
$uw_roles['Content author'] = [
'name' => 'Content author',
'id' => 'uw_role_content_author',
'object' => \Drupal\user\Entity\Role::load('uw_role_content_author'),
];
return $uw_roles;
}
/**
* Get Uw content permissions array.
*
* @return array
* The array of all permissions for uw content access form.
*/
public static function uw_get_permissions_array(): array {
$uw_permissions = [
// Blog permissions.
'Blog' => [
'Use content type' =>
UwPermissions::uw_build_role_permissions_list_content_type('uw_ct_blog'),
],
// Event permissions.
'Event' => [
'Use content type' => UwPermissions::uw_build_role_permissions_list_content_type('uw_ct_event'),
'Create/Edit tags' => UwPermissions::uw_build_role_permissions_list_taxonomy_term('uw_tax_event_tags', ['create', 'edit']),
'Delete tags' => UwPermissions::uw_build_role_permissions_list_taxonomy_term('uw_tax_event_tags', ['delete']),
'Create/Edit types' => UwPermissions::uw_build_role_permissions_list_taxonomy_term('uw_tax_event_type', ['create', 'edit']),
'Delete types' => UwPermissions::uw_build_role_permissions_list_taxonomy_term('uw_tax_event_type', ['delete']),
],
// News permissions.
'News' => [
'Use content type' =>
UwPermissions::uw_build_role_permissions_list_content_type('uw_ct_news_item'),
],
// Site footer permissions.
'Site footer' => [
'Use content type' =>
UwPermissions::uw_build_role_permissions_list_content_type('uw_ct_site_footer'),
],
// Special alert permissions.
'Special alert' => [
'Use content type' =>
UwPermissions::uw_build_role_permissions_list_custom('administer special alert'),
],
// Web page permissions.
'Web page' => [
'Use content type' =>
UwPermissions::uw_build_role_permissions_list_content_type('uw_ct_web_page'),
],
];
return $uw_permissions;
}
/**
* 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.
*/
public static 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 a custom permission.
*
* @param string $permission_name
* The machine name of the taxonomy term.
* @return array
* An array of the uw permissions.
*/
public static function uw_build_role_permissions_list_custom(string $permission_name): array {
// The roles used for the uw permissions.
$uw_roles = UwPermissions::uw_get_roles();
// Step through each role and add permission.
foreach ($uw_roles as $uw_role) {
// Set the permission.
$uw_permissions[$uw_role['name']][] = [
$permission_name,
];
}
return $uw_permissions;
}
/**
* 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.
*/
public static function uw_build_role_permissions_list_taxonomy_term(string $tax_name, array $permission_types): array {
// The roles used for the uw permissions.
$uw_roles = UWPermissions::uw_get_roles();
// 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['name']][] = $permission_type . ' terms in ' . $tax_name;
}
}
return $uw_permissions;
}
/**
* Save UW permissions.
*
* @parm array $uw_roles
* The array of roles to be saved.
*/
public static function uw_save_permissions(array $uw_roles) {
// Step through each of the roles and save the role object,
// so that the permissions get saved.
foreach ($uw_roles as $uw_role) {
// Save the role object.
$uw_role['object']->save();
}
}
}
\ No newline at end of file
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