You are here

public function ContentRoleManager::grantPermissions in Lightning Core 8

Same name and namespace in other branches
  1. 8.5 modules/lightning_roles/src/ContentRoleManager.php \Drupal\lightning_roles\ContentRoleManager::grantPermissions()
  2. 8.2 modules/lightning_roles/src/ContentRoleManager.php \Drupal\lightning_roles\ContentRoleManager::grantPermissions()
  3. 8.3 modules/lightning_roles/src/ContentRoleManager.php \Drupal\lightning_roles\ContentRoleManager::grantPermissions()
  4. 8.4 modules/lightning_roles/src/ContentRoleManager.php \Drupal\lightning_roles\ContentRoleManager::grantPermissions()

Grants permissions (or meta-permissions) to a content role.

Parameters

string $role_id: The content role ID.

string[] $permissions: The permissions to grant. Can contain the '?' token, which will be replaced with the node type ID.

Return value

$this The called object, for chaining.

File

modules/lightning_roles/src/ContentRoleManager.php, line 52

Class

ContentRoleManager
A service for managing the configuration and deployment of content roles.

Namespace

Drupal\lightning_roles

Code

public function grantPermissions($role_id, array $permissions) {
  $key = "content_roles.{$role_id}";
  $config = $this->configFactory
    ->getEditable('lightning_roles.settings');

  // Add the raw permissions to the content role.
  $role = $config
    ->get($key);
  $role['permissions'] = array_merge($role['permissions'], $permissions);
  $config
    ->set($key, $role)
    ->save();

  // Look up all node type IDs.
  $node_types = $this->entityQuery
    ->get('node_type')
    ->execute();
  if ($role['enabled']) {
    foreach ($node_types as $node_type) {
      $permissions = str_replace('?', $node_type, $role['permissions']);
      user_role_grant_permissions($node_type . '_' . $role_id, $permissions);
    }
  }
  return $this;
}