Passed
Push — master ( 1e16e5...7bdd06 )
by Goffy
03:14
created

PermissionHandler::getPermTextblocksEdit()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 9
nc 6
nop 1
dl 0
loc 16
rs 8.4444
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
4
namespace XoopsModules\Wgevents;
5
6
/*
7
 You may not change or alter any portion of this comment or credits
8
 of supporting developers from this source code or any supporting source code
9
 which is considered copyrighted (c) material of the original comment or credit authors.
10
11
 This program is distributed in the hope that it will be useful,
12
 but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
*/
15
16
/**
17
 * wgEvents module for xoops
18
 *
19
 * @copyright    2021 XOOPS Project (https://xoops.org)
20
 * @license      GPL 2.0 or later
21
 * @package      wgevents
22
 * @since        1.0.0
23
 * @min_xoops    2.5.11 Beta1
24
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
25
 */
26
27
use XoopsModule;
28
use XoopsModules\Wgevents;
29
30
\defined('XOOPS_ROOT_PATH') || die('Restricted access');
31
32
/**
33
 * Class Object PermissionHandler
34
 */
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
        $memberHandler = \xoops_getHandler('member');
69
        if (0 == $currentuid) {
70
            $my_group_ids = [\XOOPS_GROUP_ANONYMOUS];
71
        } else {
72
            $my_group_ids = $memberHandler->getGroupsByUser($currentuid);
0 ignored issues
show
Bug introduced by
The method getGroupsByUser() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsMembershipHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
            /** @scrutinizer ignore-call */ 
73
            $my_group_ids = $memberHandler->getGroupsByUser($currentuid);
Loading history...
73
        }
74
        if ($grouppermHandler->checkRight('wgevents_ac', $constantPerm, $my_group_ids, $mid)) {
0 ignored issues
show
Bug introduced by
The method checkRight() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsGroupPermHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        if ($grouppermHandler->/** @scrutinizer ignore-call */ checkRight('wgevents_ac', $constantPerm, $my_group_ids, $mid)) {
Loading history...
75
            return true;
76
        }
77
78
        return false;
79
    }
80
81
    /**
82
     * @private function getPermSubmit
83
     * returns right to submit for given perm
84
     * @param $constantPerm
85
     * @return bool
86
     */
87
    private function getPermApprove($constantPerm)
88
    {
89
        if ($this->getPermGlobalApprove()) {
90
            return true;
91
        }
92
93
        return $this->getPerm($constantPerm);
94
    }
95
    /**
96
     * @private function getPermSubmit
97
     * returns right to submit for given perm
98
     * @param $constantPerm
99
     * @return bool
100
     */
101
    private function getPermSubmit($constantPerm)
102
    {
103
        if ($this->getPermGlobalSubmit()) {
104
            return true;
105
        }
106
107
        return $this->getPerm($constantPerm);
108
    }
109
110
    /**
111
     * @private function getPermView
112
     * returns right for view of given perm
113
     * @param $constantPerm
114
     * @return bool
115
     */
116
    private function getPermView($constantPerm)
117
    {
118
        if ($this->getPermGlobalView()) {
119
            return true;
120
        }
121
122
        return $this->getPerm($constantPerm);
123
    }
124
    /**
125
     * @public function permGlobalApprove
126
     * returns right for global approve
127
     *
128
     * @param null
129
     * @return bool
130
     */
131
    public function getPermGlobalApprove()
132
    {
133
        return $this->getPerm(Constants::PERM_GLOBAL_APPROVE);
134
    }
135
136
    /**
137
     * @public function permGlobalSubmit
138
     * returns right for global submit
139
     *
140
     * @param null
141
     * @return bool
142
     */
143
    public function getPermGlobalSubmit()
144
    {
145
        if ($this->getPermGlobalApprove()) {
146
            return true;
147
        }
148
        return $this->getPerm(Constants::PERM_GLOBAL_SUBMIT);
149
    }
150
151
    /**
152
     * @public function permGlobalView
153
     * returns right for global view
154
     *
155
     * @param null
156
     * @return bool
157
     */
158
    public function getPermGlobalView()
159
    {
160
        if ($this->getPermGlobalApprove() || $this->getPermGlobalSubmit()) {
161
            return true;
162
        }
163
        return $this->getPerm(Constants::PERM_GLOBAL_APPROVE);
164
    }
165
166
    /**
167
     * @public function getPermEventsApprove
168
     * returns right for approve events
169
     * @param null
170
     * @return bool
171
     */
172
    public function getPermEventsApprove()
173
    {
174
        if ($this->getPermGlobalApprove()) {
175
            return true;
176
        }
177
        return $this->getPermApprove(Constants::PERM_EVENTS_APPROVE);
178
    }
179
180
    /**
181
     * @public function getPermEventsApproveAuto
182
     * returns right for approve events
183
     * @param null
184
     * @return bool
185
     */
186
    public function getPermEventsApproveAuto()
187
    {
188
        if ($this->getPermEventsApprove()) {
189
            return true;
190
        }
191
        return $this->getPermApprove(Constants::PERM_EVENTS_APPROVE_AUTO);
192
    }
193
194
    /**
195
     * @public function getPermEventsSubmit
196
     * returns right for submit events
197
     * @param null
198
     * @return bool
199
     */
200
    public function getPermEventsSubmit()
201
    {
202
        if ($this->getPermGlobalSubmit()) {
203
            return true;
204
        }
205
        return $this->getPermSubmit(Constants::PERM_EVENTS_SUBMIT);
206
    }
207
208
    /**
209
     * @public function getPermEventsEdit
210
     * returns right for edit/delete events
211
     *  - User must have perm to submit and must be owner
212
     *  - transaction is not closed
213
     * @param $evSubmitter
214
     * @param $evStatus
215
     * @return bool
216
     */
217
    public function getPermEventsEdit($evSubmitter, $evStatus)
218
    {
219
        global $xoopsUser, $xoopsModule;
220
221
        if ($this->getPermEventsApprove()) {
222
            return true;
223
        }
224
225
        if (Constants::STATUS_LOCKED == $evStatus) {
226
            return false;
227
        }
228
229
        $currentuid = 0;
230
        if (isset($xoopsUser) && \is_object($xoopsUser)) {
231
            if (isset($xoopsModule) && \is_object($xoopsModule) && $xoopsUser->isAdmin($xoopsModule->mid())) {
232
                return true;
233
            }
234
            $currentuid = $xoopsUser->uid();
235
        }
236
        return $this->getPermEventsSubmit() && $currentuid == $evSubmitter;
237
    }
238
239
    /**
240
     * @public function getPermEventsView
241
     * returns right for view Event
242
     * @param null
243
     * @return bool
244
     */
245
    public function getPermEventsView()
246
    {
247
        return $this->getPermView(Constants::PERM_EVENTS_VIEW);
248
    }
249
250
    /**
251
     * @public function getPermQuestionsAdmin
252
     * returns right for submit/edit/delete questions
253
     *  - User must have perm edit the event
254
     * @param $evSubmitter
255
     * @param $evStatus
256
     * @return bool
257
     */
258
    public function getPermQuestionsAdmin($evSubmitter, $evStatus)
259
    {
260
        if ($this->getPermEventsEdit($evSubmitter, $evStatus)) {
261
            return true;
262
        }
263
264
        return false;
265
    }
266
267
    /**
268
     * @public function getPermRegistrationsApprove
269
     * returns right for approve registrations
270
     *  - User must have perm edit the event (owner or approver)
271
     * @param $evSubmitter
272
     * @param $evStatus
273
     * @return bool
274
     */
275
    public function getPermRegistrationsApprove($evSubmitter, $evStatus)
276
    {
277
        if ($this->getPermEventsEdit($evSubmitter, $evStatus)) {
278
            return true;
279
        }
280
281
        return false;
282
    }
283
284
    /**
285
     * @public function getPermRegistrationsVerif
286
     * returns right for submit registrations without question verification by mail
287
     *  - User must have perm to submit registrations
288
     * @param null
289
     * @return bool
290
     */
291
    public function getPermRegistrationsVerif()
292
    {
293
        return $this->getPerm(Constants::PERM_REGISTRATIONS_AUTOVERIF);
294
    }
295
296
    /**
297
     * @public function getPermRegistrationsSubmit
298
     * returns right for submit registrations
299
     * @param null
300
     * @return bool
301
     */
302
    public function getPermRegistrationsSubmit()
303
    {
304
        if ($this->getPermRegistrationsVerif()) {
305
            return true;
306
        }
307
        return $this->getPermSubmit(Constants::PERM_REGISTRATIONS_SUBMIT);
308
    }
309
310
    /**
311
     * @public function getPermRegistrationsEdit
312
     * returns right for edit/delete registrations
313
     *  - User have permission to edit the event
314
     *  - User must have perm to submit and must be owner
315
     * @param $regIp
316
     * @param $regSubmitter
317
     * @param $evSubmitter
318
     * @param $evStatus
319
     * @return bool
320
     */
321
    public function getPermRegistrationsEdit($regIp, $regSubmitter, $evSubmitter, $evStatus)
322
    {
323
        if ($this->getPermEventsEdit($evSubmitter, $evStatus)) {
324
            return true;
325
        }
326
327
        if ($this->getPermRegistrationsSubmit() && $this->getPermRegistrationsView($regIp, $regSubmitter)) {
328
            return true;
329
        }
330
331
        return false;
332
    }
333
334
    /**
335
     * @public function getPermRegistrationsView
336
     * returns right for view Registration
337
     *  - user must be the submitter_text of registration or same IP must be used
338
     * @param $regIp
339
     * @param $regSubmitter
340
     * @return bool
341
     */
342
    public function getPermRegistrationsView($regIp, $regSubmitter)
343
    {
344
        $uidCurrent = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->uid() : 0;
345
        if ($regSubmitter == $uidCurrent) {
346
            return true;
347
        }
348
        $ipCurrent = $_SERVER['REMOTE_ADDR'];
349
        return $regIp == $ipCurrent;
350
    }
351
352
    /**
353
     * @public function getPermTextblocksSubmit
354
     * returns right for submit textblocks
355
     * @param null
356
     * @return bool
357
     */
358
    public function getPermTextblocksSubmit()
359
    {
360
        return $this->getPermEventsSubmit();
361
    }
362
363
    /**
364
     * @public function getPermTextblocksAdmin
365
     * returns right for edit/delete textblocks
366
     *  - User must have perm to submit and must be owner
367
     * @param $tbSubmitter
368
     * @return bool
369
     */
370
    public function getPermTextblocksEdit($tbSubmitter)
371
    {
372
        global $xoopsUser, $xoopsModule;
373
374
        if ($this->getPermGlobalApprove()) {
375
            return true;
376
        }
377
378
        $currentuid = 0;
379
        if (isset($xoopsUser) && \is_object($xoopsUser)) {
380
            if (isset($xoopsModule) && \is_object($xoopsModule) && $xoopsUser->isAdmin($xoopsModule->mid())) {
381
                return true;
382
            }
383
            $currentuid = $xoopsUser->uid();
384
        }
385
        return $this->getPermTextblocksSubmit() && $currentuid == $tbSubmitter;
386
    }
387
388
    /**
389
     * @public function getPermTextblocksAdmin
390
     * returns right for edit/delete textblocks
391
     *  - User must have perm to submit and must be owner
392
     * @param $tbSubmitter
393
     * @return bool
394
     */
395
    public function getPermTextblocksAdmin($tbSubmitter)
396
    {
397
        global $xoopsUser, $xoopsModule;
398
399
        if ($this->getPermGlobalApprove()) {
400
            return true;
401
        }
402
403
        $currentuid = 0;
404
        if (isset($xoopsUser) && \is_object($xoopsUser)) {
405
            if (isset($xoopsModule) && \is_object($xoopsModule) && $xoopsUser->isAdmin($xoopsModule->mid())) {
406
                return true;
407
            }
408
            $currentuid = $xoopsUser->uid();
409
        }
410
        return $this->getPermTextblocksSubmit() && $currentuid == $tbSubmitter;
411
    }
412
413
414
415
416
417
418
419
420
421
    /** Check permission for categories
422
     * @param string $permName
423
     * @param int $catId
424
     * @return int
425
426
    private function permPermCats($permName, $catId)
427
    {
428
        global $xoopsUser, $xoopsModule;
429
430
        $currentuid = 0;
431
        if (isset($xoopsUser) && \is_object($xoopsUser)) {
432
            if ($xoopsUser->isAdmin()) {
433
                return 1;
434
            }
435
            $currentuid = $xoopsUser->uid();
436
        }
437
        $grouppermHandler  = \xoops_getHandler('groupperm');
438
        $mid           = $xoopsModule->mid();
439
        $memberHandler = \xoops_getHandler('member');
440
        if (0 == $currentuid) {
441
            $my_group_ids = [\XOOPS_GROUP_ANONYMOUS];
442
        } else {
443
            $my_group_ids = $memberHandler->getGroupsByUser($currentuid);
444
        }
445
446
        return $grouppermHandler->checkRight($permName, $catId, $my_group_ids, $mid);
447
    }*/
448
449
    /**
450
     * @public function getPermCatEventsView
451
     * returns right for view events of this category
452
     * @param int $catId
453
     * @return bool
454
455
    public function getPermCatEventsView($catId)
456
    {
457
        return $this->permPermCats('wgevents_view_cat_events',  $catId);
458
    }*/
459
460
    /**
461
     * @public function getPermCatEventsSubmit
462
     * returns right for submit events to this category
463
     * @param int $catId
464
     * @return bool
465
466
    public function getPermCatEventsSubmit($catId)
467
    {
468
        return $this->permPermCats('wgevents_submit_cat_events',  $catId);
469
    }*/
470
471
    /**
472
     * @public function getPermCatEventsApprove
473
     * returns right for approve events of this category
474
     * @param int $catId
475
     * @return bool
476
477
    public function getPermCatEventsApprove($catId)
478
    {
479
        return $this->permPermCats('wgevents_approve_cat_events',  $catId);
480
    }*/
481
482
    /**
483
     * @public function getPermCatRegsView
484
     * returns right for view registrations of events of this category
485
     * @param int $catId
486
     * @return bool
487
488
    public function getPermCatRegsView($catId)
489
    {
490
        return $this->permPermCats('wgevents_view_cat_regs',  $catId);
491
    }*/
492
493
    /**
494
     * @public function getPermCatRegsSubmit
495
     * returns right for submit registrations to events to this category
496
     * @param int $catId
497
     * @return bool
498
499
    public function getPermCatRegsSubmit($catId)
500
    {
501
        return $this->permPermCats('wgevents_submit_cat_regs',  $catId);
502
    }*/
503
504
    /**
505
     * @public function getPermCatRegsApprove
506
     * returns right for approve registrations of events of this category
507
     * @param int $catId
508
     * @return bool
509
510
    public function getPermCatRegsApprove($catId)
511
    {
512
        return $this->permPermCats('wgevents_approve_cat_regs',  $catId);
513
    }*/
514
515
}
516