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

pictcode / app / Controller / UsersController.php @ 787484d2

履歴 | 表示 | アノテート | ダウンロード (11.206 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' => 'error'));
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
                                if($this->Auth->user('status') == 1){
223
                                        $this->redirect('/Users/login_top');                          
224
                                }else{
225
                                $this->Flash->loginerror('まだ本登録が完了していません。送られてきたメールを見てね!', array(
226
                                    'key' => 'positive',
227
                                        ));
228
                                }
229
                        } else {
230
                                $this->Flash->loginerror('ニックネームか パスワードにまちがいが あるよ!', array(
231
                                    'key' => 'positive',
232
                                        ));
233
                        }
234
                }
235
        }        
236
/**
237
 * logout methods
238
 *
239
 * @throws NotFoundException
240
 * @param string $id
241
 * @return void
242
 */
243
        public function logout() {
244
            $this->redirect($this->Auth->logout());
245
        }
246

    
247

    
248
/**
249
 * login_top method
250
 *
251
 */
252
        public function login_top() {
253
        }
254

    
255

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

    
285

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

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

    
319
        }
320

    
321

    
322

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

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

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

    
349
            }
350
        }
351

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

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

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

    
387
        }
388

    
389

    
390

    
391
}
392

    
393