You are here

class GroupMembershipPermissionProvider in Group 2.0.x

Provides group permissions for the group_membership relation plugin.

Hierarchy

Expanded class hierarchy of GroupMembershipPermissionProvider

1 string reference to 'GroupMembershipPermissionProvider'
group.services.yml in ./group.services.yml
group.services.yml
1 service uses GroupMembershipPermissionProvider
group.relation_handler.permission_provider.group_membership in ./group.services.yml
Drupal\group\Plugin\Group\RelationHandler\GroupMembershipPermissionProvider

File

src/Plugin/Group/RelationHandler/GroupMembershipPermissionProvider.php, line 8

Namespace

Drupal\group\Plugin\Group\RelationHandler
View source
class GroupMembershipPermissionProvider implements PermissionProviderInterface {
  use PermissionProviderTrait;

  /**
   * Constructs a new GroupMembershipPermissionProvider.
   *
   * @param \Drupal\group\Plugin\Group\RelationHandler\PermissionProviderInterface $parent
   *   The default permission provider.
   */
  public function __construct(PermissionProviderInterface $parent) {
    $this->parent = $parent;
  }

  /**
   * {@inheritdoc}
   */
  public function getPermission($operation, $target, $scope = 'any') {

    // The following permissions are handled by the admin permission or have a
    // different permission name.
    if ($target === 'relation') {
      switch ($operation) {
        case 'create':
          return FALSE;
        case 'delete':
          return $scope === 'own' ? 'leave group' : FALSE;
        case 'update':
          if ($scope === 'any') {
            return FALSE;
          }
          break;
      }
    }
    return $this->parent
      ->getPermission($operation, $target, $scope);
  }

  /**
   * {@inheritdoc}
   */
  public function buildPermissions() {
    $permissions = $this->parent
      ->buildPermissions();

    // Add in the join group permission.
    $permissions['join group'] = [
      'title' => 'Join group',
      'allowed for' => [
        'outsider',
      ],
    ];

    // Alter the update own permission.
    if ($name = $this->parent
      ->getPermission('update', 'relation', 'own')) {
      $permissions[$name]['title'] = 'Edit own membership';
      $permissions[$name]['allowed for'] = [
        'member',
      ];
    }

    // Alter and rename the delete own permission.
    if ($name = $this->parent
      ->getPermission('delete', 'relation', 'own')) {
      $permissions[$name]['title'] = 'Leave group';
      $permissions[$name]['allowed for'] = [
        'member',
      ];
      $permissions[$this
        ->getPermission('delete', 'relation', 'own')] = $permissions[$name];
      unset($permissions[$name]);
    }

    // The following permissions are handled by the admin permission.
    foreach ([
      'create',
      'update',
      'delete',
    ] as $operation) {
      if ($name = $this->parent
        ->getPermission($operation, 'relation')) {
        unset($permissions[$name]);
      }
    }

    // Update the labels of the default permissions.
    $permissions[$this
      ->getAdminPermission()]['title'] = 'Administer group members';
    $permissions[$this
      ->getPermission('view', 'relation')]['title'] = 'View individual group members';
    return $permissions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GroupMembershipPermissionProvider::buildPermissions public function Provides a list of group permissions the plugin exposes. Overrides PermissionProviderTrait::buildPermissions
GroupMembershipPermissionProvider::getPermission public function Gets the name of the permission for the given operation, target and scope. Overrides PermissionProviderTrait::getPermission
GroupMembershipPermissionProvider::__construct public function Constructs a new GroupMembershipPermissionProvider.
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::getAdminPermission public function 4
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