PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / libraries / adapter / acl / access.php

access.php in VikBooking Hotel Booking Engine & PMS trunk, at libraries/adapter/acl/access.php

227 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikWP - Libraries
4 * @subpackage adapter.acl
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Class that handles all access authorisation routines.
16 *
17 * @since 10.0
18 */
19 class JAccess
20 {
21 /**
22 * A list used to cache the users groups found.
23 *
24 * @var array
25 */
26 protected static $usersGroups = array();
27
28 /**
29 * Adjusts the default notation used by Joomla to the Wordpress needs.
30 *
31 * @param integer $groupId The path to the group for which to check authorisation.
32 * @param string $assetKey The asset name. Null fallback to root asset.
33 *
34 * @return string The rule name.
35 */
36 public static function adjustCapability($action, $assetKey = null)
37 {
38 /**
39 * In case the $assetKey is not provided, we should recover it from the
40 * request, because Joomla typical rules are not compatible with
41 * Wordpress capabilities. So, rules like "core.admin" must be converted
42 * into "com_[PLUGIN]_admin".
43 *
44 * @since 10.1.16
45 */
46 if (!$assetKey)
47 {
48 $assetKey = JFactory::getApplication()->input->get('option');
49 }
50
51 if ($assetKey)
52 {
53 $action = preg_replace("/^core/", $assetKey, $action);
54 }
55
56 $action = preg_replace("/\./", '_', $action);
57
58 return $action;
59 }
60
61 /**
62 * Method to check if a group is authorised to perform an action, optionally on an asset.
63 *
64 * @param string $groupId The slug of the user group.
65 * @param string $action The name of the action to authorise.
66 * @param string $assetKey The asset name. Null fallback to root asset.
67 *
68 * @return boolean True if authorised.
69 *
70 * @uses adjustCapability()
71 */
72 public static function checkGroup($groupId, $action, $assetKey = null)
73 {
74 $action = static::adjustCapability($action, $assetKey);
75
76 $role = get_role($groupId);
77
78 if (!$role)
79 {
80 return false;
81 }
82
83 return $role->has_cap($action);
84 }
85
86 /**
87 * Method to return a list of user groups mapped to a user. The returned list can optionally hold
88 * only the groups explicitly mapped to the user or all groups both explicitly mapped and inherited
89 * by the user.
90 *
91 * @param integer $userId Id of the user for which to get the list of groups.
92 *
93 * @return array List of user group ids to which the user is mapped.
94 */
95 public static function getGroupsByUser($userId)
96 {
97 if (!isset(static::$usersGroups[$userId]))
98 {
99 $user = get_user_by('id', $userId);
100
101 if ($user)
102 {
103 static::$usersGroups[$userId] = $user->roles;
104 }
105 else
106 {
107 static::$usersGroups[$userId] = array();
108 }
109 }
110
111 return static::$usersGroups[$userId];
112 }
113
114 /**
115 * Method to return a list of actions for which permissions can be set given a component and section.
116 *
117 * @param string $component The component from which to retrieve the actions.
118 * @param string $section The name of the section within the component from which to retrieve the actions.
119 *
120 * @return array List of actions available for the given component and section.
121 *
122 * @uses getActionsFromFile()
123 */
124 public static function getActions($component, $section = 'component')
125 {
126 $path = str_replace('/', DIRECTORY_SEPARATOR, WP_PLUGIN_DIR . "/$component/admin/access.xml");
127
128 return static::getActionsFromFile($path);
129 }
130
131 /**
132 * Method to return a list of actions from a file for which permissions can be set.
133 *
134 * @param string $file The path to the XML file.
135 *
136 * @return boolean|array False in case of error or the list of actions available.
137 *
138 * @uses getActionsFromData()
139 */
140 public static function getActionsFromFile($file)
141 {
142 // if unable to find the file return false
143 if (!is_file($file) || !is_readable($file))
144 {
145 return false;
146 }
147
148 // otherwise return the actions from the xml
149 $xml = simplexml_load_file($file);
150
151 return static::getActionsFromData($xml);
152 }
153
154 /**
155 * Method to return a list of actions from a string or from an xml for which permissions can be set.
156 *
157 * @param string|SimpleXMLElement $data The XML string or an XML element.
158 *
159 * @return boolean|array False in case of error or the list of actions available.
160 */
161 public static function getActionsFromData($data)
162 {
163 // if the data to load isn't already an XML element or string return false
164 if (!$data instanceof SimpleXMLElement && !is_string($data))
165 {
166 return false;
167 }
168
169 // attempt to load the XML if a string
170 if (is_string($data))
171 {
172 try
173 {
174 $data = new SimpleXMLElement($data);
175 }
176 catch (Exception $e)
177 {
178 return false;
179 }
180
181 // make sure the XML loaded correctly
182 if (!$data)
183 {
184 return false;
185 }
186 }
187
188 $list = array();
189
190 // $plugin = (string) $data->attributes()->component;
191
192 if (isset($data->section))
193 {
194 foreach ($data->section->action as $action)
195 {
196 $attrs = $action->attributes();
197
198 $rule = new stdClass;
199 $rule->name = (string) $attrs->name;
200 $rule->title = (string) $attrs->title;
201 $rule->description = (string) $attrs->description;
202
203 if (!empty($rule->title))
204 {
205 $rule->title = JText::translate($rule->title);
206 }
207 else
208 {
209 $rule->title = $rule->name;
210 }
211
212 if (!empty($rule->description))
213 {
214 $rule->description = JText::translate($rule->description);
215 }
216
217 if (!empty($rule->name))
218 {
219 $list[] = $rule;
220 }
221 }
222 }
223
224 return $list;
225 }
226 }
227