INFRA-132 - CRM/Core - phpcbf
[civicrm-core.git] / CRM / Core / Permission / DrupalBase.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 *
38 */
39class CRM_Core_Permission_DrupalBase extends CRM_Core_Permission_Base {
42762e35
DL
40
41 /**
100fef9d 42 * Is this user someone with access for the entire system
42762e35
DL
43 *
44 * @var boolean
45 */
46 protected $_viewAdminUser = FALSE;
47 protected $_editAdminUser = FALSE;
48
49 /**
100fef9d 50 * Am in in view permission or edit permission?
42762e35
DL
51 * @var boolean
52 */
53 protected $_viewPermission = FALSE;
54 protected $_editPermission = FALSE;
55
56 /**
100fef9d 57 * The current set of permissioned groups for the user
42762e35
DL
58 *
59 * @var array
60 */
61 protected $_viewPermissionedGroups;
62 protected $_editPermissionedGroups;
63
64 /**
65 * Get all groups from database, filtered by permissions
66 * for this user
67 *
6a0b768e
TO
68 * @param string $groupType
69 * Type of group(Access/Mailing).
70 * @param bool $excludeHidden
71 * Exclude hidden groups.
42762e35 72 *
42762e35
DL
73 *
74 * @return array - array reference of all groups.
42762e35
DL
75 */
76 public function group($groupType = NULL, $excludeHidden = TRUE) {
77 if (!isset($this->_viewPermissionedGroups)) {
78 $this->_viewPermissionedGroups = $this->_editPermissionedGroups = array();
79 }
80
81 $groupKey = $groupType ? $groupType : 'all';
82
83 if (!isset($this->_viewPermissionedGroups[$groupKey])) {
84 $this->_viewPermissionedGroups[$groupKey] = $this->_editPermissionedGroups[$groupKey] = array();
85
86 $groups = CRM_Core_PseudoConstant::allGroup($groupType, $excludeHidden);
87
88 if ($this->check('edit all contacts')) {
89 // this is the most powerful permission, so we return
90 // immediately rather than dilute it further
91 $this->_editAdminUser = $this->_viewAdminUser = TRUE;
92 $this->_editPermission = $this->_viewPermission = TRUE;
93 $this->_editPermissionedGroups[$groupKey] = $groups;
94 $this->_viewPermissionedGroups[$groupKey] = $groups;
95 return $this->_viewPermissionedGroups[$groupKey];
96 }
97 elseif ($this->check('view all contacts')) {
98 $this->_viewAdminUser = TRUE;
99 $this->_viewPermission = TRUE;
100 $this->_viewPermissionedGroups[$groupKey] = $groups;
101 }
102
42762e35
DL
103 $ids = CRM_ACL_API::group(CRM_Core_Permission::VIEW, NULL, 'civicrm_saved_search', $groups);
104 if (!empty($ids)) {
105 foreach (array_values($ids) as $id) {
106 $title = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $id, 'title');
107 $this->_viewPermissionedGroups[$groupKey][$id] = $title;
108 $this->_viewPermission = TRUE;
109 }
110 }
111
112 $ids = CRM_ACL_API::group(CRM_Core_Permission::EDIT, NULL, 'civicrm_saved_search', $groups);
113 if (!empty($ids)) {
114 foreach (array_values($ids) as $id) {
115 $title = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $id, 'title');
116 $this->_editPermissionedGroups[$groupKey][$id] = $title;
117 $this->_viewPermissionedGroups[$groupKey][$id] = $title;
118 $this->_editPermission = TRUE;
119 $this->_viewPermission = TRUE;
120 }
121 }
122 }
123
124 return $this->_viewPermissionedGroups[$groupKey];
125 }
126
127 /**
128 * Get group clause for this user. The group Clause filters the
129 * list of groups that the user is permitted to see in a group listing.
130 * For example it will filter both the list on the 'Manage Groups' page
131 * and on the contact 'Groups' tab
132 *
133 * the aclGroup hook & configured ACLs contribute to this data.
134 * If the contact is allowed to see all contacts the function will return ( 1 )
135 *
136 * @todo the history of this function is that there was some confusion as to
137 * whether it was filtering contacts or groups & some cruft may remain
138 *
6a0b768e
TO
139 * @param int $type
140 * The type of permission needed.
141 * @param array $tables
142 * (reference) add the tables that are needed for the select clause.
143 * @param array $whereTables
144 * (reference) add the tables that are needed for the where clause.
42762e35
DL
145 *
146 * @return string the clause to add to the query retrieving viewable groups
42762e35
DL
147 */
148 public function groupClause($type, &$tables, &$whereTables) {
149 if (!isset($this->_viewPermissionedGroups)) {
150 $this->group();
151 }
152
153 // we basically get all the groups here
154 $groupKey = 'all';
155 if ($type == CRM_Core_Permission::EDIT) {
156 if ($this->_editAdminUser) {
157 $clause = ' ( 1 ) ';
158 }
159 elseif (empty($this->_editPermissionedGroups[$groupKey])) {
160 $clause = ' ( 0 ) ';
161 }
162 else {
163 $clauses = array();
164 $groups = implode(', ', $this->_editPermissionedGroups[$groupKey]);
165 $clauses[] = ' ( civicrm_group_contact.group_id IN ( ' . implode(', ', array_keys($this->_editPermissionedGroups[$groupKey])) . " ) AND civicrm_group_contact.status = 'Added' ) ";
166 $tables['civicrm_group_contact'] = 1;
167 $whereTables['civicrm_group_contact'] = 1;
168
169 // foreach group that is potentially a saved search, add the saved search clause
170 foreach (array_keys($this->_editPermissionedGroups[$groupKey]) as $id) {
171 $group = new CRM_Contact_DAO_Group();
172 $group->id = $id;
173 if ($group->find(TRUE) && $group->saved_search_id) {
174 $clause = CRM_Contact_BAO_SavedSearch::whereClause($group->saved_search_id,
175 $tables,
176 $whereTables
177 );
178 if (trim($clause)) {
179 $clauses[] = $clause;
180 }
181 }
182 }
183 $clause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
184 }
185 }
186 else {
187 if ($this->_viewAdminUser) {
188 $clause = ' ( 1 ) ';
189 }
190 elseif (empty($this->_viewPermissionedGroups[$groupKey])) {
191 $clause = ' ( 0 ) ';
192 }
193 else {
194 $clauses = array();
195 $groups = implode(', ', $this->_viewPermissionedGroups[$groupKey]);
196 $clauses[] = ' civicrm_group.id IN (' . implode(', ', array_keys($this->_viewPermissionedGroups[$groupKey])) . " ) ";
197 $tables['civicrm_group'] = 1;
198 $whereTables['civicrm_group'] = 1;
199 $clause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
200 }
201 }
202
203 return $clause;
204 }
205
206 /**
100fef9d 207 * Get the current permission of this user
42762e35
DL
208 *
209 * @return string the permission of the user (edit or view or null)
210 */
211 public function getPermission() {
212 $this->group();
213
214 if ($this->_editPermission) {
215 return CRM_Core_Permission::EDIT;
216 }
217 elseif ($this->_viewPermission) {
218 return CRM_Core_Permission::VIEW;
219 }
220 return NULL;
221 }
222
a0ee3941
EM
223 /**
224 * @param $uids
225 *
226 * @return string
227 */
00be9182 228 public function getContactEmails($uids) {
42762e35
DL
229 if (empty($uids)) {
230 return '';
231 }
232 $uidString = implode(',', $uids);
233 $sql = "
234 SELECT e.email
235 FROM civicrm_contact c
236 INNER JOIN civicrm_email e ON ( c.id = e.contact_id AND e.is_primary = 1 )
237 INNER JOIN civicrm_uf_match uf ON ( c.id = uf.contact_id )
238 WHERE c.is_deceased = 0
239 AND c.is_deleted = 0
240 AND uf.uf_id IN ( $uidString )
241 ";
242
243 $dao = CRM_Core_DAO::executeQuery($sql);
244
245 $emails = array();
246 while ($dao->fetch()) {
2aa397bc 247 $emails[] = $dao->email;
42762e35
DL
248 }
249
250 return implode(', ', $emails);
251 }
68be1dfe
EM
252
253 /**
254 * Given a roles array, check for access requirements
255 *
6a0b768e
TO
256 * @param array $array
257 * The roles to check.
68be1dfe
EM
258 *
259 * @return boolean true if yes, else false
68be1dfe
EM
260 *
261 */
00be9182 262 public function checkGroupRole($array) {
68be1dfe
EM
263 if (function_exists('user_load') && isset($array)) {
264 $user = user_load( $GLOBALS['user']->uid);
265 //if giver roles found in user roles - return true
266 foreach ($array as $key => $value) {
267 if (in_array($value, $user->roles)) {
268 return TRUE;
269 }
270 }
271 }
272 return FALSE;
273 }
274
275 /**
276 * {@inheritDoc}
277 */
278 public function isModulePermissionSupported() {
279 return TRUE;
280 }
281
282 /**
283 * Get all the contact emails for users that have a specific permission
284 *
6a0b768e
TO
285 * @param string $permissionName
286 * Name of the permission we are interested in.
68be1dfe
EM
287 *
288 * @return string a comma separated list of email addresses
289 */
290 public function permissionEmails($permissionName) {
291 static $_cache = array();
292
293 if (isset($_cache[$permissionName])) {
294 return $_cache[$permissionName];
295 }
296
297 $uids = array();
298 $sql = "
299 SELECT {users}.uid, {role_permission}.permission
300 FROM {users}
301 JOIN {users_roles}
302 ON {users}.uid = {users_roles}.uid
303 JOIN {role_permission}
304 ON {role_permission}.rid = {users_roles}.rid
305 WHERE {role_permission}.permission = '{$permissionName}'
306 AND {users}.status = 1
307 ";
308
309 $result = db_query($sql);
310 foreach ( $result as $record ) {
311 $uids[] = $record->uid;
312 }
313
314 $_cache[$permissionName] = self::getContactEmails($uids);
315 return $_cache[$permissionName];
316 }
317
318 /**
319 * {@inheritdoc}
320 *
321 */
00be9182 322 public function upgradePermissions($permissions) {
68be1dfe
EM
323 if (empty($permissions)) {
324 throw new CRM_Core_Exception("Cannot upgrade permissions: permission list missing");
325 }
326 $query = db_delete('role_permission')
327 ->condition('module', 'civicrm')
328 ->condition('permission', array_keys($permissions), 'NOT IN');
329 $query->execute();
330 }
232624b1 331}