Passed
Pull Request — master (#20)
by Nikolay
04:51
created

SessionController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 53
c 1
b 0
f 0
dl 0
loc 113
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A flushCache() 0 7 3
A indexAction() 0 8 1
A startAction() 0 21 4
A endAction() 0 5 1
A _registerSession() 0 6 1
A updateSystemLanguage() 0 14 4
A changeLanguageAction() 0 11 3
1
<?php
2
/**
3
 * Copyright (C) MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Nikolay Beketov, 6 2018
7
 *
8
 */
9
10
namespace MikoPBX\AdminCabinet\Controllers;
11
12
use GuzzleHttp\Client;
13
use MikoPBX\AdminCabinet\Forms\LoginForm;
14
use MikoPBX\Common\Models\PbxSettings;
15
use MikoPBX\Core\System\Util;
16
17
/**
18
 * SessionController
19
 *
20
 * Allows to authenticate users
21
 */
22
class SessionController extends BaseController
23
{
24
    public function indexAction(): void
25
    {
26
        $this->flushCache();
27
        $this->view->NameFromSettings
28
                          = PbxSettings::getValueByKey('Name');
29
        $this->view->DescriptionFromSettings
30
                          = PbxSettings::getValueByKey('Description');
31
        $this->view->form = new LoginForm();
32
    }
33
34
    /**
35
     * Flush all cache folders
36
     */
37
    private function flushCache(): void
38
    {
39
        if ($this->di->has('modelsCache')) {
40
            $this->di->getShared('modelsCache')->clear();
41
        }
42
        if ($this->di->has('managedCache')) {
43
            $this->di->getShared('managedCache')->clear();
44
        }
45
    }
46
47
    /**
48
     * This action authenticate and logs an user into the application
49
     *
50
     */
51
    public function startAction(): void
52
    {
53
        if ( ! $this->request->isPost()) {
54
            $this->forward('session/index');
55
        }
56
        $loginFromUser = $this->request->getPost('login');
57
        $passFromUser  = $this->request->getPost('password');
58
        $this->flash->clear();
59
        $login    = PbxSettings::getValueByKey('WebAdminLogin');
60
        $password = PbxSettings::getValueByKey('WebAdminPassword');
61
        if ($password === $passFromUser && $login === $loginFromUser) {
62
            $this->_registerSession('admins');
63
            $this->updateSystemLanguage();
64
            $this->view->success = true;
65
            $this->view->reload  = 'index/index';
66
        } else {
67
            $this->view->success = false;
68
            $this->flash->error($this->translation->_('auth_WrongLoginPassword'));
69
            $remoteAddress = $this->request->getClientAddress(true);
70
            $userAgent     = $this->request->getUserAgent();
71
            Util::sysLogMsg('web_auth', "From: {$remoteAddress} UserAgent:{$userAgent} Cause: Wrong password");
72
        }
73
    }
74
75
    /**
76
     * Register an authenticated user into session data
77
     *
78
     * @param  $role
79
     */
80
    private function _registerSession($role): void
81
    {
82
        $sessionParams = [
83
            'role' => $role,
84
        ];
85
        $this->session->set('auth', $sessionParams);
86
    }
87
88
    /**
89
     * Updates system settings for language
90
     *
91
     */
92
    private function updateSystemLanguage(): void
93
    {
94
        $newLanguage = $this->session->get('WebAdminLanguage');
95
        if ( ! isset($newLanguage)) {
96
            return;
97
        }
98
        $languageSettings = PbxSettings::findFirstByKey('WebAdminLanguage');
99
        if ($languageSettings === null) {
100
            $languageSettings      = new PbxSettings();
101
            $languageSettings->key = 'WebAdminLanguage';
102
        }
103
        if ($newLanguage !== $languageSettings->value) {
104
            $languageSettings->value = $newLanguage;
105
            $languageSettings->save();
106
        }
107
    }
108
109
    /**
110
     * Process language change
111
     */
112
    public function changeLanguageAction(): void
113
    {
114
        $newLanguage = $this->request->getPost('newLanguage', 'string');
115
        if (array_key_exists($newLanguage, $this->elements->getAvailableWebAdminLanguages())) {
116
            $this->session->set('WebAdminLanguage', $newLanguage);
117
            if ($this->session->has('auth')) {
118
                $this->updateSystemLanguage();
119
            }
120
            $this->view->success = true;
121
        } else {
122
            $this->view->success = false;
123
        }
124
    }
125
126
    /**
127
     * Finishes the active session redirecting to the index
128
     *
129
     */
130
    public function endAction(): void
131
    {
132
        $this->flushCache();
133
        $this->session->remove('auth');
134
        $this->session->destroy();
135
    }
136
}
137