1: | <?php
2: | namespace Opencart\Admin\Controller\User;
3: | /**
4: | * Class User
5: | *
6: | * @package Opencart\Admin\Controller\User
7: | */
8: | class User extends \Opencart\System\Engine\Controller {
9: | /**
10: | * Index
11: | *
12: | * @return void
13: | */
14: | public function index(): void {
15: | $this->load->language('user/user');
16: |
17: | $this->document->setTitle($this->language->get('heading_title'));
18: |
19: | if (isset($this->request->get['filter_username'])) {
20: | $filter_username = (string)$this->request->get['filter_username'];
21: | } else {
22: | $filter_username = '';
23: | }
24: |
25: | if (isset($this->request->get['filter_name'])) {
26: | $filter_name = (string)$this->request->get['filter_name'];
27: | } else {
28: | $filter_name = '';
29: | }
30: |
31: | if (isset($this->request->get['filter_email'])) {
32: | $filter_email = (string)$this->request->get['filter_email'];
33: | } else {
34: | $filter_email = '';
35: | }
36: |
37: | if (isset($this->request->get['filter_user_group_id'])) {
38: | $filter_user_group_id = (int)$this->request->get['filter_user_group_id'];
39: | } else {
40: | $filter_user_group_id = '';
41: | }
42: |
43: | if (isset($this->request->get['filter_status'])) {
44: | $filter_status = (bool)$this->request->get['filter_status'];
45: | } else {
46: | $filter_status = '';
47: | }
48: |
49: | if (isset($this->request->get['filter_ip'])) {
50: | $filter_ip = (string)$this->request->get['filter_ip'];
51: | } else {
52: | $filter_ip = '';
53: | }
54: |
55: | $url = '';
56: |
57: | if (isset($this->request->get['filter_username'])) {
58: | $url .= '&filter_username=' . urlencode(html_entity_decode($this->request->get['filter_username'], ENT_QUOTES, 'UTF-8'));
59: | }
60: |
61: | if (isset($this->request->get['filter_name'])) {
62: | $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
63: | }
64: |
65: | if (isset($this->request->get['filter_email'])) {
66: | $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
67: | }
68: |
69: | if (isset($this->request->get['filter_user_group_id'])) {
70: | $url .= '&filter_user_group_id=' . $this->request->get['filter_user_group_id'];
71: | }
72: |
73: | if (isset($this->request->get['filter_status'])) {
74: | $url .= '&filter_status=' . $this->request->get['filter_status'];
75: | }
76: |
77: | if (isset($this->request->get['filter_ip'])) {
78: | $url .= '&filter_ip=' . $this->request->get['filter_ip'];
79: | }
80: |
81: | if (isset($this->request->get['sort'])) {
82: | $url .= '&sort=' . $this->request->get['sort'];
83: | }
84: |
85: | if (isset($this->request->get['order'])) {
86: | $url .= '&order=' . $this->request->get['order'];
87: | }
88: |
89: | if (isset($this->request->get['page'])) {
90: | $url .= '&page=' . $this->request->get['page'];
91: | }
92: |
93: | $data['breadcrumbs'] = [];
94: |
95: | $data['breadcrumbs'][] = [
96: | 'text' => $this->language->get('text_home'),
97: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
98: | ];
99: |
100: | $data['breadcrumbs'][] = [
101: | 'text' => $this->language->get('heading_title'),
102: | 'href' => $this->url->link('user/user', 'user_token=' . $this->session->data['user_token'] . $url)
103: | ];
104: |
105: | $data['add'] = $this->url->link('user/user.form', 'user_token=' . $this->session->data['user_token'] . $url);
106: | $data['delete'] = $this->url->link('user/user.delete', 'user_token=' . $this->session->data['user_token']);
107: |
108: | $data['list'] = $this->getList();
109: |
110: | $this->load->model('user/user_group');
111: |
112: | $data['user_groups'] = $this->model_user_user_group->getUserGroups();
113: |
114: | $data['filter_username'] = $filter_name;
115: | $data['filter_name'] = $filter_name;
116: | $data['filter_email'] = $filter_email;
117: | $data['filter_user_group_id'] = $filter_user_group_id;
118: | $data['filter_status'] = $filter_status;
119: | $data['filter_ip'] = $filter_ip;
120: |
121: | $data['user_token'] = $this->session->data['user_token'];
122: |
123: | $data['header'] = $this->load->controller('common/header');
124: | $data['column_left'] = $this->load->controller('common/column_left');
125: | $data['footer'] = $this->load->controller('common/footer');
126: |
127: | $this->response->setOutput($this->load->view('user/user', $data));
128: | }
129: |
130: | /**
131: | * List
132: | *
133: | * @return void
134: | */
135: | public function list(): void {
136: | $this->load->language('user/user');
137: |
138: | $this->response->setOutput($this->getList());
139: | }
140: |
141: | /**
142: | * Get List
143: | *
144: | * @return string
145: | */
146: | protected function getList(): string {
147: | if (isset($this->request->get['filter_username'])) {
148: | $filter_username = $this->request->get['filter_username'];
149: | } else {
150: | $filter_username = '';
151: | }
152: |
153: | if (isset($this->request->get['filter_name'])) {
154: | $filter_name = $this->request->get['filter_name'];
155: | } else {
156: | $filter_name = '';
157: | }
158: |
159: | if (isset($this->request->get['filter_email'])) {
160: | $filter_email = $this->request->get['filter_email'];
161: | } else {
162: | $filter_email = '';
163: | }
164: |
165: | if (isset($this->request->get['filter_user_group_id'])) {
166: | $filter_user_group_id = (int)$this->request->get['filter_user_group_id'];
167: | } else {
168: | $filter_user_group_id = '';
169: | }
170: |
171: | if (isset($this->request->get['filter_status'])) {
172: | $filter_status = (bool)$this->request->get['filter_status'];
173: | } else {
174: | $filter_status = '';
175: | }
176: |
177: | if (isset($this->request->get['filter_ip'])) {
178: | $filter_ip = (string)$this->request->get['filter_ip'];
179: | } else {
180: | $filter_ip = '';
181: | }
182: |
183: | if (isset($this->request->get['sort'])) {
184: | $sort = (string)$this->request->get['sort'];
185: | } else {
186: | $sort = 'username';
187: | }
188: |
189: | if (isset($this->request->get['order'])) {
190: | $order = (string)$this->request->get['order'];
191: | } else {
192: | $order = 'ASC';
193: | }
194: |
195: | if (isset($this->request->get['page'])) {
196: | $page = (int)$this->request->get['page'];
197: | } else {
198: | $page = 1;
199: | }
200: |
201: | $url = '';
202: |
203: | if (isset($this->request->get['filter_username'])) {
204: | $url .= '&filter_username=' . urlencode(html_entity_decode($this->request->get['filter_username'], ENT_QUOTES, 'UTF-8'));
205: | }
206: |
207: | if (isset($this->request->get['filter_name'])) {
208: | $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
209: | }
210: |
211: | if (isset($this->request->get['filter_email'])) {
212: | $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
213: | }
214: |
215: | if (isset($this->request->get['filter_user_group_id'])) {
216: | $url .= '&filter_user_group_id=' . $this->request->get['filter_user_group_id'];
217: | }
218: |
219: | if (isset($this->request->get['filter_status'])) {
220: | $url .= '&filter_status=' . $this->request->get['filter_status'];
221: | }
222: |
223: | if (isset($this->request->get['filter_ip'])) {
224: | $url .= '&filter_ip=' . $this->request->get['filter_ip'];
225: | }
226: |
227: | if (isset($this->request->get['sort'])) {
228: | $url .= '&sort=' . $this->request->get['sort'];
229: | }
230: |
231: | if (isset($this->request->get['order'])) {
232: | $url .= '&order=' . $this->request->get['order'];
233: | }
234: |
235: | if (isset($this->request->get['page'])) {
236: | $url .= '&page=' . $this->request->get['page'];
237: | }
238: |
239: | $data['action'] = $this->url->link('user/user.list', 'user_token=' . $this->session->data['user_token'] . $url);
240: |
241: | $data['users'] = [];
242: |
243: | $filter_data = [
244: | 'filter_username' => $filter_username,
245: | 'filter_name' => $filter_name,
246: | 'filter_email' => $filter_email,
247: | 'filter_user_group_id' => $filter_user_group_id,
248: | 'filter_status' => $filter_status,
249: | 'filter_ip' => $filter_ip,
250: | 'sort' => $sort,
251: | 'order' => $order,
252: | 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
253: | 'limit' => $this->config->get('config_pagination_admin')
254: | ];
255: |
256: | $this->load->model('user/user');
257: |
258: | $results = $this->model_user_user->getUsers($filter_data);
259: |
260: | foreach ($results as $result) {
261: | $data['users'][] = [
262: | 'user_id' => $result['user_id'],
263: | 'username' => $result['username'],
264: | 'name' => $result['name'],
265: | 'email' => $result['email'],
266: | 'user_group' => $result['user_group'],
267: | 'status' => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
268: | 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
269: | 'edit' => $this->url->link('user/user.form', 'user_token=' . $this->session->data['user_token'] . '&user_id=' . $result['user_id'] . $url)
270: | ];
271: | }
272: |
273: | $url = '';
274: |
275: | if (isset($this->request->get['filter_username'])) {
276: | $url .= '&filter_username=' . urlencode(html_entity_decode($this->request->get['filter_username'], ENT_QUOTES, 'UTF-8'));
277: | }
278: |
279: | if (isset($this->request->get['filter_name'])) {
280: | $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
281: | }
282: |
283: | if (isset($this->request->get['filter_email'])) {
284: | $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
285: | }
286: |
287: | if (isset($this->request->get['filter_user_group_id'])) {
288: | $url .= '&filter_user_group_id=' . $this->request->get['filter_user_group_id'];
289: | }
290: |
291: | if (isset($this->request->get['filter_status'])) {
292: | $url .= '&filter_status=' . $this->request->get['filter_status'];
293: | }
294: |
295: | if (isset($this->request->get['filter_ip'])) {
296: | $url .= '&filter_ip=' . $this->request->get['filter_ip'];
297: | }
298: |
299: | if ($order == 'ASC') {
300: | $url .= '&order=DESC';
301: | } else {
302: | $url .= '&order=ASC';
303: | }
304: |
305: | $data['sort_username'] = $this->url->link('user/user.list', 'user_token=' . $this->session->data['user_token'] . '&sort=username' . $url);
306: | $data['sort_name'] = $this->url->link('user/user.list', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url);
307: | $data['sort_email'] = $this->url->link('user/user.list', 'user_token=' . $this->session->data['user_token'] . '&sort=u.email' . $url);
308: | $data['sort_user_group'] = $this->url->link('user/user.list', 'user_token=' . $this->session->data['user_token'] . '&sort=user_group' . $url);
309: | $data['sort_status'] = $this->url->link('user/user.list', 'user_token=' . $this->session->data['user_token'] . '&sort=u.status' . $url);
310: | $data['sort_date_added'] = $this->url->link('user/user.list', 'user_token=' . $this->session->data['user_token'] . '&sort=u.date_added' . $url);
311: |
312: | $url = '';
313: |
314: | if (isset($this->request->get['filter_username'])) {
315: | $url .= '&filter_username=' . urlencode(html_entity_decode($this->request->get['filter_username'], ENT_QUOTES, 'UTF-8'));
316: | }
317: |
318: | if (isset($this->request->get['filter_name'])) {
319: | $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
320: | }
321: |
322: | if (isset($this->request->get['filter_email'])) {
323: | $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
324: | }
325: |
326: | if (isset($this->request->get['filter_user_group_id'])) {
327: | $url .= '&filter_user_group_id=' . $this->request->get['filter_user_group_id'];
328: | }
329: |
330: | if (isset($this->request->get['filter_status'])) {
331: | $url .= '&filter_status=' . $this->request->get['filter_status'];
332: | }
333: |
334: | if (isset($this->request->get['filter_ip'])) {
335: | $url .= '&filter_ip=' . $this->request->get['filter_ip'];
336: | }
337: |
338: | if (isset($this->request->get['sort'])) {
339: | $url .= '&sort=' . $this->request->get['sort'];
340: | }
341: |
342: | if (isset($this->request->get['order'])) {
343: | $url .= '&order=' . $this->request->get['order'];
344: | }
345: |
346: | $user_total = $this->model_user_user->getTotalUsers();
347: |
348: | $data['pagination'] = $this->load->controller('common/pagination', [
349: | 'total' => $user_total,
350: | 'page' => $page,
351: | 'limit' => $this->config->get('config_pagination_admin'),
352: | 'url' => $this->url->link('user/user.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
353: | ]);
354: |
355: | $data['results'] = sprintf($this->language->get('text_pagination'), ($user_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($user_total - $this->config->get('config_pagination_admin'))) ? $user_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $user_total, ceil($user_total / $this->config->get('config_pagination_admin')));
356: |
357: | $data['sort'] = $sort;
358: | $data['order'] = $order;
359: |
360: | return $this->load->view('user/user_list', $data);
361: | }
362: |
363: | /**
364: | * Form
365: | *
366: | * @return void
367: | */
368: | public function form(): void {
369: | $this->load->language('user/user');
370: |
371: | $this->document->setTitle($this->language->get('heading_title'));
372: |
373: | $data['text_form'] = !isset($this->request->get['user_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
374: |
375: | $url = '';
376: |
377: | if (isset($this->request->get['filter_username'])) {
378: | $url .= '&filter_username=' . urlencode(html_entity_decode($this->request->get['filter_username'], ENT_QUOTES, 'UTF-8'));
379: | }
380: |
381: | if (isset($this->request->get['filter_name'])) {
382: | $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
383: | }
384: |
385: | if (isset($this->request->get['filter_email'])) {
386: | $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
387: | }
388: |
389: | if (isset($this->request->get['filter_user_group_id'])) {
390: | $url .= '&filter_user_group_id=' . $this->request->get['filter_user_group_id'];
391: | }
392: |
393: | if (isset($this->request->get['filter_status'])) {
394: | $url .= '&filter_status=' . $this->request->get['filter_status'];
395: | }
396: |
397: | if (isset($this->request->get['filter_ip'])) {
398: | $url .= '&filter_ip=' . $this->request->get['filter_ip'];
399: | }
400: |
401: | if (isset($this->request->get['sort'])) {
402: | $url .= '&sort=' . $this->request->get['sort'];
403: | }
404: |
405: | if (isset($this->request->get['order'])) {
406: | $url .= '&order=' . $this->request->get['order'];
407: | }
408: |
409: | if (isset($this->request->get['page'])) {
410: | $url .= '&page=' . $this->request->get['page'];
411: | }
412: |
413: | $data['breadcrumbs'] = [];
414: |
415: | $data['breadcrumbs'][] = [
416: | 'text' => $this->language->get('text_home'),
417: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
418: | ];
419: |
420: | $data['breadcrumbs'][] = [
421: | 'text' => $this->language->get('heading_title'),
422: | 'href' => $this->url->link('user/user_permission', 'user_token=' . $this->session->data['user_token'] . $url)
423: | ];
424: |
425: | $data['save'] = $this->url->link('user/user.save', 'user_token=' . $this->session->data['user_token']);
426: | $data['back'] = $this->url->link('user/user', 'user_token=' . $this->session->data['user_token'] . $url);
427: |
428: | if (isset($this->request->get['user_id'])) {
429: | $this->load->model('user/user');
430: |
431: | $user_info = $this->model_user_user->getUser($this->request->get['user_id']);
432: | }
433: |
434: | if (isset($this->request->get['user_id'])) {
435: | $data['user_id'] = (int)$this->request->get['user_id'];
436: | } else {
437: | $data['user_id'] = 0;
438: | }
439: |
440: | if (!empty($user_info)) {
441: | $data['username'] = $user_info['username'];
442: | } else {
443: | $data['username'] = '';
444: | }
445: |
446: | $this->load->model('user/user_group');
447: |
448: | $data['user_groups'] = $this->model_user_user_group->getUserGroups();
449: |
450: | if (!empty($user_info)) {
451: | $data['user_group_id'] = $user_info['user_group_id'];
452: | } else {
453: | $data['user_group_id'] = 0;
454: | }
455: |
456: | if (!empty($user_info)) {
457: | $data['firstname'] = $user_info['firstname'];
458: | } else {
459: | $data['firstname'] = '';
460: | }
461: |
462: | if (!empty($user_info)) {
463: | $data['lastname'] = $user_info['lastname'];
464: | } else {
465: | $data['lastname'] = '';
466: | }
467: |
468: | if (!empty($user_info)) {
469: | $data['email'] = $user_info['email'];
470: | } else {
471: | $data['email'] = '';
472: | }
473: |
474: | if (!empty($user_info)) {
475: | $data['image'] = $user_info['image'];
476: | } else {
477: | $data['image'] = '';
478: | }
479: |
480: | $this->load->model('tool/image');
481: |
482: | $data['placeholder'] = $this->model_tool_image->resize('no_image.png', $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height'));
483: |
484: | if ($data['image'] && is_file(DIR_IMAGE . html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8'))) {
485: | $data['thumb'] = $this->model_tool_image->resize($data['image'], $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height'));
486: | } else {
487: | $data['thumb'] = $data['placeholder'];
488: | }
489: |
490: | if (!empty($user_info)) {
491: | $data['status'] = $user_info['status'];
492: | } else {
493: | $data['status'] = 0;
494: | }
495: |
496: | $data['authorize'] = $this->getAuthorize();
497: | $data['login'] = $this->getLogin();
498: |
499: | $data['user_token'] = $this->session->data['user_token'];
500: |
501: | $data['header'] = $this->load->controller('common/header');
502: | $data['column_left'] = $this->load->controller('common/column_left');
503: | $data['footer'] = $this->load->controller('common/footer');
504: |
505: | $this->response->setOutput($this->load->view('user/user_form', $data));
506: | }
507: |
508: | /**
509: | * Save
510: | *
511: | * @return void
512: | */
513: | public function save(): void {
514: | $this->load->language('user/user');
515: |
516: | $json = [];
517: |
518: | if (!$this->user->hasPermission('modify', 'user/user')) {
519: | $json['error']['warning'] = $this->language->get('error_permission');
520: | }
521: |
522: | if ((oc_strlen($this->request->post['username']) < 3) || (oc_strlen($this->request->post['username']) > 20)) {
523: | $json['error']['username'] = $this->language->get('error_username');
524: | }
525: |
526: | $this->load->model('user/user');
527: |
528: | $user_info = $this->model_user_user->getUserByUsername($this->request->post['username']);
529: |
530: | if (!$this->request->post['user_id']) {
531: | if ($user_info) {
532: | $json['error']['warning'] = $this->language->get('error_username_exists');
533: | }
534: | } else {
535: | if ($user_info && ($this->request->post['user_id'] != $user_info['user_id'])) {
536: | $json['error']['warning'] = $this->language->get('error_username_exists');
537: | }
538: | }
539: |
540: | if (!oc_validate_length($this->request->post['firstname'], 1, 32)) {
541: | $json['error']['firstname'] = $this->language->get('error_firstname');
542: | }
543: |
544: | if (!oc_validate_length($this->request->post['lastname'], 1, 32)) {
545: | $json['error']['lastname'] = $this->language->get('error_lastname');
546: | }
547: |
548: | if ((oc_strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
549: | $json['error']['email'] = $this->language->get('error_email');
550: | }
551: |
552: | $user_info = $this->model_user_user->getUserByEmail($this->request->post['email']);
553: |
554: | if (!$this->request->post['user_id']) {
555: | if ($user_info) {
556: | $json['error']['warning'] = $this->language->get('error_email_exists');
557: | }
558: | } else {
559: | if ($user_info && ($this->request->post['user_id'] != $user_info['user_id'])) {
560: | $json['error']['warning'] = $this->language->get('error_email_exists');
561: | }
562: | }
563: |
564: | if ($this->request->post['password'] || (!isset($this->request->post['user_id']))) {
565: | if ((oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) < 6) || (oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) > 40)) {
566: | $json['error']['password'] = $this->language->get('error_password');
567: | }
568: |
569: | if ($this->request->post['password'] != $this->request->post['confirm']) {
570: | $json['error']['confirm'] = $this->language->get('error_confirm');
571: | }
572: | }
573: |
574: | if (!$json) {
575: | if (!$this->request->post['user_id']) {
576: | $json['user_id'] = $this->model_user_user->addUser($this->request->post);
577: | } else {
578: | $this->model_user_user->editUser($this->request->post['user_id'], $this->request->post);
579: | }
580: |
581: | $json['success'] = $this->language->get('text_success');
582: | }
583: |
584: | $this->response->addHeader('Content-Type: application/json');
585: | $this->response->setOutput(json_encode($json));
586: | }
587: |
588: | /**
589: | * Delete
590: | *
591: | * @return void
592: | */
593: | public function delete(): void {
594: | $this->load->language('user/user');
595: |
596: | $json = [];
597: |
598: | if (isset($this->request->post['selected'])) {
599: | $selected = $this->request->post['selected'];
600: | } else {
601: | $selected = [];
602: | }
603: |
604: | if (!$this->user->hasPermission('modify', 'user/user')) {
605: | $json['error'] = $this->language->get('error_permission');
606: | }
607: |
608: | foreach ($selected as $user_id) {
609: | if ($this->user->getId() == $user_id) {
610: | $json['error']['warning'] = $this->language->get('error_account');
611: | }
612: | }
613: |
614: | if (!$json) {
615: | $this->load->model('user/user');
616: |
617: | foreach ($selected as $user_id) {
618: | $this->model_user_user->deleteUser($user_id);
619: | }
620: |
621: | $json['success'] = $this->language->get('text_success');
622: | }
623: |
624: | $this->response->addHeader('Content-Type: application/json');
625: | $this->response->setOutput(json_encode($json));
626: | }
627: |
628: | /**
629: | * Authorize
630: | *
631: | * @return void
632: | */
633: | public function authorize(): void {
634: | $this->load->language('user/user');
635: |
636: | $this->response->setOutput($this->getAuthorize());
637: | }
638: |
639: | /**
640: | * Get Authorize
641: | *
642: | * @return string
643: | */
644: | public function getAuthorize(): string {
645: | if (isset($this->request->get['user_id'])) {
646: | $user_id = (int)$this->request->get['user_id'];
647: | } else {
648: | $user_id = 0;
649: | }
650: |
651: | if (isset($this->request->get['page']) && $this->request->get['route'] == 'user/user.login') {
652: | $page = (int)$this->request->get['page'];
653: | } else {
654: | $page = 1;
655: | }
656: |
657: | $limit = 10;
658: |
659: | $data['authorizes'] = [];
660: |
661: | $this->load->model('user/user');
662: |
663: | $results = $this->model_user_user->getAuthorizes($user_id, ($page - 1) * $limit, $limit);
664: |
665: | foreach ($results as $result) {
666: | $data['authorizes'][] = [
667: | 'token' => $result['token'],
668: | 'ip' => $result['ip'],
669: | 'user_agent' => $result['user_agent'],
670: | 'status' => $result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
671: | 'total' => $result['total'],
672: | 'date_added' => date($this->language->get('datetime_format'), strtotime($result['date_added'])),
673: | 'delete' => $this->url->link('user/user.deleteAuthorize', 'user_token=' . $this->session->data['user_token'] . '&user_authorize_id=' . $result['user_authorize_id'])
674: | ];
675: | }
676: |
677: | $authorize_total = $this->model_user_user->getTotalAuthorizes($user_id);
678: |
679: | $data['pagination'] = $this->load->controller('common/pagination', [
680: | 'total' => $authorize_total,
681: | 'page' => $page,
682: | 'limit' => $limit,
683: | 'url' => $this->url->link('user/user.authorize', 'user_token=' . $this->session->data['user_token'] . '&user_id=' . $user_id . '&page={page}')
684: | ]);
685: |
686: | $data['results'] = sprintf($this->language->get('text_pagination'), ($authorize_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($authorize_total - $limit)) ? $authorize_total : ((($page - 1) * $limit) + $limit), $authorize_total, ceil($authorize_total / $limit));
687: |
688: | return $this->load->view('user/user_authorize', $data);
689: | }
690: |
691: | /**
692: | * Delete Authorize
693: | *
694: | * @return void
695: | */
696: | public function deleteAuthorize(): void {
697: | $this->load->language('user/user');
698: |
699: | $json = [];
700: |
701: | if (isset($this->request->get['user_authorize_id'])) {
702: | $user_authorize_id = (int)$this->request->get['user_authorize_id'];
703: | } else {
704: | $user_authorize_id = 0;
705: | }
706: |
707: | if (isset($this->request->cookie['authorize'])) {
708: | $token = $this->request->cookie['authorize'];
709: | } else {
710: | $token = '';
711: | }
712: |
713: | if (!$this->user->hasPermission('modify', 'user/user')) {
714: | $json['error'] = $this->language->get('error_permission');
715: | }
716: |
717: | $this->load->model('user/user');
718: |
719: | $authorize_info = $this->model_user_user->getAuthorize($user_authorize_id);
720: |
721: | if (!$authorize_info) {
722: | $json['error'] = $this->language->get('error_authorize');
723: | }
724: |
725: | if (!$json) {
726: | $this->model_user_user->deleteAuthorize($authorize_info['user_id'], $user_authorize_id);
727: |
728: | // If the token is still present, then we enforce the user to log out automatically.
729: | if ($authorize_info['token'] == $token) {
730: | $this->session->data['success'] = $this->language->get('text_success');
731: |
732: | $json['redirect'] = $this->url->link('common/login', '', true);
733: | } else {
734: | $json['success'] = $this->language->get('text_success');
735: | }
736: | }
737: |
738: | $this->response->addHeader('Content-Type: application/json');
739: | $this->response->setOutput(json_encode($json));
740: | }
741: |
742: | /**
743: | * Login
744: | *
745: | * @return void
746: | */
747: | public function login(): void {
748: | $this->load->language('user/user');
749: |
750: | $this->response->setOutput($this->getLogin());
751: | }
752: |
753: | /**
754: | * Get Login
755: | *
756: | * @return string
757: | */
758: | public function getLogin(): string {
759: | if (isset($this->request->get['user_id'])) {
760: | $user_id = (int)$this->request->get['user_id'];
761: | } else {
762: | $user_id = 0;
763: | }
764: |
765: | if (isset($this->request->get['page']) && $this->request->get['route'] == 'user/user.login') {
766: | $page = (int)$this->request->get['page'];
767: | } else {
768: | $page = 1;
769: | }
770: |
771: | $limit = 10;
772: |
773: | $data['logins'] = [];
774: |
775: | $this->load->model('user/user');
776: |
777: | $results = $this->model_user_user->getLogins($user_id, ($page - 1) * $limit, $limit);
778: |
779: | foreach ($results as $result) {
780: | $data['logins'][] = [
781: | 'ip' => $result['ip'],
782: | 'user_agent' => $result['user_agent'],
783: | 'date_added' => date($this->language->get('datetime_format'), strtotime($result['date_added']))
784: | ];
785: | }
786: |
787: | $login_total = $this->model_user_user->getTotalLogins($user_id);
788: |
789: | $data['pagination'] = $this->load->controller('common/pagination', [
790: | 'total' => $login_total,
791: | 'page' => $page,
792: | 'limit' => $limit,
793: | 'url' => $this->url->link('user/user.login', 'user_token=' . $this->session->data['user_token'] . '&user_id=' . $user_id . '&page={page}')
794: | ]);
795: |
796: | $data['results'] = sprintf($this->language->get('text_pagination'), ($login_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($login_total - $limit)) ? $login_total : ((($page - 1) * $limit) + $limit), $login_total, ceil($login_total / $limit));
797: |
798: | return $this->load->view('user/user_login', $data);
799: | }
800: |
801: | /**
802: | * Autocomplete
803: | *
804: | * @return void
805: | */
806: | public function autocomplete(): void {
807: | $json = [];
808: |
809: | if (isset($this->request->get['filter_username']) || isset($this->request->get['filter_name']) || isset($this->request->get['filter_email'])) {
810: | if (isset($this->request->get['filter_username'])) {
811: | $filter_username = $this->request->get['filter_username'];
812: | } else {
813: | $filter_username = '';
814: | }
815: |
816: | if (isset($this->request->get['filter_name'])) {
817: | $filter_name = $this->request->get['filter_name'];
818: | } else {
819: | $filter_name = '';
820: | }
821: |
822: | if (isset($this->request->get['filter_email'])) {
823: | $filter_email = $this->request->get['filter_email'];
824: | } else {
825: | $filter_email = '';
826: | }
827: |
828: | $filter_data = [
829: | 'filter_username' => $filter_username,
830: | 'filter_name' => $filter_name,
831: | 'filter_email' => $filter_email,
832: | 'start' => 0,
833: | 'limit' => 5
834: | ];
835: |
836: | $this->load->model('user/user');
837: |
838: | $results = $this->model_user_user->getUsers($filter_data);
839: |
840: | foreach ($results as $result) {
841: | $json[] = [
842: | 'user_id' => $result['user_id'],
843: | 'user_group_id' => $result['user_group_id'],
844: | 'username' => $result['username'],
845: | 'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')),
846: | 'user_group' => $result['user_group'],
847: | 'firstname' => $result['firstname'],
848: | 'lastname' => $result['lastname'],
849: | 'email' => $result['email']
850: | ];
851: | }
852: | }
853: |
854: | $sort_order = [];
855: |
856: | foreach ($json as $key => $value) {
857: | $sort_order[$key] = $value['username'];
858: | }
859: |
860: | array_multisort($sort_order, SORT_ASC, $json);
861: |
862: | $this->response->addHeader('Content-Type: application/json');
863: | $this->response->setOutput(json_encode($json));
864: | }
865: | }
866: |

OpenCart API API documentation generated by ApiGen dev-master