Completed
Push — master ( 047d50...4f5633 )
by Michael
02:19
created

PublicPermHandler::getUserGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace XoopsModules\Extgallery;
2
3
/**
4
 * ExtGallery Class Manager
5
 *
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 *
13
 * @copyright   {@link https://xoops.org/ XOOPS Project}
14
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
15
 * @author      Zoullou (http://www.zoullou.net)
16
 * @package     ExtGallery
17
 */
18
19
20
use XoopsModules\Extgallery;
21
22
// 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...
23
24
/**
25
 * Class Extgallery\PublicPermHandler
26
 */
27
class PublicPermHandler
28
{
29
    /**
30
     * @return Extgallery\PublicPermHandler
31
     */
32
    public static function getInstance()
33
    {
34
        static $instance;
35
        if (null === $instance) {
36
            $instance = new static();
37
        }
38
39
        return $instance;
40
    }
41
42
    /**
43
     * @param $user
44
     *
45
     * @return string
46
     */
47
    public function getUserGroup($user)
48
    {
49
        if (is_a($user, 'XoopsUser')) {
50
            return $user->getGroups();
51
        } else {
52
            return XOOPS_GROUP_ANONYMOUS;
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Extgallery\XOOPS_GROUP_ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
53
        }
54
    }
55
56
    /**
57
     * @param \XoopsUser $user
58
     * @param            $perm
59
     *
60
     * @return mixed
61
     */
62
    public function getAuthorizedPublicCat(\XoopsUser $user, $perm)
0 ignored issues
show
Bug introduced by
The type XoopsUser 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...
63
    {
64
        static $authorizedCat;
65
        $userId = $user ? $user->getVar('uid') : 0;
66
        if (!isset($authorizedCat[$perm][$userId])) {
67
            /** @var \XoopsGroupPermHandler $groupPermHandler */
68
            $groupPermHandler = xoops_getHandler('groupperm');
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

68
            $groupPermHandler = /** @scrutinizer ignore-call */ xoops_getHandler('groupperm');
Loading history...
69
            /** @var \XoopsModuleHandler $moduleHandler */
70
            $moduleHandler                 = xoops_getHandler('module');
71
            $module                        = $moduleHandler->getByDirname('extgallery');
72
            $authorizedCat[$perm][$userId] = $groupPermHandler->getItemIds($perm, $this->getUserGroup($user), $module->getVar('mid'));
73
        }
74
75
        return $authorizedCat[$perm][$userId];
76
    }
77
78
    /**
79
     * @param \XoopsUser $user
80
     * @param            $perm
81
     * @param            $catId
82
     *
83
     * @return bool
84
     */
85
    public function isAllowed(\XoopsUser $user, $perm, $catId)
86
    {
87
        $autorizedCat = $this->getAuthorizedPublicCat($user, $perm);
88
89
        return in_array($catId, $autorizedCat);
90
    }
91
}
92