final class TeamAppAccessHandler in Apigee Edge 8
Access handler for Team App entities.
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityAccessControlHandler implements EntityAccessControlHandlerInterface
- class \Drupal\apigee_edge_teams\Entity\TeamAppAccessHandler implements EntityHandlerInterface
- class \Drupal\Core\Entity\EntityAccessControlHandler implements EntityAccessControlHandlerInterface
Expanded class hierarchy of TeamAppAccessHandler
File
- modules/
apigee_edge_teams/ src/ Entity/ TeamAppAccessHandler.php, line 38
Namespace
Drupal\apigee_edge_teams\EntityView source
final class TeamAppAccessHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private $entityTypeManager;
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
private $routeMatch;
/**
* The team permissions handler.
*
* @var \Drupal\apigee_edge_teams\TeamPermissionHandlerInterface
*/
private $teamPermissionHandler;
/**
* TeamAppAccessHandler constructor.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\apigee_edge_teams\TeamPermissionHandlerInterface $team_permission_handler
* The team permissions handler.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
*/
public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, TeamPermissionHandlerInterface $team_permission_handler, RouteMatchInterface $route_match) {
parent::__construct($entity_type);
$this->entityTypeManager = $entity_type_manager;
$this->routeMatch = $route_match;
$this->teamPermissionHandler = $team_permission_handler;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager'), $container
->get('apigee_edge_teams.team_permissions'), $container
->get('current_route_match'));
}
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\apigee_edge_teams\Entity\TeamAppInterface $entity */
$result = parent::checkAccess($entity, $operation, $account);
if ($result
->isNeutral()) {
$result = $this
->checkAccessByPermissions($account);
if ($result
->isNeutral()) {
/** @var \Drupal\apigee_edge_teams\Entity\TeamInterface $team */
$team = $this->entityTypeManager
->getStorage('team')
->load($entity
->getCompanyName());
if ($team) {
// The developer is not member of the team.
// @see hook_apigee_edge_teams_developer_permissions_by_team_alter()
$result = $this
->checkAccessByTeamMemberPermissions($team, $operation, $account);
$this
->processAccessResult($result, $account);
}
else {
// Probably this could never happen...
$result = AccessResult::neutral("The team ({$entity->getCompanyName()}) that the team app ({$entity->getAppId()}) belongs does not exist.");
}
}
}
return $result;
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
$result = parent::checkCreateAccess($account, $context, $entity_bundle);
if ($result
->isNeutral()) {
// Applies to "add-form" link template of Team App entity.
$result = $this
->checkAccessByPermissions($account);
if ($result
->isNeutral()) {
// Applies to "add-form-for-team" link template of Team App entity.
$team = $this->routeMatch
->getParameter('team');
if ($team) {
$result = $this
->checkAccessByTeamMemberPermissions($team, 'create', $account);
}
else {
$result = AccessResult::neutral("Team parameter has not been found in {$this->routeMatch->getRouteObject()->getPath()} path.");
}
}
}
return $result;
}
/**
* Performs access check based on a user's site-wide permissions.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The user for which to check access.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
private function checkAccessByPermissions(AccountInterface $account) : AccessResultInterface {
$permissions = [
TeamAppPermissionProvider::MANAGE_TEAM_APPS_PERMISSION,
];
if ($this->entityType
->getAdminPermission()) {
$permissions[] = $this->entityType
->getAdminPermission();
}
return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
}
/**
* Performs access check based on a user's team-level permissions.
*
* @param \Drupal\apigee_edge_teams\Entity\TeamInterface $team
* The team that owns the app.
* @param string $operation
* The entity operation on a team app: view, create, delete, update
* analytics, add_api_key, delete_api_key or revoke_api_key.
* @param \Drupal\Core\Session\AccountInterface $account
* The user for which to check access.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
private function checkAccessByTeamMemberPermissions(TeamInterface $team, string $operation, AccountInterface $account) : AccessResultInterface {
$covered_operations = [
'view',
'create',
'delete',
'update',
'analytics',
'add_api_key',
'delete_api_key',
'revoke_api_key',
];
if (!in_array($operation, $covered_operations)) {
return AccessResult::neutral("Team membership based access check does not support {$operation} operation on team apps.");
}
if ($account
->isAnonymous()) {
$result = AccessResult::forbidden('Anonymous user can not be member of a team.');
}
else {
$result = AccessResult::allowedIf(in_array("team_app_{$operation}", $this->teamPermissionHandler
->getDeveloperPermissionsByTeam($team, $account)));
// Ensure that access is re-evaluated when the team entity changes.
$result
->addCacheableDependency($team);
}
return $result;
}
/**
* Processes access result before it gets returned.
*
* Adds necessary cache tags to the access result object.
*
* @param \Drupal\Core\Access\AccessResultInterface $result
* The access result to be altered if needed.
* @param \Drupal\Core\Session\AccountInterface $account
* The user for which to access check has happened.
*/
private function processAccessResult(AccessResultInterface $result, AccountInterface $account) {
// Ensure that access is re-evaluated when developer entity changes.
if ($account
->isAuthenticated() && $result instanceof AccessResult) {
$developer = $this->entityTypeManager
->getStorage('developer')
->load($account
->getEmail());
if ($developer) {
$result
->addCacheableDependency($developer);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityAccessControlHandler:: |
protected | property | Stores calculated access check results. | |
EntityAccessControlHandler:: |
protected | property | Information about the entity type. | |
EntityAccessControlHandler:: |
protected | property | The entity type ID of the access control handler instance. | |
EntityAccessControlHandler:: |
protected | property | Allows to grant access to just the labels. | 5 |
EntityAccessControlHandler:: |
public | function |
Checks access to an operation on a given entity or entity translation. Overrides EntityAccessControlHandlerInterface:: |
1 |
EntityAccessControlHandler:: |
protected | function | Default field access as determined by this access control handler. | 4 |
EntityAccessControlHandler:: |
public | function |
Checks access to create an entity. Overrides EntityAccessControlHandlerInterface:: |
1 |
EntityAccessControlHandler:: |
public | function |
Checks access to an operation on a given entity field. Overrides EntityAccessControlHandlerInterface:: |
|
EntityAccessControlHandler:: |
protected | function | Tries to retrieve a previously cached access value from the static cache. | |
EntityAccessControlHandler:: |
protected | function | Loads the current account object, if it does not exist yet. | |
EntityAccessControlHandler:: |
protected | function | We grant access to the entity if both of these conditions are met: | |
EntityAccessControlHandler:: |
public | function |
Clears all cached access checks. Overrides EntityAccessControlHandlerInterface:: |
|
EntityAccessControlHandler:: |
protected | function | Statically caches whether the given user has access. | |
EntityHandlerBase:: |
protected | property | The module handler to invoke hooks on. | 2 |
EntityHandlerBase:: |
protected | function | Gets the module handler. | 2 |
EntityHandlerBase:: |
public | function | Sets the module handler for this handler. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TeamAppAccessHandler:: |
private | property | The entity type manager. | |
TeamAppAccessHandler:: |
private | property | The current route match. | |
TeamAppAccessHandler:: |
private | property | The team permissions handler. | |
TeamAppAccessHandler:: |
protected | function |
Performs access checks. Overrides EntityAccessControlHandler:: |
|
TeamAppAccessHandler:: |
private | function | Performs access check based on a user's site-wide permissions. | |
TeamAppAccessHandler:: |
private | function | Performs access check based on a user's team-level permissions. | |
TeamAppAccessHandler:: |
protected | function |
Performs create access checks. Overrides EntityAccessControlHandler:: |
|
TeamAppAccessHandler:: |
public static | function |
Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface:: |
|
TeamAppAccessHandler:: |
private | function | Processes access result before it gets returned. | |
TeamAppAccessHandler:: |
public | function |
TeamAppAccessHandler constructor. Overrides EntityAccessControlHandler:: |