Merge pull request #24022 from colemanw/afformFrontend
[civicrm-core.git] / CRM / Core / Permission / Backdrop.php
CommitLineData
84fce21c
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
84fce21c 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 |
84fce21c
TO
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
84fce21c
TO
16 */
17
18/**
19 *
20 */
21class CRM_Core_Permission_Backdrop extends CRM_Core_Permission_DrupalBase {
22
23 /**
24 * Is this user someone with access for the entire system.
25 *
b67daa72 26 * @var bool
84fce21c
TO
27 */
28 protected $_viewAdminUser = FALSE;
29 protected $_editAdminUser = FALSE;
30
31 /**
32 * Am in in view permission or edit permission?
b67daa72 33 *
34 * @var bool
84fce21c
TO
35 */
36 protected $_viewPermission = FALSE;
37 protected $_editPermission = FALSE;
38
39 /**
40 * The current set of permissioned groups for the user.
41 *
42 * @var array
43 */
44 protected $_viewPermissionedGroups;
45 protected $_editPermissionedGroups;
46
84fce21c
TO
47 /**
48 * Given a permission string, check for access requirements
49 *
50 * @param string $str
51 * The permission to check.
52 *
18be3201 53 * @param int $userId
84fce21c
TO
54 *
55 * @return bool
56 * true if yes, else false
57 */
18be3201 58 public function check($str, $userId = NULL) {
be2fb01f 59 $str = $this->translatePermission($str, 'Drupal', [
84fce21c
TO
60 'view user account' => 'access user profiles',
61 'administer users' => 'administer users',
be2fb01f 62 ]);
84fce21c
TO
63 if ($str == CRM_Core_Permission::ALWAYS_DENY_PERMISSION) {
64 return FALSE;
65 }
66 if ($str == CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION) {
67 return TRUE;
68 }
69 if (function_exists('user_access')) {
18be3201 70 $account = NULL;
ae6d2c8e 71 if ($userId || $userId === 0) {
18be3201
CW
72 $account = user_load($userId);
73 }
74 return user_access($str, $account);
84fce21c
TO
75 }
76 return TRUE;
77 }
78
79 /**
80 * Given a roles array, check for access requirements
81 *
82 * @param array $array
83 * The roles to check.
84 *
85 * @return bool
86 * true if yes, else false
87 */
88 public function checkGroupRole($array) {
89 if (function_exists('user_load') && isset($array)) {
90 $user = user_load($GLOBALS['user']->uid);
91 //if giver roles found in user roles - return true
92 foreach ($array as $key => $value) {
93 if (in_array($value, $user->roles)) {
94 return TRUE;
95 }
96 }
97 }
98 return FALSE;
99 }
100
0247a000
TO
101 /**
102 * @inheritDoc
103 */
104 public function getAvailablePermissions() {
105 // We want to list *only* Backdrop perms, so we'll *skip* Civi perms.
106 $allCorePerms = \CRM_Core_Permission::basicPermissions(TRUE);
107
108 $permissions = [];
109 $modules = system_get_info('module');
110 foreach ($modules as $moduleName => $module) {
111 $prefix = isset($module['name']) ? ($module['name'] . ': ') : '';
5fba1c58 112 foreach (module_invoke($moduleName, 'permission') ?? [] as $permName => $perm) {
0247a000
TO
113 if (isset($allCorePerms[$permName])) {
114 continue;
115 }
116
117 $permissions["Drupal:$permName"] = [
118 'title' => $prefix . strip_tags($perm['title']),
119 'description' => $perm['description'] ?? NULL,
120 ];
121 }
122 }
123 return $permissions;
124 }
125
84fce21c
TO
126 /**
127 * @inheritDoc
128 */
129 public function isModulePermissionSupported() {
130 return TRUE;
131 }
132
133 /**
134 * @inheritDoc
135 */
136 public function upgradePermissions($permissions) {
137 if (empty($permissions)) {
138 throw new CRM_Core_Exception("Cannot upgrade permissions: permission list missing");
139 }
5c6ebf4c
TO
140 // FIXME!!!!
141 /*
84fce21c 142 $query = db_delete('role_permission')
5c6ebf4c
TO
143 ->condition('module', 'civicrm')
144 ->condition('permission', array_keys($permissions), 'NOT IN');
84fce21c 145 $query->execute();
5c6ebf4c 146 */
84fce21c
TO
147 }
148
149 /**
150 * Get all the contact emails for users that have a specific permission.
151 *
152 * @param string $permissionName
153 * Name of the permission we are interested in.
154 *
155 * @return string
156 * a comma separated list of email addresses
157 */
158 public function permissionEmails($permissionName) {
be2fb01f 159 static $_cache = [];
84fce21c
TO
160
161 if (isset($_cache[$permissionName])) {
162 return $_cache[$permissionName];
163 }
164
5c6ebf4c
TO
165 // FIXME!!!!
166 /**
518fa0ee
SL
167 * $uids = array();
168 * $sql = "
169 * SELECT {users}.uid, {role_permission}.permission
170 * FROM {users}
171 * JOIN {users_roles}
172 * ON {users}.uid = {users_roles}.uid
173 * JOIN {role_permission}
174 * ON {role_permission}.rid = {users_roles}.rid
175 * WHERE {role_permission}.permission = '{$permissionName}'
176 * AND {users}.status = 1
177 * ";
178 *
179 * $result = db_query($sql);
180 * foreach ($result as $record) {
181 * $uids[] = $record->uid;
182 * }
183 *
184 * $_cache[$permissionName] = self::getContactEmails($uids);
185 * return $_cache[$permissionName];
5c6ebf4c 186 */
be2fb01f 187 return [];
84fce21c
TO
188 }
189
190}