Merge pull request #16258 from samuelsov/i18ncountriesorder
[civicrm-core.git] / CRM / Core / Permission / Drupal.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_Drupal 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, 'Drupal', [
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($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 * @inheritDoc
104 */
105 public function isModulePermissionSupported() {
106 return TRUE;
107 }
108
109 /**
110 * @inheritDoc
111 */
112 public function upgradePermissions($permissions) {
113 if (empty($permissions)) {
114 throw new CRM_Core_Exception("Cannot upgrade permissions: permission list missing");
115 }
116 $query = db_delete('role_permission')
117 ->condition('module', 'civicrm')
118 ->condition('permission', array_keys($permissions), 'NOT IN');
119 $query->execute();
120 }
121
122 /**
123 * Get all the contact emails for users that have a specific permission.
124 *
125 * @param string $permissionName
126 * Name of the permission we are interested in.
127 *
128 * @return string
129 * a comma separated list of email addresses
130 */
131 public function permissionEmails($permissionName) {
132 static $_cache = [];
133
134 if (isset($_cache[$permissionName])) {
135 return $_cache[$permissionName];
136 }
137
138 $uids = [];
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);
151 foreach ($result as $record) {
152 $uids[] = $record->uid;
153 }
154
155 $_cache[$permissionName] = self::getContactEmails($uids);
156 return $_cache[$permissionName];
157 }
158
159 }