You are here

public function UserPermissionsForm::buildForm in User Permissions 8

Builds the user permissions administration form for a specific role.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

\Drupal\user\RoleInterface|null $user_role: (optional) The user role used for this form. Defaults to NULL.

Overrides UserPermissionsRoleSpecificForm::buildForm

File

src/Form/UserPermissionsForm.php, line 68

Class

UserPermissionsForm
User permissions form for granting permissions to individual users.

Namespace

Drupal\user_permissions\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $user = NULL) {
  if (!$this->account
    ->hasPermission('administer permissions')) {
    return new RedirectResponse(Url::fromRoute('user.page'));
  }
  if (!is_null($user)) {
    $uid = $user;
    $this->user = User::load($uid);
  }
  else {
    $form['user'] = [
      '#markup' => $this
        ->t('User could not be found.'),
    ];
    return $form;
  }

  // Set user specific role name.
  $role_name = '_user_role_' . $uid;

  // Check for the existence of this role.
  $role = Role::load($role_name);
  if ($role) {

    // If role exists, use this for the UserPermissionsRoleSpecificForm.
    $this->userRole = Role::load($role_name);
    $form = parent::buildForm($form, $form_state, $this->userRole);
  }
  else {

    // If role does not exists,
    // load the dummy role and use it to define base permissions.
    $this->userRole = Role::load(USER_PERMISSIONS_NO_ROLE);
    $form = parent::buildForm($form, $form_state, $this->userRole);
    foreach ($form['permissions']['#header'] as $key => &$data) {
      if (is_array($data) && array_key_exists('data', $data)) {
        $data['data'] = $role_name;
      }
    }
    $form['permissions'][$this->userRole
      ->id()]['#default_value'] = [];
    $form['role_names']['#value'][$this->userRole
      ->id()] = $role_name;
  }

  // Check for blocked permissions.
  $blocked_permissions = [];
  $user_roles = $this->user
    ->getRoles();
  foreach (user_role_permissions($user_roles) as $rid => $permissions) {
    if ($rid != $role
      ->get('id')) {
      $blocked_permissions += array_filter($permissions);
    }
  }
  $rid = $role
    ->get('id');
  foreach ($blocked_permissions as $permission) {
    if (isset($form['permissions'][$permission][$rid])) {
      $form['permissions'][$permission][$rid]['#default_value'] = 1;
      $form['permissions'][$permission][$rid]['#value'] = $permission;
      $form['permissions'][$permission][$rid]['#disabled'] = TRUE;
      $form['permissions'][$permission][$rid]['#attributes']['checked'] = TRUE;
    }
  }
  $form['role_names'][$this->userRole
    ->id()]['#markup'] = 'Enable?';
  $form['role_name'] = [
    '#type' => 'hidden',
    '#value' => $this->userRole
      ->label(),
  ];
  $form['uid'] = [
    '#type' => 'hidden',
    '#value' => $uid,
  ];
  return $form;
}