Passed
Push — master ( d3e687...4990f6 )
by Michael
02:43
created

PermissionHandler::getPermission()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 32
nop 3
dl 0
loc 21
rs 7.551
c 0
b 0
f 0
1
<?php namespace XoopsModules\Newbb;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 17.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * NewBB 5.0x,  the forum module for XOOPS project
5
 *
6
 * @copyright      XOOPS Project (https://xoops.org)
7
 * @license        GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
8
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
9
 * @since          4.00
10
 * @package        module::newbb
11
 */
12
13
use XoopsModules\Newbb;
14
15
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
16
17
defined('NEWBB_FUNCTIONS_INI') || include $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
18
define('NEWBB_HANDLER_PERMISSION', 1);
19
20
// Initializing XoopsGroupPermHandler if not loaded yet
21
if (!class_exists('XoopsGroupPermHandler')) {
22
    require_once $GLOBALS['xoops']->path('kernel/groupperm.php');
23
}
24
25
/**
26
 * Class PermissionHandler
27
 */
28
class PermissionHandler extends \XoopsGroupPermHandler
0 ignored issues
show
Bug introduced by
The type XoopsGroupPermHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
{
30
    public $_handler;
31
    /** @var \Xmf\Module\Helper\Cache */
0 ignored issues
show
Bug introduced by
The type Xmf\Module\Helper\Cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
    protected $cacheHelper;
33
34
    /**
35
     * @param $db
36
     */
37
    public function __construct(\XoopsDatabase $db)
0 ignored issues
show
Bug introduced by
The type XoopsDatabase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
    {
39
        $this->cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
40
41
        $this->db = $db;
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42
        parent::__construct($db);
43
    }
44
45
    /**
46
     * @param $name
47
     * @return mixed
48
     */
49
    public function loadHandler($name)
50
    {
51
        if (!isset($this->_handler[$name])) {
52
//            require_once __DIR__ . "/permission.{$name}.php";
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
53
            $className             = '\\XoopsModules\\Newbb\\Permission' . ucfirst($name) . 'Handler';
54
            $this->_handler[$name] = new $className($this->db);
55
        }
56
57
        return $this->_handler[$name];
58
    }
59
60
    /**
61
     * @param  bool $fullname
62
     * @return mixed
63
     */
64
    public function getValidForumPerms($fullname = false)
65
    {
66
        $handler = $this->loadHandler('forum');
67
68
        return $handler->getValidPerms($fullname);
69
    }
70
71
    /**
72
     * @param  int  $forum
73
     * @param  bool $topic_locked
74
     * @param  bool $isAdmin
75
     * @return mixed
76
     */
77
    public function getPermissionTable($forum = 0, $topic_locked = false, $isAdmin = false)
78
    {
79
        $handler = $this->loadHandler('forum');
80
        $perm    = $handler->getPermissionTable($forum, $topic_locked, $isAdmin);
81
82
        return $perm;
83
    }
84
85
    /**
86
     * @param $forum_id
87
     * @return mixed
88
     */
89
    public function deleteByForum($forum_id)
90
    {
91
        $this->cacheHelper->delete('permission_forum');
92
        $handler = $this->loadHandler('forum');
93
94
        return $handler->deleteByForum($forum_id);
95
    }
96
97
    /**
98
     * @param $cat_id
99
     * @return mixed
100
     */
101
    public function deleteByCategory($cat_id)
102
    {
103
        $this->cacheHelper->delete('permission_category');
104
        $handler = $this->loadHandler('category');
105
106
        return $handler->deleteByCategory($cat_id);
107
    }
108
109
    /**
110
     * @param        $category
111
     * @param  array $groups
112
     * @return mixed
113
     */
114
    public function setCategoryPermission($category, array $groups = [])
115
    {
116
        $this->cacheHelper->delete('permission_category');
117
        $handler = $this->loadHandler('category');
118
119
        return $handler->setCategoryPermission($category, $groups);
120
    }
121
122
    /**
123
     * @param         $type
124
     * @param  string $gperm_name
125
     * @param  int    $id
126
     * @return bool
127
     */
128
    public function getPermission($type, $gperm_name = 'access', $id = 0)
129
    {
130
        global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
131
        $ret = false;
132
        if ($GLOBALS['xoopsUserIsAdmin'] && 'newbb' === $xoopsModule->getVar('dirname')) {
133
            $ret = true;
134
        }
135
136
        $groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS];
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\XOOPS_GROUP_ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
137
        if (!$groups) {
138
            $ret = false;
139
        }
140
        if (!$allowed_groups = $this->getGroups("{$type}_{$gperm_name}", $id)) {
141
            $ret = false;
142
        }
143
144
        if (count(array_intersect($allowed_groups, $groups)) > 0) {
145
            $ret = true;
146
        }
147
148
        return $ret;
149
    }
150
151
    /**
152
     * @param  string $perm_name
153
     * @return array
154
     */
155
    public function &getCategories($perm_name = 'access')
156
    {
157
        $ret = $this->getAllowedItems('category', "category_{$perm_name}");
158
159
        return $ret;
160
    }
161
162
    /**
163
     * @param  string $perm_name
164
     * @return array
165
     */
166
    public function getForums($perm_name = 'access')
167
    {
168
        $ret = $this->getAllowedItems('forum', "forum_{$perm_name}");
169
170
        return $ret;
171
    }
172
173
    /**
174
     * @param $type
175
     * @param $perm_name
176
     * @return array
177
     */
178
    public function getAllowedItems($type, $perm_name)
179
    {
180
        $ret = [];
181
182
        $groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS];
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\XOOPS_GROUP_ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
183
        if (count($groups) < 1) {
184
            return $ret;
185
        }
186
187
        if (!$_cachedPerms = $this->loadPermData($perm_name, $type)) {
0 ignored issues
show
Unused Code introduced by
The call to XoopsModules\Newbb\Permi...Handler::loadPermData() has too many arguments starting with $type. ( Ignorable by Annotation )

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

187
        if (!$_cachedPerms = $this->/** @scrutinizer ignore-call */ loadPermData($perm_name, $type)) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
188
            return $ret;
189
        }
190
191
        $allowed_items = [];
192
        foreach ($_cachedPerms as $id => $allowed_groups) {
193
            if (0 == $id || empty($allowed_groups)) {
194
                continue;
195
            }
196
197
            if (array_intersect($groups, $allowed_groups)) {
198
                $allowed_items[$id] = 1;
199
            }
200
        }
201
        unset($_cachedPerms);
202
        $ret = array_keys($allowed_items);
203
204
        return $ret;
205
    }
206
207
    /**
208
     * @param        $gperm_name
209
     * @param  int   $id
210
     * @return array
211
     */
212
    public function getGroups($gperm_name, $id = 0)
213
    {
214
        $_cachedPerms = $this->loadPermData($gperm_name);
215
        $groups       = empty($_cachedPerms[$id]) ? [] : array_unique($_cachedPerms[$id]);
216
        unset($_cachedPerms);
217
218
        return $groups;
219
    }
220
221
    /**
222
     * @param  string $perm_name
223
     * @return array
224
     */
225
    public function createPermData($perm_name = 'forum_all')
226
    {
227
        global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
228
        /** @var \XoopsModuleHandler $moduleHandler */
229
        $perms = [];
230
231
        if (is_object($xoopsModule) && 'newbb' === $xoopsModule->getVar('dirname')) {
232
            $modid = $xoopsModule->getVar('mid');
233
        } else {
234
            $moduleHandler = xoops_getHandler('module');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

234
            $moduleHandler = /** @scrutinizer ignore-call */ xoops_getHandler('module');
Loading history...
235
            $module        = $moduleHandler->getByDirname('newbb');
236
            $modid         = $module->getVar('mid');
237
            unset($module);
238
        }
239
240
        if (in_array($perm_name, ['forum_all', 'category_all'], true)) {
241
            /** @var \XoopsMemberHandler $memberHandler */
242
            $memberHandler = xoops_getHandler('member');
243
            $groups        = array_keys($memberHandler->getGroupList());
244
245
            $type          = ('category_all' === $perm_name) ? 'category' : 'forum';
246
            $objectHandler = Newbb\Helper::getInstance()->getHandler($type);
247
            $object_ids    = $objectHandler->getIds();
248
            foreach ($object_ids as $item_id) {
249
                $perms[$perm_name][$item_id] = $groups;
250
            }
251
        } else {
252
            $gpermHandler = xoops_getHandler('groupperm');
0 ignored issues
show
Unused Code introduced by
The assignment to $gpermHandler is dead and can be removed.
Loading history...
253
            $criteria     = new \CriteriaCompo(new \Criteria('gperm_modid', $modid));
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
254
            if (!empty($perm_name) && 'forum_all' !== $perm_name && 'category_all' !== $perm_name) {
255
                $criteria->add(new \Criteria('gperm_name', $perm_name));
256
            }
257
            $permissions = $this->getObjects($criteria);
258
259
            foreach ($permissions as $gperm) {
260
                $item_id                                         = $gperm->getVar('gperm_itemid');
261
                $group_id                                        = (int)$gperm->getVar('gperm_groupid');
262
                $perms[$gperm->getVar('gperm_name')][$item_id][] = $group_id;
263
            }
264
        }
265
        if (count($perms) > 0) {
266
            foreach (array_keys($perms) as $perm) {
267
                $this->cacheHelper->write("permission_{$perm}", $perms[$perm]);
268
            }
269
        }
270
        $ret = !empty($perm_name) ? @$perms[$perm_name] : $perms;
271
272
        return $ret;
273
    }
274
275
    /**
276
     * @param  string $perm_name
277
     * @return array|mixed|null
278
     */
279
    public function &loadPermData($perm_name = 'forum_access')
280
    {
281
        if (!$perms = $this->cacheHelper->read("permission_{$perm_name}")) {
282
            $perms = $this->createPermData($perm_name);
283
        }
284
285
        return $perms;
286
    }
287
288
    /**
289
     * @param       $perm
290
     * @param       $itemid
291
     * @param       $groupid
292
     * @param  null $mid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $mid is correct as it would always require null to be passed?
Loading history...
293
     * @return bool
294
     */
295
    public function validateRight($perm, $itemid, $groupid, $mid = null)
296
    {
297
        if (empty($mid)) {
298
            if (is_object($GLOBALS['xoopsModule']) && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
299
                $mid = $GLOBALS['xoopsModule']->getVar('mid');
300
            } else {
301
                /** @var \XoopsModuleHandler $moduleHandler */
302
                $moduleHandler = xoops_getHandler('module');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

302
                $moduleHandler = /** @scrutinizer ignore-call */ xoops_getHandler('module');
Loading history...
303
                $mod           = $moduleHandler->getByDirname('newbb');
304
                $mid           = $mod->getVar('mid');
305
                unset($mod);
306
            }
307
        }
308
        if ($this->myCheckRight($perm, $itemid, $groupid, $mid)) {
309
            return true;
310
        }
311
        $this->cacheHelper->delete('permission');
312
        $this->addRight($perm, $itemid, $groupid, $mid);
313
314
        return true;
315
    }
316
317
    /**
318
     * Check permission (directly)
319
     *
320
     * @param string $gperm_name   Name of permission
321
     * @param int    $gperm_itemid ID of an item
322
     * @param        int           /array $gperm_groupid A group ID or an array of group IDs
323
     * @param int    $gperm_modid  ID of a module
324
     *
325
     * @return bool TRUE if permission is enabled
326
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment /array at position 0 could not be parsed: Unknown type name '/array' at position 0 in /array.
Loading history...
327
    public function myCheckRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
328
    {
329
        $ret      = false;
330
        $criteria = new \CriteriaCompo(new \Criteria('gperm_modid', $gperm_modid));
331
        $criteria->add(new \Criteria('gperm_name', $gperm_name));
332
        $gperm_itemid = (int)$gperm_itemid;
333
        if ($gperm_itemid > 0) {
334
            $criteria->add(new \Criteria('gperm_itemid', $gperm_itemid));
335
        }
336
        if (is_array($gperm_groupid)) {
337
            $criteria2 = new \CriteriaCompo();
338
            foreach ($gperm_groupid as $gid) {
339
                $criteria2->add(new \Criteria('gperm_groupid', $gid), 'OR');
340
            }
341
            $criteria->add($criteria2);
342
        } else {
343
            $criteria->add(new \Criteria('gperm_groupid', $gperm_groupid));
344
        }
345
        if ($this->getCount($criteria) > 0) {
346
            $ret = true;
347
        }
348
349
        return $ret;
350
    }
351
352
    /**
353
     * @param       $perm
354
     * @param       $itemid
355
     * @param       $groupid
356
     * @param  null $mid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $mid is correct as it would always require null to be passed?
Loading history...
357
     * @return bool
358
     */
359
    public function deleteRight($perm, $itemid, $groupid, $mid = null)
360
    {
361
        $this->cacheHelper->delete('permission');
362
        if (null === $mid) {
363
            if (is_object($GLOBALS['xoopsModule']) && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
364
                $mid = $GLOBALS['xoopsModule']->getVar('mid');
365
            } else {
366
                /** @var \XoopsModuleHandler $moduleHandler */
367
                $moduleHandler = xoops_getHandler('module');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

367
                $moduleHandler = /** @scrutinizer ignore-call */ xoops_getHandler('module');
Loading history...
368
                $mod           = $moduleHandler->getByDirname('newbb');
369
                $mid           = $mod->getVar('mid');
370
                unset($mod);
371
            }
372
        }
373
        if (is_callable([&$this->XoopsGroupPermHandler, 'deleteRight'])) {
374
            return parent::deleteRight($perm, $itemid, $groupid, $mid);
375
        } else {
376
            $criteria = new \CriteriaCompo(new \Criteria('gperm_name', $perm));
377
            $criteria->add(new \Criteria('gperm_groupid', $groupid));
378
            $criteria->add(new \Criteria('gperm_itemid', $itemid));
379
            $criteria->add(new \Criteria('gperm_modid', $mid));
380
            $permsObject =& $this->getObjects($criteria);
381
            if (!empty($permsObject)) {
382
                foreach ($permsObject as $permObject) {
383
                    $this->delete($permObject);
384
                }
385
            }
386
            unset($criteria, $permsObject);
387
        }
388
389
        return true;
390
    }
391
392
    /**
393
     * @param        $forum
394
     * @param  int   $mid
395
     * @return mixed
396
     */
397
    public function applyTemplate($forum, $mid = 0)
398
    {
399
        $this->cacheHelper->delete('permission_forum');
400
        $handler = $this->loadHandler('forum');
401
402
        return $handler->applyTemplate($forum, $mid);
403
    }
404
405
    /**
406
     * @return mixed
407
     */
408
    public function getTemplate()
409
    {
410
        $handler  = $this->loadHandler('forum');
411
        $template = $handler->getTemplate();
412
413
        return $template;
414
    }
415
416
    /**
417
     * @param $perms
418
     * @return mixed
419
     */
420
    public function setTemplate($perms)
421
    {
422
        $handler = $this->loadHandler('forum');
423
424
        return $handler->setTemplate($perms);
425
    }
426
}
427