Completed
Push — master ( a6ba4b...d9c9eb )
by
unknown
02:29 queued 11s
created

AccessRuleFilter::matchRole()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.2742

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 7
cts 9
cp 0.7778
rs 9.3554
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 5.2742
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 parent::matchRole($user);
56
        }
57
58
        // We just check our custom role "admin" otherwise call back the original implementation
59 2
        if (!in_array("admin", $this->roles)) {
60
            return parent::matchRole($user);
61
        }
62
        /** @var User $identity */
63 2
        $identity = $user->getIdentity();
64 2
        if (!$user->getIsGuest() && $identity->getIsAdmin()) {
65 2
            return true;
66
        }
67
68
        return parent::matchRole($user);
69
    }
70
}
71