Merge pull request #14033 from eileenmcnaughton/recur_cancel_api
[civicrm-core.git] / CRM / Core / Permission / Joomla.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 *
38 */
39class CRM_Core_Permission_Joomla extends CRM_Core_Permission_Base {
518fa0ee 40
6a488035 41 /**
100fef9d 42 * Given a permission string, check for access requirements
6a488035 43 *
6a0b768e
TO
44 * @param string $str
45 * The permission to check.
18be3201 46 * @param int $userId
6a488035 47 *
c301f76e 48 * @return bool
a6c01b45 49 * true if yes, else false
6a488035 50 */
18be3201 51 public function check($str, $userId = NULL) {
6a488035 52 $config = CRM_Core_Config::singleton();
18be3201
CW
53 // JFactory::getUser does strict type checking, so convert falesy values to NULL
54 if (!$userId) {
55 $userId = NULL;
56 }
6a488035 57
cc222cb6
TO
58 $translated = $this->translateJoomlaPermission($str);
59 if ($translated === CRM_Core_Permission::ALWAYS_DENY_PERMISSION) {
60 return FALSE;
61 }
62 if ($translated === CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION) {
63 return TRUE;
64 }
65
6a488035
TO
66 // ensure that we are running in a joomla context
67 // we've not yet figured out how to bootstrap joomla, so we should
68 // not execute hooks if joomla is not loaded
69 if (defined('_JEXEC')) {
18be3201 70 $user = JFactory::getUser($userId);
906e5a45 71 $api_key = CRM_Utils_Request::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST');
d37cd2a2 72
a386d65b 73 // If we are coming from REST we don't have a user but we do have the api_key for a user.
906e5a45 74 if ($user->id === 0 && !is_null($api_key)) {
a386d65b
EW
75 // This is a codeblock copied from /Civicrm/Utils/REST
76 $uid = NULL;
77 if (!$uid) {
518fa0ee 78 $store = NULL;
d37cd2a2 79
a386d65b 80 $contact_id = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $api_key, 'id', 'api_key');
d37cd2a2 81
a386d65b
EW
82 if ($contact_id) {
83 $uid = CRM_Core_BAO_UFMatch::getUFId($contact_id);
84 }
85 $user = JFactory::getUser($uid);
d37cd2a2
EW
86
87 }
88 }
89
c50bc0a1 90 return $user->authorise($translated[0], $translated[1]);
d37cd2a2 91
6a488035
TO
92 }
93 else {
d37cd2a2 94
a386d65b 95 return FALSE;
6a488035
TO
96 }
97 }
98
e5db5646
FG
99 public function isModulePermissionSupported() {
100 return TRUE;
101 }
102
cc222cb6 103 /**
77b97be7
EM
104 * @param $perm
105 *
106 * @internal param string $name e.g. "administer CiviCRM", "cms:access user record", "Drupal:administer content", "Joomla:example.action:com_some_asset"
cc222cb6
TO
107 * @return ALWAYS_DENY_PERMISSION|ALWAYS_ALLOW_PERMISSION|array(0 => $joomlaAction, 1 => $joomlaAsset)
108 */
00be9182 109 public function translateJoomlaPermission($perm) {
cc222cb6
TO
110 if ($perm === CRM_Core_Permission::ALWAYS_DENY_PERMISSION || $perm === CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION) {
111 return $perm;
112 }
113
114 list ($civiPrefix, $name) = CRM_Utils_String::parsePrefix(':', $perm, NULL);
22e263ad 115 switch ($civiPrefix) {
cc222cb6
TO
116 case 'Joomla':
117 return explode(':', $name);
2aa397bc 118
cc222cb6
TO
119 case 'cms':
120 // FIXME: This needn't be DENY, but we don't currently have any translations.
121 return CRM_Core_Permission::ALWAYS_DENY_PERMISSION;
2aa397bc 122
cc222cb6 123 case NULL:
be2fb01f 124 return ['civicrm.' . CRM_Utils_String::munge(strtolower($name)), 'com_civicrm'];
2aa397bc 125
cc222cb6
TO
126 default:
127 return CRM_Core_Permission::ALWAYS_DENY_PERMISSION;
128 }
129 }
130
6a488035
TO
131 /**
132 * Given a roles array, check for access requirements
133 *
6a0b768e
TO
134 * @param array $array
135 * The roles to check.
6a488035 136 *
c301f76e 137 * @return bool
a6c01b45 138 * true if yes, else false
6a488035 139 */
00be9182 140 public function checkGroupRole($array) {
6a488035
TO
141 return FALSE;
142 }
96025800 143
e5db5646
FG
144 /**
145 * @inheritDoc
146 */
147 public function upgradePermissions($permissions) {
be2fb01f 148 $translatedPerms = [];
b415fb17
FG
149
150 // Flipping the $permissions array gives us just the raw names of the
151 // permissions. The descriptions, etc., are irrelevant for the purposes of
152 // this method.
e5db5646
FG
153 foreach (array_flip($permissions) as $perm) {
154 $translated = $this->translateJoomlaPermission($perm);
155 $translatedPerms[] = $translated[0];
156 }
157
158 $associations = $this->getUserGroupPermsAssociations();
b415fb17 159 $cmsPermsHaveGoneStale = FALSE;
e5db5646
FG
160 foreach (array_keys(get_object_vars($associations)) as $permName) {
161 if (!in_array($permName, $translatedPerms)) {
162 unset($associations->$permName);
b415fb17 163 $cmsPermsHaveGoneStale = TRUE;
e5db5646
FG
164 }
165 }
166
b415fb17 167 if ($cmsPermsHaveGoneStale) {
e5db5646
FG
168 $this->updateGroupPermsAssociations($associations);
169 }
170 }
171
172 /**
173 * Fetches the associations between user groups and CiviCRM permissions.
174 *
b415fb17 175 * @see https://docs.joomla.org/Selecting_data_using_JDatabase
f746881c 176 * @return object
e5db5646
FG
177 * Properties of the object are Joomla-fied permission names.
178 */
179 private function getUserGroupPermsAssociations() {
e5db5646 180 $db = JFactory::getDbo();
f746881c 181 $query = $db->getQuery(TRUE);
e5db5646 182
e5db5646 183 $query
518fa0ee
SL
184 ->select($db->quoteName('rules'))
185 ->from($db->quoteName('#__assets'))
186 ->where($db->quoteName('name') . ' = ' . $db->quote('com_civicrm'));
e5db5646 187
e5db5646
FG
188 $db->setQuery($query);
189
76d556c7
FG
190 // Joomla gotcha: loadObject returns NULL in the case of no matches.
191 $result = $db->loadObject();
be2fb01f 192 return $result ? json_decode($result->rules) : (object) [];
e5db5646
FG
193 }
194
195 /**
196 * Writes user-group/permissions associations back to Joomla.
197 *
b415fb17 198 * @see https://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase
f746881c
FG
199 * @param object $associations
200 * Same format as the return of
201 * CRM_Core_Permission_Joomla->getUserGroupPermsAssociations().
e5db5646 202 */
a1f60f01 203 private function updateGroupPermsAssociations($associations) {
e5db5646 204 $db = JFactory::getDbo();
f746881c 205 $query = $db->getQuery(TRUE);
e5db5646 206
e5db5646 207 $query
518fa0ee
SL
208 ->update($db->quoteName('#__assets'))
209 ->set($db->quoteName('rules') . ' = ' . $db->quote(json_encode($associations)))
210 ->where($db->quoteName('name') . ' = ' . $db->quote('com_civicrm'));
e5db5646
FG
211
212 $db->setQuery($query)->execute();
213 }
214
6a488035 215}