Commit 5787a570 authored by catch's avatar catch
Browse files

Issue #987978 by vsujeetkumar, paulocs, ankithashetty, Gauravmahlawat,...

Issue #987978 by vsujeetkumar, paulocs, ankithashetty, Gauravmahlawat, yogen.prasad, Suresh Prabhu Parkala, vikashsoni, dww, AaronMcHale, naught101: Move "administrator role" setting to new Role Settings form
parent 9b0462f1
Loading
Loading
Loading
Loading
+0 −44
Original line number Diff line number Diff line
@@ -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();
      }
    }
  }

}
+106 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\user\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\RoleInterface;
use Drupal\user\RoleStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Configure administrator role settings for this site.
 */
class RoleSettingsForm extends FormBase {

  /**
   * The role storage used when changing the admin role.
   *
   * @var \Drupal\user\RoleStorageInterface
   */
  protected $roleStorage;

  /**
   * Constructs a \Drupal\user\Form\RoleSettingsForm object.
   *
   * @param \Drupal\user\RoleStorageInterface $role_storage
   *   The role storage.
   */
  public function __construct(RoleStorageInterface $role_storage) {
    $this->roleStorage = $role_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager')->getStorage('user_role')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'role_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $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,
    ];

    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save configuration'),
      '#button_type' => 'primary',
    ];
    return $form;
  }

  /**
   * {@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();
      }
    }
  }

}
+4 −4
Original line number Diff line number Diff line
@@ -108,7 +108,7 @@ public function testUserPermissionChanges() {
   */
  public function testAdministratorRole() {
    $this->drupalLogin($this->adminUser);
    $this->drupalGet('admin/config/people/accounts');
    $this->drupalGet('admin/people/role-settings');

    // 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/role-settings');
    $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/role-settings');
    $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/role-settings');
    $this->assertSession()->fieldNotExists('user_admin_role');
  }

+3 −3
Original line number Diff line number Diff line
@@ -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', 'user.role.settings']]],
      ['user.admin_permissions', [['entity.user.collection', 'user.admin_permissions', 'entity.user_role.collection', 'user.role.settings']]],
      ['entity.user_role.collection', [['entity.user.collection', 'user.admin_permissions', 'entity.user_role.collection', 'user.role.settings']]],
      ['entity.user.admin_form', [['user.account_settings_tab']]],
    ];
  }
+6 −0
Original line number Diff line number Diff line
@@ -49,3 +49,9 @@ entity.user_role.collection:
  route_name: entity.user_role.collection
  base_route: entity.user.collection
  weight: 10

user.role.settings:
  title: 'Role settings'
  route_name: user.role.settings
  base_route: entity.user.collection
  weight: 11
Loading