From 6ceaab4fe3c8999959af27575f1d20b64325cf93 Mon Sep 17 00:00:00 2001 From: Kristiaan Van den Eynde Date: Wed, 31 May 2017 14:59:02 +0200 Subject: [PATCH] interdiff --- .../user/tests/src/Kernel/UserInstallTest.php | 5 +++++ core/modules/user/user.install | 26 ++++++++++++++++++++++ core/modules/user/user.post_update.php | 11 +++++++++ 3 files changed, 42 insertions(+) diff --git a/core/modules/user/tests/src/Kernel/UserInstallTest.php b/core/modules/user/tests/src/Kernel/UserInstallTest.php index f61811b..2228607 100644 --- a/core/modules/user/tests/src/Kernel/UserInstallTest.php +++ b/core/modules/user/tests/src/Kernel/UserInstallTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\user\Kernel; +use Drupal\Core\Session\AccountInterface; use Drupal\KernelTests\KernelTestBase; /** @@ -49,6 +50,10 @@ public function testUserInstall() { $this->assertEqual($admin->status, 1); // Test that the anonymous user is blocked. $this->assertEqual($anon->status, 0); + + // Verify that UID 1 gets the administrator role. + $rids = db_query('SELECT roles_target_id FROM {user__roles} WHERE entity_id = 1')->fetchCol(); + $this->assertEquals([AccountInterface::ADMINISTRATOR_ROLE], $rids, 'Admin user has administrator role'); } } diff --git a/core/modules/user/user.install b/core/modules/user/user.install index e28a4f1..bdab712 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -5,6 +5,8 @@ * Install, update and uninstall functions for the user module. */ +use Drupal\Core\Config\ExtensionInstallStorage; +use Drupal\Core\Config\InstallStorage; use Drupal\Core\Session\AccountInterface; /** @@ -101,3 +103,27 @@ function user_update_8100() { $config->set('status_blocked', $mail)->save(TRUE); } } + +/** + * Ensure the presence of the administrator role. + */ +function user_update_8101() { + $config_factory = \Drupal::configFactory(); + $config_name = 'user.role.administrator'; + $role = $config_factory->getEditable($config_name); + + // Add the administrator role if it doesn't exist yet. + if ($role->isNew()) { + $yaml_storage = new ExtensionInstallStorage( + \Drupal::service('config.storage'), + InstallStorage::CONFIG_INSTALL_DIRECTORY + ); + $role->setData($yaml_storage->read($config_name)); + $role->save(TRUE); + } + // Give the role admin privileges if it does not have them. + elseif (!$role->get('is_admin')) { + $role->set('is_admin', TRUE); + $role->save(TRUE); + } +} diff --git a/core/modules/user/user.post_update.php b/core/modules/user/user.post_update.php index 3f19b0c..96afce8 100644 --- a/core/modules/user/user.post_update.php +++ b/core/modules/user/user.post_update.php @@ -6,6 +6,8 @@ */ use Drupal\user\Entity\Role; +use Drupal\user\Entity\User; +use Drupal\user\RoleInterface; /** * Enforce order of role permissions. @@ -20,3 +22,12 @@ function user_post_update_enforce_order_of_permissions() { }; array_map($entity_save, Role::loadMultiple()); } + +/** + * Grant user 1 the administrator role. + */ +function user_post_update_grant_user_1_admin_role() { + $account = User::load(1); + $account->addRole(RoleInterface::ADMINISTRATOR_ID); + $account->save(); +} -- 2.8.1