3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 class CRM_Core_Permission_Backdrop
extends CRM_Core_Permission_DrupalBase
{
24 * Is this user someone with access for the entire system.
28 protected $_viewAdminUser = FALSE;
29 protected $_editAdminUser = FALSE;
32 * Am in in view permission or edit permission?
36 protected $_viewPermission = FALSE;
37 protected $_editPermission = FALSE;
40 * The current set of permissioned groups for the user.
44 protected $_viewPermissionedGroups;
45 protected $_editPermissionedGroups;
48 * Given a permission string, check for access requirements
51 * The permission to check.
56 * true if yes, else false
58 public function check($str, $userId = NULL) {
59 $str = $this->translatePermission($str, 'Drupal', [
60 'view user account' => 'access user profiles',
61 'administer users' => 'administer users',
63 if ($str == CRM_Core_Permission
::ALWAYS_DENY_PERMISSION
) {
66 if ($str == CRM_Core_Permission
::ALWAYS_ALLOW_PERMISSION
) {
69 if (function_exists('user_access')) {
72 $account = user_load($userId);
74 return user_access($str, $account);
80 * Given a roles array, check for access requirements
86 * true if yes, else false
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
)) {
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);
109 $modules = system_get_info('module');
110 foreach ($modules as $moduleName => $module) {
111 $prefix = isset($module['name']) ?
($module['name'] . ': ') : '';
112 foreach (module_invoke($moduleName, 'permission') ??
[] as $permName => $perm) {
113 if (isset($allCorePerms[$permName])) {
117 $permissions["Drupal:$permName"] = [
118 'title' => $prefix . strip_tags($perm['title']),
119 'description' => $perm['description'] ??
NULL,
129 public function isModulePermissionSupported() {
136 public function upgradePermissions($permissions) {
137 if (empty($permissions)) {
138 throw new CRM_Core_Exception("Cannot upgrade permissions: permission list missing");
142 $query = db_delete('role_permission')
143 ->condition('module', 'civicrm')
144 ->condition('permission', array_keys($permissions), 'NOT IN');
150 * Get all the contact emails for users that have a specific permission.
152 * @param string $permissionName
153 * Name of the permission we are interested in.
156 * a comma separated list of email addresses
158 public function permissionEmails($permissionName) {
161 if (isset($_cache[$permissionName])) {
162 return $_cache[$permissionName];
169 * SELECT {users}.uid, {role_permission}.permission
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
179 * $result = db_query($sql);
180 * foreach ($result as $record) {
181 * $uids[] = $record->uid;
184 * $_cache[$permissionName] = self::getContactEmails($uids);
185 * return $_cache[$permissionName];