This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
/
UsersController.php
242 lines (228 loc) · 8.81 KB
/
UsersController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
namespace App\Controller;
use Cake\Auth\DefaultPasswordHasher;
use Cake\Event\Event;
use Cake\I18n\Time;
/**
* Users Controller
*
* @property \App\Model\Table\UsersTable $Users
*/
class UsersController extends AppController
{
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
if (!in_array($this->request->param('action'), ['login', 'logout', 'register'])) {
$user = $this->Auth->User();
if (!empty($user)) {
if ($this->isAdmin($user)) {
return $this->redirect(['prefix' => 'admin', 'controller' => 'Users', 'action' => 'index']);
}
}
}
// Allow users to register and logout.
// You should not add the "login" action to allow list. Doing so would
// cause problems with normal functioning of AuthComponent.
$this->Auth->allow(['register', 'logout']);
if (in_array($this->request->param('action'), ['register', 'login'])) {
$this->layout = 'form';
}
if (in_array($this->request->param('action'), ['index', 'update_info', 'view'])) {
$this->layout = 'dashboard';
}
if (strcmp($this->request->params['action'], 'login') === 0) {
if (!empty($user)) {
return $this->_goToDashboard($user);
}
}
}
protected function _goToDashboard($user)
{
// debug($user); die;
// debug($this->isAdmin($user)); die;
// if ($this->isAdmin($user)) {
// return $this->redirect(['prefix' => 'admin', 'controller' => 'Home', 'action' => 'index']);
// } else {
// return $this->redirect($this->Auth->redirectUrl());
// }
return $this->redirect('/');
}
/**
* Index method
*
* @return void
*/
public function index()
{
// $this->layout = 'dashboard';
$this->paginate = [
'contain' => ['Roles']
];
$user = $this->Users->get($this->Auth->User('id'), [
'contain' => ['Roles', 'Comments', 'Posts']
]);
$this->set('user', $user);
$this->set('users', $this->paginate($this->Users));
$this->set('_serialize', ['users']);
$this->loadModel('Posts');
$post = $this->Posts->newEntity();
$draft_posts = $this->Users->Posts->find('all', [
'conditions' => [
'Posts.user_id' => $this->Auth->User('id'),
'Posts.status' => 0
]
]);
$published_posts = $this->Users->Posts->find('all', [
'conditions' => [
'Posts.user_id' => $this->Auth->User('id'),
'Posts.status' => 3
]
]);
$this->set(compact('post', 'draft_posts', 'published_posts'));
}
/**
* View method
*
* @param string|null $id User id.
* @return void
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function view()
{
$id = $this->Auth->User('id');
$user = $this->Users->get($id, [
'contain' => ['Roles', 'Comments', 'Posts']
]);
$this->set('user', $user);
$this->set('_serialize', ['user']);
}
public function login()
{
// $this->layout = 'form';
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
$this->Flash->success(__('Chào bạn, <strong>' . $user['username'] . '</strong>', ['escape' => false]));
return $this->_goToDashboard($user);
}
$this->Flash->error(__('Thông tin đăng nhập không đúng. <br> Bạn vui lòng đăng nhập lại!'));
}
}
public function logout()
{
return $this->redirect($this->Auth->logout());
}
/**
* Register method
*
* @return void Redirects on successful register, renders view otherwise.
*/
public function register()
{
if ($this->isLoggedIn()) {
return $this->redirect(['controller' => 'Users', 'action' => 'index']);
} else {
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
$user['role_id'] = 3; //Role = User
$user['created_at'] = $user['updated_at'] = Time::now();
if ($this->Users->save($user)) {
$this->Flash->success('Bạn đã đăng ký tài khoản thành công!');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('Đã có lỗi xảy ra, bạn vui lòng đăng ký lại! <br> Rất xin lỗi vì sự bất tiện này!');
}
}
// $roles = $this->Users->Roles->find('list', ['limit' => 200]);
// $this->set(compact('user', 'roles'));
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
}
/**
* Update info method
*
* @param string|null $id User id.
* @return void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function update_info()
{
$id = $this->Auth->User('id');
$user = $this->Users->get($id);
if ($this->request->is(['patch', 'post', 'put'])) {
$update_data = $this->request->data;
$new_password = $update_data['new_password'];
$confirm_password = $update_data['confirm_password'];
$dph = new DefaultPasswordHasher();
if (!$dph->check($update_data['current_password'], $user['password'])) {
$this->Flash->error('Mật khẩu của bạn không chính xác. <br> Vui lòng thực hiện lại!');
} else {
//Kiểm tra password mới
if (empty($new_password)) {
if (!empty($confirm_password)) {
$this->Flash->error('Bạn chưa nhập password mới.');
}
} else {
if (empty($confirm_password)) {
$this->Flash->error('Bạn chưa xác nhận password mới.');
} else {
if (strcmp($new_password, $confirm_password) !== 0) {
$this->Flash->error('Chuỗi xác nhận không trùng với password mới. <br> Vui lòng kiểm tra lại.');
} else {
$update_data['password'] = $dph->hash($update_data['new_password']);
$update_data['updated_at'] = Time::now();
$user = $this->Users->patchEntity($user, $update_data);
if ($this->Users->save($user)) {
$this->Flash->success('Thông tin của bạn đã được cập nhật!');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('Cập nhật thông tin không thành công. Bạn vui lòng thử lại sau!');
}
}
}
}
}
}
// $roles = $this->Users->Roles->find('list', ['limit' => 200]);
// $this->set(compact('user', 'roles'));
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
/**
* Delete method
*
* @param string|null $id User id.
* @return void Redirects to index.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$user = $this->Users->get($id);
if ($this->Users->delete($user)) {
$this->Flash->success('The user has been deleted.');
} else {
$this->Flash->error('The user could not be deleted. Please, try again.');
}
return $this->redirect(['action' => 'index']);
}
// public function add()
// {
// $user = $this->Users->newEntity($this->request->data);
// if ($this->request->is('post')) {
// if ($this->request->data['avatar'])
// $user->avatar = $this->Upload->finalFile;
// if ($this->Users->save($user)) {
// $this->Flash->success('The user has been saved.');
// return $this->redirect(['action' => 'index']);
// } else {
// $this->Flash->error('The user could not be saved. Please, try again.');
// }
// }
// $this->set(compact('user'));
// }
}