| 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 | */ |
| 17 | |
| 18 | /** |
| 19 | * |
| 20 | */ |
| 21 | class CRM_Core_Permission_Backdrop extends CRM_Core_Permission_DrupalBase { |
| 22 | |
| 23 | /** |
| 24 | * Is this user someone with access for the entire system. |
| 25 | * |
| 26 | * @var bool |
| 27 | */ |
| 28 | protected $_viewAdminUser = FALSE; |
| 29 | protected $_editAdminUser = FALSE; |
| 30 | |
| 31 | /** |
| 32 | * Am in in view permission or edit permission? |
| 33 | * |
| 34 | * @var bool |
| 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 | |
| 47 | /** |
| 48 | * Given a permission string, check for access requirements |
| 49 | * |
| 50 | * @param string $str |
| 51 | * The permission to check. |
| 52 | * |
| 53 | * @param int $userId |
| 54 | * |
| 55 | * @return bool |
| 56 | * true if yes, else false |
| 57 | */ |
| 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', |
| 62 | ]); |
| 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')) { |
| 70 | $account = NULL; |
| 71 | if ($userId || $userId === 0) { |
| 72 | $account = user_load($userId); |
| 73 | } |
| 74 | return user_access($str, $account); |
| 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 | |
| 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'] . ': ') : ''; |
| 112 | foreach (module_invoke($moduleName, 'permission') ?? [] as $permName => $perm) { |
| 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 | |
| 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 | } |
| 140 | // FIXME!!!! |
| 141 | /* |
| 142 | $query = db_delete('role_permission') |
| 143 | ->condition('module', 'civicrm') |
| 144 | ->condition('permission', array_keys($permissions), 'NOT IN'); |
| 145 | $query->execute(); |
| 146 | */ |
| 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) { |
| 159 | static $_cache = []; |
| 160 | |
| 161 | if (isset($_cache[$permissionName])) { |
| 162 | return $_cache[$permissionName]; |
| 163 | } |
| 164 | |
| 165 | // FIXME!!!! |
| 166 | /** |
| 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]; |
| 186 | */ |
| 187 | return []; |
| 188 | } |
| 189 | |
| 190 | } |