AllowedIpsController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 86
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2
ccs 0
cts 66
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A behaviors() 0 36 4
B actionNotAllowedIp() 0 35 10
1
<?php
2
/**
3
 * Multi-factor authentication for Yii2 projects
4
 *
5
 * @link      https://github.com/hiqdev/yii2-mfa
6
 * @package   yii2-mfa
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\yii2\mfa\controllers;
12
13
use hiqdev\php\confirmator\ServiceInterface;
14
use hiqdev\yii2\mfa\base\MfaIdentityInterface;
15
use hiqdev\yii2\mfa\exceptions\AuthenticationException;
16
use hiqdev\yii2\mfa\filters\ValidateAuthenticationFilter;
17
use Yii;
18
use yii\filters\AccessControl;
19
20
/**
21
 * Allowed IPs controller.
22
 */
23
class AllowedIpsController extends \yii\web\Controller
24
{
25
    /**
26
     * @var ServiceInterface
27
     */
28
    private $confirmator;
29
30
    public function __construct($id, $module, ServiceInterface $confirmator, $config = [])
31
    {
32
        parent::__construct($id, $module, $config);
33
        $this->confirmator = $confirmator;
34
    }
35
36
    public function behaviors()
37
    {
38
        return [
39
            'access' => [
40
                'class' => AccessControl::class,
41
                'only' => ['not-allowed-ip', 'other'],
42
                'denyCallback' => function () {
43
                    return $this->goHome();
44
                },
45
                'rules' => [
46
                    [
47
                        'actions' => ['not-allowed-ip'],
48
                        'allow' => true,
49
                        'matchCallback' => function ($action) {
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
                            $filter = new ValidateAuthenticationFilter();
51
52
                            $identity = Yii::$app->user->identity ?: $this->module->getHalfUser();
53
54
                            if ($identity === null) {
55
                                return false;
56
                            }
57
58
                            try {
59
                                $filter->validateAuthentication($identity);
60
                            } catch (AuthenticationException $e) {
61
                                // Show this page only when user have problems with IP
62
                                return true;
63
                            }
64
65
                            return false;
66
                        },
67
                    ],
68
                ],
69
            ],
70
        ];
71
    }
72
73
    public function actionNotAllowedIp($token = null)
74
    {
75
        $ip = Yii::$app->request->getUserIP();
76
        /** @var MfaIdentityInterface $user */
77
        $user = $this->module->getHalfUser();
78
        if ($user && $token === 'send') {
79
            if ($this->confirmator->mailToken($user, 'add-allowed-ip', ['ip' => $ip])) {
80
                Yii::$app->session->setFlash('success', Yii::t('mfa', 'Check your email for further instructions.'));
81
            } else {
82
                Yii::$app->session->setFlash('error', Yii::t('mfa', 'Sorry, we are unable to add allowed IP for the user.'));
83
            }
84
85
            return $this->goHome();
86
        }
87
        if ($user && $token) {
88
            $token = $this->confirmator->findToken($token);
89
            if ($token && $token->check([
90
                'username' => $user->getUsername(),
91
                'action' => 'add-allowed-ip',
92
                'ip' => $ip,
93
            ])) {
94
                $user->addAllowedIp($ip);
95
                if ($user->save() && Yii::$app->user->login($user)) {
96
                    Yii::$app->session->setFlash('success', Yii::t('mfa', 'Now you are allowed to login from {ip}.', ['ip' => $ip]));
97
98
                    return $this->goBack();
99
                }
100
            }
101
            Yii::$app->session->setFlash('error', Yii::t('mfa', 'Sorry, we are unable to add allowed IP for the user.'));
102
103
            return $this->goHome();
104
        }
105
106
        return $this->render('notAllowedIp', compact('ip'));
107
    }
108
}
109