Proyectos de Subversion LeadersLinked - Backend

Rev

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