From 8dab107d33a670e357d41da833e05229393b08a6 Mon Sep 17 00:00:00 2001
From: Lily Yan <lily.yan@uwaterloo.ca>
Date: Wed, 28 Jul 2021 08:28:07 -0400
Subject: [PATCH] ISTWCMS-3002 Add hook update to load config and assign
 permissions to roles which are depended on uw_sites_all hook update 8105

---
 uw_cfg_common.install | 60 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/uw_cfg_common.install b/uw_cfg_common.install
index be15d0dd..2e2bd791 100644
--- a/uw_cfg_common.install
+++ b/uw_cfg_common.install
@@ -9,6 +9,7 @@ use Drupal\taxonomy\Entity\Term;
 use Drupal\node\Entity\Node;
 use Drupal\uw_cfg_common\UwPermissions\UwPermissions;
 use Drupal\user\Entity\Role;
+use Drupal\Core\Config\FileStorage;
 
 /**
  * Implements hook_install().
@@ -374,3 +375,62 @@ function uw_cfg_common_update_8102() {
     $role_object->save();
   }
 }
+
+/**
+ * Load config and assigns permissions to roles.
+ *
+ * Load quick_node_clone config.
+ */
+function uw_cfg_common_update_8103() {
+
+  // Load config that is not being set properly by config distro update.
+  $config_path = drupal_get_path('module', 'uw_cfg_common') . '/config/install';
+  $source = new FileStorage($config_path);
+  $config_storage = \Drupal::service('config.storage');
+  $config_storage->write('quick_node_clone.settings', $source->read('quick_node_clone.settings'));
+
+  // List of permissions needed to be applied to roles.
+  $permissions = [
+    'clone uw_ct_blog content',
+    'clone uw_ct_catalog_item content',
+    'clone uw_ct_contact content',
+    'clone uw_ct_event content',
+    'clone uw_ct_news_item content',
+    'clone uw_ct_profile content',
+    'clone uw_ct_sidebar content',
+    'clone uw_ct_web_page content',
+  ];
+
+  // Roles that require updated permissions.
+  $role_ids = [
+    'uw_role_site_manager',
+    'uw_role_content_editor',
+    'uw_role_content_author',
+  ];
+
+  $roles = Role::loadMultiple($role_ids);
+
+  // Grant all all roles.
+  foreach ($permissions as $permission) {
+    $roles['uw_role_site_manager']->grantPermission($permission);
+    $roles['uw_role_content_editor']->grantPermission($permission);
+    $roles['uw_role_content_author']->grantPermission($permission);
+  }
+
+  // Save role with new permissions.
+  $roles['uw_role_site_manager']->save();
+  $roles['uw_role_content_editor']->save();
+  $roles['uw_role_content_author']->save();
+
+}
+
+/**
+ * Implements hook_update_dependencies().
+ */
+function uw_cfg_common_update_dependencies() {
+  $dependencies['uw_cfg_common'][8103] = [
+    'uw_sites_all' => 8105,
+  ];
+
+  return $dependencies;
+}
-- 
GitLab