Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15444 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Authentication\Result as AuthResult;
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
 
12
use LeadersLinked\Authentication\AuthOneTimePasswordAdapter;
13
use Laminas\Authentication\AuthenticationService;
14
 
15
use Laminas\View\Model\JsonModel;
16
use Laminas\View\Model\ViewModel;
17
 
18
 
19
class AuthController extends AbstractActionController
20
{
21
    /**
22
     *
23
     * @var AdapterInterface
24
     */
25
    private $adapter;
26
 
27
 
28
    /**
29
     *
30
     * @var AbstractAdapter
31
     */
32
    private $cache;
33
 
34
    /**
35
     *
36
     * @var  LoggerInterface
37
     */
38
    private $logger;
39
 
40
    /**
41
     *
42
     * @var array
43
     */
44
    private $config;
45
 
46
 
47
 
48
 
49
    /**
50
     *
51
     * @param AdapterInterface $adapter
52
     * @param AbstractAdapter $cache
53
     * @param LoggerInterface $logger
54
     * @param array $config
55
     */
56
    public function __construct($adapter, $cache , $logger, $config)
57
    {
58
        $this->adapter      = $adapter;
59
        $this->cache        = $cache;
60
        $this->logger       = $logger;
61
        $this->config       = $config;
62
    }
63
 
64
    public function indexAction()
65
    {
66
        $this->layout()->setTemplate('layout/auth');
67
        $viewModel = new ViewModel();
68
        $viewModel->setTemplate('leaders-linked/auth/index.phtml');
69
 
70
        return $viewModel ;
71
    }
72
 
73
    public function signoutAction()
74
    {
75
        $auth = new AuthenticationService();
76
        $auth->clearIdentity();
77
 
78
        return $this->redirect()->toRoute('home');
79
    }
80
 
81
    public function signinAdminAction()
82
    {
83
 
84
 
85
        $request = $this->getRequest();
86
        if($request->isGet()) {
87
            $user_uuid  = filter_var($this->params()->fromQuery('user_uuid'), FILTER_SANITIZE_STRING);
88
            $rand       = filter_var($this->params()->fromQuery('rand'), FILTER_SANITIZE_NUMBER_INT);
89
            $timestamp  = filter_var($this->params()->fromQuery('time'), FILTER_SANITIZE_NUMBER_INT);
90
            $password   = filter_var($this->params()->fromQuery('password'), FILTER_SANITIZE_STRING);
91
 
92
 
93
            if(!$user_uuid || !$rand || !$timestamp || !$password ) {
94
                throw new \Exception('ERROR_PARAMETERS_ARE_INVALID');
95
            }
96
 
97
 
98
            $authAdapter = new AuthOneTimePasswordAdapter ($this->adapter, $this->config);
99
            $authAdapter->setDataAdmin($user_uuid, $password, $timestamp, $rand);
100
 
101
            $authService = new AuthenticationService();
102
            $result = $authService->authenticate($authAdapter);
103
 
104
 
105
            if($result->getCode() == AuthResult::SUCCESS) {
106
                return $this->redirect()->toRoute('dashboard');
107
            } else {
108
                throw new \Exception($result->getMessages()[0]);
109
            }
110
        }
111
 
112
        return new JsonModel([
113
            'success' => false,
114
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
115
        ]);
116
 
117
    }
118
 
119
    public function signinCompanyAction()
120
    {
121
        $request = $this->getRequest();
122
        if($request->isGet()) {
123
            $company_uuid = filter_var($this->params()->fromQuery('company_uuid'), FILTER_SANITIZE_STRING);
124
            $user_uuid  = filter_var($this->params()->fromQuery('user_uuid'), FILTER_SANITIZE_STRING);
125
            $timestamp   = filter_var($this->params()->fromQuery('time'), FILTER_SANITIZE_STRING);
126
            $password   = filter_var($this->params()->fromQuery('password'), FILTER_SANITIZE_STRING);
127
            $rand       = filter_var($this->params()->fromQuery('rand'), FILTER_SANITIZE_NUMBER_INT);
128
 
129
            if(empty($user_uuid)  || empty($company_uuid) || empty($user_uuid) || empty($timestamp)  || empty($password) || empty($rand)) {
130
                return new JsonModel([
131
                    'success' => false,
132
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID'
133
                ]);
134
            }
135
 
136
            $authAdapter = new AuthOneTimePasswordAdapter ($this->adapter, $this->config);
137
            $authAdapter->setDataCompany($user_uuid, $password, $timestamp, $rand, $company_uuid);
138
 
139
            $authService = new AuthenticationService();
140
            $result = $authService->authenticate($authAdapter);
141
 
142
 
143
            if($result->getCode() == AuthResult::SUCCESS) {
144
                return $this->redirect()->toRoute('dashboard');
145
            } else {
146
                throw new \Exception($result->getMessages()[0]);
147
            }
148
        }
149
 
150
        return new JsonModel([
151
            'success' => false,
152
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
153
        ]);
154
    }
155
}