Completed
Push — master ( a2eb7a...442d5a )
by Andrii
01:39
created

AllowedIpsController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 85
ccs 0
cts 66
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A behaviors() 0 36 4
B actionNotAllowedIp() 0 34 11
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\exceptions\AuthenticationException;
15
use hiqdev\yii2\mfa\filters\ValidateAuthenticationFilter;
16
use Yii;
17
use yii\filters\AccessControl;
18
19
/**
20
 * Allowed IPs controller.
21
 */
22
class AllowedIpsController extends \yii\web\Controller
23
{
24
    /**
25
     * @var ServiceInterface
26
     */
27
    private $confirmator;
28
29
    public function __construct($id, $module, ServiceInterface $confirmator, $config = [])
30
    {
31
        parent::__construct($id, $module, $config);
32
        $this->confirmator = $confirmator;
33
    }
34
35
    public function behaviors()
36
    {
37
        return [
38
            'access' => [
39
                'class' => AccessControl::class,
40
                'only' => ['not-allowed-ip', 'other'],
41
                'denyCallback' => function () {
42
                    return $this->goHome();
43
                },
44
                'rules' => [
45
                    [
46
                        'actions' => ['not-allowed-ip'],
47
                        'allow' => true,
48
                        '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...
49
                            $filter = new ValidateAuthenticationFilter();
50
51
                            $identity = Yii::$app->user->identity ?: $this->module->getHalfUser();
52
53
                            if ($identity === null) {
54
                                return false;
55
                            }
56
57
                            try {
58
                                $filter->validateAuthentication($identity);
59
                            } catch (AuthenticationException $e) {
60
                                // Show this page only when user have problems with IP
61
                                return true;
62
                            }
63
64
                            return false;
65
                        },
66
                    ],
67
                ],
68
            ],
69
        ];
70
    }
71
72
    public function actionNotAllowedIp($token = null)
73
    {
74
        $ip = Yii::$app->request->getUserIP();
75
        $user = $this->module->getHalfUser();
76
        if ($user && $token === 'send') {
77
            if ($this->confirmator->mailToken($user, 'add-allowed-ip', ['ip' => $ip])) {
78
                Yii::$app->session->setFlash('success', Yii::t('mfa', 'Check your email for further instructions.'));
79
            } else {
80
                Yii::$app->session->setFlash('error', Yii::t('mfa', 'Sorry, we are unable to add allowed IP for the user.'));
81
            }
82
83
            return $this->goHome();
84
        }
85
        if ($user && $token) {
86
            $token = $this->confirmator->findToken($token);
87
            if ($token && $token->check([
88
                'username' => $user->username,
89
                'action' => 'add-allowed-ip',
90
                'ip' => $ip,
91
            ])) {
92
                $user->allowed_ips .= ($user->allowed_ips ? ',' : '') . $ip;
93
                if ($user->save() && Yii::$app->user->login($user)) {
94
                    Yii::$app->session->setFlash('success', Yii::t('mfa', 'Now you are allowed to login from {ip}.', ['ip' => $ip]));
95
96
                    return $this->goBack();
97
                }
98
            }
99
            Yii::$app->session->setFlash('error', Yii::t('mfa', 'Sorry, we are unable to add allowed IP for the user.'));
100
101
            return $this->goHome();
102
        }
103
104
        return $this->render('notAllowedIp', compact('ip'));
105
    }
106
}
107