Passed
Push — master ( b162a4...87a689 )
by Ross
03:05
created

Postdispatch::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
1
<?php
2
/**
3
 * A two factor authentication module that protects both the admin and customer logins
4
 * Copyright (C) 2017  Ross Mitchell
5
 *
6
 * This file is part of Rossmitchell/Twofactor.
7
 *
8
 * Rossmitchell/Twofactor is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
namespace Rossmitchell\Twofactor\Observer\Controller\Admin;
23
24
use Magento\Backend\App\Action;
25
use Magento\Framework\Event\Observer;
26
use Magento\Framework\Event\ObserverInterface;
27
use Rossmitchell\Twofactor\Model\Admin\AdminUser;
28
use Rossmitchell\Twofactor\Model\Admin\Attribute\IsUsingTwoFactor;
29
use Rossmitchell\Twofactor\Model\Admin\Session;
30
use Rossmitchell\Twofactor\Model\TwoFactorUrls;
31
use Rossmitchell\Twofactor\Model\Verification\IsVerified;
32
33
class Postdispatch implements ObserverInterface
34
{
35
    /**
36
     * @var AdminUser
37
     */
38
    private $adminUser;
39
    /**
40
     * @var IsUsingTwoFactor
41
     */
42
    private $isUsingTwoFactor;
43
    /**
44
     * @var Session
45
     */
46
    private $session;
47
    /**
48
     * @var IsVerified
49
     */
50
    private $isVerified;
51
    /**
52
     * @var TwoFactorUrls
53
     */
54
    private $twoFactorUrls;
55
56
    /**
57
     * Postdispatch constructor.
58
     *
59
     * @param AdminUser        $adminUser
60
     * @param IsUsingTwoFactor $isUsingTwoFactor
61
     * @param Session          $session
62
     * @param IsVerified       $isVerified
63
     * @param TwoFactorUrls    $twoFactorUrls
64
     */
65
    public function __construct(
66
        AdminUser $adminUser,
67
        IsUsingTwoFactor $isUsingTwoFactor,
68
        Session $session,
69
        IsVerified $isVerified,
70
        TwoFactorUrls $twoFactorUrls
71
    ) {
72
        $this->adminUser        = $adminUser;
73
        $this->isUsingTwoFactor = $isUsingTwoFactor;
74
        $this->session          = $session;
75
        $this->isVerified       = $isVerified;
76
        $this->twoFactorUrls    = $twoFactorUrls;
77
    }
78
79
    /**
80
     * @param Observer $observer
81
     *
82
     * @return void
83
     */
84
    public function execute(Observer $observer)
85
    {
86
        if ($this->shouldTheUserBeRedirected() === false) {
87
            return;
88
        }
89
90
        if ($this->areWeOnANonRedirectingPage() === true) {
91
            return;
92
        }
93
94
        $controller = $observer->getEvent()->getData('controller_action');
95
        $this->redirectToAuthenticationPage($controller);
96
    }
97
98
    private function shouldTheUserBeRedirected()
99
    {
100
        $adminUser = $this->adminUser;
101
        if ($adminUser->hasAdminUser() === false) {
102
            return false;
103
        }
104
        $user = $this->adminUser->getAdminUser();
105
106
        if ($this->isUsingTwoFactor->getValue($user) === false) {
107
            return false;
108
        }
109
110
        if ($this->isVerified->isVerified($this->session) === true) {
111
            return false;
112
        }
113
114
        return true;
115
    }
116
117 View Code Duplication
    private function areWeOnANonRedirectingPage()
118
    {
119
        $urls = $this->twoFactorUrls;
120
121
        if ($urls->areWeOnTheAuthenticationPage(true) === true) {
122
            return true;
123
        }
124
125
        if ($urls->areWeOnTheVerificationPage(true) === true) {
126
            return true;
127
        }
128
129
        return false;
130
    }
131
132
    private function redirectToAuthenticationPage(Action $controller)
133
    {
134
        $twoFactorCheckUrl = $this->twoFactorUrls->getAdminVerificationUrl();
135
        $response          = $controller->getResponse();
136
        $response->setRedirect($twoFactorCheckUrl);
137
    }
138
}
139