Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3639 | 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\AuthenticationService;
7
use Laminas\Authentication\Result as AuthResult;
8
use Laminas\Db\Adapter\AdapterInterface;
9
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
10
use Laminas\Http\Header\SetCookie;
11
use Laminas\Mvc\Controller\AbstractActionController;
12
use Laminas\Log\LoggerInterface;
13
use Laminas\View\Model\ViewModel;
14
use Laminas\View\Model\JsonModel;
15
use GeoIp2\Database\Reader As GeoIp2Reader;
16
use LeadersLinked\Form\Auth\SigninForm;
17
use LeadersLinked\Form\Auth\ResetPasswordForm;
18
use LeadersLinked\Form\Auth\ForgotPasswordForm;
19
use LeadersLinked\Form\Auth\SignupForm;
20
use LeadersLinked\Authentication\AuthAdapter;
21
use LeadersLinked\Mapper\UserMapper;
22
use LeadersLinked\Mapper\EmailTemplateMapper;
23
use LeadersLinked\Model\User;
24
use LeadersLinked\Model\UserType;
25
use LeadersLinked\Library\QueueEmail;
26
use LeadersLinked\Library\Functions;
27
use LeadersLinked\Model\EmailTemplate;
28
use LeadersLinked\Mapper\UserPasswordMapper;
29
use LeadersLinked\Model\UserBrowser;
30
use LeadersLinked\Mapper\UserBrowserMapper;
31
use LeadersLinked\Mapper\UserIpMapper;
32
use LeadersLinked\Model\UserIp;
33
use LeadersLinked\Form\Auth\MoodleForm;
34
use LeadersLinked\Library\Rsa;
35
use LeadersLinked\Library\Image;
36
use LeadersLinked\Authentication\AuthEmailAdapter;
37
use Nullix\CryptoJsAes\CryptoJsAes;
38
use LeadersLinked\Model\UserPassword;
39
use LeadersLinked\Mapper\CompanyMapper;
40
use LeadersLinked\Mapper\CompanyUserMapper;
41
use LeadersLinked\Model\CompanyUser;
42
 
43
 
44
class BackendController extends AbstractActionController
45
{
46
    /**
47
     *
48
     * @var AdapterInterface
49
     */
50
    private $adapter;
51
 
52
 
53
    /**
54
     *
55
     * @var AbstractAdapter
56
     */
57
    private $cache;
58
 
59
    /**
60
     *
61
     * @var  LoggerInterface
62
     */
63
    private $logger;
64
 
65
    /**
66
     *
67
     * @var array
68
     */
69
    private $config;
70
 
71
 
72
 
73
 
74
    /**
75
     *
76
     * @param AdapterInterface $adapter
77
     * @param AbstractAdapter $cache
78
     * @param LoggerInterface $logger
79
     * @param array $config
80
     */
81
    public function __construct($adapter, $cache , $logger, $config)
82
    {
83
        $this->adapter      = $adapter;
84
        $this->cache        = $cache;
85
        $this->logger       = $logger;
86
        $this->config       = $config;
87
    }
88
 
89
    public function signinAdminAction()
90
    {
91
 
92
        $request = $this->getRequest();
93
        if($request->isGet()) {
94
            $currentUserPlugin = $this->plugin('currentUserPlugin');
95
            $currentUser = $currentUserPlugin->getUser();
96
 
97
 
98
            if($currentUser && $currentUser->usertype_id == UserType::ADMIN) {
99
 
100
 
101
                if(!$currentUser->one_time_password) {
102
                    $one_time_password = Functions::generatePassword(25);
103
 
104
                    $currentUser->one_time_password = $one_time_password;
105
 
106
                    $userMapper = UserMapper::getInstance($this->adapter);
107
                    $userMapper->updateOneTimePassword($currentUser, $one_time_password);
108
                }
109
 
110
                $sandbox = $this->config['leaderslinked.runmode.sandbox'];
111
                if($sandbox) {
112
                    $url = $this->config['leaderslinked.backend.sandbox_url'];
113
                    $salt = $this->config['leaderslinked.backend.sandbox_salt'];
114
                } else {
115
                    $url = $this->config['leaderslinked.backend.production_url'];
116
                    $salt = $this->config['leaderslinked.backend.production_salt'];
117
                }
118
 
119
 
120
                $rand = 1000 + mt_rand(1, 999);
121
                $timestamp = time();
122
                $password = md5($currentUser->one_time_password . '-' . $rand . '-' . $timestamp . '-' . $salt);
123
 
124
                $params = [
125
                    'user_uuid' => $currentUser->uuid,
126
                    'password' => $password,
127
                    'rand' => $rand,
128
                    'time' => $timestamp,
129
                ];
130
 
131
                $link_admin = $url . '/signin-admin' . '?' . http_build_query($params);
132
            } else {
133
                $link_admin = '';
134
            }
135
 
136
            $data = [
137
                'success' => true,
138
                'data' => $link_admin
139
            ];
140
 
141
            return new JsonModel($data);
142
 
143
        } else {
144
            $data = [
145
                'success' => false,
146
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
147
            ];
148
 
149
            return new JsonModel($data);
150
        }
151
 
152
        return new JsonModel($data);
153
 
154
 
155
    }
156
 
157
    public function signinCompanyAction()
158
    {
159
 
160
        $request = $this->getRequest();
161
        if($request->isGet()) {
162
 
163
 
164
            $currentUserPlugin = $this->plugin('currentUserPlugin');
165
            $currentUser = $currentUserPlugin->getUser();
166
 
167
            $id = $this->params()->fromRoute('id');
168
            $companyMapper = CompanyMapper::getInstance($this->adapter);
169
            $company = $companyMapper->fetchOneByUuid($id);
170
 
171
            $link_admin = '';
172
 
173
            if($company) {
174
 
175
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
176
                $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
177
 
178
                if($companyUser && $companyUser->status == CompanyUser::STATUS_ACCEPTED && $companyUser->backend == CompanyUser::BACKEND_YES ) {
179
                    if(!$currentUser->one_time_password) {
180
                        $one_time_password = Functions::generatePassword(25);
181
 
182
                        $currentUser->one_time_password = $one_time_password;
183
 
184
                        $userMapper = UserMapper::getInstance($this->adapter);
185
                        $userMapper->updateOneTimePassword($currentUser, $one_time_password);
186
                    }
187
 
188
                    $timestamp = date('Y-m-d\TH:i:s');
189
                    $rand = 1000 + rand(1, 8999);
190
 
191
 
192
 
193
                    $sandbox = $this->config['leaderslinked.runmode.sandbox'];
194
                    if($sandbox) {
195
                        $url = $this->config['leaderslinked.backend.sandbox_url'];
196
                        $salt = $this->config['leaderslinked.backend.sandbox_salt'];
197
                    } else {
198
                        $url = $this->config['leaderslinked.backend.production_url'];
199
                        $salt = $this->config['leaderslinked.backend.production_salt'];
200
                    }
201
 
202
 
203
                    $rand = 1000 + mt_rand(1, 999);
204
                    $timestamp = time();
205
                    $password = md5($currentUser->one_time_password . '-' . $rand . '-' . $timestamp . '-' . $salt);
206
 
207
                    $link_admin = $url . '/signin-company?user_uuid='. $currentUser->uuid .
208
                    '&rand=' . $rand . '&time=' . $timestamp . '&password=' . $password .
209
                    '&company_uuid=' . $company->uuid;
210
 
211
                }
212
 
213
 
214
 
215
            }
216
 
217
 
218
 
219
            $data = [
220
                'success' => true,
221
                'data' => $link_admin
222
            ];
223
 
224
            return new JsonModel($data);
225
 
226
        } else {
227
            $data = [
228
                'success' => false,
229
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
230
            ];
231
 
232
            return new JsonModel($data);
233
        }
234
 
235
        return new JsonModel($data);
236
 
237
 
238
    }
239
 
240
 
241
}