User::can()   B
last analyzed

Complexity

Conditions 9
Paths 45

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 23
cp 0
rs 8.0555
c 0
b 0
f 0
cc 9
nc 45
nop 3
crap 90
1
<?php
2
/**
3
 * User.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author Philippe Gaultier <[email protected]>
8
 * @copyright 2010-2017 Philippe Gaultier
9
 * @license http://www.sweelix.net/license license
10
 * @version 1.2.0
11
 * @link http://www.sweelix.net
12
 * @package sweelix\oauth2\server\models
13
 * @since 1.0.0
14
 */
15
16
namespace sweelix\oauth2\server\web;
17
18
use sweelix\oauth2\server\interfaces\UserModelInterface;
19
use yii\web\User as BaseUser;
20
21
/**
22
 * This user model extends yii\web\User to handle scope authorization in can() assertion
23
 *
24
 * @author Philippe Gaultier <[email protected]>
25
 * @copyright 2010-2017 Philippe Gaultier
26
 * @license http://www.sweelix.net/license license
27
 * @version 1.2.0
28
 * @link http://www.sweelix.net
29
 * @package sweelix\oauth2\server\models
30
 * @since 1.0.0
31
 */
32
class User extends BaseUser
33
{
34
    /**
35
     * @inheritdoc
36
     */
37
    public function can($permissionName, $params = [], $allowCaching = true)
38
    {
39
        $oauth = true;
40
        $rbac = true;
41
        $status = true;
42
        if (strncmp('oauth2:', $permissionName, 7) === 0) {
43
            $permissionName = substr($permissionName, 7);
44
            // check only the scope
45
            $rbac = false;
46
        } elseif (strncmp('rbac:', $permissionName, 5) === 0) {
47
            $permissionName = substr($permissionName, 5);
48
            // check only rbac
49
            $oauth = false;
50
        }
51
52
        if ($oauth === true) {
53
            // Check if scope is authorized
54
            $scopeCheck = true;
55
            if (($this->identity instanceof UserModelInterface) && ($this->identity->getRestrictedScopes() !== null)) {
56
                $scopeCheck = in_array($permissionName, $this->identity->getRestrictedScopes());
57
            }
58
            $status = $status && $scopeCheck;
59
        }
60
61
        if ($rbac === true) {
62
            $regularCheck = parent::can($permissionName, $params, $allowCaching);
63
            $status = $status && $regularCheck;
64
        }
65
66
        // perform regular check
67
68
69
        return $status;
70
    }
71
}
72