Merge pull request #15881 from civicrm/5.20
[civicrm-core.git] / CRM / Core / Permission / Drupal.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 * $Id$
17 *
18 */
19
20/**
21 *
22 */
d3e86119 23class CRM_Core_Permission_Drupal extends CRM_Core_Permission_DrupalBase {
6a488035
TO
24
25 /**
d09edf64 26 * Is this user someone with access for the entire system.
6a488035 27 *
b67daa72 28 * @var bool
6a488035
TO
29 */
30 protected $_viewAdminUser = FALSE;
31 protected $_editAdminUser = FALSE;
32
33 /**
100fef9d 34 * Am in in view permission or edit permission?
b67daa72 35 * @var bool
6a488035
TO
36 */
37 protected $_viewPermission = FALSE;
38 protected $_editPermission = FALSE;
39
40 /**
d09edf64 41 * The current set of permissioned groups for the user.
6a488035
TO
42 *
43 * @var array
44 */
45 protected $_viewPermissionedGroups;
46 protected $_editPermissionedGroups;
47
085823c1 48 /**
100fef9d 49 * Given a permission string, check for access requirements
085823c1 50 *
6a0b768e
TO
51 * @param string $str
52 * The permission to check.
085823c1 53 *
18be3201 54 * @param int $userId
77b97be7 55 *
e7483cbe 56 * @return bool
a6c01b45 57 * true if yes, else false
085823c1 58 */
18be3201 59 public function check($str, $userId = NULL) {
be2fb01f 60 $str = $this->translatePermission($str, 'Drupal', [
085823c1 61 'view user account' => 'access user profiles',
ccd74e76 62 'administer users' => 'administer users',
be2fb01f 63 ]);
085823c1
TO
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')) {
18be3201
CW
71 $account = NULL;
72 if ($userId) {
73 $account = user_load($userId);
74 }
75 return user_access($str, $account);
085823c1
TO
76 }
77 return TRUE;
78 }
6a488035
TO
79
80 /**
81 * Given a roles array, check for access requirements
82 *
6a0b768e
TO
83 * @param array $array
84 * The roles to check.
6a488035 85 *
e7483cbe 86 * @return bool
a6c01b45 87 * true if yes, else false
6a488035 88 */
00be9182 89 public function checkGroupRole($array) {
6a488035 90 if (function_exists('user_load') && isset($array)) {
481a74f4 91 $user = user_load($GLOBALS['user']->uid);
6a488035
TO
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
7fccad46 102 /**
e7c15cb6 103 * @inheritDoc
7fccad46
TO
104 */
105 public function isModulePermissionSupported() {
106 return TRUE;
107 }
108
6a488035 109 /**
e7c15cb6 110 * @inheritDoc
6a488035 111 */
00be9182 112 public function upgradePermissions($permissions) {
0d8fc497
TO
113 if (empty($permissions)) {
114 throw new CRM_Core_Exception("Cannot upgrade permissions: permission list missing");
6a488035 115 }
0d8fc497
TO
116 $query = db_delete('role_permission')
117 ->condition('module', 'civicrm')
118 ->condition('permission', array_keys($permissions), 'NOT IN');
6a488035
TO
119 $query->execute();
120 }
d0c9a093
BS
121
122 /**
d09edf64 123 * Get all the contact emails for users that have a specific permission.
d0c9a093 124 *
6a0b768e
TO
125 * @param string $permissionName
126 * Name of the permission we are interested in.
d0c9a093 127 *
a6c01b45
CW
128 * @return string
129 * a comma separated list of email addresses
d0c9a093
BS
130 */
131 public function permissionEmails($permissionName) {
be2fb01f 132 static $_cache = [];
d0c9a093
BS
133
134 if (isset($_cache[$permissionName])) {
135 return $_cache[$permissionName];
136 }
137
be2fb01f 138 $uids = [];
d0c9a093
BS
139 $sql = "
140 SELECT {users}.uid, {role_permission}.permission
141 FROM {users}
142 JOIN {users_roles}
143 ON {users}.uid = {users_roles}.uid
144 JOIN {role_permission}
145 ON {role_permission}.rid = {users_roles}.rid
146 WHERE {role_permission}.permission = '{$permissionName}'
147 AND {users}.status = 1
148 ";
149
150 $result = db_query($sql);
481a74f4 151 foreach ($result as $record) {
d0c9a093
BS
152 $uids[] = $record->uid;
153 }
154
155 $_cache[$permissionName] = self::getContactEmails($uids);
156 return $_cache[$permissionName];
157 }
96025800 158
6a488035 159}