pictcode / app / Controller / UsersController.php @ 304d523f
履歴 | 表示 | アノテート | ダウンロード (11.438 KB)
1 |
<?php
|
---|---|
2 |
App::uses('AppController', 'Controller'); |
3 |
/**
|
4 |
* Users Controller
|
5 |
*
|
6 |
* @property User $User
|
7 |
* @property PaginatorComponent $Paginator
|
8 |
*/
|
9 |
class UsersController extends AppController { |
10 |
|
11 |
public $layout = 'procedure'; |
12 |
public $name = 'users'; |
13 |
public $uses = array('User'); |
14 |
|
15 |
public function beforeFilter() { |
16 |
parent::beforeFilter();
|
17 |
$this->Auth->allow('register','activate','confirm','sent','login','reset_pwd','reset_pwd_confirm','reset_pwd_sent','newpwd','reset_pwd_comp_mail','withdraw_comp'); |
18 |
} |
19 |
|
20 |
/**
|
21 |
* Components
|
22 |
*
|
23 |
* @var array
|
24 |
*/
|
25 |
public $components = array('Paginator','Recaptcha.Recaptcha'); |
26 |
|
27 |
/**
|
28 |
* index method
|
29 |
*
|
30 |
* @return void
|
31 |
*/
|
32 |
public function index() { |
33 |
$this->User->recursive = 0; |
34 |
$this->set('users', $this->Paginator->paginate()); |
35 |
} |
36 |
|
37 |
/**
|
38 |
* view method
|
39 |
*
|
40 |
* @throws NotFoundException
|
41 |
* @param string $id
|
42 |
* @return void
|
43 |
*/
|
44 |
public function view($id = null) { |
45 |
if (!$this->User->exists($id)) { |
46 |
throw new NotFoundException(__('Invalid user')); |
47 |
} |
48 |
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id)); |
49 |
$this->set('user', $this->User->find('first', $options)); |
50 |
} |
51 |
|
52 |
|
53 |
/**
|
54 |
* register method
|
55 |
*
|
56 |
* @return void
|
57 |
*/
|
58 |
public function register() { |
59 |
if($this->request->is('post') || $this->request->is('put')){ |
60 |
$this->User->set($this->request->data); |
61 |
if($this->User->validates()){ |
62 |
$this->Session->write('register',$this->request->data); |
63 |
$this->redirect(array('action'=>'confirm')); |
64 |
}else{
|
65 |
} |
66 |
} |
67 |
} |
68 |
|
69 |
/**
|
70 |
* register confirm
|
71 |
*/
|
72 |
public function confirm() { |
73 |
if($this->Session->read('register')){ |
74 |
$this->set('register',$this->Session->read('register')); |
75 |
}else{
|
76 |
$this->redirect(array('action'=>'register')); |
77 |
} |
78 |
} |
79 |
|
80 |
|
81 |
/**
|
82 |
* register sent
|
83 |
*/
|
84 |
public function sent() { |
85 |
// if (!empty( $this->data)){
|
86 |
// // 保存
|
87 |
if( $this->User->save($this->Session->read('register'))){ |
88 |
// メール送信
|
89 |
$this->set('register',$this->Session->read('register')); |
90 |
$name = $this->Session->read('register.User.login_id'); |
91 |
$mail = $this->Session->read('register.User.email'); |
92 |
// ユーザアクティベート(本登録)用URLの作成 DSはスラッシュの意味
|
93 |
$url =
|
94 |
DS . 'users' . // コントローラ |
95 |
DS . 'activate' . // アクション |
96 |
DS . $this->User->id . // ユーザID |
97 |
DS . $this->User->activationHash(); // ハッシュ値 |
98 |
$url = Router::url( $url, true); // ドメイン(+サブディレクトリ)を付与 |
99 |
$comment = $url; |
100 |
|
101 |
$Email = new CakeEmail(); |
102 |
$Email->charset('ISO-2022-JP'); |
103 |
$Email->emailFormat('text'); |
104 |
$Email->template('register_mail'); |
105 |
$Email->viewVars(array('name'=>$name,'comment'=>$comment)); |
106 |
$Email->from( MAIL_FROM ); //MAIL_FROM:Config/const.phpにて定義 |
107 |
$Email->to($mail); |
108 |
$Email->subject('【PictCode】仮登録が完了しました。'); |
109 |
$Email->send();
|
110 |
} |
111 |
|
112 |
} |
113 |
|
114 |
/**
|
115 |
* register activate
|
116 |
*/
|
117 |
public function activate( $user_id = null, $in_hash = null) { |
118 |
// UserモデルにIDをセット
|
119 |
$this->User->id = $user_id; |
120 |
$mail = $this->Session->read('register.User.email'); |
121 |
|
122 |
$options = array('conditions' => array('User.email' => $mail, 'User.status' => 1)); |
123 |
$user = $this->User->find('count', $options); |
124 |
if($user > 0){ |
125 |
// 本登録に無効なURL
|
126 |
$this->Session->setFlash( 'このメールアドレスは登録済みです'); |
127 |
return $this->redirect(array('controller' => 'top', 'action' => 'error')); |
128 |
} |
129 |
if ($this->User->exists() && $in_hash == $this->User->activationHash()) { |
130 |
// 本登録に有効なURL
|
131 |
// statusフィールドを1に更新
|
132 |
$this->User->saveField( 'status', 1); |
133 |
$this->Session->setFlash( 'Your account has been activated.'); |
134 |
|
135 |
// exit;
|
136 |
|
137 |
$Email = new CakeEmail(); |
138 |
$Email->charset('ISO-2022-JP'); |
139 |
$Email->emailFormat('text'); |
140 |
$Email->template('register_comp'); |
141 |
// $Email->viewVars(array('name'=>$name,'comment'=>$comment));
|
142 |
$Email->from( MAIL_FROM ); //MAIL_FROM:Config/const.phpにて定義 |
143 |
$Email->to($mail); |
144 |
$Email->subject('【PictCode】本登録が完了しました。'); |
145 |
$Email->send();
|
146 |
|
147 |
}else{
|
148 |
// 本登録に無効なURL
|
149 |
$this->Session->setFlash( 'Invalid activation URL'); |
150 |
return $this->redirect(array('controller' => 'top', 'action' => 'error')); |
151 |
} |
152 |
} |
153 |
|
154 |
/**
|
155 |
* add method
|
156 |
*
|
157 |
* @return void
|
158 |
*/
|
159 |
public function add() { |
160 |
if ($this->request->is('post')) { |
161 |
$this->User->create(); |
162 |
if ($this->User->save($this->request->data)) { |
163 |
$this->Flash->success(__('The user has been saved.')); |
164 |
return $this->redirect(array('action' => 'index')); |
165 |
} else {
|
166 |
$this->Flash->error(__('The user could not be saved. Please, try again.')); |
167 |
} |
168 |
} |
169 |
} |
170 |
|
171 |
/**
|
172 |
* edit method
|
173 |
*
|
174 |
* @throws NotFoundException
|
175 |
* @param string $id
|
176 |
* @return void
|
177 |
*/
|
178 |
public function edit($id = null) { |
179 |
if (!$this->User->exists($id)) { |
180 |
throw new NotFoundException(__('Invalid user')); |
181 |
} |
182 |
if ($this->request->is(array('post', 'put'))) { |
183 |
if ($this->User->save($this->request->data)) { |
184 |
$this->Flash->success(__('The user has been saved.')); |
185 |
return $this->redirect(array('action' => 'index')); |
186 |
} else {
|
187 |
$this->Flash->error(__('The user could not be saved. Please, try again.')); |
188 |
} |
189 |
} else {
|
190 |
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id)); |
191 |
$this->request->data = $this->User->find('first', $options); |
192 |
} |
193 |
} |
194 |
|
195 |
|
196 |
/**
|
197 |
* register confirm
|
198 |
*/
|
199 |
public function withdraw_check() { |
200 |
// if (!$this->User->exists()) {
|
201 |
// return $this->redirect(array('controller' => 'top','action' => 'index'));
|
202 |
// }
|
203 |
} |
204 |
|
205 |
/**
|
206 |
* delete method
|
207 |
*
|
208 |
* @throws NotFoundException
|
209 |
* @param string $id
|
210 |
* @return void
|
211 |
*/
|
212 |
public function withdraw_comp() { |
213 |
$this->User->id = $this->Auth->user('id'); |
214 |
$this->User->saveField( 'status', 0); |
215 |
$this->Session->write('Auth', $user); |
216 |
} |
217 |
|
218 |
/**
|
219 |
* login method
|
220 |
*
|
221 |
* @throws NotFoundException
|
222 |
* @param string $id
|
223 |
* @return void
|
224 |
*/
|
225 |
public function login() { |
226 |
if($this->Auth->user('status') == 1){ |
227 |
$this->redirect('/users/login_top'); |
228 |
} |
229 |
if ($this->request->is('post')) { |
230 |
if ($this->Auth->login()) { |
231 |
if($this->Auth->user('status') == 1){ |
232 |
$this->redirect('/Users/login_top'); |
233 |
}else{
|
234 |
$this->Flash->loginerror('まだ本登録が完了していません。送られてきたメールを見てね!', array( |
235 |
'key' => 'positive', |
236 |
)); |
237 |
} |
238 |
} else {
|
239 |
$this->Flash->loginerror('ニックネームか パスワードにまちがいが あるよ!', array( |
240 |
'key' => 'positive', |
241 |
)); |
242 |
} |
243 |
} |
244 |
} |
245 |
/**
|
246 |
* logout methods
|
247 |
*
|
248 |
* @throws NotFoundException
|
249 |
* @param string $id
|
250 |
* @return void
|
251 |
*/
|
252 |
public function logout() { |
253 |
$this->redirect($this->Auth->logout()); |
254 |
} |
255 |
|
256 |
|
257 |
/**
|
258 |
* login_top method
|
259 |
*
|
260 |
*/
|
261 |
public function login_top() { |
262 |
} |
263 |
|
264 |
|
265 |
/**
|
266 |
* reset_pwd
|
267 |
*/
|
268 |
public function reset_pwd() { |
269 |
$this->User->validate = $this->User->reset_pwd_validate; |
270 |
if ($this->request->is(array('post', 'put'))) { |
271 |
$this->User->set($this->request->data); |
272 |
if($this->User->validates()){ |
273 |
$this->Session->write('register',$this->request->data); |
274 |
$this->redirect(array('action'=>'reset_pwd_confirm')); |
275 |
// }else{
|
276 |
// $this->Flash->loginerror('メールアドレスが一致しません。誤りがないかもう一度ご確認の上、正確にご入力ください。', array(
|
277 |
// 'key' => 'positive',
|
278 |
// ));
|
279 |
} |
280 |
} |
281 |
} |
282 |
|
283 |
/**
|
284 |
* reset_pwd confirm
|
285 |
*/
|
286 |
public function reset_pwd_confirm() { |
287 |
if($this->Session->read('register')){ |
288 |
$this->set('register',$this->Session->read('register')); |
289 |
}else{
|
290 |
$this->redirect(array('action'=>'reset_pwd')); |
291 |
} |
292 |
} |
293 |
|
294 |
|
295 |
/**
|
296 |
* register sent
|
297 |
*/
|
298 |
public function reset_pwd_sent() { |
299 |
// if (!empty( $this->data)){
|
300 |
// // 保存
|
301 |
if($this->Session->read('register')){ |
302 |
// メール送信
|
303 |
$this->set('register',$this->Session->read('register')); |
304 |
$mail = $this->Session->read('register.User.email'); |
305 |
$options = array('conditions' => array('User.email' => $mail, 'User.status' => 1)); |
306 |
$user = $this->User->find('first', $options); |
307 |
$name = $user['User']['login_id']; |
308 |
|
309 |
// ユーザアクティベート(本登録)用URLの作成 DSはスラッシュの意味
|
310 |
$url =
|
311 |
DS . 'users' . // コントローラ |
312 |
DS . 'newpwd' . // アクション |
313 |
DS . $user['User']['id'] . // ユーザID |
314 |
DS . $this->User->getActivationHash($user['User']['id']); // ハッシュ値 |
315 |
$url = Router::url( $url, true); // ドメイン(+サブディレクトリ)を付与 |
316 |
$comment = $url; |
317 |
$Email = new CakeEmail(); |
318 |
$Email->charset('ISO-2022-JP'); |
319 |
$Email->emailFormat('text'); |
320 |
$Email->template('user_reset_pwd'); |
321 |
$Email->viewVars(array('name'=>$name,'comment'=>$comment)); |
322 |
$Email->from( MAIL_FROM ); //MAIL_FROM:Config/const.phpにて定義 |
323 |
$Email->to($mail); |
324 |
$Email->subject('【PictCode】パスワードの再発行を受け付けました。'); |
325 |
$Email->send();
|
326 |
} |
327 |
|
328 |
} |
329 |
|
330 |
|
331 |
|
332 |
/**
|
333 |
* new password
|
334 |
*/
|
335 |
public function newpwd( $user_id = null, $in_hash = null) { |
336 |
$this->User->validate = $this->User->new_pwd_validate; |
337 |
|
338 |
// UserモデルにIDをセット
|
339 |
$this->User->id = $user_id; |
340 |
if ($this->User->exists() && $in_hash == $this->User->activationHash()) { |
341 |
//idとハッシュ値が正規の場合、パスワード変更画面を表示
|
342 |
if ($this->request->is(array('post', 'put'))) { |
343 |
|
344 |
$this->set('user', $this->request->data); |
345 |
if ($this->User->save($this->request->data)) { |
346 |
return $this->redirect(array('action' => 'reset_pwd_comp_mail',$user_id)); |
347 |
} |
348 |
} else {
|
349 |
$options = array('conditions' => array('User.id' => $user_id,'User.status' => 1)); |
350 |
$this->request->data = $this->User->find('first', $options); |
351 |
$this->set('user', $this->request->data); |
352 |
} |
353 |
}else{
|
354 |
//idとハッシュ値が不正の場合、トップページにリダイレクト
|
355 |
$this->Session->setFlash( '無効なURLです'); |
356 |
return $this->redirect(array('controller' => 'top', 'action' => 'index')); |
357 |
|
358 |
} |
359 |
} |
360 |
|
361 |
/**
|
362 |
*
|
363 |
*/
|
364 |
public function reset_pwd_comp_mail($user_id = null) { |
365 |
// $options = array('conditions' => array('User.' . $this->User->primaryKey => $user_id));
|
366 |
// $this->request->data = $this->User->find('first', $options);
|
367 |
// //バリデーションを無効にして保存
|
368 |
// $this->User->validate = $this->User->reset_pwd_comp_mail_validate;
|
369 |
|
370 |
// if( $this->User->save($this->request->data)){
|
371 |
// var_dump($this->request->data['User']);
|
372 |
// exit;
|
373 |
if( !isset($this->request->data['User'])){ |
374 |
// $this->User->save($this->request->data,false);
|
375 |
// メール送信
|
376 |
$mail = $this->Session->read('register.User.email'); |
377 |
|
378 |
$Email = new CakeEmail(); |
379 |
$Email->charset('ISO-2022-JP'); |
380 |
$Email->emailFormat('text'); |
381 |
$Email->template('comp_reset_pwd'); |
382 |
// $Email->viewVars(array('name'=>$name,'comment'=>$comment));
|
383 |
$Email->from( MAIL_FROM ); //MAIL_FROM:Config/const.phpにて定義 |
384 |
$Email->to($mail); |
385 |
$Email->subject('【PictCode】パスワードの再設定が完了しました。'); |
386 |
$Email->send();
|
387 |
} |
388 |
else{
|
389 |
$options = array('conditions' => array('User.' . $this->User->primaryKey => $user_id)); |
390 |
$this->request->data = $this->User->find('first', $options); |
391 |
//バリデーションを無効にして保存
|
392 |
// $this->User->validate = $this->User->reset_pwd_comp_mail_validate;
|
393 |
$this->User->save($this->request->data,false); |
394 |
} |
395 |
|
396 |
} |
397 |
|
398 |
|
399 |
|
400 |
} |
401 |
|
402 |
|