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

pictcode / app / Controller / UsersController.php @ d37b000c

履歴 | 表示 | アノテート | ダウンロード (11.481 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');
26
        // public $components = array('Paginator','Recaptcha.Recaptcha');
27

    
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

    
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
                $name = $this->Session->read('register.User.login_id');
92
                $mail = $this->Session->read('register.User.email');
93
        // ユーザアクティベート(本登録)用URLの作成 DSはスラッシュの意味
94
        $url = 
95
            DS . 'users' .          // コントローラ
96
            DS . 'activate' .                       // アクション
97
            DS . $this->User->id .                  // ユーザID
98
            DS . $this->User->activationHash();  // ハッシュ値
99
        $url = Router::url( $url, true);  // ドメイン(+サブディレクトリ)を付与
100
                $comment = $url;
101

    
102
                $Email = new CakeEmail();
103
                $Email->charset('ISO-2022-JP');
104
                $Email->emailFormat('text');
105
                $Email->template('register_mail');
106
                $Email->viewVars(array('name'=>$name,'comment'=>$comment));
107
                $Email->from( MAIL_FROM ); //MAIL_FROM:Config/const.phpにて定義
108
                $Email->to($mail);
109
                $Email->subject('【PictCode】仮登録が完了しました。');
110
                $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
                $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
            if ($this->User->exists() && $in_hash == $this->User->activationHash()) {
131
            // 本登録に有効なURL
132
                // statusフィールドを1に更新
133
                $this->User->saveField( 'status', 1);
134
                $this->Session->setFlash( 'Your account has been activated.');
135
                        
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
            }else{
149
            // 本登録に無効なURL
150
                $this->Session->setFlash( 'Invalid activation URL');
151
                        return $this->redirect(array('controller' => 'top', 'action' => 'error'));
152
            }
153
        }
154
        
155
/**
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

    
197
/**
198
 * register confirm
199
 */
200
        public function withdraw_check() {
201
                // if (!$this->User->exists()) {
202
                //         return $this->redirect(array('controller' => 'top','action' => 'index'));
203
                // }
204
        }
205

    
206
/**
207
 * delete method
208
 *
209
 * @throws NotFoundException
210
 * @param string $id
211
 * @return void
212
 */
213
        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
        }
218

    
219
/**
220
 * login method
221
 *
222
 * @throws NotFoundException
223
 * @param string $id
224
 * @return void
225
 */
226
        public function login() {
227
                if($this->Auth->user('status') == 1){
228
                                        $this->redirect('/users/login_top');                          
229
                }
230
                if ($this->request->is('post')) {
231
                        if ($this->Auth->login()) {
232
                                if($this->Auth->user('status') == 1){
233
                                        $this->redirect('/Users/login_top');                          
234
                                }else{
235
                                $this->Flash->loginerror('まだ本登録が完了していません。送られてきたメールを見てね!', array(
236
                                    'key' => 'positive',
237
                                        ));
238
                                }
239
                        } else {
240
                                $this->Flash->loginerror('ニックネームか パスワードにまちがいが あるよ!', array(
241
                                    'key' => 'positive',
242
                                        ));
243
                        }
244
                }
245
        }        
246
/**
247
 * logout methods
248
 *
249
 * @throws NotFoundException
250
 * @param string $id
251
 * @return void
252
 */
253
        public function logout() {
254
            $this->redirect($this->Auth->logout());
255
        }
256

    
257

    
258
/**
259
 * login_top method
260
 *
261
 */
262
        public function login_top() {
263
        }
264

    
265

    
266
/**
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
                        // }else{
277
                        //         $this->Flash->loginerror('メールアドレスが一致しません。誤りがないかもう一度ご確認の上、正確にご入力ください。', array(
278
                        //             'key' => 'positive',
279
                        //                 ));
280
                        }
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
                $options = array('conditions' => array('User.email' => $mail, 'User.status' => 1));
307
                $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
            DS . $this->User->getActivationHash($user['User']['id']);  // ハッシュ値
316
        $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
                $Email->subject('【PictCode】パスワードの再発行を受け付けました。');
326
                $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
            if ($this->User->exists() && $in_hash == $this->User->activationHash()) {
342
                    //idとハッシュ値が正規の場合、パスワード変更画面を表示
343
                    if ($this->request->is(array('post', 'put'))) {
344

    
345
                                $this->set('user', $this->request->data);
346
                                if ($this->User->save($this->request->data)) {
347
                                        return $this->redirect(array('action' => 'reset_pwd_comp_mail',$user_id));
348
                                } 
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
            }else{
355
                    //idとハッシュ値が不正の場合、トップページにリダイレクト
356
                        $this->Session->setFlash( '無効なURLです');
357
                        return $this->redirect(array('controller' => 'top', 'action' => 'index'));
358

    
359
            }
360
        }
361

    
362
/**
363
 * 
364
 */
365
        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
                        $mail = $this->Session->read('register.User.email');
378

    
379
                        $Email = new CakeEmail();
380
                        $Email->charset('ISO-2022-JP');
381
                        $Email->emailFormat('text');
382
                        $Email->template('comp_reset_pwd');
383
                        // $Email->viewVars(array('name'=>$name,'comment'=>$comment));
384
                        $Email->from( MAIL_FROM ); //MAIL_FROM:Config/const.phpにて定義
385
                        $Email->to($mail);
386
                        $Email->subject('【PictCode】パスワードの再設定が完了しました。');
387
                        $Email->send();
388
            }
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
        }
398

    
399

    
400

    
401
}
402

    
403