3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC https://civicrm.org/licensing
39 class CRM_Core_Permission_DrupalBase
extends CRM_Core_Permission_Base
{
42 * Is this user someone with access for the entire system.
46 protected $_viewAdminUser = FALSE;
47 protected $_editAdminUser = FALSE;
50 * Am in in view permission or edit permission?
54 protected $_viewPermission = FALSE;
55 protected $_editPermission = FALSE;
58 * The current set of permissioned groups for the user.
62 protected $_viewPermissionedGroups;
63 protected $_editPermissionedGroups;
66 * Get all groups from database, filtered by permissions
69 * @param string $groupType
70 * Type of group(Access/Mailing).
71 * @param bool $excludeHidden
72 * Exclude hidden groups.
76 * array reference of all groups.
78 public function group($groupType = NULL, $excludeHidden = TRUE) {
79 if (!isset($this->_viewPermissionedGroups
)) {
80 $this->_viewPermissionedGroups
= $this->_editPermissionedGroups
= [];
83 $groupKey = $groupType ?
$groupType : 'all';
85 if (!isset($this->_viewPermissionedGroups
[$groupKey])) {
86 $this->_viewPermissionedGroups
[$groupKey] = $this->_editPermissionedGroups
[$groupKey] = [];
88 $groups = CRM_Core_PseudoConstant
::allGroup($groupType, $excludeHidden);
90 if ($this->check('edit all contacts')) {
91 // this is the most powerful permission, so we return
92 // immediately rather than dilute it further
93 $this->_editAdminUser
= $this->_viewAdminUser
= TRUE;
94 $this->_editPermission
= $this->_viewPermission
= TRUE;
95 $this->_editPermissionedGroups
[$groupKey] = $groups;
96 $this->_viewPermissionedGroups
[$groupKey] = $groups;
97 return $this->_viewPermissionedGroups
[$groupKey];
99 elseif ($this->check('view all contacts')) {
100 $this->_viewAdminUser
= TRUE;
101 $this->_viewPermission
= TRUE;
102 $this->_viewPermissionedGroups
[$groupKey] = $groups;
105 $ids = CRM_ACL_API
::group(CRM_Core_Permission
::VIEW
, NULL, 'civicrm_saved_search', $groups);
107 foreach (array_values($ids) as $id) {
108 $title = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $id, 'title');
109 $this->_viewPermissionedGroups
[$groupKey][$id] = $title;
110 $this->_viewPermission
= TRUE;
114 $ids = CRM_ACL_API
::group(CRM_Core_Permission
::EDIT
, NULL, 'civicrm_saved_search', $groups);
116 foreach (array_values($ids) as $id) {
117 $title = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $id, 'title');
118 $this->_editPermissionedGroups
[$groupKey][$id] = $title;
119 $this->_viewPermissionedGroups
[$groupKey][$id] = $title;
120 $this->_editPermission
= TRUE;
121 $this->_viewPermission
= TRUE;
126 return $this->_viewPermissionedGroups
[$groupKey];
130 * Get group clause for this user. The group Clause filters the
131 * list of groups that the user is permitted to see in a group listing.
132 * For example it will filter both the list on the 'Manage Groups' page
133 * and on the contact 'Groups' tab
135 * the aclGroup hook & configured ACLs contribute to this data.
136 * If the contact is allowed to see all contacts the function will return ( 1 )
138 * @todo the history of this function is that there was some confusion as to
139 * whether it was filtering contacts or groups & some cruft may remain
142 * The type of permission needed.
143 * @param array $tables
144 * (reference) add the tables that are needed for the select clause.
145 * @param array $whereTables
146 * (reference) add the tables that are needed for the where clause.
149 * the clause to add to the query retrieving viewable groups
151 public function groupClause($type, &$tables, &$whereTables) {
152 if (!isset($this->_viewPermissionedGroups
)) {
156 // we basically get all the groups here
158 if ($type == CRM_Core_Permission
::EDIT
) {
159 if ($this->_editAdminUser
) {
162 elseif (empty($this->_editPermissionedGroups
[$groupKey])) {
167 $groups = implode(', ', $this->_editPermissionedGroups
[$groupKey]);
168 $clauses[] = ' ( civicrm_group_contact.group_id IN ( ' . implode(', ', array_keys($this->_editPermissionedGroups
[$groupKey])) . " ) AND civicrm_group_contact.status = 'Added' ) ";
169 $tables['civicrm_group_contact'] = 1;
170 $whereTables['civicrm_group_contact'] = 1;
172 // foreach group that is potentially a saved search, add the saved search clause
173 foreach (array_keys($this->_editPermissionedGroups
[$groupKey]) as $id) {
174 $group = new CRM_Contact_DAO_Group();
176 if ($group->find(TRUE) && $group->saved_search_id
) {
177 $clause = CRM_Contact_BAO_SavedSearch
::whereClause($group->saved_search_id
,
182 $clauses[] = $clause;
186 $clause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
190 if ($this->_viewAdminUser
) {
193 elseif (empty($this->_viewPermissionedGroups
[$groupKey])) {
198 $groups = implode(', ', $this->_viewPermissionedGroups
[$groupKey]);
199 $clauses[] = ' civicrm_group.id IN (' . implode(', ', array_keys($this->_viewPermissionedGroups
[$groupKey])) . " ) ";
200 $tables['civicrm_group'] = 1;
201 $whereTables['civicrm_group'] = 1;
202 $clause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
210 * Get the current permission of this user.
213 * the permission of the user (edit or view or null)
215 public function getPermission() {
218 if ($this->_editPermission
) {
219 return CRM_Core_Permission
::EDIT
;
221 elseif ($this->_viewPermission
) {
222 return CRM_Core_Permission
::VIEW
;
232 public function getContactEmails($uids) {
236 $uidString = implode(',', $uids);
239 FROM civicrm_contact c
240 INNER JOIN civicrm_email e ON ( c.id = e.contact_id AND e.is_primary = 1 )
241 INNER JOIN civicrm_uf_match uf ON ( c.id = uf.contact_id )
242 WHERE c.is_deceased = 0
244 AND uf.uf_id IN ( $uidString )
247 $dao = CRM_Core_DAO
::executeQuery($sql);
250 while ($dao->fetch()) {
251 $emails[] = $dao->email
;
254 return implode(', ', $emails);
258 * Given a roles array, check for access requirements
260 * @param array $array
261 * The roles to check.
264 * true if yes, else false
266 public function checkGroupRole($array) {
267 if (function_exists('user_load') && isset($array)) {
268 $user = user_load($GLOBALS['user']->uid
);
269 //if giver roles found in user roles - return true
270 foreach ($array as $key => $value) {
271 if (in_array($value, $user->roles
)) {
282 public function isModulePermissionSupported() {
287 * Get all the contact emails for users that have a specific permission.
289 * @param string $permissionName
290 * Name of the permission we are interested in.
293 * a comma separated list of email addresses
295 public function permissionEmails($permissionName) {
298 if (isset($_cache[$permissionName])) {
299 return $_cache[$permissionName];
304 SELECT {users}.uid, {role_permission}.permission
307 ON {users}.uid = {users_roles}.uid
308 JOIN {role_permission}
309 ON {role_permission}.rid = {users_roles}.rid
310 WHERE {role_permission}.permission = '{$permissionName}'
311 AND {users}.status = 1
314 $result = db_query($sql);
315 foreach ($result as $record) {
316 $uids[] = $record->uid
;
319 $_cache[$permissionName] = self
::getContactEmails($uids);
320 return $_cache[$permissionName];
326 public function upgradePermissions($permissions) {
327 if (empty($permissions)) {
328 throw new CRM_Core_Exception("Cannot upgrade permissions: permission list missing");
330 $query = db_delete('role_permission')
331 ->condition('module', 'civicrm')
332 ->condition('permission', array_keys($permissions), 'NOT IN');