Total Complexity | 60 |
Total Lines | 377 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PermissionHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PermissionHandler, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
35 | class PermissionHandler extends \XoopsPersistableObjectHandler |
||
36 | { |
||
37 | /** |
||
38 | * Constructor |
||
39 | * |
||
40 | * @param null |
||
41 | */ |
||
42 | public function __construct() |
||
43 | { |
||
44 | } |
||
45 | |||
46 | |||
47 | /** |
||
48 | * @private function getPermSubmit |
||
49 | * returns right to submit for given perm |
||
50 | * @param $constantPerm |
||
51 | * @return bool |
||
52 | */ |
||
53 | private function getPerm($constantPerm) |
||
54 | { |
||
55 | global $xoopsUser; |
||
56 | |||
57 | $moduleDirName = \basename(\dirname(__DIR__));; |
||
58 | $mid = XoopsModule::getByDirname($moduleDirName)->mid(); |
||
59 | $currentuid = 0; |
||
60 | if (isset($xoopsUser) && \is_object($xoopsUser)) { |
||
61 | if ($xoopsUser->isAdmin($mid)) { |
||
62 | return true; |
||
63 | } |
||
64 | $currentuid = $xoopsUser->uid(); |
||
65 | } |
||
66 | $grouppermHandler = \xoops_getHandler('groupperm'); |
||
67 | |||
68 | |||
69 | $memberHandler = \xoops_getHandler('member'); |
||
70 | if (0 == $currentuid) { |
||
71 | $my_group_ids = [\XOOPS_GROUP_ANONYMOUS]; |
||
72 | } else { |
||
73 | $my_group_ids = $memberHandler->getGroupsByUser($currentuid); |
||
|
|||
74 | } |
||
75 | if ($grouppermHandler->checkRight('wgevents_ac', $constantPerm, $my_group_ids, $mid)) { |
||
76 | return true; |
||
77 | } |
||
78 | |||
79 | return false; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @private function getPermSubmit |
||
84 | * returns right to submit for given perm |
||
85 | * @param $constantPerm |
||
86 | * @return bool |
||
87 | */ |
||
88 | private function getPermApprove($constantPerm) |
||
89 | { |
||
90 | if ($this->getPermGlobalApprove()) { |
||
91 | return true; |
||
92 | } |
||
93 | |||
94 | return $this->getPerm($constantPerm); |
||
95 | } |
||
96 | /** |
||
97 | * @private function getPermSubmit |
||
98 | * returns right to submit for given perm |
||
99 | * @param $constantPerm |
||
100 | * @return bool |
||
101 | */ |
||
102 | private function getPermSubmit($constantPerm) |
||
103 | { |
||
104 | if ($this->getPermGlobalSubmit()) { |
||
105 | return true; |
||
106 | } |
||
107 | |||
108 | return $this->getPerm($constantPerm); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @private function getPermView |
||
113 | * returns right for view of given perm |
||
114 | * @param $constantPerm |
||
115 | * @return bool |
||
116 | */ |
||
117 | private function getPermView($constantPerm) |
||
118 | { |
||
119 | if ($this->getPermGlobalView()) { |
||
120 | return true; |
||
121 | } |
||
122 | |||
123 | return $this->getPerm($constantPerm); |
||
124 | } |
||
125 | /** |
||
126 | * @public function permGlobalApprove |
||
127 | * returns right for global approve |
||
128 | * |
||
129 | * @param null |
||
130 | * @return bool |
||
131 | */ |
||
132 | public function getPermGlobalApprove() |
||
133 | { |
||
134 | return $this->getPerm(Constants::PERM_GLOBAL_APPROVE); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @public function permGlobalSubmit |
||
139 | * returns right for global submit |
||
140 | * |
||
141 | * @param null |
||
142 | * @return bool |
||
143 | */ |
||
144 | public function getPermGlobalSubmit() |
||
145 | { |
||
146 | if ($this->getPermGlobalApprove()) { |
||
147 | return true; |
||
148 | } |
||
149 | return $this->getPerm(Constants::PERM_GLOBAL_SUBMIT); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @public function permGlobalView |
||
154 | * returns right for global view |
||
155 | * |
||
156 | * @param null |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function getPermGlobalView() |
||
160 | { |
||
161 | if ($this->getPermGlobalApprove() || $this->getPermGlobalSubmit()) { |
||
162 | return true; |
||
163 | } |
||
164 | return $this->getPerm(Constants::PERM_GLOBAL_APPROVE); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @public function getPermEventsApprove |
||
169 | * returns right for approve events |
||
170 | * @param null |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function getPermEventsApprove() |
||
174 | { |
||
175 | if ($this->getPermGlobalApprove()) { |
||
176 | return true; |
||
177 | } |
||
178 | return $this->getPermApprove(Constants::PERM_EVENTS_APPROVE); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @public function getPermEventsApproveAuto |
||
183 | * returns right for approve events |
||
184 | * @param null |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function getPermEventsApproveAuto() |
||
188 | { |
||
189 | if ($this->getPermEventsApprove()) { |
||
190 | return true; |
||
191 | } |
||
192 | return $this->getPermApprove(Constants::PERM_EVENTS_APPROVE_AUTO); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @public function getPermEventsSubmit |
||
197 | * returns right for submit events |
||
198 | * @param null |
||
199 | * @return bool |
||
200 | */ |
||
201 | public function getPermEventsSubmit() |
||
202 | { |
||
203 | if ($this->getPermGlobalSubmit()) { |
||
204 | return true; |
||
205 | } |
||
206 | return $this->getPermSubmit(Constants::PERM_EVENTS_SUBMIT); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @public function getPermEventsEdit |
||
211 | * returns right for edit/delete events |
||
212 | * - User must have perm to submit and must be owner |
||
213 | * - transaction is not closed |
||
214 | * @param $evSubmitter |
||
215 | * @param $evStatus |
||
216 | * @return bool |
||
217 | */ |
||
218 | public function getPermEventsEdit($evSubmitter, $evStatus) |
||
219 | { |
||
220 | global $xoopsUser, $xoopsModule; |
||
221 | |||
222 | if ($this->getPermEventsApprove()) { |
||
223 | return true; |
||
224 | } |
||
225 | |||
226 | if (Constants::STATUS_LOCKED == $evStatus) { |
||
227 | return false; |
||
228 | } |
||
229 | |||
230 | $currentuid = 0; |
||
231 | if (isset($xoopsUser) && \is_object($xoopsUser)) { |
||
232 | if ($xoopsUser->isAdmin($xoopsModule->mid())) { |
||
233 | return true; |
||
234 | } |
||
235 | $currentuid = $xoopsUser->uid(); |
||
236 | } |
||
237 | return $this->getPermEventsSubmit() && $currentuid == $evSubmitter; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @public function getPermEventsView |
||
242 | * returns right for view Event |
||
243 | * @param null |
||
244 | * @return bool |
||
245 | */ |
||
246 | public function getPermEventsView() |
||
247 | { |
||
248 | return $this->getPermView(Constants::PERM_EVENTS_VIEW); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @public function getPermQuestionsAdmin |
||
253 | * returns right for submit/edit/delete questions |
||
254 | * - User must have perm edit the event |
||
255 | * @param $evSubmitter |
||
256 | * @param $evStatus |
||
257 | * @return bool |
||
258 | */ |
||
259 | public function getPermQuestionsAdmin($evSubmitter, $evStatus) |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @public function getPermRegistrationsApprove |
||
270 | * returns right for approve registrations |
||
271 | * - User must have perm edit the event (owner or approver) |
||
272 | * @param $evSubmitter |
||
273 | * @param $evStatus |
||
274 | * @return bool |
||
275 | */ |
||
276 | public function getPermRegistrationsApprove($evSubmitter, $evStatus) |
||
277 | { |
||
278 | if ($this->getPermEventsEdit($evSubmitter, $evStatus)) { |
||
279 | return true; |
||
280 | } |
||
281 | |||
282 | return false; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @public function getPermRegistrationsVerif |
||
287 | * returns right for submit registrations without question verification by mail |
||
288 | * - User must have perm to submit registrations |
||
289 | * @param null |
||
290 | * @return bool |
||
291 | */ |
||
292 | public function getPermRegistrationsVerif() |
||
293 | { |
||
294 | return $this->getPerm(Constants::PERM_REGISTRATIONS_AUTOVERIF); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * @public function getPermRegistrationsSubmit |
||
299 | * returns right for submit registrations |
||
300 | * @param null |
||
301 | * @return bool |
||
302 | */ |
||
303 | public function getPermRegistrationsSubmit() |
||
304 | { |
||
305 | if ($this->getPermRegistrationsVerif()) { |
||
306 | return true; |
||
307 | } |
||
308 | return $this->getPermSubmit(Constants::PERM_REGISTRATIONS_SUBMIT); |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @public function getPermRegistrationsEdit |
||
313 | * returns right for edit/delete registrations |
||
314 | * - User have permission to edit the event |
||
315 | * - User must have perm to submit and must be owner |
||
316 | * @param $regIp |
||
317 | * @param $regSubmitter |
||
318 | * @param $evSubmitter |
||
319 | * @param $evStatus |
||
320 | * @return bool |
||
321 | */ |
||
322 | public function getPermRegistrationsEdit($regIp, $regSubmitter, $evSubmitter, $evStatus) |
||
323 | { |
||
324 | if ($this->getPermEventsEdit($evSubmitter, $evStatus)) { |
||
325 | return true; |
||
326 | } |
||
327 | |||
328 | if ($this->getPermRegistrationsSubmit() && $this->getPermRegistrationsView($regIp, $regSubmitter)) { |
||
329 | return true; |
||
330 | } |
||
331 | |||
332 | return false; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @public function getPermRegistrationsView |
||
337 | * returns right for view Registration |
||
338 | * - user must be the submitter_text of registration or same IP must be used |
||
339 | * @param $regIp |
||
340 | * @param $regSubmitter |
||
341 | * @return bool |
||
342 | */ |
||
343 | public function getPermRegistrationsView($regIp, $regSubmitter) |
||
344 | { |
||
345 | $uidCurrent = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->uid() : 0; |
||
346 | if ($regSubmitter == $uidCurrent) { |
||
347 | return true; |
||
348 | } |
||
349 | $ipCurrent = $_SERVER['REMOTE_ADDR']; |
||
350 | return $regIp == $ipCurrent; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * @public function getPermTextblocksSubmit |
||
355 | * returns right for submit textblocks |
||
356 | * @param null |
||
357 | * @return bool |
||
358 | */ |
||
359 | public function getPermTextblocksSubmit() |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * @public function getPermTextblocksAdmin |
||
366 | * returns right for edit/delete textblocks |
||
367 | * - User must have perm to submit and must be owner |
||
368 | * @param $tbSubmitter |
||
369 | * @return bool |
||
370 | */ |
||
371 | public function getPermTextblocksEdit($tbSubmitter) |
||
372 | { |
||
373 | global $xoopsUser, $xoopsModule; |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @public function getPermTextblocksAdmin |
||
391 | * returns right for edit/delete textblocks |
||
392 | * - User must have perm to submit and must be owner |
||
393 | * @param $tbSubmitter |
||
394 | * @return bool |
||
395 | */ |
||
396 | public function getPermTextblocksAdmin($tbSubmitter) |
||
412 | } |
||
413 | |||
414 | |||
415 | |||
416 | |||
517 |