diff --git a/core/modules/user/src/AccountSettingsForm.php b/core/modules/user/src/AccountSettingsForm.php index 0ef0920cbf5..1334e0e2c0e 100644 --- a/core/modules/user/src/AccountSettingsForm.php +++ b/core/modules/user/src/AccountSettingsForm.php @@ -101,34 +101,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#required' => TRUE, ]; - // Administrative role option. - $form['admin_role'] = [ - '#type' => 'details', - '#title' => $this->t('Administrator role'), - '#open' => TRUE, - ]; - // Do not allow users to set the anonymous or authenticated user roles as the - // administrator role. - $roles = user_role_names(TRUE); - unset($roles[RoleInterface::AUTHENTICATED_ID]); - - $admin_roles = $this->roleStorage->getQuery() - ->condition('is_admin', TRUE) - ->execute(); - $default_value = reset($admin_roles); - - $form['admin_role']['user_admin_role'] = [ - '#type' => 'select', - '#title' => $this->t('Administrator role'), - '#empty_value' => '', - '#default_value' => $default_value, - '#options' => $roles, - '#description' => $this->t('This role will be automatically assigned new permissions whenever a module is enabled. Changing this setting will not affect existing permissions.'), - // Don't allow to select a single admin role in case multiple roles got - // marked as admin role already. - '#access' => count($admin_roles) <= 1, - ]; - // @todo Remove this check once language settings are generalized. if ($this->moduleHandler->moduleExists('content_translation')) { $form['language'] = [ @@ -461,22 +433,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $this->config('system.site') ->set('mail_notification', $form_state->getValue('mail_notification_address')) ->save(); - - // Change the admin role. - if ($form_state->hasValue('user_admin_role')) { - $admin_roles = $this->roleStorage->getQuery() - ->condition('is_admin', TRUE) - ->execute(); - - foreach ($admin_roles as $rid) { - $this->roleStorage->load($rid)->setIsAdmin(FALSE)->save(); - } - - $new_admin_role = $form_state->getValue('user_admin_role'); - if ($new_admin_role) { - $this->roleStorage->load($new_admin_role)->setIsAdmin(TRUE)->save(); - } - } } } diff --git a/core/modules/user/src/AdministratorRoleSettingsForm.php b/core/modules/user/src/AdministratorRoleSettingsForm.php new file mode 100644 index 00000000000..00ed0c361c8 --- /dev/null +++ b/core/modules/user/src/AdministratorRoleSettingsForm.php @@ -0,0 +1,117 @@ +roleStorage = $role_storage; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('entity_type.manager')->getStorage('user_role') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'administrator_role_settings'; + } + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames() { + return [ + 'system.site', + 'user.mail', + 'user.settings', + ]; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $form = parent::buildForm($form, $form_state); + + // Administrative role option. + $form['admin_role'] = [ + '#type' => 'details', + '#title' => $this->t('Administrator role'), + '#open' => TRUE, + ]; + // Do not allow users to set the anonymous or authenticated user roles as + // the administrator role. + $roles = user_role_names(TRUE); + unset($roles[RoleInterface::AUTHENTICATED_ID]); + $admin_roles = $this->roleStorage->getQuery() + ->condition('is_admin', TRUE) + ->execute(); + $default_value = reset($admin_roles); + $form['admin_role']['user_admin_role'] = [ + '#type' => 'select', + '#title' => $this->t('Administrator role'), + '#empty_value' => '', + '#default_value' => $default_value, + '#options' => $roles, + '#description' => $this->t('This role will be automatically assigned new permissions whenever a module is enabled. Changing this setting will not affect existing permissions.'), + // Don't allow to select a single admin role in case multiple roles got + // marked as admin role already. + '#access' => count($admin_roles) <= 1, + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + if ($form_state->hasValue('user_admin_role')) { + $admin_roles = $this->roleStorage->getQuery() + ->condition('is_admin', TRUE) + ->execute(); + foreach ($admin_roles as $rid) { + $this->roleStorage->load($rid)->setIsAdmin(FALSE)->save(); + } + $new_admin_role = $form_state->getValue('user_admin_role'); + if ($new_admin_role) { + $this->roleStorage->load($new_admin_role)->setIsAdmin(TRUE)->save(); + } + } + parent::submitForm($form, $form_state); + } + +} diff --git a/core/modules/user/tests/src/Functional/UserPermissionsTest.php b/core/modules/user/tests/src/Functional/UserPermissionsTest.php index be7b143a738..eab17431035 100644 --- a/core/modules/user/tests/src/Functional/UserPermissionsTest.php +++ b/core/modules/user/tests/src/Functional/UserPermissionsTest.php @@ -108,7 +108,7 @@ public function testUserPermissionChanges() { */ public function testAdministratorRole() { $this->drupalLogin($this->adminUser); - $this->drupalGet('admin/config/people/accounts'); + $this->drupalGet('/admin/people/permissions/roles'); // Verify that the administration role is none by default. $this->assertTrue($this->assertSession()->optionExists('edit-user-admin-role', '')->isSelected()); @@ -118,7 +118,7 @@ public function testAdministratorRole() { // Set the user's role to be the administrator role. $edit = []; $edit['user_admin_role'] = $this->rid; - $this->drupalGet('admin/config/people/accounts'); + $this->drupalGet('/admin/people/permissions/roles'); $this->submitForm($edit, 'Save configuration'); \Drupal::entityTypeManager()->getStorage('user_role')->resetCache(); @@ -133,7 +133,7 @@ public function testAdministratorRole() { // Ensure that selecting '- None -' removes the admin role. $edit = []; $edit['user_admin_role'] = ''; - $this->drupalGet('admin/config/people/accounts'); + $this->drupalGet('/admin/people/permissions/roles'); $this->submitForm($edit, 'Save configuration'); \Drupal::entityTypeManager()->getStorage('user_role')->resetCache(); @@ -144,7 +144,7 @@ public function testAdministratorRole() { // hidden. Role::create(['id' => 'admin_role_0', 'is_admin' => TRUE, 'label' => 'Admin role 0'])->save(); Role::create(['id' => 'admin_role_1', 'is_admin' => TRUE, 'label' => 'Admin role 1'])->save(); - $this->drupalGet('admin/config/people/accounts'); + $this->drupalGet('/admin/people/permissions/roles'); $this->assertSession()->fieldNotExists('user_admin_role'); } diff --git a/core/modules/user/tests/src/Unit/Menu/UserLocalTasksTest.php b/core/modules/user/tests/src/Unit/Menu/UserLocalTasksTest.php index ebd7e33c881..250a3dbff9a 100644 --- a/core/modules/user/tests/src/Unit/Menu/UserLocalTasksTest.php +++ b/core/modules/user/tests/src/Unit/Menu/UserLocalTasksTest.php @@ -30,9 +30,9 @@ public function testUserAdminLocalTasks($route, $expected) { */ public function getUserAdminRoutes() { return [ - ['entity.user.collection', [['entity.user.collection', 'user.admin_permissions', 'entity.user_role.collection']]], - ['user.admin_permissions', [['entity.user.collection', 'user.admin_permissions', 'entity.user_role.collection']]], - ['entity.user_role.collection', [['entity.user.collection', 'user.admin_permissions', 'entity.user_role.collection']]], + ['entity.user.collection', [['entity.user.collection', 'user.admin_permissions', 'entity.user_role.collection', 'entity.user.administrator_role']]], + ['user.admin_permissions', [['entity.user.collection', 'user.admin_permissions', 'entity.user_role.collection', 'entity.user.administrator_role']]], + ['entity.user_role.collection', [['entity.user.collection', 'user.admin_permissions', 'entity.user_role.collection', 'entity.user.administrator_role']]], ['entity.user.admin_form', [['user.account_settings_tab']]], ]; } diff --git a/core/modules/user/user.links.task.yml b/core/modules/user/user.links.task.yml index 5b0d1648f46..2c3900c3e74 100644 --- a/core/modules/user/user.links.task.yml +++ b/core/modules/user/user.links.task.yml @@ -49,3 +49,9 @@ entity.user_role.collection: route_name: entity.user_role.collection base_route: entity.user.collection weight: 10 + +entity.user.administrator_role: + title: 'Administrator Role settings' + route_name: entity.user.administrator_role + base_route: entity.user.collection + weight: 11 diff --git a/core/modules/user/user.routing.yml b/core/modules/user/user.routing.yml index dc618eed8a7..4b0b4c317ef 100644 --- a/core/modules/user/user.routing.yml +++ b/core/modules/user/user.routing.yml @@ -29,6 +29,14 @@ entity.user.admin_form: requirements: _permission: 'administer account settings' +entity.user.administrator_role: + path: '/admin/people/permissions/roles' + defaults: + _form: '\Drupal\user\AdministratorRoleSettingsForm' + _title: 'Administrator Role settings' + requirements: + _permission: 'administer account settings' + entity.user.collection: path: '/admin/people' defaults: