You are here

class PermissionProvider in Group 2.0.x

Provides group permissions for group relation plugins.

Hierarchy

Expanded class hierarchy of PermissionProvider

1 file declares its use of PermissionProvider
PermissionProviderTest.php in tests/src/Unit/PermissionProviderTest.php
1 string reference to 'PermissionProvider'
group.services.yml in ./group.services.yml
group.services.yml
1 service uses PermissionProvider
group.relation_handler.permission_provider in ./group.services.yml
Drupal\group\Plugin\Group\RelationHandlerDefault\PermissionProvider

File

src/Plugin/Group/RelationHandlerDefault/PermissionProvider.php, line 12

Namespace

Drupal\group\Plugin\Group\RelationHandlerDefault
View source
class PermissionProvider implements PermissionProviderInterface {
  use PermissionProviderTrait;

  /**
   * Constructs a new PermissionProvider.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getAdminPermission() {
    return $this->definition['admin_permission'];
  }

  /**
   * {@inheritdoc}
   */
  public function getPermission($operation, $target, $scope = 'any') {
    assert(in_array($target, [
      'relation',
      'entity',
    ], TRUE), '$target must be either "relation" or "entity"');
    assert(in_array($scope, [
      'any',
      'own',
    ], TRUE), '$target must be either "relation" or "entity"');
    if ($target === 'relation') {
      switch ($operation) {
        case 'view':
          return $this
            ->getRelationViewPermission($scope);
        case 'update':
          return $this
            ->getRelationUpdatePermission($scope);
        case 'delete':
          return $this
            ->getRelationDeletePermission($scope);
        case 'create':
          return $this
            ->getRelationCreatePermission();
      }
    }
    elseif ($target === 'entity') {
      switch ($operation) {
        case 'view':
          return $this
            ->getEntityViewPermission($scope);
        case 'view unpublished':
          return $this
            ->getEntityViewUnpublishedPermission($scope);
        case 'update':
          return $this
            ->getEntityUpdatePermission($scope);
        case 'delete':
          return $this
            ->getEntityDeletePermission($scope);
        case 'create':
          return $this
            ->getEntityCreatePermission();
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function buildPermissions() {
    $permissions = [];

    // Provide permissions for the relation (i.e.: The group content entity).
    $prefix = 'Relation:';
    if ($name = $this
      ->getAdminPermission()) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} Administer relations");
      $permissions[$name]['restrict access'] = TRUE;
    }
    if ($name = $this
      ->getPermission('view', 'relation')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} View any entity relations");
    }
    if ($name = $this
      ->getPermission('view', 'relation', 'own')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} View own entity relations");
    }
    if ($name = $this
      ->getPermission('update', 'relation')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} Edit any entity relations");
    }
    if ($name = $this
      ->getPermission('update', 'relation', 'own')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} Edit own entity relations");
    }
    if ($name = $this
      ->getPermission('delete', 'relation')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} Delete any entity relations");
    }
    if ($name = $this
      ->getPermission('delete', 'relation', 'own')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} Delete own entity relations");
    }
    if ($name = $this
      ->getPermission('create', 'relation')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} Add entity relations", 'Allows you to add an existing %entity_type entity to the group.');
    }

    // Provide permissions for the actual entity being added to the group.
    $prefix = 'Entity:';
    if ($name = $this
      ->getPermission('view', 'entity')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} View any %entity_type entities");
    }
    if ($name = $this
      ->getPermission('view', 'entity', 'own')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} View own %entity_type entities");
    }
    if ($name = $this
      ->getPermission('view unpublished', 'entity')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} View any unpublished %entity_type entities");
    }
    if ($name = $this
      ->getPermission('view unpublished', 'entity', 'own')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} View own unpublished %entity_type entities");
    }
    if ($name = $this
      ->getPermission('update', 'entity')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} Edit any %entity_type entities");
    }
    if ($name = $this
      ->getPermission('update', 'entity', 'own')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} Edit own %entity_type entities");
    }
    if ($name = $this
      ->getPermission('delete', 'entity')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} Delete any %entity_type entities");
    }
    if ($name = $this
      ->getPermission('delete', 'entity', 'own')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} Delete own %entity_type entities");
    }
    if ($name = $this
      ->getPermission('create', 'entity')) {
      $permissions[$name] = $this
        ->buildPermission("{$prefix} Add %entity_type entities", 'Allows you to create a new %entity_type entity and add it to the group.');
    }
    return $permissions;
  }

  /**
   * Gets the name of the view permission for the relation.
   *
   * @param string $scope
   *   (optional) Whether the 'any' or 'own' permission name should be returned.
   *   Defaults to 'any'.
   *
   * @return string|false
   *   The permission name or FALSE if it does not apply.
   */
  protected function getRelationViewPermission($scope = 'any') {

    // @todo Implement view own permission.
    if ($scope === 'any') {

      // Backwards compatible permission name for 'any' scope.
      return "view {$this->pluginId} relation";
    }
    return FALSE;
  }

  /**
   * Gets the name of the update permission for the relation.
   *
   * @param string $scope
   *   (optional) Whether the 'any' or 'own' permission name should be returned.
   *   Defaults to 'any'.
   *
   * @return string|false
   *   The permission name or FALSE if it does not apply.
   */
  protected function getRelationUpdatePermission($scope = 'any') {
    return "update {$scope} {$this->pluginId} relation";
  }

  /**
   * Gets the name of the delete permission for the relation.
   *
   * @param string $scope
   *   (optional) Whether the 'any' or 'own' permission name should be returned.
   *   Defaults to 'any'.
   *
   * @return string|false
   *   The permission name or FALSE if it does not apply.
   */
  protected function getRelationDeletePermission($scope = 'any') {
    return "delete {$scope} {$this->pluginId} relation";
  }

  /**
   * Gets the name of the create permission for the relation.
   *
   * @return string|false
   *   The permission name or FALSE if it does not apply.
   */
  protected function getRelationCreatePermission() {
    return "create {$this->pluginId} relation";
  }

  /**
   * Gets the name of the view permission for the entity.
   *
   * @param string $scope
   *   (optional) Whether the 'any' or 'own' permission name should be returned.
   *   Defaults to 'any'.
   *
   * @return string|false
   *   The permission name or FALSE if it does not apply.
   */
  protected function getEntityViewPermission($scope = 'any') {
    if ($this->definesEntityPermissions) {

      // @todo Implement view own permission.
      if ($scope === 'any') {

        // Backwards compatible permission name for 'any' scope.
        return "view {$this->pluginId} entity";
      }
    }
    return FALSE;
  }

  /**
   * Gets the name of the view unpublished permission for the entity.
   *
   * @param string $scope
   *   (optional) Whether the 'any' or 'own' permission name should be returned.
   *   Defaults to 'any'.
   *
   * @return string|false
   *   The permission name or FALSE if it does not apply.
   */
  protected function getEntityViewUnpublishedPermission($scope = 'any') {
    if ($this->definesEntityPermissions) {
      if ($this->implementsPublishedInterface) {

        // @todo Implement view own unpublished permission and add it here by
        // checking for $this->implementsOwnerInterface.
        if ($scope === 'any') {
          return "view {$scope} unpublished {$this->pluginId} entity";
        }
      }
    }
    return FALSE;
  }

  /**
   * Gets the name of the update permission for the entity.
   *
   * @param string $scope
   *   (optional) Whether the 'any' or 'own' permission name should be returned.
   *   Defaults to 'any'.
   *
   * @return string|false
   *   The permission name or FALSE if it does not apply.
   */
  protected function getEntityUpdatePermission($scope = 'any') {
    if ($this->definesEntityPermissions) {
      if ($this->implementsOwnerInterface || $scope === 'any') {
        return "update {$scope} {$this->pluginId} entity";
      }
    }
    return FALSE;
  }

  /**
   * Gets the name of the delete permission for the entity.
   *
   * @param string $scope
   *   (optional) Whether the 'any' or 'own' permission name should be returned.
   *   Defaults to 'any'.
   *
   * @return string|false
   *   The permission name or FALSE if it does not apply.
   */
  protected function getEntityDeletePermission($scope = 'any') {
    if ($this->definesEntityPermissions) {
      if ($this->implementsOwnerInterface || $scope === 'any') {
        return "delete {$scope} {$this->pluginId} entity";
      }
    }
    return FALSE;
  }

  /**
   * Gets the name of the create permission for the entity.
   *
   * @return string|false
   *   The permission name or FALSE if it does not apply.
   */
  protected function getEntityCreatePermission() {
    if ($this->definesEntityPermissions) {
      return "create {$this->pluginId} entity";
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PermissionProvider::buildPermissions public function Provides a list of group permissions the plugin exposes. Overrides PermissionProviderTrait::buildPermissions
PermissionProvider::getAdminPermission public function Gets the name of the admin permission. Overrides PermissionProviderTrait::getAdminPermission
PermissionProvider::getEntityCreatePermission protected function Gets the name of the create permission for the entity.
PermissionProvider::getEntityDeletePermission protected function Gets the name of the delete permission for the entity.
PermissionProvider::getEntityUpdatePermission protected function Gets the name of the update permission for the entity.
PermissionProvider::getEntityViewPermission protected function Gets the name of the view permission for the entity.
PermissionProvider::getEntityViewUnpublishedPermission protected function Gets the name of the view unpublished permission for the entity.
PermissionProvider::getPermission public function Gets the name of the permission for the given operation, target and scope. Overrides PermissionProviderTrait::getPermission
PermissionProvider::getRelationCreatePermission protected function Gets the name of the create permission for the relation.
PermissionProvider::getRelationDeletePermission protected function Gets the name of the delete permission for the relation.
PermissionProvider::getRelationUpdatePermission protected function Gets the name of the update permission for the relation.
PermissionProvider::getRelationViewPermission protected function Gets the name of the view permission for the relation.
PermissionProvider::__construct public function Constructs a new PermissionProvider.
PermissionProviderTrait::$definesEntityPermissions protected property Whether the plugin defines permissions for the target entity type.
PermissionProviderTrait::$entityType protected property The entity type the plugin handler is for.
PermissionProviderTrait::$implementsOwnerInterface protected property Whether the target entity type implements the EntityOwnerInterface.
PermissionProviderTrait::$implementsPublishedInterface protected property Whether the target entity type implements the EntityPublishedInterface.
PermissionProviderTrait::buildPermission protected function Builds a permission with common translation arguments predefined.
PermissionProviderTrait::init public function
RelationHandlerTrait::$definition protected property The plugin definition.
RelationHandlerTrait::$entityTypeManager protected property The entity type manager.
RelationHandlerTrait::$groupRelationManager protected property The group relation manager.
RelationHandlerTrait::$parent protected property The parent relation handler in the decorator chain.
RelationHandlerTrait::$pluginId protected property The plugin ID as read from the definition.
RelationHandlerTrait::entityTypeManager protected function Gets the entity type manager service.
RelationHandlerTrait::groupRelationManager protected function Gets the group relation manager service.
RelationHandlerTrait::init public function Aliased as: traitInit