Completed
Push — master ( 879f46...16ff0f )
by Andrii
02:19
created

AuthManager::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
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 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...
14
15
/**
16
 * HiPanel AuthManager.
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class AuthManager extends \yii\rbac\PhpManager
21
{
22
    use SetterTrait;
23
24
    public function __construct()
25
    {
26
        if (class_exists('Yii')) {
27
            parent::init();
28
        } else {
29
            $dir = __DIR__ . '/files';
30
            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

30
            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

30
            parent::/** @scrutinizer ignore-call */ 
31
                    __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...
31
        }
32
    }
33
34
    /**
35
     * We don't keep all the assignments, only persistent.
36
     * @see persistAssignments
37
     */
38
    protected function saveAssignments()
39
    {
40
    }
41
42
    /**
43
     * Does real assignments saving.
44
     * The idea is to split persistent assignments from session only.
45
     */
46
    public function persistAssignments()
47
    {
48
        parent::saveAssignments();
49
    }
50
51
    public function checkAccess($userId, $permission, $params = [])
52
    {
53
        if (empty($this->getAssignments($userId))) {
54
            $this->applyUserAssignments($userId);
55
        }
56
57
        return parent::checkAccess($userId, $permission, $params)
58
            && !parent::checkAccess($userId, "deny:$permission", $params);
59
    }
60
61
    public function applyUserAssignments($userId)
62
    {
63
        $roles = '';
64
65
        if (isset($this->getApp()->user)) {
66
            $user = $this->getApp()->user->identity;
67
            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...
68
                $user = call_user_func([$this->getApp()->user->identityClass, 'findIdentity'], $userId);
69
            }
70
            if (isset($user->roles)) {
71
                $roles = $user->roles;
72
            }
73
        }
74
75
        if (empty($userId)) {
76
            $userId = '';
77
            $roles = 'role:unauthorized';
78
        }
79
80
        if ($roles) {
81
            $this->setAssignments($roles, $userId);
82
        }
83
    }
84
85
    protected function getApp()
86
    {
87
        return class_exists('Yii') ? \Yii::$app : \yii\helpers\Yii::getApp();
0 ignored issues
show
Bug introduced by
The type yii\helpers\Yii 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...
88
    }
89
}
90