Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4398 | Rev 5050 | Ir a la última revisión | | Comparar con el anterior | 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;
3639 efrain 42
use LeadersLinked\Mapper\NetworkMapper;
4398 efrain 43
use LeadersLinked\Model\CalendarEvent;
4656 efrain 44
use LeadersLinked\Mapper\PerformanceEvaluationTestMapper;
1 www 45
 
46
 
47
class BackendController extends AbstractActionController
48
{
49
    /**
50
     *
51
     * @var AdapterInterface
52
     */
53
    private $adapter;
54
 
55
 
56
    /**
57
     *
58
     * @var AbstractAdapter
59
     */
60
    private $cache;
61
 
62
    /**
63
     *
64
     * @var  LoggerInterface
65
     */
66
    private $logger;
67
 
68
    /**
69
     *
70
     * @var array
71
     */
72
    private $config;
73
 
74
 
75
 
76
 
77
    /**
78
     *
79
     * @param AdapterInterface $adapter
80
     * @param AbstractAdapter $cache
81
     * @param LoggerInterface $logger
82
     * @param array $config
83
     */
84
    public function __construct($adapter, $cache , $logger, $config)
85
    {
86
        $this->adapter      = $adapter;
87
        $this->cache        = $cache;
88
        $this->logger       = $logger;
89
        $this->config       = $config;
90
    }
91
 
92
    public function signinAdminAction()
93
    {
94
 
95
        $request = $this->getRequest();
96
        if($request->isGet()) {
97
            $currentUserPlugin = $this->plugin('currentUserPlugin');
98
            $currentUser = $currentUserPlugin->getUser();
99
 
3639 efrain 100
            $networkMapper = NetworkMapper::getInstance($this->adapter);
101
            $network = $networkMapper->fetchOne($currentUser->network_id);
1 www 102
 
3639 efrain 103
            $sandbox = $this->config['leaderslinked.runmode.sandbox'];
104
            if($sandbox) {
105
                $salt = $this->config['leaderslinked.backend.sandbox_salt'];
106
            } else {
107
                $salt = $this->config['leaderslinked.backend.production_salt'];
108
            }
109
 
110
 
111
 
1 www 112
            if($currentUser && $currentUser->usertype_id == UserType::ADMIN) {
113
 
114
 
115
                if(!$currentUser->one_time_password) {
116
                    $one_time_password = Functions::generatePassword(25);
117
 
118
                    $currentUser->one_time_password = $one_time_password;
119
 
120
                    $userMapper = UserMapper::getInstance($this->adapter);
121
                    $userMapper->updateOneTimePassword($currentUser, $one_time_password);
122
                }
123
 
3639 efrain 124
 
1 www 125
 
126
 
127
                $rand = 1000 + mt_rand(1, 999);
128
                $timestamp = time();
129
                $password = md5($currentUser->one_time_password . '-' . $rand . '-' . $timestamp . '-' . $salt);
130
 
131
                $params = [
132
                    'user_uuid' => $currentUser->uuid,
133
                    'password' => $password,
134
                    'rand' => $rand,
135
                    'time' => $timestamp,
136
                ];
137
 
3639 efrain 138
                $link_admin = 'https://'. $network->admin_hostname . '/signin-admin' . '?' . http_build_query($params);
1 www 139
            } else {
140
                $link_admin = '';
141
            }
142
 
143
            $data = [
144
                'success' => true,
145
                'data' => $link_admin
146
            ];
147
 
148
            return new JsonModel($data);
149
 
150
        } else {
151
            $data = [
152
                'success' => false,
153
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
154
            ];
155
 
156
            return new JsonModel($data);
157
        }
158
 
159
        return new JsonModel($data);
160
 
161
 
162
    }
163
 
164
    public function signinCompanyAction()
165
    {
166
 
167
        $request = $this->getRequest();
168
        if($request->isGet()) {
169
 
170
 
171
            $currentUserPlugin = $this->plugin('currentUserPlugin');
172
            $currentUser = $currentUserPlugin->getUser();
3639 efrain 173
 
1 www 174
 
175
            $id = $this->params()->fromRoute('id');
4398 efrain 176
            $type = $this->params()->fromRoute('type');
177
            $relational = $this->params()->fromRoute('relational');
178
 
179
 
180
 
1 www 181
            $companyMapper = CompanyMapper::getInstance($this->adapter);
182
            $company = $companyMapper->fetchOneByUuid($id);
183
 
184
            $link_admin = '';
185
 
186
            if($company) {
187
 
3639 efrain 188
                $networkMapper = NetworkMapper::getInstance($this->adapter);
189
                $network = $networkMapper->fetchOne($currentUser->network_id);
190
 
1 www 191
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
192
                $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
193
 
194
                if($companyUser && $companyUser->status == CompanyUser::STATUS_ACCEPTED && $companyUser->backend == CompanyUser::BACKEND_YES ) {
195
                    if(!$currentUser->one_time_password) {
196
                        $one_time_password = Functions::generatePassword(25);
197
 
198
                        $currentUser->one_time_password = $one_time_password;
199
 
200
                        $userMapper = UserMapper::getInstance($this->adapter);
201
                        $userMapper->updateOneTimePassword($currentUser, $one_time_password);
202
                    }
203
 
204
 
205
                    $sandbox = $this->config['leaderslinked.runmode.sandbox'];
206
                    if($sandbox) {
207
                        $salt = $this->config['leaderslinked.backend.sandbox_salt'];
208
                    } else {
209
                        $salt = $this->config['leaderslinked.backend.production_salt'];
210
                    }
4398 efrain 211
 
212
                    if($relational && $type) {
213
                        switch($type)
214
                        {
215
                            case CalendarEvent::TYPE_PERFORMANCE_EVALUATION :
4656 efrain 216
                                $performanceEvaluationTestMapper = PerformanceEvaluationTestMapper::getInstance($this->adapter);
217
                                $performanceEvaluationTest = $performanceEvaluationTestMapper->fetchOneByUuid($relational);
218
                                if($performanceEvaluationTest) {
4398 efrain 219
 
220
 
4656 efrain 221
                                    if($performanceEvaluationTest->employee_id != $currentUser->id
222
                                        && $performanceEvaluationTest->supervisor_id != $currentUser->id) {
4398 efrain 223
                                            $data = [
224
                                                'success' => false,
225
                                                'data' => 'ERROR_BACKEND_COMPANY_RELATIONAL_RECORD_UNAUTHORIZE'
226
                                            ];
227
 
228
                                            return new JsonModel($data);
229
                                    }
230
 
231
                                } else {
232
                                    $data = [
233
                                        'success' => false,
234
                                        'data' => 'ERROR_BACKEND_COMPANY_RELATIONAL_RECORD_NOT_FOUND'
235
                                    ];
236
 
237
                                    return new JsonModel($data);
238
                                }
239
 
240
                                break;
241
 
242
                            default :
243
                                $data = [
244
                                    'success' => false,
245
                                    'data' => 'ERROR_BACKEND_COMPANY_RELATIONAL_TYPE_NOT_FOUND'
246
                                ];
247
 
248
                                return new JsonModel($data);
249
 
250
 
251
                        }
252
                    }
1 www 253
 
254
 
3639 efrain 255
                    $timestamp = date('Y-m-d\TH:i:s');
256
                    $rand = 1000 + rand(1, 8999);
257
 
258
 
259
 
1 www 260
                    $rand = 1000 + mt_rand(1, 999);
261
                    $timestamp = time();
262
                    $password = md5($currentUser->one_time_password . '-' . $rand . '-' . $timestamp . '-' . $salt);
263
 
3639 efrain 264
                    $params = [
265
                        'user_uuid' => $currentUser->uuid,
266
                        'password' => $password,
267
                        'rand' => $rand,
268
                        'time' => $timestamp,
269
                        'company_uuid' => $company->uuid
4398 efrain 270
 
3639 efrain 271
                    ];
1 www 272
 
4398 efrain 273
                    if($relational && $type) {
274
                        $params['relational'] = $relational;
275
                        $params['type'] = $type;
276
                    }
277
 
3639 efrain 278
                    $link_admin = 'https://'. $network->admin_hostname . '/signin-company' . '?' . http_build_query($params);
279
               }
1 www 280
 
281
 
282
 
283
            }
284
 
285
 
286
 
287
            $data = [
288
                'success' => true,
289
                'data' => $link_admin
290
            ];
291
 
292
            return new JsonModel($data);
293
 
294
        } else {
295
            $data = [
296
                'success' => false,
297
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
298
            ];
299
 
300
            return new JsonModel($data);
301
        }
302
 
303
        return new JsonModel($data);
304
 
305
 
306
    }
307
 
308
 
309
}