1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits |
4
|
|
|
of supporting developers from this source code or any supporting source code |
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xmf\Module\Helper; |
13
|
|
|
|
14
|
|
|
use Xmf\Module\Helper; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Methods to help manage permissions within a module |
18
|
|
|
* |
19
|
|
|
* @category Xmf\Module\Helper\Permission |
20
|
|
|
* @package Xmf |
21
|
|
|
* @author trabis <[email protected]> |
22
|
|
|
* @author Richard Griffith <[email protected]> |
23
|
|
|
* @copyright 2011-2018 XOOPS Project (https://xoops.org) |
24
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
25
|
|
|
* @link https://xoops.org |
26
|
|
|
*/ |
27
|
|
|
class Permission extends AbstractHelper |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $mid; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $dirname; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \XoopsGrouppermHandler |
41
|
|
|
*/ |
42
|
|
|
protected $permissionHandler; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Initialize parent::__construct calls this after verifying module object. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function init() |
50
|
|
|
{ |
51
|
|
|
$this->mid = $this->module->getVar('mid'); |
52
|
|
|
$this->dirname = $this->module->getVar('dirname'); |
53
|
|
|
/* @var $this->permissionHandler XoopsGroupPermHandler */ |
54
|
|
|
$this->permissionHandler = xoops_getHandler('groupperm'); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Check if the user has permission for an item |
59
|
|
|
* |
60
|
|
|
* @param string $gperm_name name of the permission to test |
61
|
|
|
* @param int $gperm_itemid id of the object to check |
62
|
|
|
* @param bool $trueifadmin true to always return true for admin groups |
63
|
|
|
* |
64
|
|
|
* @return bool true if user has access, false if not |
65
|
|
|
**/ |
66
|
|
|
public function checkPermission($gperm_name, $gperm_itemid, $trueifadmin = true) |
67
|
|
|
{ |
68
|
|
|
$gperm_itemid = (int) $gperm_itemid; |
69
|
|
|
$gperm_groupid = $this->getUserGroups(); |
70
|
|
|
|
71
|
|
|
return $this->permissionHandler->checkRight( |
72
|
|
|
$gperm_name, |
73
|
|
|
$gperm_itemid, |
74
|
|
|
$gperm_groupid, |
75
|
|
|
$this->mid, |
76
|
|
|
(bool) $trueifadmin |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get all item IDs for which a group (or set of groups) has a specific permission |
82
|
|
|
* Return an array of items for which the specified groups have the named permission |
83
|
|
|
* |
84
|
|
|
* @param string $gperm_name Name of permission |
85
|
|
|
* @param int|array $gperm_groupid A group ID or an array of group IDs |
86
|
|
|
* |
87
|
|
|
* @return array array of item IDs |
88
|
|
|
*/ |
89
|
|
|
public function getItemIds($gperm_name, $gperm_groupid) |
90
|
|
|
{ |
91
|
|
|
return $this->permissionHandler->getItemIds( |
92
|
|
|
$gperm_name, |
93
|
|
|
$gperm_groupid, |
94
|
|
|
$this->mid |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Redirect to a url if user does not have permission for an item |
100
|
|
|
* |
101
|
|
|
* @param string $gperm_name name of the permission to test |
102
|
|
|
* @param int $gperm_itemid id of the object to check |
103
|
|
|
* @param string $url module relative url to redirect to |
104
|
|
|
* @param int $time time in seconds to delay |
105
|
|
|
* @param string $message message to display with redirect |
106
|
|
|
* @param bool $trueifadmin true to always return true for admin groups |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
**/ |
110
|
|
|
public function checkPermissionRedirect( |
111
|
|
|
$gperm_name, |
112
|
|
|
$gperm_itemid, |
113
|
|
|
$url, |
114
|
|
|
$time = 3, |
115
|
|
|
$message = '', |
116
|
|
|
$trueifadmin = true |
117
|
|
|
) { |
118
|
|
|
$gperm_itemid = (int) $gperm_itemid; |
119
|
|
|
$gperm_groupid = $this->getUserGroups(); |
120
|
|
|
$permission = $this->permissionHandler->checkRight( |
121
|
|
|
$gperm_name, |
122
|
|
|
$gperm_itemid, |
123
|
|
|
$gperm_groupid, |
124
|
|
|
$this->mid, |
125
|
|
|
(bool) $trueifadmin |
126
|
|
|
); |
127
|
|
|
if (!$permission) { |
128
|
|
|
$helper = Helper::getHelper($this->dirname); |
129
|
|
|
$helper->redirect($url, $time, $message); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get array of groups with named permission to an item |
135
|
|
|
* |
136
|
|
|
* @param string $gperm_name name of the permission to test |
137
|
|
|
* @param int $gperm_itemid id of the object to check |
138
|
|
|
* |
139
|
|
|
* @return array groups with permission for item |
140
|
|
|
**/ |
141
|
|
|
public function getGroupsForItem($gperm_name, $gperm_itemid) |
142
|
|
|
{ |
143
|
|
|
$gperm_itemid = (int) $gperm_itemid; |
144
|
|
|
return $this->permissionHandler->getGroupIds($gperm_name, $gperm_itemid, $this->mid); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Save group permissions for an item |
149
|
|
|
* |
150
|
|
|
* @param string $gperm_name name of the permission to test |
151
|
|
|
* @param int $gperm_itemid id of the object to check |
152
|
|
|
* @param array $groups group ids to grant permission to |
153
|
|
|
* |
154
|
|
|
* @return bool true if no errors |
155
|
|
|
**/ |
156
|
|
|
public function savePermissionForItem($gperm_name, $gperm_itemid, $groups) |
157
|
|
|
{ |
158
|
|
|
$gperm_itemid = (int) $gperm_itemid; |
159
|
|
|
foreach ($groups as $index => $group) { |
160
|
|
|
$groups[$index] = (int) $group; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$result = true; |
164
|
|
|
|
165
|
|
|
// First, delete any existing permissions for this name and id |
166
|
|
|
$this->deletePermissionForItem($gperm_name, $gperm_itemid); |
167
|
|
|
|
168
|
|
|
// Save the new permissions |
169
|
|
|
if (count($groups) > 0) { |
170
|
|
|
foreach ($groups as $group_id) { |
171
|
|
|
$this->permissionHandler->addRight( |
172
|
|
|
$gperm_name, |
173
|
|
|
$gperm_itemid, |
174
|
|
|
$group_id, |
175
|
|
|
$this->mid |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $result; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Delete all permissions for an item and a specific name or array of names |
185
|
|
|
* |
186
|
|
|
* @param string|string[] $gperm_name name(s) of the permission to delete |
187
|
|
|
* @param int $gperm_itemid id of the object to check |
188
|
|
|
* |
189
|
|
|
* @return bool true if no errors |
190
|
|
|
*/ |
191
|
|
|
public function deletePermissionForItem($gperm_name, $gperm_itemid) |
192
|
|
|
{ |
193
|
|
|
$gperm_itemid = (int) $gperm_itemid; |
194
|
|
|
if (!is_array($gperm_name)) { |
195
|
|
|
$gperm_name = (array) $gperm_name; |
196
|
|
|
} |
197
|
|
|
$return = true; |
198
|
|
|
foreach ($gperm_name as $pname) { |
199
|
|
|
$return = $return && $this->permissionHandler->deleteByModule($this->mid, $pname, $gperm_itemid); |
200
|
|
|
} |
201
|
|
|
return $return; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Generate a XoopsFormElement to select groups to grant permission |
206
|
|
|
* to a specific gperm_name and gperm_item. Field will be preset |
207
|
|
|
* with existing permissions. |
208
|
|
|
* |
209
|
|
|
* @param string $gperm_name name of the permission to test |
210
|
|
|
* @param int $gperm_itemid id of the object to check |
211
|
|
|
* @param string $caption caption for form field |
212
|
|
|
* @param string $name name/id of form field |
213
|
|
|
* @param bool $include_anon true to include anonymous group |
214
|
|
|
* @param int $size size of list |
215
|
|
|
* @param bool $multiple true to allow multiple selections |
216
|
|
|
* |
217
|
|
|
* @return object XoopsFormSelectGroup |
218
|
|
|
*/ |
219
|
|
|
public function getGroupSelectFormForItem( |
220
|
|
|
$gperm_name, |
221
|
|
|
$gperm_itemid, |
222
|
|
|
$caption, |
223
|
|
|
$name = null, |
224
|
|
|
$include_anon = false, |
225
|
|
|
$size = 5, |
226
|
|
|
$multiple = true |
227
|
|
|
) { |
228
|
|
|
if (!class_exists('XoopsFormSelectGroup', true)) { |
229
|
|
|
include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php'; |
230
|
|
|
} |
231
|
|
|
if (empty($name)) { |
232
|
|
|
$name = $this->defaultFieldName($gperm_name, $gperm_itemid); |
233
|
|
|
} |
234
|
|
|
$gperm_itemid = (int) $gperm_itemid; |
235
|
|
|
$value = $this->getGroupsForItem($gperm_name, $gperm_itemid); |
236
|
|
|
$element = new \XoopsFormSelectGroup( |
237
|
|
|
$caption, |
238
|
|
|
$name, |
239
|
|
|
$include_anon, |
240
|
|
|
$value, |
241
|
|
|
$size, |
242
|
|
|
$multiple |
243
|
|
|
); |
244
|
|
|
|
245
|
|
|
return $element; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Generate a default name for a XoopsFormElement based on |
250
|
|
|
* module, gperm_name and gperm_itemid |
251
|
|
|
* |
252
|
|
|
* @param string $gperm_name name of the permission to test |
253
|
|
|
* @param int $gperm_itemid id of the object to check |
254
|
|
|
* |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
public function defaultFieldName($gperm_name, $gperm_itemid) |
258
|
|
|
{ |
259
|
|
|
$gperm_itemid = (int) $gperm_itemid; |
260
|
|
|
$name = $this->module->getVar('dirname') . '_' . |
261
|
|
|
$gperm_name . '_' . $gperm_itemid; |
262
|
|
|
|
263
|
|
|
return $name; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get any groups associated with the current user |
268
|
|
|
* |
269
|
|
|
* @return int|int[] group id(s) |
270
|
|
|
*/ |
271
|
|
|
protected function getUserGroups() |
272
|
|
|
{ |
273
|
|
|
global $xoopsUser; |
274
|
|
|
|
275
|
|
|
$groups = $xoopsUser ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS; |
276
|
|
|
|
277
|
|
|
return $groups; |
|
|
|
|
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..