Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
43.48% |
10 / 23 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ContactAccessControlHandler | |
43.48% |
10 / 23 |
|
0.00% |
0 / 2 |
15.85 | |
0.00% |
0 / 1 |
| checkAccess | |
55.56% |
10 / 18 |
|
0.00% |
0 / 1 |
9.16 | |||
| checkCreateAccess | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| 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 entity type. |
| 12 | */ |
| 13 | class ContactAccessControlHandler extends EntityAccessControlHandler { |
| 14 | |
| 15 | /** |
| 16 | * {@inheritdoc} |
| 17 | */ |
| 18 | protected $viewLabelOperation = TRUE; |
| 19 | |
| 20 | /** |
| 21 | * {@inheritdoc} |
| 22 | */ |
| 23 | protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) { |
| 24 | $bundle = $entity->bundle(); |
| 25 | |
| 26 | switch ($operation) { |
| 27 | case 'view label': |
| 28 | $permissions = ['view any crm_contact label']; |
| 29 | $permissions[] = "view any $bundle crm_contact label"; |
| 30 | |
| 31 | return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR'); |
| 32 | |
| 33 | case 'view': |
| 34 | $permissions = ['view any crm_contact']; |
| 35 | $permissions[] = "view any $bundle crm_contact"; |
| 36 | |
| 37 | return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR'); |
| 38 | |
| 39 | case 'update': |
| 40 | $permissions = ['edit any crm_contact']; |
| 41 | $permissions[] = "edit any $bundle crm_contact"; |
| 42 | |
| 43 | return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR'); |
| 44 | |
| 45 | case 'delete': |
| 46 | $permissions = ['delete any crm_contact']; |
| 47 | $permissions[] = "delete any $bundle crm_contact"; |
| 48 | |
| 49 | return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR'); |
| 50 | |
| 51 | default: |
| 52 | // No opinion. |
| 53 | return AccessResult::neutral(); |
| 54 | |
| 55 | } |
| 56 | |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * {@inheritdoc} |
| 61 | */ |
| 62 | protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { |
| 63 | return AccessResult::allowedIfHasPermissions( |
| 64 | $account, |
| 65 | ['create crm_contact', "create $entity_bundle crm_contact"], |
| 66 | 'OR', |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | } |