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
23 class CRM_Core_Permission_Joomla
extends CRM_Core_Permission_Base
{
26 * Given a permission string, check for access requirements
29 * The permission to check.
33 * true if yes, else false
35 public function check($str, $userId = NULL) {
36 $config = CRM_Core_Config
::singleton();
37 // JFactory::getUser does strict type checking, so convert falesy values to NULL
42 $translated = $this->translateJoomlaPermission($str);
43 if ($translated === CRM_Core_Permission
::ALWAYS_DENY_PERMISSION
) {
46 if ($translated === CRM_Core_Permission
::ALWAYS_ALLOW_PERMISSION
) {
50 // ensure that we are running in a joomla context
51 // we've not yet figured out how to bootstrap joomla, so we should
52 // not execute hooks if joomla is not loaded
53 if (defined('_JEXEC')) {
54 $user = JFactory
::getUser($userId);
55 $api_key = CRM_Utils_Request
::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST');
57 // If we are coming from REST we don't have a user but we do have the api_key for a user.
58 if ($user->id
=== 0 && !is_null($api_key)) {
59 // This is a codeblock copied from /Civicrm/Utils/REST
64 $contact_id = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $api_key, 'id', 'api_key');
67 $uid = CRM_Core_BAO_UFMatch
::getUFId($contact_id);
69 $user = JFactory
::getUser($uid);
74 return $user->authorise($translated[0], $translated[1]);
83 public function isModulePermissionSupported() {
90 * @internal param string $name e.g. "administer CiviCRM", "cms:access user record", "Drupal:administer content", "Joomla:example.action:com_some_asset"
91 * @return ALWAYS_DENY_PERMISSION|ALWAYS_ALLOW_PERMISSION|array(0 => $joomlaAction, 1 => $joomlaAsset)
93 public function translateJoomlaPermission($perm) {
94 if ($perm === CRM_Core_Permission
::ALWAYS_DENY_PERMISSION ||
$perm === CRM_Core_Permission
::ALWAYS_ALLOW_PERMISSION
) {
98 list ($civiPrefix, $name) = CRM_Utils_String
::parsePrefix(':', $perm, NULL);
99 switch ($civiPrefix) {
101 return explode(':', $name);
104 // FIXME: This needn't be DENY, but we don't currently have any translations.
105 return CRM_Core_Permission
::ALWAYS_DENY_PERMISSION
;
108 return ['civicrm.' . CRM_Utils_String
::munge(strtolower($name)), 'com_civicrm'];
111 return CRM_Core_Permission
::ALWAYS_DENY_PERMISSION
;
116 * Given a roles array, check for access requirements
118 * @param array $array
119 * The roles to check.
122 * true if yes, else false
124 public function checkGroupRole($array) {
131 public function upgradePermissions($permissions) {
132 $translatedPerms = [];
134 // Flipping the $permissions array gives us just the raw names of the
135 // permissions. The descriptions, etc., are irrelevant for the purposes of
137 foreach (array_flip($permissions) as $perm) {
138 $translated = $this->translateJoomlaPermission($perm);
139 $translatedPerms[] = $translated[0];
142 $associations = $this->getUserGroupPermsAssociations();
143 $cmsPermsHaveGoneStale = FALSE;
144 foreach (array_keys(get_object_vars($associations)) as $permName) {
145 if (!in_array($permName, $translatedPerms)) {
146 unset($associations->$permName);
147 $cmsPermsHaveGoneStale = TRUE;
151 if ($cmsPermsHaveGoneStale) {
152 $this->updateGroupPermsAssociations($associations);
157 * Fetches the associations between user groups and CiviCRM permissions.
159 * @see https://docs.joomla.org/Selecting_data_using_JDatabase
161 * Properties of the object are Joomla-fied permission names.
163 private function getUserGroupPermsAssociations() {
164 $db = JFactory
::getDbo();
165 $query = $db->getQuery(TRUE);
168 ->select($db->quoteName('rules'))
169 ->from($db->quoteName('#__assets'))
170 ->where($db->quoteName('name') . ' = ' . $db->quote('com_civicrm'));
172 $db->setQuery($query);
174 // Joomla gotcha: loadObject returns NULL in the case of no matches.
175 $result = $db->loadObject();
176 return $result ?
json_decode($result->rules
) : (object) [];
180 * Writes user-group/permissions associations back to Joomla.
182 * @see https://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase
183 * @param object $associations
184 * Same format as the return of
185 * CRM_Core_Permission_Joomla->getUserGroupPermsAssociations().
187 private function updateGroupPermsAssociations($associations) {
188 $db = JFactory
::getDbo();
189 $query = $db->getQuery(TRUE);
192 ->update($db->quoteName('#__assets'))
193 ->set($db->quoteName('rules') . ' = ' . $db->quote(json_encode($associations)))
194 ->where($db->quoteName('name') . ' = ' . $db->quote('com_civicrm'));
196 $db->setQuery($query)->execute();