Completed
Push — master ( 06202b...97cefe )
by Antonio
18s queued 11s
created

AccessRuleFilter::allows()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 8.6506
c 0
b 0
f 0
cc 7
nc 3
nop 3
crap 7
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/gdpr-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
                        /** @var User $identity */
38 1
                        $identity = $user->identity;
39 1
                        if (!$identity->gdpr_consent) {
40 1
                            Yii::$app->response->redirect([ "/$consentAction"])->send();
41
                        }
42
                    }
43
                }
44
            }
45
        }
46 9
        return parent::allows($action, $user, $request);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     * */
52 9
    protected function matchRole($user)
53
    {
54 9
        if (empty($this->roles)) {
55 9
            return true;
56
        }
57
58 2
        foreach ($this->roles as $role) {
59 2
            if ($role === '?') {
60
                if ($user->getIsGuest()) {
61
                    return true;
62
                }
63 2
            } elseif ($role === '@') {
64
                if (!$user->getIsGuest()) {
65
                    return true;
66
                }
67 2
            } elseif ($role === 'admin') {
68
                /** @var User $identity */
69 2
                $identity = $user->getIdentity();
70
71 2
                if (!$user->getIsGuest() && $identity->getIsAdmin()) {
72 2
                    return true;
73
                }
74
            } else {
75
                $roleParams = $this->roleParams instanceof Closure
76
                    ? call_user_func($this->roleParams, $this)
77
                    : $this->roleParams;
78
79
                if ($user->can($role, $roleParams)) {
80
                    return true;
81
                }
82
            }
83
        }
84
85
        return false;
86
    }
87
}
88