Merge pull request #17655 from samuelsov/lab1827
[civicrm-core.git] / CRM / Core / Permission / Backdrop.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_Backdrop 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 *
36 * @var bool
37 */
38 protected $_viewPermission = FALSE;
39 protected $_editPermission = FALSE;
40
41 /**
42 * The current set of permissioned groups for the user.
43 *
44 * @var array
45 */
46 protected $_viewPermissionedGroups;
47 protected $_editPermissionedGroups;
48
49 /**
50 * Given a permission string, check for access requirements
51 *
52 * @param string $str
53 * The permission to check.
54 *
55 * @param int $userId
56 *
57 * @return bool
58 * true if yes, else false
59 */
60 public function check($str, $userId = NULL) {
61 $str = $this->translatePermission($str, 'Drupal', [
62 'view user account' => 'access user profiles',
63 'administer users' => 'administer users',
64 ]);
65 if ($str == CRM_Core_Permission::ALWAYS_DENY_PERMISSION) {
66 return FALSE;
67 }
68 if ($str == CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION) {
69 return TRUE;
70 }
71 if (function_exists('user_access')) {
72 $account = NULL;
73 if ($userId) {
74 $account = user_load($userId);
75 }
76 return user_access($str, $account);
77 }
78 return TRUE;
79 }
80
81 /**
82 * Given a roles array, check for access requirements
83 *
84 * @param array $array
85 * The roles to check.
86 *
87 * @return bool
88 * true if yes, else false
89 */
90 public function checkGroupRole($array) {
91 if (function_exists('user_load') && isset($array)) {
92 $user = user_load($GLOBALS['user']->uid);
93 //if giver roles found in user roles - return true
94 foreach ($array as $key => $value) {
95 if (in_array($value, $user->roles)) {
96 return TRUE;
97 }
98 }
99 }
100 return FALSE;
101 }
102
103 /**
104 * @inheritDoc
105 */
106 public function isModulePermissionSupported() {
107 return TRUE;
108 }
109
110 /**
111 * @inheritDoc
112 */
113 public function upgradePermissions($permissions) {
114 if (empty($permissions)) {
115 throw new CRM_Core_Exception("Cannot upgrade permissions: permission list missing");
116 }
117 // FIXME!!!!
118 /*
119 $query = db_delete('role_permission')
120 ->condition('module', 'civicrm')
121 ->condition('permission', array_keys($permissions), 'NOT IN');
122 $query->execute();
123 */
124 }
125
126 /**
127 * Get all the contact emails for users that have a specific permission.
128 *
129 * @param string $permissionName
130 * Name of the permission we are interested in.
131 *
132 * @return string
133 * a comma separated list of email addresses
134 */
135 public function permissionEmails($permissionName) {
136 static $_cache = [];
137
138 if (isset($_cache[$permissionName])) {
139 return $_cache[$permissionName];
140 }
141
142 // FIXME!!!!
143 /**
144 * $uids = array();
145 * $sql = "
146 * SELECT {users}.uid, {role_permission}.permission
147 * FROM {users}
148 * JOIN {users_roles}
149 * ON {users}.uid = {users_roles}.uid
150 * JOIN {role_permission}
151 * ON {role_permission}.rid = {users_roles}.rid
152 * WHERE {role_permission}.permission = '{$permissionName}'
153 * AND {users}.status = 1
154 * ";
155 *
156 * $result = db_query($sql);
157 * foreach ($result as $record) {
158 * $uids[] = $record->uid;
159 * }
160 *
161 * $_cache[$permissionName] = self::getContactEmails($uids);
162 * return $_cache[$permissionName];
163 */
164 return [];
165 }
166
167 }