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