Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15459 | Rev 16248 | 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\Db\Adapter\AdapterInterface;
7
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
8
 
9
 
10
use Laminas\Mvc\Controller\AbstractActionController;
11
use Laminas\Log\LoggerInterface;
12
 
13
use Laminas\View\Model\ViewModel;
14
use Laminas\View\Model\JsonModel;
15
use LeadersLinked\Library\Functions;
16
use LeadersLinked\Mapper\UserMapper;
17
use LeadersLinked\Mapper\UserPasswordMapper;
18
 
19
use LeadersLinked\Model\User;
20
use LeadersLinked\Form\ChangePasswordForm;
21
use LeadersLinked\Model\CompanyUser;
22
use LeadersLinked\Mapper\CompanyUserMapper;
23
use LeadersLinked\Mapper\CompanyUserRoleMapper;
24
use LeadersLinked\Mapper\RoleMapper;
25
use LeadersLinked\Model\UserType;
26
use LeadersLinked\Model\UserPassword;
27
use PhpOffice\PhpSpreadsheet\IOFactory;
28
use LeadersLinked\Form\UserUploadForm;
29
use LeadersLinked\Mapper\CompanyServiceMapper;
30
use LeadersLinked\Model\CompanyService;
31
use LeadersLinked\Model\Role;
32
use LeadersLinked\Mapper\CompanyRoleMapper;
33
use LeadersLinked\Model\CompanyUserRole;
34
use LeadersLinked\Model\Notification;
35
use LeadersLinked\Model\EmailTemplate;
36
use LeadersLinked\Mapper\NotificationMapper;
37
use LeadersLinked\Mapper\UserNotificationSettingMapper;
38
use LeadersLinked\Mapper\EmailTemplateMapper;
39
use LeadersLinked\Library\QueueEmail;
15351 efrain 40
use LeadersLinked\Mapper\NetworkMapper;
41
use LeadersLinked\Model\Network;
15460 efrain 42
use LeadersLinked\Mapper\CompanyMapper;
43
use LeadersLinked\Mapper\ConnectionMapper;
44
use LeadersLinked\Model\Connection;
45
use LeadersLinked\Mapper\CompanyFollowerMapper;
46
use LeadersLinked\Model\CompanyFollower;
47
use LeadersLinked\Mapper\CountryMapper;
48
use LeadersLinked\Mapper\LocationMapper;
49
use LeadersLinked\Model\Location;
1 www 50
 
51
class UserController extends AbstractActionController
52
{
53
    /**
54
     *
55
     * @var AdapterInterface
56
     */
57
    private $adapter;
58
 
59
 
60
    /**
61
     *
62
     * @var AbstractAdapter
63
     */
64
    private $cache;
65
 
66
    /**
67
     *
68
     * @var  LoggerInterface
69
     */
70
    private $logger;
71
 
72
 
73
    /**
74
     *
75
     * @var array
76
     */
77
    private $config;
78
 
79
 
80
    /**
81
     *
82
     * @param AdapterInterface $adapter
83
     * @param AbstractAdapter $cache
84
     * @param LoggerInterface $logger
85
     * @param array $config
86
     */
87
    public function __construct($adapter, $cache , $logger, $config)
88
    {
89
        $this->adapter      = $adapter;
90
        $this->cache        = $cache;
91
        $this->logger       = $logger;
92
        $this->config       = $config;
93
 
94
    }
95
 
96
    public function indexAction()
97
    {
98
        $currentUserPlugin = $this->plugin('currentUserPlugin');
99
        $currentUser = $currentUserPlugin->getUser();
100
        $currentCompany = $currentUserPlugin->getCompany();
101
 
15351 efrain 102
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
103
        $network = $currentNetworkPlugin->getNetwork();
104
 
105
 
1 www 106
        $request = $this->getRequest();
107
 
108
        if($request->isGet())
109
 
110
 
111
            $headers  = $request->getHeaders();
112
 
113
            $isJson = false;
114
            if($headers->has('Accept')) {
115
                $accept = $headers->get('Accept');
116
 
117
                $prioritized = $accept->getPrioritized();
118
 
119
                foreach($prioritized as $key => $value) {
120
                    $raw = trim($value->getRaw());
121
 
122
                    if(!$isJson) {
123
                        $isJson = strpos($raw, 'json');
124
                    }
125
 
126
                }
127
            }
128
 
129
            if($isJson) {
130
            {
15351 efrain 131
 
1 www 132
                $search = $this->params()->fromQuery('search', []);
133
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
134
 
3343 efrain 135
                //$page               = intval($this->params()->fromQuery('start', 1), 10);
136
                //$records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
137
 
1 www 138
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
3343 efrain 139
                $page               = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
140
 
1 www 141
                $order =  $this->params()->fromQuery('order', []);
142
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
143
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
144
 
145
                $fields =  ['first_name', 'last_name', 'email'];
146
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
147
 
148
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
149
                    $order_direction = 'ASC';
150
                }
151
 
152
 
153
                if($currentCompany) {
154
                    $companyUserRoleMapper = CompanyUserRoleMapper::getInstance($this->adapter);
155
 
156
                    $roles = [];
157
                    $roleMapper = RoleMapper::getInstance($this->adapter);
158
                    $records = $roleMapper->fetchAll();
159
                    foreach ($records as $record)
160
                    {
161
                        $roles[ $record->id ] = $record->name;
162
                    }
163
 
164
 
165
                    //Usuarios cuando el nivel es empresa
166
 
167
                    $acl = $this->getEvent()->getViewModel()->getVariable('acl');
168
                    $allowUnblock = $acl->isAllowed($currentUser->usertype_id, 'users/unblock');
169
                    $allowChangePassword = $acl->isAllowed($currentUser->usertype_id, 'users/change-password');
170
 
171
                    $allowAccept    = $acl->isAllowed($currentUser->usertype_id, 'users/accept') ? 1 : 0;
172
                    $allowCancel    = $acl->isAllowed($currentUser->usertype_id, 'users/cancel') ? 1 : 0;
173
                    $allowReject    = $acl->isAllowed($currentUser->usertype_id, 'users/reject') ? 1 : 0;
174
                    $allowEdit     = $acl->isAllowed($currentUser->usertype_id, 'users/edit') ? 1 : 0;
175
 
176
 
177
                    $status = filter_var($this->params()->fromQuery('status'), FILTER_SANITIZE_STRING);
178
                    if(!in_array($status, [
179
                        CompanyUser::STATUS_ACCEPTED,
180
                        CompanyUser::STATUS_ADMIN_WILL_ADD,
181
                        CompanyUser::STATUS_CANCELLED,
182
                        CompanyUser::STATUS_PENDING,
183
                        CompanyUser::STATUS_REJECTED,
184
                        CompanyUser::STATUS_SENT,
185
                    ])) {
186
                        $status = '';
187
                    }
188
 
189
 
190
 
191
                    $userMapper = UserMapper::getInstance($this->adapter);
192
                    $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
193
 
194
                    $paginator = $userMapper->fetchAllDataTableByCompanyId($currentCompany->id, $status,  $search, $page, $records_x_page, $order_field, $order_direction);
195
 
196
                    $items = [];
197
                    $records = $paginator->getCurrentItems();
198
 
199
                    foreach($records as $record)
200
                    {
201
 
202
                        $actions = [];
15351 efrain 203
                        $actions['link_profile'] = 'https://'. $network->main_hostname . '/profile/view/' . $record['uuid'];
1 www 204
 
205
 
206
 
207
 
208
                        $details = [];
209
                        switch ($record['status'])
210
                        {
211
 
212
                            case CompanyUser::STATUS_PENDING :
213
                                $details['status']  = 'LABEL_PENDING';
214
                                $actions['link_accept'] = $allowAccept ? $this->url()->fromRoute('users/accept', ['id' => $record['uuid']]) : '';
215
                                $actions['link_reject'] = $allowReject ? $this->url()->fromRoute('users/reject', ['id' => $record['uuid']]) : '';
216
                                break;
217
 
218
                            case CompanyUser::STATUS_ACCEPTED :
219
                                $details['status']  = 'LABEL_ACCEPTED';
220
                                if($record['creator'] == CompanyUser::CREATOR_NO) {
221
                                    $actions['link_edit'] = $allowEdit ? $this->url()->fromRoute('users/edit', ['id' => $record['uuid']]) : '';
222
 
223
 
224
 
225
                                    $actions['link_cancel'] = $allowCancel ? $this->url()->fromRoute('users/cancel', ['id' => $record['uuid']]) : '';
226
                                }
227
 
228
                                break;
229
 
230
                            case CompanyUser::STATUS_ADMIN_WILL_ADD :
231
                                $details['status']  = 'LABEL_ADMIN_WILL_ADD';
232
                                $actions['link_cancel'] = $allowCancel ? $this->url()->fromRoute('users/cancel', ['id' => $record['uuid']]) : '';
233
                                $actions['link_edit'] = $allowEdit ? $this->url()->fromRoute('users/edit', ['id' => $record['uuid']]) : '';
234
 
235
                                break;
236
 
237
                            case CompanyUser::STATUS_SENT :
238
                                $details['status']  = 'LABEL_INVITED';
239
                                $actions['link_accept'] = $allowAccept ? $this->url()->fromRoute('users/accept', ['id' => $record['uuid']]) : '';
240
                                $actions['link_reject'] = $allowReject ? $this->url()->fromRoute('users/reject', ['id' => $record['uuid']]) : '';
241
                                break;
242
 
243
                            case CompanyUser::STATUS_REJECTED :
244
                                $actions['link_accept'] = $allowAccept ? $this->url()->fromRoute('users/accept', ['id' => $record['uuid']]) : '';
245
                                $details['status']  = 'LABEL_REJECTED';
246
                                break;
247
 
248
                            case CompanyUser::STATUS_CANCELLED :
249
                                $actions['link_accept'] = $allowAccept ? $this->url()->fromRoute('users/accept', ['id' => $record['uuid']]) : '';
250
                                $details['status']  = 'LABEL_CANCELLED';
251
                                break;
252
 
253
                            default :
254
                                $details['status']  = '';
255
                                break;
256
 
257
                        }
258
 
259
                        $totalOtherCompanies = $companyUserMapper->fetchCountOtherCompaniesByCompanyIdAndUserId($currentCompany->id, $record['id']);
260
                        if(!$totalOtherCompanies) {
261
 
262
                            $actions['link_change_password'] = $allowChangePassword ? $this->url()->fromRoute('users/change-password', ['id' => $record['uuid'] ]) : '';
263
                            if($record['blocked'] == User::BLOCKED_YES ) {
264
                                $actions['link_unblock'] = $allowUnblock ? $this->url()->fromRoute('users/unblock', ['id' => $record['uuid'] ]) : '';
265
                            }
266
                        }
267
 
268
                        if($record['blocked'] == User::BLOCKED_YES ) {
269
                            $details['blocked'] = 'LABEL_YES';
270
                        } else if($record['blocked'] == User::BLOCKED_NO ) {
271
                            $details['blocked'] = 'LABEL_NO';
272
                        }
273
                        if($record['email_verified'] == User::EMAIL_VERIFIED_YES ) {
274
                            $details['email_verified'] = 'LABEL_YES';
275
 
276
                        } else if($record['email_verified'] == User::EMAIL_VERIFIED_NO ) {
277
                            $details['email_verified'] = 'LABEL_NO';
278
                        }
279
                        $details['login_attempt'] = $record['login_attempt'];
280
 
281
 
282
                        $company_user_roles = $companyUserRoleMapper->fetchAllByCompanyIdAndUserId($currentCompany->id, $record['id']);
283
 
284
                        $details['roles'] = [];
285
 
286
                        if($record['creator'] == CompanyUser::CREATOR_YES) {
287
                            $details['roles'][] = 'LABEL_ALL_PERMITS';
288
                            $details['creator'] = 'LABEL_YES' ;
289
                        }  else {
290
                            $details['creator'] = 'LABEL_NO';
291
                            foreach($company_user_roles as $company_user_role)
292
                            {
293
                                $role = $roles[ $company_user_role->role_id ];
294
                                $details['roles'][] = $role;
295
 
296
                            }
297
                        }
298
 
299
 
300
                        $details['backend'] = $record['backend'] == CompanyUser::BACKEND_YES ? 'LABEL_YES' : 'LABEL_NO';
301
 
302
 
303
 
304
 
305
 
306
 
307
                        $item = [
308
                            'first_name' => $record['first_name'] ,
309
                            'last_name' => $record['last_name'],
310
                            'email' => $record['email'],
311
                            'details' => $details,
312
                            'actions' =>  $actions ,
313
                       ];
314
 
315
                        array_push($items, $item);
316
                    }
317
                } else {
318
                    //Usuario cuando el nivel es administrador
319
 
320
 
321
 
322
                    $acl = $this->getEvent()->getViewModel()->getVariable('acl');
323
                    $allowUnblock = $acl->isAllowed($currentUser->usertype_id, 'users/unblock');
324
                    $allowChangePassword = $acl->isAllowed($currentUser->usertype_id, 'users/change-password');
325
 
326
                    $userMapper = UserMapper::getInstance($this->adapter);
15455 efrain 327
                    $paginator = $userMapper->fetchAllDataTableByNetworkId($network->id, $search, $page, $records_x_page, $order_field, $order_direction);
1 www 328
 
329
                    $items = [];
330
                    $records = $paginator->getCurrentItems();
331
 
332
                    foreach($records as $record)
333
                    {
334
                        $actions = [];
15351 efrain 335
                        $actions['link_profile'] = 'https://'. $network->main_hostname . '/profile/view/' . $record->uuid;
1 www 336
 
337
 
338
                        $details = [] ;
339
                        if($record->status == User::STATUS_ACTIVE ) {
340
                            $details['status'] = 'LABEL_ACTIVE';
341
                        } else if($record->status == User::STATUS_INACTIVE ) {
342
                            $details['status'] = 'LABEL_INACTIVE';
343
                        }
344
                        if($record->blocked == User::BLOCKED_YES ) {
345
                            $details['blocked'] = 'LABEL_YES';
346
                        } else if($record->blocked == User::BLOCKED_NO ) {
347
                            $details['blocked'] = 'LABEL_NO';
348
                        }
349
                        if($record->email_verified == User::EMAIL_VERIFIED_YES ) {
350
                            $details['email_verified'] = 'LABEL_YES';
351
 
352
                        } else if($record->email_verified == User::EMAIL_VERIFIED_NO ) {
353
                            $details['email_verified'] = 'LABEL_NO';
354
                        }
355
                        $details['login_attempt'] = $record->login_attempt;
356
 
357
 
358
                        $actions['link_change_password'] = $allowChangePassword ? $this->url()->fromRoute('users/change-password', ['id' => $record->uuid ]) : '';
359
                        $actions['link_unblock'] = $allowUnblock && $record->blocked == User::BLOCKED_YES ? $this->url()->fromRoute('users/unblock', ['id' => $record->uuid ]) : '';
360
 
361
                        $item = [
362
 
363
 
364
                            'first_name' => $record->first_name,
365
                            'last_name' => $record->last_name,
366
                            'email' => $record->email,
367
                            'details' => $details,
368
                            'actions' => $actions
369
                         ];
370
 
371
                        array_push($items, $item);
372
                    }
373
                }
374
            }
375
 
376
            return new JsonModel([
377
                'success' => true,
378
                'data' => [
379
                    'items' => $items,
380
                    'total' => $paginator->getTotalItemCount(),
381
                ]
382
            ]);
383
 
384
 
385
 
386
        }
387
        else if($request->isGet()) {
388
            $this->layout()->setTemplate('layout/layout-backend');
389
            $viewModel = new ViewModel();
390
 
15460 efrain 391
 
392
            $formUploadUsers = new UserUploadForm();
1 www 393
            $formChangePassword = new ChangePasswordForm();
394
            $company = $currentUserPlugin->getCompany();
395
            if($company) {
396
                $viewModel->setTemplate('leaders-linked/users/company.phtml');
15460 efrain 397
 
1 www 398
            } else {
399
                $viewModel->setTemplate('leaders-linked/users/index.phtml');
400
            }
15460 efrain 401
            $viewModel->setVariables([
402
                'formUploadUsers' => $formUploadUsers,
403
                'formChangePassword' => $formChangePassword,
404
            ] );
1 www 405
            return $viewModel ;
406
 
407
        } else {
408
            return new JsonModel([
409
                'success' => false,
410
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
411
            ]);;
412
        }
413
    }
414
    /*
415
    public function addAction()
416
    {
417
        $currentUserPlugin = $this->plugin('currentUserPlugin');
418
        $currentUser = $currentUserPlugin->getUser();
419
 
420
        $request = $this->getRequest();
421
 
422
 
423
        if($request->isPost()) {
424
            $form = new  AddForm($this->adapter);
425
            $dataPost = $request->getPost()->toArray();
426
 
427
            $form->setData($dataPost);
428
 
429
            if($form->isValid()) {
430
                $dataPost = (array) $form->getData();
431
 
432
                $hydrator = new ObjectPropertyHydrator();
433
                $user = new User();
434
                $hydrator->hydrate($dataPost, $user);
435
 
436
 
437
                $userMapper = UserMapper::getInstance($this->adapter);
438
                $result = $userMapper->insert($user);
439
 
440
                if($result) {
441
                    $this->logger->info('Se agrego el usuario ' . $user->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
442
 
443
                    $data = [
444
                        'success'   => true,
445
                        'data'   => 'LABEL_RECORD_ADDED'
446
                    ];
447
                } else {
448
                    $data = [
449
                        'success'   => false,
450
                        'data'      => $userMapper->getError()
451
                    ];
452
 
453
                }
454
 
455
                return new JsonModel($data);
456
 
457
            } else {
458
                $messages = [];
459
                $form_messages = (array) $form->getMessages();
460
                foreach($form_messages  as $fieldname => $field_messages)
461
                {
462
 
463
                    $messages[$fieldname] = array_values($field_messages);
464
                }
465
 
466
                return new JsonModel([
467
                    'success'   => false,
468
                    'data'   => $messages
469
                ]);
470
            }
471
 
472
        } else {
473
            $data = [
474
                'success' => false,
475
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
476
            ];
477
 
478
            return new JsonModel($data);
479
        }
480
 
481
        return new JsonModel($data);
482
    }
483
 
484
    public function editAction()
485
    {
486
        $currentUserPlugin = $this->plugin('currentUserPlugin');
487
        $currentUser = $currentUserPlugin->getUser();
488
 
489
        $request = $this->getRequest();
490
        $id = $this->params()->fromRoute('id');
491
 
492
 
493
        if(!$id) {
494
            $data = [
495
                'success'   => false,
496
                'data'   => 'ERROR_INVALID_PARAMETER'
497
            ];
498
 
499
            return new JsonModel($data);
500
        }
501
 
502
        $userMapper = UserMapper::getInstance($this->adapter);
503
        $user = $userMapper->fetchOne($id);
504
        if(!$user) {
505
            $data = [
506
                'success'   => false,
507
                'data'   => 'ERROR_RECORD_NOT_FOUND'
508
            ];
509
 
510
            return new JsonModel($data);
511
        }
512
 
513
        if($request->isPost()) {
514
            $form = new  EditForm($this->adapter);
515
            $dataPost = $request->getPost()->toArray();
516
 
517
            $form->setData($dataPost);
518
 
519
            if($form->isValid()) {
520
                $dataPost = (array) $form->getData();
521
 
522
                $hydrator = new ObjectPropertyHydrator();
523
                $hydrator->hydrate($dataPost, $user);
524
                $result = $userMapper->update($user);
525
 
526
                if($result) {
527
                    $this->logger->info('Se actualizo el usuario ' . $user->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
528
 
529
                    $data = [
530
                        'success' => true,
531
                        'data' => 'LABEL_RECORD_UPDATED'
532
                    ];
533
                } else {
534
                    $data = [
535
                        'success'   => false,
536
                        'data'      => $userMapper->getError()
537
                    ];
538
                }
539
 
540
                return new JsonModel($data);
541
 
542
            } else {
543
                $messages = [];
544
                $form_messages = (array) $form->getMessages();
545
                foreach($form_messages  as $fieldname => $field_messages)
546
                {
547
                    $messages[$fieldname] = array_values($field_messages);
548
                }
549
 
550
                return new JsonModel([
551
                    'success'   => false,
552
                    'data'   => $messages
553
                ]);
554
            }
555
        } else if ($request->isGet()) {
556
            $hydrator = new ObjectPropertyHydrator();
557
 
558
            $data = [
559
                'success' => true,
560
                'data' => $hydrator->extract($user)
561
            ];
562
 
563
            return new JsonModel($data);
564
        } else {
565
            $data = [
566
                'success' => false,
567
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
568
            ];
569
 
570
            return new JsonModel($data);
571
        }
572
 
573
        return new JsonModel($data);
574
    }
575
 
576
    public function deleteAction()
577
    {
578
        $currentUserPlugin = $this->plugin('currentUserPlugin');
579
        $currentUser = $currentUserPlugin->getUser();
580
 
581
        $request = $this->getRequest();
582
        $id = $this->params()->fromRoute('id');
583
 
584
        if(!$id) {
585
            $data = [
586
                'success'   => false,
587
                'data'   => 'ERROR_INVALID_PARAMETER'
588
            ];
589
 
590
            return new JsonModel($data);
591
        }
592
 
593
 
594
        $userMapper = UserMapper::getInstance($this->adapter);
595
        $user = $userMapper->fetchOne($id);
596
        if(!$user) {
597
            $data = [
598
                'success'   => false,
599
                'data'   => 'ERROR_RECORD_NOT_FOUND'
600
            ];
601
 
602
            return new JsonModel($data);
603
        }
604
 
605
        if($request->isPost()) {
606
            $result = $userMapper->delete($user);
607
            if($result) {
608
                $this->logger->info('Se borro el usuario ' . $user->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
609
 
610
                $data = [
611
                    'success' => true,
612
                    'data' => 'LABEL_RECORD_DELETED'
613
                ];
614
            } else {
615
 
616
                $data = [
617
                    'success'   => false,
618
                    'data'      => $userMapper->getError()
619
                ];
620
 
621
                return new JsonModel($data);
622
            }
623
 
624
        } else {
625
            $data = [
626
                'success' => false,
627
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
628
            ];
629
 
630
            return new JsonModel($data);
631
        }
632
 
633
        return new JsonModel($data);
634
    }*/
635
 
636
    public function unblockAction()
637
    {
638
        $currentUserPlugin = $this->plugin('currentUserPlugin');
639
        $currentUser = $currentUserPlugin->getUser();
640
        $request = $this->getRequest();
641
 
642
 
643
        if($request->isPost()) {
644
 
645
            $uuid = $this->params()->fromRoute('id');
646
            if(!$uuid) {
647
                return new JsonModel([
648
                    'success'   => false,
649
                    'data'      => 'ERROR_INVALID_PARAMETER'
650
                ]);
651
            }
652
 
653
            $userMapper = UserMapper::getInstance($this->adapter);
654
            $user = $userMapper->fetchOneByUuid($uuid);
655
 
656
            if(!$user) {
657
                return new JsonModel([
658
                    'success'   => false,
659
                    'data'      => 'ERROR_USER_NOT_FOUND'
660
                ]);
661
            }
662
 
663
            if($user->blocked == User::BLOCKED_NO) {
664
                return new JsonModel([
665
                    'success'   => false,
666
                    'data'      => 'ERROR_USER_IS_NOT_BLOCKED'
667
                ]);
668
            }
669
 
670
 
671
 
672
            $result = $userMapper->unblock($user);
673
            if($result) {
674
                $this->logger->info('El usuario : ' . $user->email . ' ha sido desbloqueado ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
675
 
676
                return new JsonModel([
677
                    'success'   => true,
678
                    'data'      => 'LABEL_USER_HAS_BEEN_UNBLOCKED',
679
                ]);
680
            }  else {
681
 
682
                return new JsonModel([
683
                    'success'   => false,
684
                    'data'      => $userMapper->getError()
685
                ]);
686
            }
687
 
688
 
689
        }
690
 
691
 
692
 
693
        return new JsonModel([
694
            'success' => false,
695
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
696
        ]);
697
    }
698
 
129 efrain 699
    public function emailVerifyAction()
700
    {
701
        $currentUserPlugin = $this->plugin('currentUserPlugin');
702
        $currentUser = $currentUserPlugin->getUser();
703
        $request = $this->getRequest();
704
 
705
 
706
        if($request->isPost()) {
707
 
708
            $uuid = $this->params()->fromRoute('id');
709
            if(!$uuid) {
710
                return new JsonModel([
711
                    'success'   => false,
712
                    'data'      => 'ERROR_INVALID_PARAMETER'
713
                ]);
714
            }
715
 
716
            $userMapper = UserMapper::getInstance($this->adapter);
717
            $user = $userMapper->fetchOneByUuid($uuid);
718
 
719
            if(!$user) {
720
                return new JsonModel([
721
                    'success'   => false,
722
                    'data'      => 'ERROR_USER_NOT_FOUND'
723
                ]);
724
            }
725
 
726
            if($user->blocked == User::BLOCKED_NO) {
727
                return new JsonModel([
728
                    'success'   => false,
729
                    'data'      => 'ERROR_USER_IS_NOT_PENDING_FOR_EMAIL_VERIFY'
730
                ]);
731
            }
732
 
733
 
734
 
735
            $result = $userMapper->emailVerifyAndActive($user);
736
            if($result) {
737
                $this->logger->info('El usuario : ' . $user->email . ' ha sido desbloqueado ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
738
 
739
                return new JsonModel([
740
                    'success'   => true,
741
                    'data'      => 'LABEL_USER_HAS_BEEN_UNBLOCKED',
742
                ]);
743
            }  else {
744
 
745
                return new JsonModel([
746
                    'success'   => false,
747
                    'data'      => $userMapper->getError()
748
                ]);
749
            }
750
 
751
 
752
        }
753
 
754
 
755
 
756
        return new JsonModel([
757
            'success' => false,
758
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
759
        ]);
760
    }
761
 
1 www 762
    public function changePasswordAction()
763
    {
764
        $currentUserPlugin = $this->plugin('currentUserPlugin');
765
        $currentUser = $currentUserPlugin->getUser();
766
 
767
        $request = $this->getRequest();
768
 
769
        if($request->isGet()) {
770
            $uuid = $this->params()->fromRoute('id');
771
            if(!$uuid) {
772
                return new JsonModel([
773
                    'success'   => false,
774
                    'data'      => 'ERROR_INVALID_PARAMETER'
775
                ]);
776
            }
777
 
778
            $userMapper = UserMapper::getInstance($this->adapter);
779
            $user = $userMapper->fetchOneByUuid($uuid);
780
 
781
 
782
 
783
            if($user) {
784
                return new JsonModel([
785
                    'success'   => true,
786
                    'data'      => [
787
                        'first_name' => $user->first_name,
788
                        'last_name' => $user->last_name,
789
                        'email' => $user->email,
790
                    ]
791
                ]);
792
            } else {
793
                return new JsonModel([
794
                    'success'   => false,
795
                    'data'      => 'ERROR_USER_NOT_FOUND'
796
                ]);
797
            }
798
 
799
        }
800
 
801
        if($request->isPost()) {
802
 
803
            $uuid = $this->params()->fromRoute('id');
804
            if(!$uuid) {
805
                return new JsonModel([
806
                    'success'   => false,
807
                    'data'      => 'ERROR_INVALID_PARAMETER'
808
                ]);
809
            }
810
 
811
            $userMapper = UserMapper::getInstance($this->adapter);
812
            $user = $userMapper->fetchOneByUuid($uuid);
813
 
814
            if(!$user) {
815
                return new JsonModel([
816
                    'success'   => false,
817
                    'data'      => 'ERROR_USER_NOT_FOUND'
818
                ]);
819
            }
820
 
821
 
822
            $dataPost = $request->getPost()->toArray();
823
            $form = new ChangePasswordForm();
824
            $form->setData($dataPost);
825
 
826
            if($form->isValid()) {
827
 
828
 
829
 
830
                $data = (array) $form->getData();
831
                $password = $data['password'];
832
 
833
 
834
 
835
                $userPasswordMapper = UserPasswordMapper::getInstance($this->adapter);
836
                $userPasswords = $userPasswordMapper->fetchAllByUserId($user->id);
837
 
838
                $oldPassword = false;
839
                foreach($userPasswords as $userPassword)
840
                {
841
                    if(password_verify($password, $userPassword->password) || (md5($password) == $userPassword->password))
842
                    {
843
                        $oldPassword = true;
844
                        break;
845
                    }
846
                }
847
 
848
                if($oldPassword) {
849
                    $this->logger->err('Cambio de contraseña del usuario - error contraseña ya utilizada anteriormente', ['user_id' =>  $currentUser->id, 'ip' => Functions::getUserIP()]);
850
 
851
                    return new JsonModel([
852
                        'success'   => false,
853
                        'data'      => 'ERROR_PASSWORD_HAS_ALREADY_BEEN_USED'
854
 
855
                    ]);
856
                } else {
857
                    $password_hash = password_hash($password, PASSWORD_DEFAULT);
858
 
859
 
860
                    $result = $userMapper->updatePassword($user, $password_hash);
861
                    if($result) {
862
                        $this->logger->info('Cambio de contraseña del usuario realizado', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
863
 
864
 
865
                        return new JsonModel([
866
                            'success'   => true,
867
                            'data'      => 'LABEL_YOUR_PASSWORD_HAS_BEEN_UPDATED'
868
 
869
                        ]);
870
                    } else {
871
                        $this->logger->err('Cambio de contraseña del usuario - error desconocido', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
872
 
873
                        return new JsonModel([
874
                            'success'   => true,
875
                            'data'      => 'ERROR_THERE_WAS_AN_ERROR'
876
 
877
                        ]);
878
                    }
879
                }
880
 
881
            } else {
882
                $messages = [];
883
 
884
                $form_messages = (array) $form->getMessages();
885
                foreach($form_messages  as $fieldname => $field_messages)
886
                {
887
                    $messages[$fieldname] = array_values($field_messages);
888
                }
889
 
890
                return new JsonModel([
891
                    'success'   => false,
892
                    'data'   => $messages
893
                ]);
894
            }
895
 
896
        }
897
 
898
 
899
 
900
        return new JsonModel([
901
            'success' => false,
902
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
903
        ]);
904
    }
905
 
906
    public function cancelAction()
907
    {
908
        $currentUserPlugin = $this->plugin('currentUserPlugin');
909
        $currentUser = $currentUserPlugin->getUser();
910
 
911
        $currentCompany = $currentUserPlugin->getCompany();
912
 
913
        $request = $this->getRequest();
914
 
915
 
916
        if($request->isPost()) {
917
 
918
            $uuid = $this->params()->fromRoute('id');
919
            if(!$uuid) {
920
                return new JsonModel([
921
                    'success'   => false,
922
                    'data'      => 'ERROR_INVALID_PARAMETER'
923
                ]);
924
            }
925
 
926
            $userMapper = UserMapper::getInstance($this->adapter);
927
            $user = $userMapper->fetchOneByUuid($uuid);
928
 
929
            if(!$user) {
930
                return new JsonModel([
931
                    'success'   => false,
932
                    'data'      => 'ERROR_USER_COMPANY_NOT_FOUND'
933
                ]);
934
            }
935
 
936
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
937
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
938
 
939
            if(!$companyUser) {
940
                return new JsonModel([
941
                    'success'   => false,
942
                    'data'      => 'ERROR_USER_NOT_FOUND'
943
                ]);
944
            }
945
 
946
 
947
            if(!in_array($companyUser->status, [  CompanyUser::STATUS_ADMIN_WILL_ADD, CompanyUser::STATUS_ACCEPTED])) {
948
                return new JsonModel([
949
                    'success'   => false,
950
                    'data'      => 'ERROR_USER_COMPANY_WRONG_STATUS'
951
                ]);
952
            }
953
 
954
            $companyUser->status = CompanyUser::STATUS_CANCELLED;
955
            $result = $companyUserMapper->update($companyUser);
956
            if($result) {
957
                $this->logger->info('La relación del usuario : ' . $user->email . ' con la empresa : ' . $currentCompany->name  . ' ha sido cancelada ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
958
 
959
                return new JsonModel([
960
                    'success'   => true,
961
                    'data'      => 'LABEL_USER_COMPANY_HAS_BEEN_CANCELLED',
962
                ]);
963
            }  else {
964
 
965
                return new JsonModel([
966
                    'success'   => false,
967
                    'data'      => $userMapper->getError()
968
                ]);
969
            }
970
 
971
 
972
        }
973
 
974
 
975
 
976
        return new JsonModel([
977
            'success' => false,
978
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
979
        ]);
980
    }
981
 
982
    public function acceptAction()
983
    {
984
        $currentUserPlugin = $this->plugin('currentUserPlugin');
985
        $currentUser = $currentUserPlugin->getUser();
986
 
987
        $currentCompany = $currentUserPlugin->getCompany();
988
 
989
        $request = $this->getRequest();
990
 
991
 
992
        if($request->isPost()) {
993
 
994
            $uuid = $this->params()->fromRoute('id');
995
            if(!$uuid) {
996
                return new JsonModel([
997
                    'success'   => false,
998
                    'data'      => 'ERROR_INVALID_PARAMETER'
999
                ]);
1000
            }
1001
 
1002
            $userMapper = UserMapper::getInstance($this->adapter);
1003
            $user = $userMapper->fetchOneByUuid($uuid);
1004
 
1005
            if(!$user) {
1006
                return new JsonModel([
1007
                    'success'   => false,
1008
                    'data'      => 'ERROR_USER_NOT_FOUND'
1009
                ]);
1010
            }
1011
 
1012
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1013
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
1014
 
1015
            if(!$companyUser) {
1016
                return new JsonModel([
1017
                    'success'   => false,
1018
                    'data'      => 'ERROR_USER_NOT_FOUND'
1019
                ]);
1020
            }
1021
 
1022
            if($companyUser->status != CompanyUser::STATUS_PENDING
1023
                && $companyUser->status != CompanyUser::STATUS_SENT
1024
                && $companyUser->status != CompanyUser::STATUS_CANCELLED
1025
                && $companyUser->status != CompanyUser::STATUS_REJECTED) {
1026
                return new JsonModel([
1027
                    'success'   => false,
1028
                    'data'      => 'ERROR_USER_COMPANY_WRONG_STATUS'
1029
                ]);
1030
            }
1031
 
1032
            $companyUser->status = CompanyUser::STATUS_ACCEPTED;
1033
            $result = $companyUserMapper->update($companyUser);
1034
            if($result) {
1035
                $this->logger->info('La relación del usuario : ' . $user->email . ' con la empresa : ' . $currentCompany->name  . ' ha sido aceptada ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1036
 
1037
                return new JsonModel([
1038
                    'success'   => true,
1039
                    'data'      => 'LABEL_USER_COMPANY_HAS_BEEN_ACCEPTED'
1040
                ]);
1041
            }  else {
1042
 
1043
                return new JsonModel([
1044
                    'success'   => false,
1045
                    'data'      => $userMapper->getError()
1046
                ]);
1047
            }
1048
 
1049
 
1050
        }
1051
 
1052
 
1053
 
1054
        return new JsonModel([
1055
            'success' => false,
1056
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1057
        ]);
1058
    }
1059
 
1060
    public function rejectAction()
1061
    {
1062
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1063
        $currentUser = $currentUserPlugin->getUser();
1064
 
1065
        $currentCompany = $currentUserPlugin->getCompany();
1066
 
1067
        $request = $this->getRequest();
1068
 
1069
 
1070
        if($request->isPost()) {
1071
 
1072
            $uuid = $this->params()->fromRoute('id');
1073
            if(!$uuid) {
1074
                return new JsonModel([
1075
                    'success'   => false,
1076
                    'data'      => 'ERROR_INVALID_PARAMETER'
1077
                ]);
1078
            }
1079
 
1080
            $userMapper = UserMapper::getInstance($this->adapter);
1081
            $user = $userMapper->fetchOneByUuid($uuid);
1082
 
1083
            if(!$user) {
1084
                return new JsonModel([
1085
                    'success'   => false,
1086
                    'data'      => 'ERROR_USER_NOT_FOUND'
1087
                ]);
1088
            }
1089
 
1090
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1091
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
1092
 
1093
            if(!$companyUser) {
1094
                return new JsonModel([
1095
                    'success'   => false,
1096
                    'data'      => 'ERROR_USER_COMPANY_NOT_FOUND'
1097
                ]);
1098
            }
1099
 
1100
            if($companyUser->status != CompanyUser::STATUS_PENDING
1101
                && $companyUser->status != CompanyUser::STATUS_SENT) {
1102
                return new JsonModel([
1103
                    'success'   => false,
1104
                    'data'      => 'ERROR_USER_COMPANY_WRONG_STATUS'
1105
                ]);
1106
            }
1107
 
1108
            $companyUser->status = CompanyUser::STATUS_REJECTED;
1109
            $result = $companyUserMapper->update($companyUser);
1110
            if($result) {
1111
                $this->logger->info('La relación del usuario : ' . $user->email . ' con la empresa : ' . $currentCompany->name  . ' ha sido rechazada ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1112
 
1113
                return new JsonModel([
1114
                    'success'   => true,
1115
                    'data'      => 'LABEL_USER_COMPANY_HAS_BEEN_REJECTED',
1116
                ]);
1117
            }  else {
1118
 
1119
                return new JsonModel([
1120
                    'success'   => false,
1121
                    'data'      => $userMapper->getError()
1122
                ]);
1123
            }
1124
 
1125
 
1126
        }
1127
 
1128
 
1129
 
1130
        return new JsonModel([
1131
            'success' => false,
1132
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1133
        ]);
1134
    }
1135
 
1136
    public function inviteAction()
1137
    {
1138
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1139
        $currentUser = $currentUserPlugin->getUser();
1140
 
15351 efrain 1141
 
1142
 
1 www 1143
        $currentCompany = $currentUserPlugin->getCompany();
1144
 
1145
        $request = $this->getRequest();
1146
 
1147
        if($request->isGet()) {
1148
 
1149
            $search = trim(filter_var( $this->params()->fromQuery('search'), FILTER_SANITIZE_STRING)) ;
1150
            if(strlen($search) >= 3) {
1151
 
1152
                $userMapper = UserMapper::getInstance($this->adapter);
1153
                $records  = $userMapper->fetchAllSuggestForInvitationByCompanyId($currentCompany->id, $search);
1154
 
1155
                $users = [];
1156
                foreach($records as $record)
1157
                {
1158
                    array_push($users, [
1159
                        'value' => $record->uuid,
1160
                        'text' => trim($record->first_name . ' ' . $record->last_name) . ' (' . $record->email . ')'
1161
 
1162
                    ]);
1163
                }
1164
 
1165
                return new JsonModel([
1166
                    'success' => true,
1167
                    'data' => $users
1168
                ]);
1169
 
1170
 
1171
 
1172
 
1173
            } else {
1174
                return new JsonModel([
1175
                    'success' => true,
1176
                    'data' => [
1177
 
1178
                    ]
1179
                ]);
1180
            }
1181
 
1182
 
1183
 
1184
 
1185
 
1186
 
1187
        } else if($request->isPost()) {
1188
 
1189
            $uuid = $this->params()->fromPost('id');
1190
            if(!$uuid) {
1191
                return new JsonModel([
1192
                    'success'   => false,
1193
                    'data'      => 'ERROR_INVALID_PARAMETER'
1194
                ]);
1195
            }
1196
 
1197
            $userMapper = UserMapper::getInstance($this->adapter);
1198
            $user = $userMapper->fetchOneByUuid($uuid);
1199
 
1200
            if(!$user) {
1201
                return new JsonModel([
1202
                    'success'   => false,
1203
                    'data'      => 'ERROR_USER_NOT_FOUND'
1204
                ]);
1205
            }
1206
 
1207
            if($user->status != User::STATUS_ACTIVE) {
1208
                return new JsonModel([
1209
                    'success'   => false,
1210
                    'data'      => 'ERROR_USER_IS_INACTIVE'
1211
                ]);
1212
            }
1213
 
1214
 
1215
 
1216
 
1217
 
1218
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1219
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
1220
 
1221
            if($companyUser && $companyUser->status == CompanyUser::STATUS_ACCEPTED) {
1222
                return new JsonModel([
1223
                    'success'   => false,
1224
                    'data'      => 'ERROR_USER_COMPANY_FOUND'
1225
                ]);
1226
            }
15351 efrain 1227
            $networkMapper = NetworkMapper::getInstance($this->adapter);
1228
            $network = $networkMapper->fetchOne($currentUser->network_id);
1 www 1229
 
1230
 
1231
            if($companyUser) {
1232
 
15351 efrain 1233
 
1234
                if($network->default == Network::DEFAULT_YES) {
1235
                    $companyUser->status = CompanyUser::STATUS_ADMIN_WILL_ADD;
1236
                } else {
1237
                    $companyUser->status = CompanyUser::STATUS_ACCEPTED;
1238
                }
1 www 1239
                $result = $companyUserMapper->update($companyUser);
1240
 
1241
            } else {
15351 efrain 1242
 
1243
 
1 www 1244
                $companyUser = new CompanyUser();
1245
                $companyUser->company_id = $currentCompany->id;
1246
                $companyUser->backend = CompanyUser::BACKEND_NO;
1247
                $companyUser->creator = CompanyUser::CREATOR_NO;
1248
                $companyUser->owner = CompanyUser::OWNER_NO;
15351 efrain 1249
 
1250
                if($network->default == Network::DEFAULT_YES) {
1251
                    $companyUser->status = CompanyUser::STATUS_ADMIN_WILL_ADD;
1252
                } else {
1253
                    $companyUser->status = CompanyUser::STATUS_ACCEPTED;
1254
                }
1255
 
1256
 
1 www 1257
                $companyUser->status = CompanyUser::STATUS_ADMIN_WILL_ADD;
1258
                $companyUser->user_id = $user->id;
1259
 
1260
 
1261
 
1262
                $result = $companyUserMapper->insert($companyUser);
1263
            }
1264
 
1265
 
1266
 
1267
            if($result) {
1268
 
1269
 
1270
                $notification = new Notification();
1271
                $notification->type     = Notification::TYPE_RECEIVE_INVITATION_COMPANY;
1272
                $notification->read     = Notification::NO;
1273
                $notification->user_id  = $user->id;
1274
                $notification->company_id = $currentCompany->id;
1275
                $notification->message  = 'LABEL_NOTIFICATION_RECEIVE_INVITATION_COMPANY';
1276
                $notification->url      = 'company/view/' . $currentCompany->uuid;
1277
 
1278
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
1279
                $notificationMapper->insert($notification);
1280
 
1281
                $userNotificationMapper = UserNotificationSettingMapper::getInstance($this->adapter);
1282
                $userNotification = $userNotificationMapper->fetchOne($user->id);
1283
 
1284
                if($userNotification && $userNotification->receive_invitation_company)
1285
                {
1286
                    $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
15351 efrain 1287
                    $emailTemplate = $emailTemplateMapper->fetchOneByCodeAndNetworkId(EmailTemplate::CODE_RECEIVE_INVITATION_COMPANY, $currentUser->network_id);
1 www 1288
 
15351 efrain 1289
                    if($emailTemplate)
1290
                    {
1291
                        $company_profile_url = 'https://'. $network->main_hostname . '/company/view/' . $currentCompany->uuid;
1 www 1292
 
1293
 
1294
                        $arrayCont = [
1295
                            'firstname'             => $currentUser->first_name,
1296
                            'lastname'              => $currentUser->last_name,
1297
                            'other_user_firstname'  => $user->first_name,
1298
                            'other_user_lastname'   => $user->last_name,
1299
                            'company_name'          => $currentCompany->name,
1300
                            'group_name'            => '',
1301
                            'content'               => '',
1302
                            'code'                  => '',
1303
                            'link'                  => $company_profile_url,
1304
                        ];
1305
 
1306
                        $email = new QueueEmail($this->adapter);
1307
                        $email->processEmailTemplate($emailTemplate, $arrayCont, $user->email, trim($user->first_name . ' ' . $user->last_name));
1308
                    }
1309
                }
1310
 
1311
 
1312
                $this->logger->info('La empresa : ' . $currentCompany->name . ' envio al usuario : ' . $user->email . ' una invitación ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1313
 
15351 efrain 1314
                if($network->default == Network::DEFAULT_YES) {
1315
                    return new JsonModel([
1316
                        'success'   => true,
1317
                        'data'      => 'LABEL_USER_COMPANY_HAS_BEEN_CREATED',
1318
                    ]);
1319
                } else {
1320
                    return new JsonModel([
1321
                        'success'   => true,
1322
                        'data'      => 'LABEL_USER_COMPANY_HAS_BEEN_INVITED_SENT',
1323
                    ]);
1324
                }
1325
 
1326
 
1 www 1327
            }  else {
1328
 
1329
                return new JsonModel([
1330
                    'success'   => false,
1331
                    'data'      => $userMapper->getError()
1332
                ]);
1333
            }
1334
 
1335
 
1336
        }
1337
 
1338
 
1339
 
1340
        return new JsonModel([
1341
            'success' => false,
1342
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1343
        ]);
1344
    }
1345
 
1346
 
1347
    public function deleteAction()
1348
    {
1349
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1350
        $currentUser = $currentUserPlugin->getUser();
1351
 
1352
        $currentCompany = $currentUserPlugin->getCompany();
1353
 
1354
        $request = $this->getRequest();
1355
 
1356
 
1357
        if($request->isPost()) {
1358
 
1359
            $uuid = $this->params()->fromRoute('id');
1360
            if(!$uuid) {
1361
                return new JsonModel([
1362
                    'success'   => false,
1363
                    'data'      => 'ERROR_INVALID_PARAMETER'
1364
                ]);
1365
            }
1366
 
1367
            $userMapper = UserMapper::getInstance($this->adapter);
1368
            $user = $userMapper->fetchOneByUuid($uuid);
1369
 
1370
            if(!$user) {
1371
                return new JsonModel([
1372
                    'success'   => false,
1373
                    'data'      => 'ERROR_USER_NOT_FOUND'
1374
 
1375
                ]);
1376
            }
1377
 
1378
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1379
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
1380
 
1381
            if($companyUser) {
1382
                return new JsonModel([
1383
                    'success'   => false,
1384
                    'data'      => 'ERROR_USER_COMPANY_NOT_FOUND'
1385
                ]);
1386
            }
1387
 
1388
 
1389
            if(!$currentCompany->internal) {
1390
                return new JsonModel([
1391
                    'success'   => false,
1392
                    'data'      => 'ERROR_INTERNAL_COMPANY_ONLY'
1393
                ]);
1394
            }
1395
 
1396
 
1397
 
1398
            $result = $companyUserMapper->delete($companyUser->id);
1399
            if($result) {
1400
                $this->logger->info('La relación del usuario : ' . $user->email . ' con la empresa : ' . $currentCompany->name  . ' ha sido eliminada ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1401
 
1402
                return new JsonModel([
1403
                    'success'   => true,
1404
                    'data'      => 'LABEL_USER_COMPANY_HAS_BEEN_DELETED',
1405
                ]);
1406
            }  else {
1407
 
1408
                return new JsonModel([
1409
                    'success'   => false,
1410
                    'data'      => $userMapper->getError()
1411
                ]);
1412
            }
1413
 
1414
 
1415
        }
1416
 
1417
 
1418
 
1419
        return new JsonModel([
1420
            'success' => false,
1421
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1422
        ]);
1423
    }
1424
 
1425
 
1426
    public function uploadAction()
1427
    {
1428
        $request = $this->getRequest();
1429
 
15459 efrain 1430
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
1431
        $currentNetwork    = $currentNetworkPlugin->getNetwork();
1432
 
1 www 1433
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1434
        $currentUser    = $currentUserPlugin->getUser();
1435
        $currentCompany = $currentUserPlugin->getCompany();
1436
 
1437
        $request    = $this->getRequest();
1438
 
1439
        if($request->isPost()) {
1440
 
1441
            $step = filter_var( $this->params()->fromPost('step'), FILTER_SANITIZE_STRING);
1442
            if($step == 'validation') {
1443
                $userMapper = UserMapper::getInstance($this->adapter);
1444
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1445
 
1446
                $form = new  UserUploadForm();
1447
                $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1448
 
1449
                $form->setData($dataPost);
1450
 
1451
                if($form->isValid()) {
1452
 
1453
                    $file = $_FILES['file'];
1454
                    $tmp_filename = $file['tmp_name'];
1455
                    $final_filename =  'data/' . $file['name'];
1456
 
1457
                    if(!move_uploaded_file($tmp_filename, $final_filename)) {
1458
                        return new JsonModel([
1459
                            'success' => false,
1460
                            'data' => 'ERROR_UPLOAD_FILE'
1461
                        ]);
1462
                    }
1463
 
1464
 
1465
                    $users = [];
1466
 
1467
 
1468
                    $spreadsheet = IOFactory::load($final_filename);
1469
                    $records = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
1470
 
1471
                    $emails = [];
1472
 
1473
                    foreach($records as $record)
1474
                    {
1475
                        //A = Nombre 	B = Apellidos	C = Email 	D = contraseña
1476
 
15460 efrain 1477
 
1 www 1478
                        $first_name = trim(filter_var($record['A'], FILTER_SANITIZE_STRING));
1479
                        $last_name = trim(filter_var($record['B'], FILTER_SANITIZE_STRING));
15460 efrain 1480
                        $email = trim(filter_var($record['C'], FILTER_SANITIZE_EMAIL));
1481
 
1 www 1482
                        $password = trim(filter_var($record['D'], FILTER_SANITIZE_STRING));
15460 efrain 1483
                        $isAdult = strtolower(trim(filter_var($record['E'], FILTER_SANITIZE_STRING)));
1484
                        $country = strtolower(trim(filter_var($record['F'], FILTER_SANITIZE_STRING)));
1485
 
1 www 1486
 
1487
                        if(empty($first_name) || empty($last_name) || !filter_var($email, FILTER_VALIDATE_EMAIL) ||  empty($password)) {
1488
                            continue;
1489
                        }
1490
 
1491
                        if(!in_array($email, $emails)) {
1492
 
1493
                            $user = $userMapper->fetchOneByEmail($email);
1494
 
1495
                            array_push($emails, $email);
1496
                            array_push($users, [
1497
                                'first_name' => $first_name,
1498
                                'last_name' => $last_name,
1499
                                'password'  => $password,
1500
                                'email' => $email,
15460 efrain 1501
                                'is_adult' => $isAdult,
1502
                                'country' => $country,
1503
 
1 www 1504
                            ]);
1505
                        }
1506
 
1507
 
1508
 
1509
 
1510
 
1511
                    }
1512
 
1513
                    $key = md5($currentUser->id . '-' . microtime(true));
1514
                    $this->cache->setItem($key, serialize($users));
1515
 
1516
                    return new JsonModel([
1517
                        'success' => true,
1518
                        'data' => [
1519
                            'key' => $key,
1520
                            'items' => $users,
1521
                        ]
1522
                    ]);
1523
 
1524
 
1525
 
1526
                } else {
1527
                    $messages = [];
1528
                    $form_messages = (array) $form->getMessages();
1529
                    foreach($form_messages  as $fieldname => $field_messages)
1530
                    {
1531
 
1532
                        $messages[$fieldname] = array_values($field_messages);
1533
                    }
1534
 
1535
                    return new JsonModel([
1536
                        'success'   => false,
1537
                        'data'   => $messages
1538
                    ]);
1539
                }
1540
            } else if($step == 'process') {
1541
 
1542
                $key = filter_var( $this->params()->fromPost('key'), FILTER_SANITIZE_STRING);
1543
                if(!$key) {
1544
                    return new JsonModel([
1545
                        'success' => false,
1546
                        'data' => 'ERROR_CACHE_KEY_EMPTY'
1547
                    ]);
1548
                }
1549
 
1550
                $value = $this->cache->getItem($key);
1551
                if(!$value) {
1552
 
1553
                    return new JsonModel([
1554
                        'success' => false,
1555
                        'data' => 'ERROR_CACHE_NOT_FOUND'
1556
                    ]);
1557
                }
1558
 
1559
                $records = unserialize($value);
1560
                if(!$records) {
1561
                    return new JsonModel([
1562
                        'success' => false,
1563
                        'data' => 'ERROR_CACHE_INVALID'
1564
                    ]);
1565
                }
1566
 
15460 efrain 1567
                $locationMapper = LocationMapper::getInstance($this->adapter);
1568
                $countryMapper = CountryMapper::getInstance($this->adapter);
1569
 
1570
 
1571
                $networkMapper = NetworkMapper::getInstance($this->adapter);
1572
                $networkDefault = $networkMapper->fetchOneByDefault();
1573
 
1 www 1574
                $userMapper = UserMapper::getInstance($this->adapter);
1575
                $userPasswordMapper = UserPasswordMapper::getInstance($this->adapter);
15460 efrain 1576
 
1 www 1577
 
15460 efrain 1578
                $user_ids_in_default_network = [];
1579
 
1580
 
1 www 1581
                $users_created = 0;
1582
                $user_ids = [];
1583
                foreach($records as $record)
1584
                {
1585
                    $first_name = $record['first_name'];
1586
                    $last_name = $record['last_name'];
1587
                    $password = $record['password'];
1588
                    $email = $record['email'];
1589
 
1590
 
1591
                    $user = $userMapper->fetchOneByEmail($email);
1592
                    if(!$user) {
1593
                        $password_hash = password_hash($password, PASSWORD_DEFAULT);
1594
 
1595
                        $user = new User();
15459 efrain 1596
                        $user->network_id = $currentNetwork->id;
1 www 1597
                        $user->blocked = User::BLOCKED_NO;
1598
                        $user->email_verified = User::EMAIL_VERIFIED_YES;
1599
                        $user->email = $email;
1600
                        $user->first_name = $first_name;
1601
                        $user->last_name = $last_name;
1602
                        $user->password = $password_hash;
1603
                        $user->login_attempt = 0;
1604
                        $user->usertype_id = UserType::USER;
1605
                        $user->status = User::STATUS_ACTIVE;
15460 efrain 1606
                        $user->is_adult = $record['is_adult'] == 'y' ? User::IS_ADULT_YES : User::IS_ADULT_NO;
1 www 1607
 
1608
                        $result = $userMapper->insert($user);
1609
                        if($result) {
15460 efrain 1610
                            $users_created++;
1611
 
1612
                            $country = $countryMapper->fetchOneByCodeOrCountry($record['country']);
1613
                            if($country) {
1614
 
1615
                                $location = new Location();
1616
                                $location->formatted_address = $country->country;
1617
                                $location->country = $country->country;
1618
                                if($locationMapper->insert($location)) {
1619
 
1620
                                    $user->location_id = $location->id;
1621
                                    $userMapper->updateLocation($user);
1622
                                }
1623
 
1624
                            }
1625
 
1 www 1626
                            $userPassword = new UserPassword();
1627
                            $userPassword->user_id = $user->id;
1628
                            $userPassword->password = $password_hash;
1629
                            $userPasswordMapper->insert($userPassword);
15460 efrain 1630
 
1631
 
1632
                            if($currentNetwork->default == Network::DEFAULT_YES) {
1633
                                array_push($user_ids_in_default_network, $user->id);
1634
                            } else {
1635
 
1636
 
1637
 
1638
                                if($user->is_adult == User::IS_ADULT_YES) {
1639
 
1640
                                    $userInDefaultNetwork = $userMapper->fetchOneByEmailAndNetworkId($user->email, $networkDefault->id);
1641
                                    if($userInDefaultNetwork) {
1642
 
1643
                                        array_push($user_ids_in_default_network, $userInDefaultNetwork->id);
1644
 
1645
                                        if($userInDefaultNetwork->email_verified == User::EMAIL_VERIFIED_NO || $userInDefaultNetwork->status != User::STATUS_ACTIVE) {
1646
                                            $userInDefaultNetwork->email_verified = User::EMAIL_VERIFIED_YES;
1647
                                            $userInDefaultNetwork->status != User::STATUS_ACTIVE;
1648
 
1649
                                            if(!$userMapper->update($userInDefaultNetwork)) {
1650
                                                continue;
1651
                                            }
1652
                                        }
1653
 
1654
 
1655
                                    } else {
1656
                                        $userInDefaultNetwork = new User();
1657
                                        $userInDefaultNetwork->network_id = $networkDefault->id;
1658
                                        $userInDefaultNetwork->blocked = User::BLOCKED_NO;
1659
                                        $userInDefaultNetwork->email_verified = User::EMAIL_VERIFIED_YES;
1660
                                        $userInDefaultNetwork->email = $email;
1661
                                        $userInDefaultNetwork->first_name = $first_name;
1662
                                        $userInDefaultNetwork->last_name = $last_name;
1663
                                        $userInDefaultNetwork->password = $password_hash;
1664
                                        $userInDefaultNetwork->login_attempt = 0;
1665
                                        $userInDefaultNetwork->usertype_id = UserType::USER;
1666
                                        $userInDefaultNetwork->status = User::STATUS_ACTIVE;
1667
                                        $userInDefaultNetwork->is_adult = User::IS_ADULT_YES;
1668
                                        $result = $userMapper->insert($userInDefaultNetwork);
1669
                                        if($result) {
1670
                                            array_push($user_ids_in_default_network, $userInDefaultNetwork->id);
1671
 
1672
                                            if($country) {
1673
 
1674
                                                $location = new Location();
1675
                                                $location->formatted_address = $country->country;
1676
                                                $location->country = $country->country;
1677
                                                if($locationMapper->insert($location)) {
1678
 
1679
                                                    $userInDefaultNetwork->location_id = $location->id;
1680
                                                    $userMapper->updateLocation($userInDefaultNetwork);
1681
                                                }
1682
 
1683
                                            }
1684
 
1685
 
1686
                                            $userPassword = new UserPassword();
1687
                                            $userPassword->user_id = $userInDefaultNetwork->id;
1688
                                            $userPassword->password = $password_hash;
1689
                                            $userPasswordMapper->insert($userPassword);
1690
                                        }
1691
 
1692
                                    }
1693
 
1694
 
1695
                                }
1696
 
1697
 
1698
                            }
1699
 
1700
 
1701
 
1702
 
1703
 
1704
 
1 www 1705
                        } else {
1706
                            continue;
1707
                        }
129 efrain 1708
                    } else {
1709
                        if($user->email_verified == User::EMAIL_VERIFIED_NO || $user->status != User::STATUS_ACTIVE) {
1710
                            $user->email_verified = User::EMAIL_VERIFIED_YES;
1711
                            $user->status != User::STATUS_ACTIVE;
1712
 
1713
                            if(!$userMapper->update($user)) {
1714
                                continue;
1715
                            }
1716
                        }
1 www 1717
                    }
1718
 
15460 efrain 1719
                    array_push($user_ids, $user->id);
1720
 
1721
                }
1722
 
1723
                if($currentCompany) {
1724
 
1725
                    $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1726
 
1727
                    $company_users_created = 0;
1728
 
1729
                    foreach($user_ids as $user_id)
1730
                    {
1731
                        $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user_id);
1732
                        if(!$companyUser) {
1733
 
1734
                            $companyUser = new CompanyUser();
1735
                            $companyUser->company_id = $currentCompany->id;
1736
                            $companyUser->user_id = $user_id;
1737
                            $companyUser->backend = CompanyUser::BACKEND_NO;
1738
                            $companyUser->creator = CompanyUser::CREATOR_NO;
1739
                            $companyUser->owner = CompanyUser::OWNER_NO;
1740
 
1741
                            if($currentNetwork->default == Network::DEFAULT_YES) {
1742
                                $companyUser->status = CompanyUser::STATUS_ADMIN_WILL_ADD;
1743
                            } else {
1744
                                $companyUser->status = CompanyUser::STATUS_ACCEPTED;
1745
                            }
1746
 
1747
                            if($companyUserMapper->insert($companyUser)) {
1748
                                $company_users_created++;
1749
                            }
1750
 
1751
                        }
1752
 
1 www 1753
                    }
15460 efrain 1754
 
1755
                    $this->logger->info('Se agregaron ' . $users_created . '  usuarios  la empresa ' . $currentCompany->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1756
 
1757
 
1758
 
1759
                } else {
1760
 
1761
                    $this->logger->info('Se agregaron ' . $users_created . ' usuarios a la red', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1762
 
1 www 1763
                }
1764
 
15460 efrain 1765
                if($user_ids_in_default_network) {
1766
                    $companyMapper = CompanyMapper::getInstance($this->adapter);
1767
                    $companyToFollower = $companyMapper->fetchOneDefaultForFollowers();
1768
 
1769
                    $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
1770
 
1771
                    $userToConnection = $userMapper->fetchOneDefaultForConnection();
1772
 
1773
 
1774
                    $connectionMapper = ConnectionMapper::getInstance($this->adapter);
1775
 
1776
                    foreach($user_ids_in_default_network as $user_id)
1777
                    {
1778
                        if($userToConnection) {
1779
                            $connection = $connectionMapper->fetchOneByUserId1AndUserId2($user_id, $userToConnection->id);
1780
                            if(!$connection) {
1781
                                $connection = new Connection();
1782
                                $connection->request_from = $user_id;
1783
                                $connection->request_to = $userToConnection->id;
1784
                                $connection->status = Connection::STATUS_ACCEPTED;
1785
 
1786
                                $connectionMapper->insert($connection);
1787
                            } else {
1788
                                if($connection->status == Connection::STATUS_SENT) {
1789
                                    $connectionMapper->approve($connection);
1790
                                }
1791
                            }
1792
                        }
1 www 1793
 
15460 efrain 1794
                        if($companyToFollower) {
1795
                            $companyFollower = $companyFollowerMapper->fetchOneByCompanyIdAndUserId($companyToFollower->id, $user_id);
1796
                            if(!$companyFollower) {
1797
                                $companyFollower = new CompanyFollower();
1798
                                $companyFollower->company_id = $companyToFollower->id;
1799
                                $companyFollower->follower_id = $user_id;
1800
 
1801
                                $companyFollowerMapper->insert($companyFollower);
1802
 
1803
                            }
1 www 1804
                        }
1805
 
1806
                    }
1807
 
15460 efrain 1808
 
1809
 
1810
 
1 www 1811
                }
1812
 
1813
 
15460 efrain 1814
 
1 www 1815
                return new JsonModel([
1816
                    'success' => true,
1817
                    'data' => [
1818
                        'users_created' => $users_created
1819
                    ]
1820
                ]);
1821
 
1822
 
1823
 
1824
 
1825
            } else {
1826
                return new JsonModel([
1827
                    'success' => false,
1828
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID'
1829
                ]);
1830
            }
1831
 
1832
 
1833
 
1834
 
1835
        }
1836
 
1837
        return new JsonModel([
1838
            'success' => false,
1839
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1840
        ]);
1841
    }
1842
 
1843
 
1844
    public function editAction()
1845
    {
1846
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1847
        $currentUser = $currentUserPlugin->getUser();
1848
        $currentCompany = $currentUserPlugin->getCompany();
1849
 
1850
        $request = $this->getRequest();
1851
        $uuid = $this->params()->fromRoute('id');
1852
 
1853
 
1854
        if(!$uuid) {
1855
            $data = [
1856
                'success'   => false,
1857
                'data'   => 'ERROR_INVALID_PARAMETER'
1858
            ];
1859
 
1860
            return new JsonModel($data);
1861
        }
1862
 
1863
        $userMapper = UserMapper::getInstance($this->adapter);
1864
        $user = $userMapper->fetchOneByUuid($uuid);
1865
        if(!$user) {
1866
            $data = [
1867
                'success'   => false,
1868
                'data'   => 'ERROR_COMPANY_NOT_FOUND'
1869
            ];
1870
 
1871
            return new JsonModel($data);
1872
        }
1873
 
1874
        $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1875
        $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
1876
        if(!$companyUser) {
1877
            return new JsonModel([
1878
                'success'   => false,
1879
                'data'   => 'ERROR_COMPANY_USER_NOT_FOUND'
1880
            ]);
1881
 
1882
        }
1883
 
1884
        if($companyUser->status != CompanyUser::STATUS_ACCEPTED && $companyUser->status != CompanyUser::STATUS_ADMIN_WILL_ADD) {
1885
            return new JsonModel([
1886
                'success'   => false,
1887
                'data'   => 'ERROR_COMPANY_USER_IS_NOT_ACTIVE'
1888
            ]);
1889
        }
1890
 
1891
 
1892
 
1893
 
1894
        if($request->isPost()) {
1895
 
1896
            $companyRoleMapper = CompanyRoleMapper::getInstance($this->adapter);
1897
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1898
            $companyUserRoleMapper = CompanyUserRoleMapper::getInstance($this->adapter);
1899
 
1900
            $backend = filter_var( $this->params()->fromPost('backend'), FILTER_SANITIZE_STRING );
1901
            $companyUser->backend = $backend == CompanyUser::BACKEND_YES ? CompanyUser::BACKEND_YES : CompanyUser::BACKEND_NO;
1902
            $companyUserMapper->update($companyUser);
1903
 
1904
 
1905
            $roleMapper = RoleMapper::getInstance($this->adapter);
1906
            $roles = $roleMapper->fetchAll();
1907
 
1908
 
1909
            foreach($roles as $role)
1910
            {
1911
                $companyRole = $companyRoleMapper->fetchOneByCompanyIdAndRoleId($currentCompany->id, $role->id);
1912
                if(!$companyRole) {
1913
                    $companyUserRoleMapper->deleteByCompanyIdAndRoleId($currentCompany->id, $role->id);
1914
                    continue;
1915
                }
1916
 
1917
                $checked     = filter_var( $this->params()->fromPost('checked' . $role->id), FILTER_SANITIZE_NUMBER_INT);
1918
 
1919
                if($checked) {
1920
 
1921
                    $companyUserRole = $companyUserRoleMapper->fetchOneByCompanyIdAndUserIdAndRoleId($currentCompany->id, $user->id, $role->id);
1922
                    if(!$companyUserRole) {
1923
 
1924
                        $companyUserRole = new CompanyUserRole();
1925
                        $companyUserRole->company_id = $currentCompany->id;
1926
                        $companyUserRole->role_id = $role->id;
1927
                        $companyUserRole->user_id = $user->id;
1928
 
1929
                        $companyUserRoleMapper->insert($companyUserRole);
1930
 
1931
                    }
1932
 
1933
 
1934
 
1935
 
1936
                } else {
1937
 
1938
                    $companyUserRoleMapper->deleteByCompanyIdAndUserIdAndRoleId($currentCompany->id, $user->id, $role->id);
1939
                }
1940
            }
1941
 
1942
            $this->logger->info('Se actualizo los roles del usuario : ' . $user->email . ' en la empresa ' . $currentCompany->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1943
 
1944
            return new JsonModel([
1945
                'success' => true,
1946
                'data' => 'LABEL_RECORD_UPDATED'
1947
            ]);
1948
 
1949
 
1950
        } else if ($request->isGet()) {
1951
            $companyServiceMapper = CompanyServiceMapper::getInstance($this->adapter);
1952
 
1953
            $roleMapper = RoleMapper::getInstance($this->adapter);
1954
            $records = $roleMapper->fetchAll();
1955
 
4 efrain 1956
 
1957
 
1 www 1958
            $companyRoleMapper = CompanyRoleMapper::getInstance($this->adapter);
1959
            $companyUserRoleMapper = CompanyUserRoleMapper::getInstance($this->adapter);
1960
 
1961
            $roles = [];
1962
            foreach($records as $record)
1963
            {
1964
                if($record->creator == Role::CREATOR_YES) {
1965
                    continue;
1966
                }
1967
 
1968
                if($record->service_id) {
1969
                    $companyService = $companyServiceMapper->fetchOneByCompanyIdAndServiceId($currentCompany->id, $record->service_id);
1970
                    if(!$companyService || $companyService->status == CompanyService::INACTIVE) {
1971
                        continue;
1972
                    }
1973
 
1974
                }
1975
 
1976
 
1977
 
1978
                $companyRole = $companyRoleMapper->fetchOneByCompanyIdAndRoleId($currentCompany->id, $record->id);
1979
                if(!$companyRole) {
1980
                    continue;
1981
                }
1982
 
1983
                $companyUserRole  = $companyUserRoleMapper->fetchOneByCompanyIdAndUserIdAndRoleId($currentCompany->id, $user->id, $record->id);
1984
 
1985
 
1986
                $roles[ $record->id ] = [
1987
                    'id' => $record->id,
1988
                    'name' => $record->name,
1989
                    'fixed' => $record->creator == Role::CREATOR_YES ? true : false,
1990
                    'checked' => $companyUserRole ? true : false,
1991
                ];
4 efrain 1992
 
1993
 
1 www 1994
            }
1995
 
1996
 
1997
 
1998
            $data = [
1999
                'success' => true,
2000
                'data' => [
2001
                   'backend' => $companyUser->backend == CompanyUser::BACKEND_YES ? 1 : 0,
2002
                   'roles' => $roles,
2003
                ] ,
2004
            ];
2005
 
2006
 
2007
 
2008
            return new JsonModel($data);
2009
        } else {
2010
            $data = [
2011
                'success' => false,
2012
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
2013
            ];
2014
 
2015
            return new JsonModel($data);
2016
        }
2017
 
2018
        return new JsonModel($data);
2019
    }
2020
 
2021
 
2022
}