Proyectos de Subversion LeadersLinked - Backend

Rev

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