Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
CrmContactTypePermissions
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
4 / 4
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 contactTypePermissions
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 buildPermissions
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Drupal\crm;
4
5use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6use Drupal\Core\Entity\BundlePermissionHandlerTrait;
7use Drupal\Core\Entity\EntityTypeManagerInterface;
8use Drupal\Core\StringTranslation\StringTranslationTrait;
9use Symfony\Component\DependencyInjection\ContainerInterface;
10
11/**
12 * Provides dynamic permissions for each contact type.
13 */
14class CrmContactTypePermissions implements ContainerInjectionInterface {
15  use BundlePermissionHandlerTrait;
16  use StringTranslationTrait;
17
18  /**
19   * The entity type manager service.
20   *
21   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
22   */
23  protected $entityTypeManager;
24
25  /**
26   * CrmContactTypePermissions constructor.
27   *
28   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
29   *   The entity type manager service.
30   */
31  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
32    $this->entityTypeManager = $entity_type_manager;
33  }
34
35  /**
36   * {@inheritdoc}
37   */
38  public static function create(ContainerInterface $container) {
39    return new static($container->get('entity_type.manager'));
40  }
41
42  /**
43   * Returns an array of contact type permissions.
44   *
45   * @return array
46   *   The contact type permissions.
47   */
48  public function contactTypePermissions() {
49    $contact_types = $this->entityTypeManager->getStorage('crm_contact_type')->loadMultiple();
50    return $this->generatePermissions($contact_types, [$this, 'buildPermissions']);
51  }
52
53  /**
54   * Returns a list of permissions for a given contact type.
55   *
56   * @param \Drupal\crm\CrmContactTypeInterface $type
57   *   The contact type.
58   *
59   * @return array
60   *   An associative array of permission names and descriptions.
61   */
62  protected function buildPermissions($type) {
63    $type_id = $type->id();
64    $type_params = ['%type_name' => $type->label()];
65
66    return [
67      "create $type_id crm_contact" => [
68        'title' => $this->t('%type_name: Create new contact', $type_params),
69      ],
70      "edit any $type_id crm_contact" => [
71        'title' => $this->t('%type_name: Edit any contact', $type_params),
72      ],
73      "delete any $type_id crm_contact" => [
74        'title' => $this->t('%type_name: Delete any contact', $type_params),
75      ],
76      "view any $type_id crm_contact" => [
77        'title' => $this->t('%type_name: View any contact', $type_params),
78      ],
79      "view any $type_id crm_contact label" => [
80        'title' => $this->t('%type_name: View any contact', $type_params),
81      ],
82    ];
83  }
84
85}