CompositionAccessControlHandler.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Drupal\edlp_studio;
  3. use Drupal\Core\Entity\EntityAccessControlHandler;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Session\AccountInterface;
  6. use Drupal\Core\Access\AccessResult;
  7. /**
  8. * Access controller for the Composition entity.
  9. *
  10. * @see \Drupal\edlp_studio\Entity\Composition.
  11. */
  12. class CompositionAccessControlHandler extends EntityAccessControlHandler {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  17. /** @var \Drupal\edlp_studio\Entity\CompositionInterface $entity */
  18. switch ($operation) {
  19. case 'view':
  20. if (!$entity->isPublished()) {
  21. if($account->hasPermission('view own unpublished composition entities') && $account->isAuthenticated() && $account->id() == $entity->getOwnerId() ){
  22. return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
  23. }else{
  24. return AccessResult::allowedIfHasPermission($account, 'view any unpublished composition entities');
  25. }
  26. }
  27. return AccessResult::allowedIfHasPermission($account, 'view published composition entities');
  28. case 'update':
  29. if($account->hasPermission('edit own composition entities') && $account->isAuthenticated() && $account->id() == $entity->getOwnerId() ){
  30. return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
  31. }else{
  32. return AccessResult::allowedIfHasPermission($account, 'edit any composition entities');
  33. }
  34. case 'delete':
  35. if($account->hasPermission('delete own composition entities') && $account->isAuthenticated() && $account->id() == $entity->getOwnerId() ){
  36. return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
  37. }else{
  38. return AccessResult::allowedIfHasPermission($account, 'delete composition entities');
  39. }
  40. }
  41. // Unknown operation, no opinion.
  42. return AccessResult::neutral();
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
  48. return AccessResult::allowedIfHasPermission($account, 'add composition entities');
  49. }
  50. }