2018-04-29 11:58:46 +00:00
|
|
|
<?php
|
|
|
|
|
2021-02-18 12:02:25 +00:00
|
|
|
namespace Drupal\opencase_cases;
|
2018-04-29 11:58:46 +00:00
|
|
|
|
|
|
|
use Drupal\Core\Entity\EntityAccessControlHandler;
|
|
|
|
use Drupal\Core\Entity\EntityInterface;
|
|
|
|
use Drupal\Core\Session\AccountInterface;
|
|
|
|
use Drupal\Core\Access\AccessResult;
|
2021-02-18 12:02:25 +00:00
|
|
|
use Drupal\opencase_cases\CaseInvolvement;
|
2018-04-29 11:58:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Access controller for the Case entity.
|
|
|
|
*
|
2021-02-18 12:02:25 +00:00
|
|
|
* @see \Drupal\opencase_cases\Entity\OCCase.
|
2018-04-29 11:58:46 +00:00
|
|
|
*/
|
|
|
|
class OCCaseAccessControlHandler extends EntityAccessControlHandler {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
|
2021-02-18 12:02:25 +00:00
|
|
|
/** @var \Drupal\opencase_cases\Entity\OCCaseInterface $entity */
|
2018-04-29 11:58:46 +00:00
|
|
|
switch ($operation) {
|
|
|
|
case 'view':
|
|
|
|
if (!$entity->isPublished()) {
|
|
|
|
return AccessResult::allowedIfHasPermission($account, 'view unpublished case entities');
|
|
|
|
}
|
2018-07-09 11:21:52 +00:00
|
|
|
return AccessResult::allowedIf(
|
|
|
|
$account->hasPermission('view published case entities')
|
2018-07-09 17:36:47 +00:00
|
|
|
|| CaseInvolvement::userIsInvolved($account, $entity)
|
2018-07-09 11:21:52 +00:00
|
|
|
);
|
2018-07-19 13:10:02 +00:00
|
|
|
case 'update': // you can edit the case only if a) you can see it and b) you have the permission to edit cases.
|
2018-07-09 18:26:11 +00:00
|
|
|
return AccessResult::allowedIf(
|
2018-07-19 13:10:02 +00:00
|
|
|
$account->hasPermission('edit case entities')
|
2018-07-09 18:26:11 +00:00
|
|
|
);
|
2018-07-19 13:35:22 +00:00
|
|
|
case 'delete': // you can delete the case only if a) you can see it and b) you have the permission to delete cases.
|
|
|
|
return AccessResult::allowedIf(
|
|
|
|
$account->hasPermission('delete case entities')
|
|
|
|
&& ($account->hasPermission('view published case entities') || CaseInvolvement::userIsInvolved($account, $entity))
|
|
|
|
);
|
2018-04-29 11:58:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unknown operation, no opinion.
|
|
|
|
return AccessResult::neutral();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
|
|
|
|
return AccessResult::allowedIfHasPermission($account, 'add case entities');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|