You are here

public function AccessManager::permissions in Administer Users by Role 8.3

Return permissions to add.

Return value

array of permissions.

Overrides AccessManagerInterface::permissions

File

src/Services/AccessManager.php, line 62

Class

AccessManager
Access Manager.

Namespace

Drupal\administerusersbyrole\Services

Code

public function permissions() {

  // Base permissions.
  $op_titles = [
    'edit' => $this
      ->t('Edit users with allowed roles'),
    'cancel' => $this
      ->t('Cancel users with allowed roles'),
    'view' => $this
      ->t('View users with allowed roles'),
    'role-assign' => $this
      ->t('Assign allowed roles'),
  ];
  foreach ($op_titles as $op => $title) {
    $perm_string = $this
      ->buildPermString($op);
    $perms[$perm_string] = [
      'title' => $title,
    ];
  }

  // Per role permissions.
  $role_config = $this->config
    ->get('roles') ?: [];
  $role_config = array_filter($role_config, function ($s) {
    return $s == self::PERM;
  });
  $roles = array_intersect_key($this
    ->managedRoles(), $role_config);
  foreach ($roles as $rid => $role) {

    // Use a non-breaking space here to adjust the order so that these come
    // after the base permission.
    $op_role_titles = [
      'edit' => $this
        ->t("Edit users includes role %role", [
        '%role' => $role
          ->label(),
      ]),
      'cancel' => $this
        ->t("Cancel users includes role %role", [
        '%role' => $role
          ->label(),
      ]),
      'view' => $this
        ->t("View users includes role %role", [
        '%role' => $role
          ->label(),
      ]),
      'role-assign' => $this
        ->t("Assign roles includes role %role", [
        '%role' => $role
          ->label(),
      ]),
    ];
    foreach ($op_role_titles as $op => $title) {
      $perm_string = $this
        ->buildPermString($op, $rid);
      $description = $this
        ->t('This permission only works when combined with %base', [
        '%base' => $op_titles[$op],
      ]);
      $perms[$perm_string] = [
        'title' => $title,
        'description' => $description,
      ];
    }
  }
  return $perms;
}