Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContactAccessControlHandler
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
110
0.00% covered (danger)
0.00%
0 / 1
 checkAccess
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
72
 checkCreateAccess
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 isOwner
0.00% covered (danger)
0.00%
0 / 1
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
25    switch ($operation) {
26      case 'view label':
27        $permissions = ['view any crm_contact label'];
28
29        return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
30
31      case 'view':
32        $permissions = ['view any crm_contact'];
33
34        return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
35
36      case 'update':
37        $permissions = ['edit any crm_contact'];
38        if ($this->isOwner($entity, $account)) {
39          $permissions[] = 'edit own crm_contact';
40        }
41        return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR',
42        );
43
44      case 'delete':
45        $permissions = ['delete any crm_contact'];
46        if ($this->isOwner($entity, $account)) {
47          $permissions[] = 'delete own crm_contact';
48        }
49        return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR',
50        );
51
52      default:
53        // No opinion.
54        return AccessResult::neutral();
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', 'administer contact'],
66      'OR',
67    );
68  }
69
70  /**
71   * Is the given entity owned by the given account?
72   *
73   * @param \Drupal\Core\Entity\EntityInterface $entity
74   *   The entity to check.
75   * @param \Drupal\Core\Session\AccountInterface $account
76   *   The account to check.
77   *
78   * @return bool
79   *   TRUE if the entity is owned by the account, FALSE otherwise.
80   */
81  protected function isOwner(EntityInterface $entity, AccountInterface $account): bool {
82    return $entity->getOwnerId() === $account->id();
83  }
84
85}