Completed
Push — master ( b21a89...27844f )
by Andrii
02:23
created

AuthManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 59
ccs 16
cts 24
cp 0.6667
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A saveAssignments() 0 3 1
A persistAssignments() 0 4 1
A checkAccess() 0 9 3
C applyUserAssignments() 0 23 7
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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\rbac;
12
13
use Yii;
14
15
/**
16
 * HiPanel AuthManager.
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class AuthManager extends \yii\rbac\PhpManager
21
{
22
    public $itemFile       = '@hipanel/rbac/files/items.php';
23
    public $ruleFile       = '@hipanel/rbac/files/rules.php';
24
    public $assignmentFile = '@hipanel/rbac/files/assignments.php';
25
26
    use SetterTrait;
27
28
    /**
29
     * We don't keep all the assignments, only persistent.
30
     * @see persistAssignments
31
     */
32 20
    protected function saveAssignments()
33
    {
34 20
    }
35
36
    /**
37
     * Does real assignments saving.
38
     * The idea is to split persistent assignments from session only.
39
     */
40
    public function persistAssignments()
41
    {
42
        parent::saveAssignments();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (saveAssignments() instead of persistAssignments()). Are you sure this is correct? If so, you might want to change this to $this->saveAssignments().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
43
    }
44
45 20
    public function checkAccess($userId, $permission, $params = [])
46
    {
47 20
        if (empty($this->getAssignments($userId))) {
48 2
            $this->applyUserAssignments($userId);
49
        }
50
51 20
        return parent::checkAccess($userId, $permission, $params)
52 20
            && !parent::checkAccess($userId, "deny:$permission", $params);
53
    }
54
55 2
    public function applyUserAssignments($userId)
56
    {
57 2
        $roles = '';
58
59 2
        if (isset(Yii::$app->user)) {
60
            $user = Yii::$app->user->identity;
61
            if (!$user || $user->id !== $userId) {
62
                $user = call_user_func([Yii::$app->user->identityClass, 'findIdentity'], $userId);
63
            }
64
            if (isset($user->roles)) {
65
                $roles = $user->roles;
66
            }
67
        }
68
69 2
        if (empty($userId)) {
70 2
            $userId = '';
71 2
            $roles = 'role:unauthorized';
72
        }
73
74 2
        if ($roles) {
75 2
            $this->setAssignments($roles, $userId);
76
        }
77 2
    }
78
}
79