1
|
|
|
<?php |
2
|
|
|
namespace App\Controller; |
3
|
|
|
|
4
|
|
|
use App\Controller\AppController; |
5
|
|
|
use Cake\Event\Event; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Users Controller |
9
|
|
|
* |
10
|
|
|
* @property \App\Model\Table\UsersTable $Users |
11
|
|
|
*/ |
12
|
|
|
class UsersController extends AppController |
13
|
|
|
{ |
14
|
|
|
|
15
|
5 |
|
public function beforeFilter(Event $event) |
16
|
|
|
{ |
17
|
5 |
|
parent::beforeFilter($event); |
18
|
5 |
|
$this->Auth->allow('add'); |
19
|
5 |
|
} |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Index method |
24
|
|
|
* |
25
|
|
|
* @return \Cake\Network\Response|null |
26
|
|
|
*/ |
27
|
1 |
|
public function index() |
28
|
|
|
{ |
29
|
1 |
|
$users = $this->paginate($this->Users); |
30
|
|
|
|
31
|
1 |
|
$this->set(compact('users')); |
32
|
1 |
|
$this->set('_serialize', ['users']); |
33
|
1 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* View method |
37
|
|
|
* |
38
|
|
|
* @param string|null $id User id. |
39
|
|
|
* @return \Cake\Network\Response|null |
40
|
|
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. |
41
|
|
|
*/ |
42
|
1 |
|
public function view($id = null) |
43
|
|
|
{ |
44
|
1 |
|
$user = $this->Users->get($id, [ |
45
|
|
|
'contain' => [] |
46
|
1 |
|
]); |
47
|
|
|
|
48
|
1 |
|
$this->set('user', $user); |
49
|
1 |
|
$this->set('_serialize', ['user']); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Add method |
54
|
|
|
* |
55
|
|
|
* @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise. |
56
|
|
|
*/ |
57
|
1 |
View Code Duplication |
public function add() |
|
|
|
|
58
|
|
|
{ |
59
|
1 |
|
$user = $this->Users->newEntity(); |
60
|
1 |
|
if ($this->request->is('post')) { |
61
|
1 |
|
$user = $this->Users->patchEntity($user, $this->request->data); |
62
|
1 |
|
if ($this->Users->save($user)) { |
63
|
1 |
|
$this->Flash->success(__('The user has been saved.')); |
64
|
|
|
|
65
|
1 |
|
return $this->redirect(['action' => 'index']); |
66
|
|
|
} |
67
|
|
|
$this->Flash->error(__('The user could not be saved. Please, try again.')); |
68
|
|
|
} |
69
|
|
|
$this->set(compact('user')); |
70
|
|
|
$this->set('_serialize', ['user']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Edit method |
75
|
|
|
* |
76
|
|
|
* @param string|null $id User id. |
77
|
|
|
* @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise. |
78
|
|
|
* @throws \Cake\Network\Exception\NotFoundException When record not found. |
79
|
|
|
*/ |
80
|
1 |
View Code Duplication |
public function edit($id = null) |
|
|
|
|
81
|
|
|
{ |
82
|
1 |
|
$user = $this->Users->get($id, [ |
83
|
|
|
'contain' => [] |
84
|
1 |
|
]); |
85
|
1 |
|
if ($this->request->is(['patch', 'post', 'put'])) { |
86
|
1 |
|
$user = $this->Users->patchEntity($user, $this->request->data); |
87
|
1 |
|
if ($this->Users->save($user)) { |
88
|
1 |
|
$this->Flash->success(__('The user has been saved.')); |
89
|
|
|
|
90
|
1 |
|
return $this->redirect(['action' => 'index']); |
91
|
|
|
} |
92
|
|
|
$this->Flash->error(__('The user could not be saved. Please, try again.')); |
93
|
|
|
} |
94
|
1 |
|
$this->set(compact('user')); |
95
|
1 |
|
$this->set('_serialize', ['user']); |
96
|
1 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Delete method |
100
|
|
|
* |
101
|
|
|
* @param string|null $id User id. |
102
|
|
|
* @return \Cake\Network\Response|null Redirects to index. |
103
|
|
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function delete($id = null) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$this->request->allowMethod(['post', 'delete']); |
108
|
|
|
$user = $this->Users->get($id); |
109
|
|
|
if ($this->Users->delete($user)) { |
110
|
|
|
$this->Flash->success(__('The user has been deleted.')); |
111
|
|
|
} else { |
112
|
|
|
$this->Flash->error(__('The user could not be deleted. Please, try again.')); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->redirect(['action' => 'index']); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function login() { |
119
|
|
|
if ($this->request->is('post')) { |
120
|
|
|
$user = $this->Auth->identify(); |
121
|
|
|
$this->set('lu', $user); |
122
|
|
|
if ($user) { |
123
|
|
|
$this->Auth->setUser($user); |
|
|
|
|
124
|
|
|
return $this->redirect($this->Auth->redirectUrl()); |
125
|
|
|
} |
126
|
|
|
$this->Flash->error(__('Invalid username or password, try again')); |
127
|
|
|
} |
128
|
|
|
} |
129
|
1 |
|
public function logout() |
130
|
|
|
{ |
131
|
1 |
|
return $this->redirect($this->Auth->logout()); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.