Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.44% |
17 / 18 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ContactDetailAccessControlHandler | |
94.44% |
17 / 18 |
|
50.00% |
1 / 2 |
7.01 | |
0.00% |
0 / 1 |
| checkAccess | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
6.02 | |||
| checkCreateAccess | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Drupal\crm; |
| 4 | |
| 5 | use Drupal\Core\Access\AccessResult; |
| 6 | use Drupal\Core\Entity\EntityAccessControlHandler; |
| 7 | use Drupal\Core\Entity\EntityInterface; |
| 8 | use Drupal\Core\Session\AccountInterface; |
| 9 | |
| 10 | /** |
| 11 | * Defines the access control handler for the contact detail entity type. |
| 12 | */ |
| 13 | class ContactDetailAccessControlHandler extends EntityAccessControlHandler { |
| 14 | |
| 15 | /** |
| 16 | * {@inheritdoc} |
| 17 | */ |
| 18 | protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) { |
| 19 | |
| 20 | if ($account->hasPermission('administer crm')) { |
| 21 | return AccessResult::allowed(); |
| 22 | } |
| 23 | |
| 24 | /** @var \Drupal\crm\CrmContactDetailInterface $entity */ |
| 25 | $contact = $entity->get('crm_contact')?->entity; |
| 26 | // If no parent contact is found, deny access. |
| 27 | if (!$contact) { |
| 28 | return AccessResult::forbidden('Contact detail must have a parent contact.'); |
| 29 | } |
| 30 | |
| 31 | switch ($operation) { |
| 32 | case 'view': |
| 33 | return $contact->access($operation, $account, TRUE); |
| 34 | |
| 35 | case 'update': |
| 36 | return $contact->access($operation, $account, TRUE); |
| 37 | |
| 38 | case 'delete': |
| 39 | return $contact->access('update', $account, TRUE); |
| 40 | } |
| 41 | |
| 42 | return AccessResult::neutral(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * {@inheritdoc} |
| 47 | */ |
| 48 | protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { |
| 49 | $permissions = [ |
| 50 | 'create crm_contact', |
| 51 | 'edit any crm_contact', |
| 52 | 'administer crm', |
| 53 | ]; |
| 54 | |
| 55 | return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR'); |
| 56 | } |
| 57 | |
| 58 | } |