Commit | Line | Data |
---|---|---|
d3e88312 EM |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
d3e88312 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
d3e88312 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
d3e88312 EM |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
d3e88312 EM |
16 | * $Id$ |
17 | * | |
18 | */ | |
19 | ||
20 | /** | |
21 | * | |
22 | */ | |
d3e86119 | 23 | class CRM_Core_Permission_Drupal8 extends CRM_Core_Permission_DrupalBase { |
518fa0ee | 24 | |
68be1dfe EM |
25 | /** |
26 | * Given a permission string, check for access requirements | |
27 | * | |
6a0b768e TO |
28 | * @param string $str |
29 | * The permission to check. | |
68be1dfe | 30 | * |
18be3201 | 31 | * @param int $userId |
decced8b | 32 | * |
68be1dfe EM |
33 | * @return bool |
34 | */ | |
18be3201 | 35 | public function check($str, $userId = NULL) { |
be2fb01f | 36 | $str = $this->translatePermission($str, 'Drupal', [ |
68be1dfe | 37 | 'view user account' => 'access user profiles', |
be2fb01f | 38 | ]); |
68be1dfe EM |
39 | |
40 | if ($str == CRM_Core_Permission::ALWAYS_DENY_PERMISSION) { | |
41 | return FALSE; | |
42 | } | |
43 | if ($str == CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION) { | |
44 | return TRUE; | |
45 | } | |
18be3201 CW |
46 | $acct = $userId ? \Drupal\user\Entity\User::load($userId) : \Drupal::currentUser(); |
47 | return $acct->hasPermission($str); | |
68be1dfe | 48 | } |
96025800 | 49 | |
40d5632a AS |
50 | /** |
51 | * Get all the contact emails for users that have a specific permission. | |
52 | * | |
53 | * @param string $permissionName | |
54 | * Name of the permission we are interested in. | |
55 | * | |
56 | * @return string | |
57 | * a comma separated list of email addresses | |
58 | */ | |
59 | public function permissionEmails($permissionName) { | |
be2fb01f | 60 | static $_cache = []; |
40d5632a AS |
61 | |
62 | if (isset($_cache[$permissionName])) { | |
63 | return $_cache[$permissionName]; | |
64 | } | |
65 | ||
66 | $role_ids = array_map( | |
67 | function (\Drupal\user\RoleInterface $role) { | |
68 | return $role->id(); | |
69 | }, user_roles(TRUE, $permissionName) | |
70 | ); | |
be2fb01f | 71 | $users = \Drupal::entityTypeManager()->getStorage('user')->loadByProperties(['roles' => $role_ids]); |
40d5632a AS |
72 | $uids = array_keys($users); |
73 | ||
74 | $_cache[$permissionName] = self::getContactEmails($uids); | |
75 | return $_cache[$permissionName]; | |
76 | } | |
77 | ||
78 | /** | |
79 | * @inheritDoc | |
80 | */ | |
81 | public function upgradePermissions($permissions) { | |
82 | $civicrm_perms = array_keys(CRM_Core_Permission::getCorePermissions()); | |
83 | if (empty($civicrm_perms)) { | |
84 | throw new CRM_Core_Exception("Cannot upgrade permissions: permission list missing"); | |
85 | } | |
86 | ||
87 | $roles = user_roles(TRUE); | |
88 | foreach ($roles as $role) { | |
89 | foreach ($civicrm_perms as $permission) { | |
90 | $role->revokePermission($permission); | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
68be1dfe | 95 | } |