Completed
Pull Request — master (#338)
by Antonio
02:54
created

AccessRuleFilter   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 69.69%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 5
dl 0
loc 67
ccs 23
cts 33
cp 0.6969
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B allows() 0 20 7
C matchRole() 0 35 12
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Filter;
13
14
use Closure;
15
use Da\User\Model\User;
16
use Da\User\Traits\ModuleAwareTrait;
17
use Yii;
18
use yii\filters\AccessRule;
19
20
class AccessRuleFilter extends AccessRule
21
{
22
    use ModuleAwareTrait;
23
24
    /**
25
     * @inheritDoc
26
     */
27 9
    public function allows($action, $user, $request)
28
    {
29 9
        $consentAction = 'user/settings/consent';
30 9
        if (!$user->isGuest && $action->uniqueId !== $consentAction) {
31 8
            $module = $this->getModule();
32 8
            if ($module->gdprRequireConsentToAll) {
33 1
                $excludedUrls = $module->gdprConsentExcludedUrls;
34 1
                $excludedUrls[] = $module->gdprPrivacyPolicyUrl;
35 1
                foreach ($excludedUrls as $url) {
36 1
                    if (!fnmatch($url, $action->uniqueId)) {
37 1
                        $identity = $user->identity;
38 1
                        if (!$identity->gdpr_consent) {
39 1
                            Yii::$app->response->redirect([ "/$consentAction"])->send();
40
                        }
41
                    }
42
                }
43
            }
44
        }
45 9
        return parent::allows($action, $user, $request);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     * */
51 9
    protected function matchRole($user)
52
    {
53 9
        if (empty($this->roles)) {
54 9
            return true;
55
        }
56
57 2
        foreach ($this->roles as $role) {
58 2
            if ($role === '?') {
59
                if ($user->getIsGuest()) {
60
                    return true;
61
                }
62 2
            } elseif ($role === '@') {
63
                if (!$user->getIsGuest()) {
64
                    return true;
65
                }
66 2
            } elseif ($role === 'admin') {
67
                /** @var User $identity */
68 2
                $identity = $user->getIdentity();
69
70 2
                if (!$user->getIsGuest() && $identity->getIsAdmin()) {
71 2
                    return true;
72
                }
73
            } else {
74
                $roleParams = $this->roleParams instanceof Closure
75
                    ? call_user_func($this->roleParams, $this)
76
                    : $this->roleParams;
77
78
                if ($user->can($role, $roleParams)) {
79
                    return true;
80
                }
81
            }
82
        }
83
84
        return false;
85
    }
86
}
87