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

AuthManager::applyUserAssignments()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 9.2312

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
ccs 9
cts 14
cp 0.6429
rs 6.7272
cc 7
eloc 13
nc 20
nop 1
crap 9.2312
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