public function OgEventSubscriber::checkGroupContentEntityOperationAccess in Organic groups 8
Checks if a user has access to perform a group content entity operation.
Parameters
\Drupal\og\Event\GroupContentEntityOperationAccessEventInterface $event: The event fired when a group content entity operation is performed.
File
- src/
EventSubscriber/ OgEventSubscriber.php, line 390
Class
- OgEventSubscriber
- Event subscribers for Organic Groups.
Namespace
Drupal\og\EventSubscriberCode
public function checkGroupContentEntityOperationAccess(GroupContentEntityOperationAccessEventInterface $event) : void {
$group_content_entity = $event
->getGroupContent();
$group_entity = $event
->getGroup();
$user = $event
->getUser();
$operation = $event
->getOperation();
// Check if the user owns the entity which is being operated on.
$is_owner = $group_content_entity instanceof EntityOwnerInterface && $group_content_entity
->getOwnerId() == $user
->id();
// Retrieve the group content entity operation permissions.
$group_entity_type_id = $group_entity
->getEntityTypeId();
$group_bundle_id = $group_entity
->bundle();
$group_content_bundle_ids = [
$group_content_entity
->getEntityTypeId() => [
$group_content_entity
->bundle(),
],
];
$permissions = $this->permissionManager
->getDefaultEntityOperationPermissions($group_entity_type_id, $group_bundle_id, $group_content_bundle_ids);
// Filter the permissions by operation and ownership.
// If the user does not own the group content, only the non-owner permission
// is relevant (for example 'edit any article node'). However when the user
// _is_ the owner, then both permissions are relevant: an owner will have
// access if they either have the 'edit any article node' or the 'edit own
// article node' permission.
$ownerships = $is_owner ? [
FALSE,
TRUE,
] : [
FALSE,
];
$permissions = array_filter($permissions, function (GroupContentOperationPermission $permission) use ($operation, $ownerships) {
return $permission
->getOperation() === $operation && in_array($permission
->getOwner(), $ownerships);
});
if ($permissions) {
foreach ($permissions as $permission) {
$event
->mergeAccessResult($this->ogAccess
->userAccess($group_entity, $permission
->getName(), $user));
}
}
}