Merge pull request #13258 from eileenmcnaughton/isam
[civicrm-core.git] / CRM / Core / Permission / Drupal.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 *
38 */
d3e86119 39class CRM_Core_Permission_Drupal extends CRM_Core_Permission_DrupalBase {
6a488035
TO
40
41 /**
d09edf64 42 * Is this user someone with access for the entire system.
6a488035
TO
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?
6a488035
TO
51 * @var boolean
52 */
53 protected $_viewPermission = FALSE;
54 protected $_editPermission = FALSE;
55
56 /**
d09edf64 57 * The current set of permissioned groups for the user.
6a488035
TO
58 *
59 * @var array
60 */
61 protected $_viewPermissionedGroups;
62 protected $_editPermissionedGroups;
63
64
085823c1 65 /**
100fef9d 66 * Given a permission string, check for access requirements
085823c1 67 *
6a0b768e
TO
68 * @param string $str
69 * The permission to check.
085823c1 70 *
18be3201 71 * @param int $userId
77b97be7 72 *
e7483cbe 73 * @return bool
a6c01b45 74 * true if yes, else false
085823c1 75 */
18be3201 76 public function check($str, $userId = NULL) {
085823c1
TO
77 $str = $this->translatePermission($str, 'Drupal', array(
78 'view user account' => 'access user profiles',
ccd74e76 79 'administer users' => 'administer users',
085823c1
TO
80 ));
81 if ($str == CRM_Core_Permission::ALWAYS_DENY_PERMISSION) {
82 return FALSE;
83 }
84 if ($str == CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION) {
85 return TRUE;
86 }
87 if (function_exists('user_access')) {
18be3201
CW
88 $account = NULL;
89 if ($userId) {
90 $account = user_load($userId);
91 }
92 return user_access($str, $account);
085823c1
TO
93 }
94 return TRUE;
95 }
6a488035
TO
96
97 /**
98 * Given a roles array, check for access requirements
99 *
6a0b768e
TO
100 * @param array $array
101 * The roles to check.
6a488035 102 *
e7483cbe 103 * @return bool
a6c01b45 104 * true if yes, else false
6a488035 105 */
00be9182 106 public function checkGroupRole($array) {
6a488035 107 if (function_exists('user_load') && isset($array)) {
481a74f4 108 $user = user_load($GLOBALS['user']->uid);
6a488035
TO
109 //if giver roles found in user roles - return true
110 foreach ($array as $key => $value) {
111 if (in_array($value, $user->roles)) {
112 return TRUE;
113 }
114 }
115 }
116 return FALSE;
117 }
118
7fccad46 119 /**
e7c15cb6 120 * @inheritDoc
7fccad46
TO
121 */
122 public function isModulePermissionSupported() {
123 return TRUE;
124 }
125
6a488035 126 /**
e7c15cb6 127 * @inheritDoc
6a488035 128 */
00be9182 129 public function upgradePermissions($permissions) {
0d8fc497
TO
130 if (empty($permissions)) {
131 throw new CRM_Core_Exception("Cannot upgrade permissions: permission list missing");
6a488035 132 }
0d8fc497
TO
133 $query = db_delete('role_permission')
134 ->condition('module', 'civicrm')
135 ->condition('permission', array_keys($permissions), 'NOT IN');
6a488035
TO
136 $query->execute();
137 }
d0c9a093
BS
138
139 /**
d09edf64 140 * Get all the contact emails for users that have a specific permission.
d0c9a093 141 *
6a0b768e
TO
142 * @param string $permissionName
143 * Name of the permission we are interested in.
d0c9a093 144 *
a6c01b45
CW
145 * @return string
146 * a comma separated list of email addresses
d0c9a093
BS
147 */
148 public function permissionEmails($permissionName) {
149 static $_cache = array();
150
151 if (isset($_cache[$permissionName])) {
152 return $_cache[$permissionName];
153 }
154
155 $uids = array();
156 $sql = "
157 SELECT {users}.uid, {role_permission}.permission
158 FROM {users}
159 JOIN {users_roles}
160 ON {users}.uid = {users_roles}.uid
161 JOIN {role_permission}
162 ON {role_permission}.rid = {users_roles}.rid
163 WHERE {role_permission}.permission = '{$permissionName}'
164 AND {users}.status = 1
165 ";
166
167 $result = db_query($sql);
481a74f4 168 foreach ($result as $record) {
d0c9a093
BS
169 $uids[] = $record->uid;
170 }
171
172 $_cache[$permissionName] = self::getContactEmails($uids);
173 return $_cache[$permissionName];
174 }
96025800 175
6a488035 176}