Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContactAccessControlHandler
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 checkAccess
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
42
 checkCreateAccess
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Drupal\crm;
4
5use Drupal\Core\Access\AccessResult;
6use Drupal\Core\Entity\EntityAccessControlHandler;
7use Drupal\Core\Entity\EntityInterface;
8use Drupal\Core\Session\AccountInterface;
9
10/**
11 * Defines the access control handler for the contact entity type.
12 */
13class 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    switch ($operation) {
26      case 'view label':
27        $permissions = ['view any crm_contact label'];
28        $permissions[] = "view any $bundle crm_contact label";
29
30        return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
31
32      case 'view':
33        $permissions = ['view any crm_contact'];
34        $permissions[] = "view any $bundle crm_contact";
35
36        return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
37
38      case 'update':
39        $permissions = ['edit any crm_contact'];
40        $permissions[] = "edit any $bundle crm_contact";
41
42        return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
43
44      case 'delete':
45        $permissions = ['delete any crm_contact'];
46        $permissions[] = "delete any $bundle crm_contact";
47
48        return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
49
50      default:
51        // No opinion.
52        return AccessResult::neutral();
53
54    }
55
56  }
57
58  /**
59   * {@inheritdoc}
60   */
61  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
62    return AccessResult::allowedIfHasPermissions(
63      $account,
64      ['create crm_contact', "create $entity_bundle crm_contact"],
65      'OR',
66    );
67  }
68
69}