Passed
Push — master ( 221005...26261c )
by Rubén
03:12
created

LoginController::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Modules\Web\Controllers;
26
27
use SP\Bootstrap;
28
use SP\Core\Context\SessionContext;
29
use SP\Core\Events\Event;
30
use SP\Core\Events\EventMessage;
31
use SP\Core\SessionUtil;
32
use SP\Http\Uri;
33
use SP\Modules\Web\Controllers\Helpers\LayoutHelper;
34
use SP\Modules\Web\Controllers\Traits\JsonTrait;
35
use SP\Services\Auth\LoginService;
36
37
/**
38
 * Class LoginController
39
 *
40
 * @package SP\Modules\Web\Controllers
41
 */
42
final class LoginController extends ControllerBase
43
{
44
    use JsonTrait;
45
46
    /**
47
     * Login action
48
     *
49
     * @throws \Psr\Container\ContainerExceptionInterface
50
     * @throws \Psr\Container\NotFoundExceptionInterface
51
     */
52
    public function loginAction()
53
    {
54
        try {
55
            $this->checkSecurityToken($this->previousSk, $this->request);
56
57
            $loginService = $this->dic->get(LoginService::class);
58
59
            $from = $this->getSignedUriFromRequest();
60
            $loginService->setFrom($from);
61
62
            $loginResponmse = $loginService->doLogin();
63
64
            $this->checkForwarded();
65
66
            $redirector = function ($route) use ($from) {
67
                $uri = new Uri(ltrim(Bootstrap::$SUBURI, '/'));
68
                $uri->addParam('r', $route);
69
70
                if ($from !== null) {
71
                    return $uri->addParam('from', $from)
72
                        ->getUriSigned($this->configData->getPasswordSalt());
73
                }
74
75
                return $uri->getUri();
76
            };
77
78
            $this->eventDispatcher->notifyEvent('login.finish',
79
                new Event($this,
80
                    EventMessage::factory()
81
                        ->addExtra('redirect', $redirector))
82
            );
83
84
            return $this->returnJsonResponseData([
85
                'url' => $this->session->getTrasientKey('redirect') ?: $loginResponmse->getRedirect()
86
            ]);
87
        } catch (\Exception $e) {
88
            processException($e);
89
90
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
91
92
            return $this->returnJsonResponse($e->getCode(), $e->getMessage());
93
        }
94
    }
95
96
    /**
97
     * checkForwarded
98
     */
99
    private function checkForwarded()
100
    {
101
        $forward = $this->request->getForwardedFor();
102
103
        if ($forward !== null) {
104
            $this->eventDispatcher->notifyEvent('login.info',
105
                new Event($this, EventMessage::factory()
106
                    ->addDetail('Forwarded', $this->configData->isDemoEnabled() ? '***' : implode(',', $forward)))
107
            );
108
        }
109
    }
110
111
    /**
112
     * Logout action
113
     */
114
    public function logoutAction()
115
    {
116
        if ($this->session->isLoggedIn() === true) {
117
            $inactiveTime = abs(round((time() - $this->session->getLastActivity()) / 60, 2));
118
            $totalTime = abs(round((time() - $this->session->getStartActivity()) / 60, 2));
119
120
            $this->eventDispatcher->notifyEvent('logout',
121
                new Event($this, EventMessage::factory()
122
                    ->addDescription(__u('Logout session'))
123
                    ->addDetail(__u('User'), $this->session->getUserData()->getLogin())
124
                    ->addDetail(__u('Inactive time'), $inactiveTime . ' min.')
125
                    ->addDetail(__u('Total time'), $totalTime . ' min.'))
126
            );
127
128
            SessionUtil::cleanSession();
129
130
            $this->session->setAppStatus(SessionContext::APP_STATUS_LOGGEDOUT);
131
132
            $layoutHelper = $this->dic->get(LayoutHelper::class);
133
            $layoutHelper->getCustomLayout('logout', 'logout');
134
135
            $this->view();
136
        } else {
137
            $this->router->response()->redirect('index.php?r=login');
138
        }
139
    }
140
141
    /**
142
     * Index action
143
     *
144
     * @throws \Psr\Container\ContainerExceptionInterface
145
     * @throws \Psr\Container\NotFoundExceptionInterface
146
     */
147
    public function indexAction()
148
    {
149
        $this->dic->get(LayoutHelper::class)
150
            ->getCustomLayout('index', 'login');
151
152
        $this->view->assign('mailEnabled', $this->configData->isMailEnabled());
153
154
        $this->prepareSignedUriOnView();
155
156
        $this->view();
157
    }
158
159
    /**
160
     * @return void
161
     */
162
    protected function initialize()
163
    {
164
        // TODO: Implement initialize() method.
165
    }
166
}