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