Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16769 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
15457 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
 
8
 
16768 efrain 9
 
15457 efrain 10
use Laminas\Mvc\Controller\AbstractActionController;
11
use Laminas\Log\LoggerInterface;
12
 
13
use Laminas\View\Model\ViewModel;
14
use Laminas\View\Model\JsonModel;
15
use LeadersLinked\Library\Functions;
16
use LeadersLinked\Mapper\UserMapper;
17
use LeadersLinked\Model\User;
18
use LeadersLinked\Model\EmailTemplate;
19
use LeadersLinked\Mapper\EmailTemplateMapper;
20
use LeadersLinked\Library\QueueEmail;
21
 
22
class UserRequestAccessController extends AbstractActionController
23
{
24
    /**
25
     *
16769 efrain 26
     * @var \Laminas\Db\Adapter\AdapterInterface
15457 efrain 27
     */
28
    private $adapter;
29
 
30
    /**
31
     *
16769 efrain 32
     * @var \LeadersLinked\Cache\CacheInterface
15457 efrain 33
     */
16769 efrain 34
    private $cache;
35
 
36
 
37
    /**
38
     *
39
     * @var \Laminas\Log\LoggerInterface
40
     */
15457 efrain 41
    private $logger;
42
 
43
    /**
44
     *
45
     * @var array
46
     */
47
    private $config;
48
 
49
 
50
    /**
51
     *
16769 efrain 52
     * @var \Laminas\Mvc\I18n\Translator
53
     */
54
    private $translator;
55
 
56
 
57
    /**
58
     *
59
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
60
     * @param \LeadersLinked\Cache\CacheInterface $cache
61
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
15457 efrain 62
     * @param array $config
16769 efrain 63
     * @param \Laminas\Mvc\I18n\Translator $translator
15457 efrain 64
     */
16769 efrain 65
    public function __construct($adapter, $cache, $logger, $config, $translator)
15457 efrain 66
    {
67
        $this->adapter      = $adapter;
16769 efrain 68
        $this->cache        = $cache;
15457 efrain 69
        $this->logger       = $logger;
70
        $this->config       = $config;
16769 efrain 71
        $this->translator   = $translator;
15457 efrain 72
    }
73
 
74
    public function indexAction()
75
    {
76
        $currentUserPlugin = $this->plugin('currentUserPlugin');
77
        $currentUser = $currentUserPlugin->getUser();
78
        $currentCompany = $currentUserPlugin->getCompany();
79
 
80
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
81
        $network = $currentNetworkPlugin->getNetwork();
82
 
83
 
84
        $request = $this->getRequest();
85
 
86
        if($request->isGet())
87
 
88
 
89
            $headers  = $request->getHeaders();
90
 
91
            $isJson = false;
92
            if($headers->has('Accept')) {
93
                $accept = $headers->get('Accept');
94
 
95
                $prioritized = $accept->getPrioritized();
96
 
97
                foreach($prioritized as $key => $value) {
98
                    $raw = trim($value->getRaw());
99
 
100
                    if(!$isJson) {
101
                        $isJson = strpos($raw, 'json');
102
                    }
103
 
104
                }
105
            }
106
 
107
            if($isJson) {
108
            {
109
 
110
                $search = $this->params()->fromQuery('search', []);
16766 efrain 111
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
15457 efrain 112
 
113
                //$page               = intval($this->params()->fromQuery('start', 1), 10);
114
                //$records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
115
 
116
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
117
                $page               = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
118
 
119
                $order =  $this->params()->fromQuery('order', []);
120
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 121
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
15457 efrain 122
 
123
                $fields =  ['first_name', 'last_name', 'email'];
124
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
125
 
126
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
127
                    $order_direction = 'ASC';
128
                }
129
 
130
 
131
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
132
                $allowApprove = $acl->isAllowed($currentUser->usertype_id, 'users/request-access/approve');
133
                $allowReject = $acl->isAllowed($currentUser->usertype_id, 'users/request-access/reject');
134
 
135
                $userMapper = UserMapper::getInstance($this->adapter);
136
                $paginator = $userMapper->fetchAllDataTableRequestAccessPendingByNetworkId($network->id, $search, $page, $records_x_page, $order_field, $order_direction);
137
 
138
                $items = [];
139
                $records = $paginator->getCurrentItems();
140
 
141
                foreach($records as $record)
142
                {
143
                    $actions = [];
144
 
145
 
146
                    $actions['link_approve'] = $allowApprove ? $this->url()->fromRoute('users/request-access/approve', ['id' => $record->uuid ]) : '';
147
                    $actions['link_reject'] = $allowReject ? $this->url()->fromRoute('users/request-access/reject', ['id' => $record->uuid ]) : '';
148
 
149
                    $item = [
150
 
151
 
152
                        'first_name' => $record->first_name,
153
                        'last_name' => $record->last_name,
154
                        'email' => $record->email,
155
                        'actions' => $actions
156
                    ];
157
 
158
                    array_push($items, $item);
159
                }
160
            }
161
 
162
            return new JsonModel([
163
                'success' => true,
164
                'data' => [
165
                    'items' => $items,
166
                    'total' => $paginator->getTotalItemCount(),
167
                ]
168
            ]);
169
 
170
 
171
 
172
        }
173
        else if($request->isGet()) {
174
            $this->layout()->setTemplate('layout/layout-backend');
175
            $viewModel = new ViewModel();
176
            $viewModel->setTemplate('leaders-linked/users-request-access/index.phtml');
177
 
178
            return $viewModel ;
179
 
180
        } else {
181
            return new JsonModel([
182
                'success' => false,
183
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
184
            ]);;
185
        }
186
    }
187
 
188
 
189
 
190
 
191
    public function approveAction()
192
    {
193
        $currentUserPlugin = $this->plugin('currentUserPlugin');
194
        $currentUser = $currentUserPlugin->getUser();
195
 
196
        //$currentCompany = $currentUserPlugin->getCompany();
197
 
198
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
199
        $currentNetwork = $currentNetworkPlugin->getNetwork();
200
 
201
        $request = $this->getRequest();
202
 
203
 
204
        if($request->isPost()) {
205
 
206
            $uuid = $this->params()->fromRoute('id');
207
            if(!$uuid) {
208
                return new JsonModel([
209
                    'success'   => false,
210
                    'data'      => 'ERROR_INVALID_PARAMETER'
211
                ]);
212
            }
213
 
214
            $userMapper = UserMapper::getInstance($this->adapter);
215
            $user = $userMapper->fetchOneByUuid($uuid);
216
 
217
            if(!$user) {
218
                return new JsonModel([
219
                    'success'   => false,
220
                    'data'      => 'ERROR_USER_NOT_FOUND'
221
                ]);
222
            }
223
 
224
            if($user->network_id !=  $currentNetwork->id) {
225
                return new JsonModel([
226
                    'success'   => false,
227
                    'data'      => 'ERROR_UNAUTHORIZED'
228
                ]);
229
            }
230
 
231
            if($user->request_access != User::REQUEST_ACCESS_PENDING) {
232
                return new JsonModel([
233
                    'success'   => false,
234
                    'data'      => 'ERROR_REQUEST_ACCESS_IS_NOT_PENDING'
235
                ]);
236
            }
237
 
238
 
239
            $user->request_access = User::REQUEST_ACCESS_APPROVED;
240
            $result = $userMapper->update($user);
241
            if($result) {
242
 
243
 
244
                $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
245
                $emailTemplate = $emailTemplateMapper->fetchOneByCodeAndNetworkId(EmailTemplate::CODE_REQUEST_ACCESS_APPROVED, $currentNetwork->id);
246
 
247
                if($emailTemplate) {
248
                    $arrayCont = [
249
                        'firstname'             => $user->first_name,
250
                        'lastname'              => $user->last_name,
251
                        'other_user_firstname'  => '',
252
                        'other_user_lastname'   => '',
253
                        'company_name'          => '',
254
                        'group_name'            => '',
255
                        'content'               => '',
256
                        'code'                  => '',
257
                        'link'                  => $this->url()->fromRoute('home', [], ['force_canonical' => true])
258
                    ];
259
 
260
                    $email = new QueueEmail($this->adapter);
261
                    $email->processEmailTemplate($emailTemplate, $arrayCont, $user->email, trim($user->first_name . ' ' . $user->last_name));
262
                }
263
 
264
 
265
                $this->logger->info('Usted autorizo el acceso al usuario : ' .   trim($user->first_name . ' ' . $user->last_name) . '('  . $user->email . ')  ha sido autorizado ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
266
 
267
                return new JsonModel([
268
                    'success'   => true,
269
                    'data'      =>  'LABEL_USER_REQUEST_ACCESS_HAS_BEEN_APPROVED'
270
                ]);
271
            }  else {
272
 
273
                return new JsonModel([
274
                    'success'   => false,
275
                    'data'      => $userMapper->getError()
276
                ]);
277
            }
278
 
279
 
280
        }
281
 
282
 
283
 
284
        return new JsonModel([
285
            'success' => false,
286
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
287
        ]);
288
    }
289
 
290
 
291
 
292
    public function rejectAction()
293
    {
294
        $currentUserPlugin = $this->plugin('currentUserPlugin');
295
        $currentUser = $currentUserPlugin->getUser();
296
 
297
        //$currentCompany = $currentUserPlugin->getCompany();
298
 
299
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
300
        $currentNetwork = $currentNetworkPlugin->getNetwork();
301
 
302
        $request = $this->getRequest();
303
 
304
 
305
        if($request->isPost()) {
306
 
307
            $uuid = $this->params()->fromRoute('id');
308
            if(!$uuid) {
309
                return new JsonModel([
310
                    'success'   => false,
311
                    'data'      => 'ERROR_INVALID_PARAMETER'
312
                ]);
313
            }
314
 
315
            $userMapper = UserMapper::getInstance($this->adapter);
316
            $user = $userMapper->fetchOneByUuid($uuid);
317
 
318
            if(!$user) {
319
                return new JsonModel([
320
                    'success'   => false,
321
                    'data'      => 'ERROR_USER_NOT_FOUND'
322
                ]);
323
            }
324
 
325
            if($user->network_id !=  $currentNetwork->id) {
326
                return new JsonModel([
327
                    'success'   => false,
328
                    'data'      => 'ERROR_UNAUTHORIZED'
329
                ]);
330
            }
331
 
332
            if($user->request_access != User::REQUEST_ACCESS_PENDING) {
333
                return new JsonModel([
334
                    'success'   => false,
335
                    'data'      => 'ERROR_REQUEST_ACCESS_IS_NOT_PENDING'
336
                ]);
337
            }
338
 
339
 
340
            $user->request_access = User::REQUEST_ACCESS_REJECTED;
341
            $result = $userMapper->update($user);
342
            if($result) {
343
 
344
 
345
                $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
346
                $emailTemplate = $emailTemplateMapper->fetchOneByCodeAndNetworkId(EmailTemplate::CODE_REQUEST_ACCESS_REJECT, $currentNetwork->id);
347
 
348
                if($emailTemplate) {
349
                    $arrayCont = [
350
                        'firstname'             => $user->first_name,
351
                        'lastname'              => $user->last_name,
352
                        'other_user_firstname'  => '',
353
                        'other_user_lastname'   => '',
354
                        'company_name'          => '',
355
                        'group_name'            => '',
356
                        'content'               => '',
357
                        'code'                  => '',
358
                        'link'                  => $this->url()->fromRoute('home', [], ['force_canonical' => true])
359
                    ];
360
 
361
                    $email = new QueueEmail($this->adapter);
362
                    $email->processEmailTemplate($emailTemplate, $arrayCont, $user->email, trim($user->first_name . ' ' . $user->last_name));
363
                }
364
 
365
 
366
                $this->logger->info('Usted rechazo el acceso al usuario : ' .   trim($user->first_name . ' ' . $user->last_name) . '('  . $user->email . ')  ha sido autorizado ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
367
 
368
                return new JsonModel([
369
                    'success'   => true,
370
                    'data'      =>  'LABEL_USER_REQUEST_ACCESS_HAS_BEEN_REJECTED'
371
                ]);
372
            }  else {
373
 
374
                return new JsonModel([
375
                    'success'   => false,
376
                    'data'      => $userMapper->getError()
377
                ]);
378
            }
379
 
380
 
381
        }
382
 
383
 
384
 
385
        return new JsonModel([
386
            'success' => false,
387
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
388
        ]);
389
    }
390
 
391
 
392
 
393
 
394
}