Merge pull request #19956 from civicrm/5.36
[civicrm-core.git] / CRM / Core / Permission / Drupal6.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 *
20 */
21class CRM_Core_Permission_Drupal6 extends CRM_Core_Permission_DrupalBase {
22
23 /**
d09edf64 24 * Is this user someone with access for the entire system.
6a488035 25 *
b67daa72 26 * @var bool
6a488035
TO
27 */
28 protected $_viewAdminUser = FALSE;
29 protected $_editAdminUser = FALSE;
30
31 /**
100fef9d 32 * Am in in view permission or edit permission?
b67daa72 33 * @var bool
6a488035
TO
34 */
35 protected $_viewPermission = FALSE;
36 protected $_editPermission = FALSE;
37
38 /**
d09edf64 39 * The current set of permissioned groups for the user.
6a488035
TO
40 *
41 * @var array
42 */
43 protected $_viewPermissionedGroups;
44 protected $_editPermissionedGroups;
45
085823c1 46 /**
100fef9d 47 * Given a permission string, check for access requirements
085823c1 48 *
6a0b768e
TO
49 * @param string $str
50 * The permission to check.
085823c1 51 *
18be3201 52 * @param int $userId
77b97be7 53 *
317fceb4 54 * @return bool
a6c01b45 55 * true if yes, else false
085823c1 56 */
18be3201 57 public function check($str, $userId = NULL) {
be2fb01f 58 $str = $this->translatePermission($str, 'Drupal6', [
085823c1 59 'view user account' => 'access user profiles',
ccd74e76 60 'administer users' => 'administer users',
be2fb01f 61 ]);
085823c1
TO
62 if ($str == CRM_Core_Permission::ALWAYS_DENY_PERMISSION) {
63 return FALSE;
64 }
65 if ($str == CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION) {
66 return TRUE;
67 }
68 if (function_exists('user_access')) {
18be3201
CW
69 $account = NULL;
70 if ($userId) {
71 $account = user_load($userId);
72 }
73 return user_access($str, $account);
085823c1
TO
74 }
75 return TRUE;
76 }
77
6a488035
TO
78 /**
79 * Given a roles array, check for access requirements
80 *
6a0b768e
TO
81 * @param array $array
82 * The roles to check.
6a488035 83 *
317fceb4 84 * @return bool
a6c01b45 85 * true if yes, else false
6a488035 86 */
00be9182 87 public function checkGroupRole($array) {
6a488035 88 if (function_exists('user_load') && isset($array)) {
be2fb01f 89 $user = user_load(['uid' => $GLOBALS['user']->uid]);
6a488035
TO
90 //if giver roles found in user roles - return true
91 foreach ($array as $key => $value) {
92 if (in_array($value, $user->roles)) {
93 return TRUE;
94 }
95 }
96 }
97 return FALSE;
98 }
99
100 /**
d09edf64 101 * Get all the contact emails for users that have a specific role.
6a488035 102 *
6a0b768e
TO
103 * @param string $roleName
104 * Name of the role we are interested in.
6a488035 105 *
a6c01b45
CW
106 * @return string
107 * a comma separated list of email addresses
6a488035
TO
108 */
109 public function roleEmails($roleName) {
be2fb01f 110 static $_cache = [];
6a488035
TO
111
112 if (isset($_cache[$roleName])) {
113 return $_cache[$roleName];
114 }
115
be2fb01f 116 $uids = [];
6a488035
TO
117 $sql = "
118 SELECT {users}.uid
119 FROM {users}
120 LEFT JOIN {users_roles} ON {users}.uid = {users_roles}.uid
121 INNER JOIN {role} ON ( {role}.rid = {users_roles}.rid OR {role}.rid = 2 )
122 WHERE {role}. name LIKE '%%{$roleName}%%'
123 AND {users}.status = 1
124 ";
125
126 $query = db_query($sql);
127 while ($result = db_fetch_object($query)) {
2aa397bc
TO
128 $uids[] = $result->uid;
129 }
6a488035
TO
130
131 $_cache[$roleName] = self::getContactEmails($uids);
132 return $_cache[$roleName];
133 }
134
135 /**
d09edf64 136 * Get all the contact emails for users that have a specific permission.
6a488035 137 *
6a0b768e
TO
138 * @param string $permissionName
139 * Name of the permission we are interested in.
6a488035 140 *
a6c01b45
CW
141 * @return string
142 * a comma separated list of email addresses
6a488035
TO
143 */
144 public function permissionEmails($permissionName) {
be2fb01f 145 static $_cache = [];
6a488035
TO
146
147 if (isset($_cache[$permissionName])) {
148 return $_cache[$permissionName];
149 }
150
be2fb01f 151 $uids = [];
6a488035
TO
152 $sql = "
153 SELECT {users}.uid, {permission}.perm
154 FROM {users}
155 LEFT JOIN {users_roles} ON {users}.uid = {users_roles}.uid
156 INNER JOIN {permission} ON ( {permission}.rid = {users_roles}.rid OR {permission}.rid = 2 )
157 WHERE {permission}.perm LIKE '%%{$permissionName}%%'
158 AND {users}.status = 1
159 ";
160
161 $query = db_query($sql);
162 while ($result = db_fetch_object($query)) {
2aa397bc
TO
163 $uids[] = $result->uid;
164 }
6a488035 165
2aa397bc
TO
166 $_cache[$permissionName] = self::getContactEmails($uids);
167 return $_cache[$permissionName];
6a488035
TO
168 }
169
dea68872 170 /**
e7c15cb6 171 * @inheritDoc
dea68872
TO
172 */
173 public function isModulePermissionSupported() {
174 return TRUE;
175 }
176
6a488035 177 /**
e7c15cb6 178 * @inheritDoc
6a488035
TO
179 *
180 * Does nothing in Drupal 6.
181 */
00be9182 182 public function upgradePermissions($permissions) {
dea68872 183 // D6 allows us to be really lazy... things get cleaned up when the admin form is next submitted...
6a488035
TO
184 }
185
186 /**
187 * Get the permissions defined in the hook_civicrm_permission implementation
188 * of the given module.
189 *
da6b46f4
EM
190 * @param $module
191 *
16b10e64
CW
192 * @return array
193 * Array of permissions, in the same format as CRM_Core_Permission::getCorePermissions().
6a488035 194 */
ea54c6e5 195 public function getModulePermissions($module):array {
be2fb01f 196 $return_permissions = [];
6a488035
TO
197 $fn_name = "{$module}_civicrm_permission";
198 if (function_exists($fn_name)) {
199 $fn_name($return_permissions);
200 }
201 return $return_permissions;
202 }
96025800 203
6a488035 204}