TeamInvitationAccessControlHandler.php in Apigee Edge 8
File
modules/apigee_edge_teams/src/Entity/TeamInvitationAccessControlHandler.php
View source
<?php
namespace Drupal\apigee_edge_teams\Entity;
use Drupal\apigee_edge_teams\TeamPermissionHandlerInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity\EntityAccessControlHandler;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class TeamInvitationAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
protected $teamPermissionHandler;
public function __construct(EntityTypeInterface $entity_type, TeamPermissionHandlerInterface $team_permission_handler) {
parent::__construct($entity_type);
$this->teamPermissionHandler = $team_permission_handler;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('apigee_edge_teams.team_permissions'));
}
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
$account = $this
->prepareUser($account);
if (!$entity
->getTeam()) {
return AccessResult::forbidden('Team does not exist.')
->addCacheableDependency($entity);
}
if ($entity
->isPending() && $operation === 'accept') {
return AccessResult::allowedIf($account
->getEmail() == $entity
->getRecipient())
->andIf(AccessResult::allowedIfHasPermissions($account, [
'accept own team invitation',
'accept any team invitation',
], 'OR'))
->addCacheableDependency($entity)
->cachePerUser();
}
if ($entity
->isPending() && $operation === 'decline') {
return AccessResult::allowedIf($account
->getEmail() == $entity
->getRecipient())
->andIf(AccessResult::allowedIfHasPermissions($account, [
'decline own team invitation',
'decline any team invitation',
], 'OR'))
->addCacheableDependency($entity)
->cachePerUser();
}
if ($operation === 'delete' || $operation === "resend") {
return AccessResult::allowedIf(in_array('team_manage_members', $this->teamPermissionHandler
->getDeveloperPermissionsByTeam($entity
->getTeam(), $account)))
->orIf(AccessResult::allowedIfHasPermissions($account, [
'administer team',
'manage team members',
], 'OR'))
->addCacheableDependency($entity)
->cachePerUser();
}
return parent::checkAccess($entity, $operation, $account);
}
}