pictcode / app / Controller / UsersController.php @ 1368d528
履歴 | 表示 | アノテート | ダウンロード (11.438 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 | 304d523f | hasse | $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 | 16e57cae | hasse | if ($this->User->exists() && $in_hash == $this->User->activationHash()) { |
| 130 | 04e657a7 | root | // 本登録に有効なURL
|
| 131 | 8fa10255 | hasse | // statusフィールドを1に更新
|
| 132 | $this->User->saveField( 'status', 1); |
||
| 133 | 04e657a7 | root | $this->Session->setFlash( 'Your account has been activated.'); |
| 134 | 16e57cae | hasse | |
| 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 | 04e657a7 | root | }else{
|
| 148 | // 本登録に無効なURL
|
||
| 149 | $this->Session->setFlash( 'Invalid activation URL'); |
||
| 150 | 787484d2 | hasse | return $this->redirect(array('controller' => 'top', 'action' => 'error')); |
| 151 | 04e657a7 | root | } |
| 152 | } |
||
| 153 | |||
| 154 | 5ec4ad9d | admin | /**
|
| 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 | 0803fa25 | hasse | |
| 196 | /**
|
||
| 197 | * register confirm
|
||
| 198 | */
|
||
| 199 | public function withdraw_check() { |
||
| 200 | 26d1f852 | hasse | // if (!$this->User->exists()) {
|
| 201 | // return $this->redirect(array('controller' => 'top','action' => 'index'));
|
||
| 202 | // }
|
||
| 203 | 0803fa25 | hasse | } |
| 204 | |||
| 205 | 5ec4ad9d | admin | /**
|
| 206 | * delete method
|
||
| 207 | *
|
||
| 208 | * @throws NotFoundException
|
||
| 209 | * @param string $id
|
||
| 210 | * @return void
|
||
| 211 | */
|
||
| 212 | 26d1f852 | hasse | 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 | 5ec4ad9d | admin | } |
| 217 | |||
| 218 | /**
|
||
| 219 | * login method
|
||
| 220 | *
|
||
| 221 | * @throws NotFoundException
|
||
| 222 | * @param string $id
|
||
| 223 | * @return void
|
||
| 224 | */
|
||
| 225 | public function login() { |
||
| 226 | 304d523f | hasse | if($this->Auth->user('status') == 1){ |
| 227 | 16e57cae | hasse | $this->redirect('/users/login_top'); |
| 228 | 5ec4ad9d | admin | } |
| 229 | if ($this->request->is('post')) { |
||
| 230 | if ($this->Auth->login()) { |
||
| 231 | 67acbfb5 | hasse | if($this->Auth->user('status') == 1){ |
| 232 | 8fa10255 | hasse | $this->redirect('/Users/login_top'); |
| 233 | }else{
|
||
| 234 | $this->Flash->loginerror('まだ本登録が完了していません。送られてきたメールを見てね!', array( |
||
| 235 | 'key' => 'positive', |
||
| 236 | )); |
||
| 237 | } |
||
| 238 | 5ec4ad9d | admin | } else {
|
| 239 | b3a58ce1 | hasse | $this->Flash->loginerror('ニックネームか パスワードにまちがいが あるよ!', array( |
| 240 | 'key' => 'positive', |
||
| 241 | )); |
||
| 242 | 5ec4ad9d | admin | } |
| 243 | } |
||
| 244 | } |
||
| 245 | /**
|
||
| 246 | ceb21f43 | hasse | * logout methods
|
| 247 | 5ec4ad9d | admin | *
|
| 248 | * @throws NotFoundException
|
||
| 249 | * @param string $id
|
||
| 250 | * @return void
|
||
| 251 | */
|
||
| 252 | public function logout() { |
||
| 253 | 16e57cae | hasse | $this->redirect($this->Auth->logout()); |
| 254 | 5ec4ad9d | admin | } |
| 255 | 04e657a7 | root | |
| 256 | |||
| 257 | ceb21f43 | hasse | /**
|
| 258 | * login_top method
|
||
| 259 | *
|
||
| 260 | */
|
||
| 261 | public function login_top() { |
||
| 262 | } |
||
| 263 | |||
| 264 | |||
| 265 | 67acbfb5 | hasse | /**
|
| 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 | 304d523f | hasse | // }else{
|
| 276 | // $this->Flash->loginerror('メールアドレスが一致しません。誤りがないかもう一度ご確認の上、正確にご入力ください。', array(
|
||
| 277 | // 'key' => 'positive',
|
||
| 278 | // ));
|
||
| 279 | 67acbfb5 | hasse | } |
| 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 | 16e57cae | hasse | $options = array('conditions' => array('User.email' => $mail, 'User.status' => 1)); |
| 306 | 67acbfb5 | hasse | $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 | 16e57cae | hasse | DS . $this->User->getActivationHash($user['User']['id']); // ハッシュ値 |
| 315 | 67acbfb5 | hasse | $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 | 16e57cae | hasse | $Email->subject('【PictCode】パスワードの再発行を受け付けました。'); |
| 325 | 67acbfb5 | hasse | $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 | 16e57cae | hasse | if ($this->User->exists() && $in_hash == $this->User->activationHash()) { |
| 341 | //idとハッシュ値が正規の場合、パスワード変更画面を表示
|
||
| 342 | 67acbfb5 | hasse | if ($this->request->is(array('post', 'put'))) { |
| 343 | |||
| 344 | $this->set('user', $this->request->data); |
||
| 345 | if ($this->User->save($this->request->data)) { |
||
| 346 | 16e57cae | hasse | return $this->redirect(array('action' => 'reset_pwd_comp_mail',$user_id)); |
| 347 | 67acbfb5 | hasse | } |
| 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 | 16e57cae | hasse | }else{
|
| 354 | //idとハッシュ値が不正の場合、トップページにリダイレクト
|
||
| 355 | $this->Session->setFlash( '無効なURLです'); |
||
| 356 | return $this->redirect(array('controller' => 'top', 'action' => 'index')); |
||
| 357 | 67acbfb5 | hasse | |
| 358 | 16e57cae | hasse | } |
| 359 | 67acbfb5 | hasse | } |
| 360 | |||
| 361 | /**
|
||
| 362 | *
|
||
| 363 | */
|
||
| 364 | 16e57cae | hasse | 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 | 67acbfb5 | hasse | $mail = $this->Session->read('register.User.email'); |
| 377 | 16e57cae | hasse | |
| 378 | 67acbfb5 | hasse | $Email = new CakeEmail(); |
| 379 | $Email->charset('ISO-2022-JP'); |
||
| 380 | $Email->emailFormat('text'); |
||
| 381 | $Email->template('comp_reset_pwd'); |
||
| 382 | 16e57cae | hasse | // $Email->viewVars(array('name'=>$name,'comment'=>$comment));
|
| 383 | 67acbfb5 | hasse | $Email->from( MAIL_FROM ); //MAIL_FROM:Config/const.phpにて定義 |
| 384 | $Email->to($mail); |
||
| 385 | 16e57cae | hasse | $Email->subject('【PictCode】パスワードの再設定が完了しました。'); |
| 386 | 67acbfb5 | hasse | $Email->send();
|
| 387 | 16e57cae | hasse | } |
| 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 | 67acbfb5 | hasse | } |
| 397 | |||
| 398 | |||
| 399 | 04e657a7 | root | |
| 400 | 5ec4ad9d | admin | } |
| 401 | 04e657a7 | root |