forked from hantsy/angularjs-cakephp-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsersController.php
More file actions
175 lines (161 loc) · 5.15 KB
/
Copy pathUsersController.php
File metadata and controls
175 lines (161 loc) · 5.15 KB
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
<?php
App::uses('AppController', 'Controller');
App::uses('AuthComponent', 'Controller/Component');
/**
* Users Controller
*
* @property User $User
* @property PaginatorComponent $Paginator
*/
class UsersController extends AppController {
/**
* Components
*
* @var array
*/
public $components = array('Paginator', 'RequestHandler');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add', 'login');
}
public function login() {
if ($this->Session->read('Auth.User')) {
$this->set(array(
'message' => array(
'text' => __('You are logged in!'),
'type' => 'error'
),
'_serialize' => array('message')
));
}
// $username = env('PHP_AUTH_USER');
// $pass = env('PHP_AUTH_PW');
//echo "userpass", $username, $pass;
if ($this->request->is('get')) {
if ($this->Auth->login()) {
// return $this->redirect($this->Auth->redirect());
$this->set(array(
'user' => $this->Session->read('Auth.User'),
'_serialize' => array('user')
));
// $this->response->send();
} else {
$this->set(array(
'message' => array(
'text' => __('Invalid username or password, try again'),
'type' => 'error'
),
'_serialize' => array('message')
));
$this->response->statusCode(401);
// $this->response->send();
}
}
//$this->Session->setFlash(__('Invalid username or password, try again'));
}
public function logout() {
if ($this->Auth->logout()) {
$this->set(array(
'message' => array(
'text' => __('Logout successfully'),
'type' => 'info'
),
'_serialize' => array('message')
));
}
}
/**
* index method
*
* @return void
*/
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->Paginator->paginate());
}
/**
* view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function view($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
}
/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->set(array(
'message' => array(
'text' => __('Registered successfully'),
'type' => 'info'
),
'_serialize' => array('message')
));
} else {
$this->set(array(
'message' => array(
'text' => __('The user could not be saved. Please, try again.'),
'type' => 'error'
),
'_serialize' => array('message')
));
$this->response->statusCode(400);
}
}
}
/**
* edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
// public function edit($id = null) {
// if (!$this->User->exists($id)) {
// throw new NotFoundException(__('Invalid user'));
// }
// if ($this->request->is(array('post', 'put'))) {
// if ($this->User->save($this->request->data)) {
// $this->Session->setFlash(__('The user has been saved.'));
// return $this->redirect(array('action' => 'index'));
// } else {
// $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
// }
// } else {
// $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
// $this->request->data = $this->User->find('first', $options);
// }
// }
/**
* delete method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
// public function delete($id = null) {
// $this->User->id = $id;
// if (!$this->User->exists()) {
// throw new NotFoundException(__('Invalid user'));
// }
// $this->request->onlyAllow('post', 'delete');
// if ($this->User->delete()) {
// $this->Session->setFlash(__('The user has been deleted.'));
// } else {
// $this->Session->setFlash(__('The user could not be deleted. Please, try again.'));
// }
// return $this->redirect(array('action' => 'index'));
// }
}