You are here

class ContentRoleManager in Lightning Core 8.2

Same name and namespace in other branches
  1. 8.5 modules/lightning_roles/src/ContentRoleManager.php \Drupal\lightning_roles\ContentRoleManager
  2. 8 modules/lightning_roles/src/ContentRoleManager.php \Drupal\lightning_roles\ContentRoleManager
  3. 8.3 modules/lightning_roles/src/ContentRoleManager.php \Drupal\lightning_roles\ContentRoleManager
  4. 8.4 modules/lightning_roles/src/ContentRoleManager.php \Drupal\lightning_roles\ContentRoleManager

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

Hierarchy

Expanded class hierarchy of ContentRoleManager

1 string reference to 'ContentRoleManager'
lightning_roles.services.yml in modules/lightning_roles/lightning_roles.services.yml
modules/lightning_roles/lightning_roles.services.yml
1 service uses ContentRoleManager
lightning.content_roles in modules/lightning_roles/lightning_roles.services.yml
\Drupal\lightning_roles\ContentRoleManager

File

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

Namespace

Drupal\lightning_roles
View source
class ContentRoleManager {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The entity query factory.
   *
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
  protected $entityQuery;

  /**
   * ContentRoleManager constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query
   *   The entity query factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory, QueryFactory $entity_query) {
    $this->configFactory = $config_factory;
    $this->entityQuery = $entity_query;
  }

  /**
   * Grants permissions (or meta-permissions) to a content role.
   *
   * @param string $role_id
   *   The content role ID.
   * @param string[] $permissions
   *   The permissions to grant. Can contain the '?' token, which will be
   *   replaced with the node type ID.
   *
   * @return $this
   *   The called object, for chaining.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentRoleManager::$configFactory protected property The config factory.
ContentRoleManager::$entityQuery protected property The entity query factory.
ContentRoleManager::grantPermissions public function Grants permissions (or meta-permissions) to a content role.
ContentRoleManager::__construct public function ContentRoleManager constructor.