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

pictcode / app / Controller / UsersController.php @ 16e57cae

履歴 | 表示 | アノテート | ダウンロード (11.434 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');
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
            if ($this->User->exists() && $in_hash == $this->User->activationHash()) {
121
            // 本登録に有効なURL
122
                // statusフィールドを1に更新
123
                $this->User->saveField( 'status', 1);
124
                $this->Session->setFlash( 'Your account has been activated.');
125
                        
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
            }else{
140
            // 本登録に無効なURL
141
                $this->Session->setFlash( 'Invalid activation URL');
142
                        return $this->redirect(array('controller' => 'top', 'action' => 'index'));
143
            }
144
        }
145
        
146
/**
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
/**
188
 * delete method
189
 *
190
 * @throws NotFoundException
191
 * @param string $id
192
 * @return void
193
 */
194
        public function delete($id = null) {
195
                $this->User->id = $id;
196
                if (!$this->User->exists()) {
197
                        throw new NotFoundException(__('Invalid user'));
198
                }
199
                $this->request->allowMethod('post', 'delete');
200
                if ($this->User->delete()) {
201
                        $this->Flash->success(__('The user has been deleted.'));
202
                } else {
203
                        $this->Flash->error(__('The user could not be deleted. Please, try again.'));
204
                }
205
                return $this->redirect(array('action' => 'index'));
206
        }
207

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

    
250

    
251
/**
252
 * login_top method
253
 *
254
 */
255
        public function login_top() {
256
        }
257

    
258

    
259
/**
260
 * reset_pwd
261
 */
262
        public function reset_pwd() {
263
                $this->User->validate = $this->User->reset_pwd_validate;
264
                if ($this->request->is(array('post', 'put'))) {
265
                        $this->User->set($this->request->data);
266
                        if($this->User->validates()){
267
                                $this->Session->write('register',$this->request->data);
268
                                $this->redirect(array('action'=>'reset_pwd_confirm'));
269
                        }else{
270
                                $this->Flash->loginerror('メールアドレスが一致しません。誤りがないかもう一度ご確認の上、正確にご入力ください。', array(
271
                                    'key' => 'positive',
272
                                        ));
273
                        }
274
                }
275
        }
276
        
277
/**
278
 * reset_pwd confirm
279
 */
280
        public function reset_pwd_confirm() {
281
                if($this->Session->read('register')){
282
                        $this->set('register',$this->Session->read('register'));
283
                }else{
284
                        $this->redirect(array('action'=>'reset_pwd'));
285
                }
286
        }
287

    
288

    
289
/**
290
 * register sent
291
 */
292
        public function reset_pwd_sent() {
293
                // if (!empty( $this->data)){
294
         //        //  保存
295
            if($this->Session->read('register')){
296
                    //  メール送信
297
                $this->set('register',$this->Session->read('register'));
298
                $mail = $this->Session->read('register.User.email');
299
                $options = array('conditions' => array('User.email' => $mail, 'User.status' => 1));
300
                $user = $this->User->find('first', $options);
301
                $name = $user['User']['login_id'];
302

    
303
        // ユーザアクティベート(本登録)用URLの作成 DSはスラッシュの意味
304
        $url = 
305
            DS . 'users' .          // コントローラ
306
            DS . 'newpwd' .                       // アクション
307
            DS . $user['User']['id'] .                  // ユーザID
308
            DS . $this->User->getActivationHash($user['User']['id']);  // ハッシュ値
309
        $url = Router::url( $url, true);  // ドメイン(+サブディレクトリ)を付与
310
                $comment = $url;
311
                $Email = new CakeEmail();
312
                $Email->charset('ISO-2022-JP');
313
                $Email->emailFormat('text');
314
                $Email->template('user_reset_pwd');
315
                $Email->viewVars(array('name'=>$name,'comment'=>$comment));
316
                $Email->from( MAIL_FROM ); //MAIL_FROM:Config/const.phpにて定義
317
                $Email->to($mail);
318
                $Email->subject('【PictCode】パスワードの再発行を受け付けました。');
319
                $Email->send();
320
            }
321

    
322
        }
323

    
324

    
325

    
326
/**
327
 * new password
328
 */
329
        public function newpwd( $user_id = null, $in_hash = null) {
330
                $this->User->validate = $this->User->new_pwd_validate;
331

    
332
            // UserモデルにIDをセット
333
            $this->User->id = $user_id;
334
            if ($this->User->exists() && $in_hash == $this->User->activationHash()) {
335
                    //idとハッシュ値が正規の場合、パスワード変更画面を表示
336
                    if ($this->request->is(array('post', 'put'))) {
337

    
338
                                $this->set('user', $this->request->data);
339
                                if ($this->User->save($this->request->data)) {
340
                                        return $this->redirect(array('action' => 'reset_pwd_comp_mail',$user_id));
341
                                } 
342
                        } else {
343
                            $options = array('conditions' => array('User.id' => $user_id,'User.status' => 1));
344
                                $this->request->data = $this->User->find('first', $options);
345
                                $this->set('user', $this->request->data);
346
                        }
347
            }else{
348
                    //idとハッシュ値が不正の場合、トップページにリダイレクト
349
                        $this->Session->setFlash( '無効なURLです');
350
                        return $this->redirect(array('controller' => 'top', 'action' => 'index'));
351

    
352
            }
353
        }
354

    
355
/**
356
 * 
357
 */
358
        public function reset_pwd_comp_mail($user_id = null) {
359
                // $options = array('conditions' => array('User.' . $this->User->primaryKey => $user_id));
360
                // $this->request->data = $this->User->find('first', $options);
361
                // //バリデーションを無効にして保存
362
                // $this->User->validate = $this->User->reset_pwd_comp_mail_validate;
363

    
364
                // if( $this->User->save($this->request->data)){
365
                // var_dump($this->request->data['User']);
366
                // exit;
367
                if( !isset($this->request->data['User'])){
368
                // $this->User->save($this->request->data,false);
369
                    //  メール送信
370
                        $mail = $this->Session->read('register.User.email');
371

    
372
                        $Email = new CakeEmail();
373
                        $Email->charset('ISO-2022-JP');
374
                        $Email->emailFormat('text');
375
                        $Email->template('comp_reset_pwd');
376
                        // $Email->viewVars(array('name'=>$name,'comment'=>$comment));
377
                        $Email->from( MAIL_FROM ); //MAIL_FROM:Config/const.phpにて定義
378
                        $Email->to($mail);
379
                        $Email->subject('【PictCode】パスワードの再設定が完了しました。');
380
                        $Email->send();
381
            }
382
            else{
383
                $options = array('conditions' => array('User.' . $this->User->primaryKey => $user_id));
384
                $this->request->data = $this->User->find('first', $options);
385
                //バリデーションを無効にして保存
386
                // $this->User->validate = $this->User->reset_pwd_comp_mail_validate;
387
                $this->User->save($this->request->data,false);
388
            }
389

    
390
        }
391

    
392

    
393

    
394
}
395

    
396