You are here

class ContentRoleManager in Lightning Core 8.5

Same name and namespace in other branches
  1. 8 modules/lightning_roles/src/ContentRoleManager.php \Drupal\lightning_roles\ContentRoleManager
  2. 8.2 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 node type entity storage handler.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $nodeTypeStorage;

  /**
   * ContentRoleManager constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
    $this->configFactory = $config_factory;
    $this->nodeTypeStorage = $entity_type_manager
      ->getStorage('node_type');
  }

  /**
   * 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();
    if ($role['enabled']) {

      // Look up all node type IDs.
      $node_types = $this->nodeTypeStorage
        ->getQuery()
        ->execute();
      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::$nodeTypeStorage protected property The node type entity storage handler.
ContentRoleManager::grantPermissions public function Grants permissions (or meta-permissions) to a content role.
ContentRoleManager::__construct public function ContentRoleManager constructor.