統計
| ブランチ: | リビジョン:

pictcode / app / Controller / UsersController.php @ d37b000c

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