Passed
Push — master ( 01c53a...7f8ffa )
by Andrii
02:39
created

AuthManager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 65.52%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 66
ccs 19
cts 29
cp 0.6552
rs 10
c 0
b 0
f 0
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A persistAssignments() 0 3 1
A __construct() 0 7 2
B applyUserAssignments() 0 21 8
A checkAccess() 0 8 3
A saveAssignments() 0 2 1
1
<?php
2
/**
3
 * RBAC implementation for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-rbac
6
 * @package   hipanel-rbac
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\rbac;
12
13
use hiqdev\yii\compat\yii;
14
use yii\rbac\RuleFactory;
0 ignored issues
show
Bug introduced by
The type yii\rbac\RuleFactory 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...
15
16
/**
17
 * HiPanel AuthManager.
18
 *
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class AuthManager extends \yii\rbac\PhpManager
22
{
23
    use SetterTrait;
24
25
    public $itemFile       = '@hipanel/rbac/files/items.php';
26
    public $ruleFile       = '@hipanel/rbac/files/rules.php';
27
    public $assignmentFile = '@hipanel/rbac/files/assignments.php';
28
29 31
    public function __construct()
30
    {
31 31
        if (yii::is2()) {
32 31
            parent::init();
33
        } else {
34
            $dir = __DIR__ . '/files';
35
            parent::__construct($dir, new RuleFactory());
0 ignored issues
show
Bug introduced by
$dir of type string is incompatible with the type array expected by parameter $config of yii\base\BaseObject::__construct(). ( Ignorable by Annotation )

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

35
            parent::__construct(/** @scrutinizer ignore-type */ $dir, new RuleFactory());
Loading history...
Unused Code introduced by
The call to yii\base\BaseObject::__construct() has too many arguments starting with new yii\rbac\RuleFactory(). ( Ignorable by Annotation )

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

35
            parent::/** @scrutinizer ignore-call */ 
36
                    __construct($dir, new RuleFactory());

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...
36
        }
37 31
    }
38
39
    /**
40
     * We don't keep all the assignments, only persistent.
41
     * @see persistAssignments
42
     */
43 31
    protected function saveAssignments()
44
    {
45 31
    }
46
47
    /**
48
     * Does real assignments saving.
49
     * The idea is to split persistent assignments from session only.
50
     */
51
    public function persistAssignments()
52
    {
53
        parent::saveAssignments();
54
    }
55
56 30
    public function checkAccess($userId, $permission, $params = [])
57
    {
58 30
        if (empty($this->getAssignments($userId))) {
59 2
            $this->applyUserAssignments($userId);
60
        }
61
62 30
        return parent::checkAccess($userId, $permission, $params)
63 30
            && !parent::checkAccess($userId, "deny:$permission", $params);
64
    }
65
66 2
    public function applyUserAssignments($userId)
67
    {
68 2
        $roles = '';
69
70 2
        if (isset(yii::getApp()->user)) {
71
            $user = yii::getApp()->user->identity;
72
            if ((!$user || $user->id !== $userId) && $userId) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface yii\web\IdentityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
73
                $user = call_user_func([yii::getApp()->user->identityClass, 'findIdentity'], $userId);
74
            }
75
            if (isset($user->roles)) {
76
                $roles = $user->roles;
77
            }
78
        }
79
80 2
        if (empty($userId)) {
81 2
            $userId = '';
82 2
            $roles = 'role:unauthorized';
83
        }
84
85 2
        if ($roles) {
86 2
            $this->setAssignments($roles, $userId);
87
        }
88 2
    }
89
}
90