Merge pull request #19487 from civicrm/5.34
[civicrm-core.git] / CRM / Core / Permission / DrupalBase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 *
20 */
21 class CRM_Core_Permission_DrupalBase extends CRM_Core_Permission_Base {
22
23 /**
24 * @param $uids
25 *
26 * @return string
27 */
28 public function getContactEmails($uids) {
29 if (empty($uids)) {
30 return '';
31 }
32 $uidString = implode(',', $uids);
33 $sql = "
34 SELECT e.email
35 FROM civicrm_contact c
36 INNER JOIN civicrm_email e ON ( c.id = e.contact_id AND e.is_primary = 1 )
37 INNER JOIN civicrm_uf_match uf ON ( c.id = uf.contact_id )
38 WHERE c.is_deceased = 0
39 AND c.is_deleted = 0
40 AND uf.uf_id IN ( $uidString )
41 ";
42
43 $dao = CRM_Core_DAO::executeQuery($sql);
44
45 $emails = [];
46 while ($dao->fetch()) {
47 $emails[] = $dao->email;
48 }
49
50 return implode(', ', $emails);
51 }
52
53 /**
54 * Given a roles array, check for access requirements
55 *
56 * @param array $array
57 * The roles to check.
58 *
59 * @return bool
60 * true if yes, else false
61 */
62 public function checkGroupRole($array) {
63 if (function_exists('user_load') && isset($array)) {
64 $user = user_load($GLOBALS['user']->uid);
65 //if giver roles found in user roles - return true
66 foreach ($array as $key => $value) {
67 if (in_array($value, $user->roles)) {
68 return TRUE;
69 }
70 }
71 }
72 return FALSE;
73 }
74
75 /**
76 * @inheritDoc
77 */
78 public function isModulePermissionSupported() {
79 return TRUE;
80 }
81
82 /**
83 * Get all the contact emails for users that have a specific permission.
84 *
85 * @param string $permissionName
86 * Name of the permission we are interested in.
87 *
88 * @return string
89 * a comma separated list of email addresses
90 */
91 public function permissionEmails($permissionName) {
92 static $_cache = [];
93
94 if (isset($_cache[$permissionName])) {
95 return $_cache[$permissionName];
96 }
97
98 $uids = [];
99 $sql = "
100 SELECT {users}.uid, {role_permission}.permission
101 FROM {users}
102 JOIN {users_roles}
103 ON {users}.uid = {users_roles}.uid
104 JOIN {role_permission}
105 ON {role_permission}.rid = {users_roles}.rid
106 WHERE {role_permission}.permission = '{$permissionName}'
107 AND {users}.status = 1
108 ";
109
110 $result = db_query($sql);
111 foreach ($result as $record) {
112 $uids[] = $record->uid;
113 }
114
115 $_cache[$permissionName] = self::getContactEmails($uids);
116 return $_cache[$permissionName];
117 }
118
119 /**
120 * @inheritDoc
121 */
122 public function upgradePermissions($permissions) {
123 if (empty($permissions)) {
124 throw new CRM_Core_Exception("Cannot upgrade permissions: permission list missing");
125 }
126 $query = db_delete('role_permission')
127 ->condition('module', 'civicrm')
128 ->condition('permission', array_keys($permissions), 'NOT IN');
129 $query->execute();
130 }
131
132 }