1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use App\Controller\AppController; |
6
|
|
|
use Cake\Event\EventInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Users Controller |
10
|
|
|
* |
11
|
|
|
* @property \App\Model\Table\UsersTable $Users |
12
|
|
|
*/ |
13
|
|
|
class UsersController extends AppController { |
14
|
|
|
|
15
|
3 |
|
public function beforeFilter(EventInterface $event) { |
16
|
3 |
|
parent::beforeFilter($event); |
17
|
|
|
//$this->Auth->allow('add','login','logout'); |
|
|
|
|
18
|
3 |
|
$this->Authentication->addUnauthenticatedActions(['add','index','edit']); |
19
|
3 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Index method |
23
|
|
|
* |
24
|
|
|
* @return \Cake\Network\Response|null |
25
|
|
|
*/ |
26
|
1 |
|
public function index() { |
27
|
1 |
|
$users = $this->paginate($this->Users); |
28
|
|
|
|
29
|
1 |
|
$this->set(compact('users')); |
30
|
1 |
|
$this->set('_serialize', ['users']); |
31
|
1 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* View method |
35
|
|
|
* |
36
|
|
|
* @param string|null $id User id. |
37
|
|
|
* @return \Cake\Network\Response|null |
38
|
|
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. |
39
|
|
|
*/ |
40
|
|
|
public function view($id = null) { |
41
|
|
|
$user = $this->Users->get($id, [ |
42
|
|
|
'contain' => [] |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
$this->set('user', $user); |
46
|
|
|
$this->set('_serialize', ['user']); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Add method |
51
|
|
|
* |
52
|
|
|
* @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise. |
53
|
|
|
*/ |
54
|
|
|
public function add() { |
55
|
|
|
$user = $this->Users->newEmptyEntity(); |
56
|
|
|
if ($this->request->is('post')) { |
57
|
|
|
$user = $this->Users->patchEntity($user, $this->request->getData()); |
58
|
|
|
if ($this->Users->save($user)) { |
59
|
|
|
$this->Flash->success(__('The user has been saved.')); |
60
|
|
|
|
61
|
|
|
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']); |
62
|
|
|
} |
63
|
|
|
$this->Flash->error(__('The user could not be saved. Please, try again.')); |
64
|
|
|
} |
65
|
|
|
$this->set(compact('user')); |
66
|
|
|
$this->set('_serialize', ['user']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Edit method |
71
|
|
|
* |
72
|
|
|
* @param string|null $id User id. |
73
|
|
|
* @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise. |
74
|
|
|
* @throws \Cake\Network\Exception\NotFoundException When record not found. |
75
|
|
|
*/ |
76
|
|
|
public function edit($id = null) { |
77
|
|
|
$user = $this->Users->get($id, [ |
78
|
|
|
'contain' => [] |
79
|
|
|
]); |
80
|
|
|
if ($this->request->is(['patch', 'post', 'put'])) { |
81
|
|
|
$user = $this->Users->patchEntity($user, $this->getRequest()->getData()); |
82
|
|
|
if ($this->Users->save($user)) { |
83
|
|
|
$this->Flash->success(__('The user has been saved.')); |
84
|
|
|
|
85
|
|
|
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']); |
86
|
|
|
} |
87
|
|
|
$this->Flash->error(__('The user could not be saved. Please, try again.')); |
88
|
|
|
} |
89
|
|
|
$this->set(compact('user')); |
90
|
|
|
$this->set('_serialize', ['user']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Delete method |
95
|
|
|
* |
96
|
|
|
* @param string|null $id User id. |
97
|
|
|
* @return \Cake\Network\Response|null Redirects to index. |
98
|
|
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. |
99
|
|
|
*/ |
100
|
1 |
|
public function delete($id = null) { |
101
|
1 |
|
$this->request->allowMethod(['post', 'delete']); |
102
|
1 |
|
$user = $this->Users->get($id); |
103
|
1 |
|
if ($this->Users->delete($user)) { |
104
|
1 |
|
$this->Flash->success(__('The user has been deleted.')); |
105
|
|
|
} else { |
106
|
|
|
$this->Flash->error(__('The user could not be deleted. Please, try again.')); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function login() { |
113
|
|
|
/* |
|
|
|
|
114
|
|
|
if ($this->request->is('post')) { |
115
|
|
|
//$user = $this->Auth->identify(); |
116
|
|
|
$user = $this->Authentication->getResult(); |
117
|
|
|
$this->set('lu', $user); |
118
|
|
|
if ($user) { |
119
|
|
|
//$this->Auth->setUser($user); |
120
|
|
|
return $this->redirect($this->Authentication->getLoginRedirect()); |
121
|
|
|
} |
122
|
|
|
$this->Flash->error(__('Invalid username or password, try again')); |
123
|
|
|
} |
124
|
|
|
*/ |
125
|
|
|
$result = $this->Authentication->getResult(); |
126
|
|
|
// If the user is logged in send them away. |
127
|
|
|
if ($result->isValid()) { |
128
|
|
|
$target = $this->Authentication->getLoginRedirect() ?? '/home'; |
129
|
|
|
return $this->redirect($target); |
130
|
|
|
} |
131
|
|
|
if ($this->request->is('post') && !$result->isValid()) { |
132
|
|
|
$this->Flash->error('Invalid username or password'); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function logout() { |
137
|
1 |
|
$this->Authentication->logout(); |
138
|
1 |
|
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'login']); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.