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

AllowedIpsController::behaviors()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 31
cp 0
rs 9.344
c 0
b 0
f 0
cc 4
nc 1
nop 0
crap 20
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