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

pictcode / app / Controller / UsersController.php @ 26d1f852

履歴 | 表示 | アノテート | ダウンロード (11.067 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','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
/**
189
 * register confirm
190
 */
191
        public function withdraw_check() {
192
                // if (!$this->User->exists()) {
193
                //         return $this->redirect(array('controller' => 'top','action' => 'index'));
194
                // }
195
        }
196

    
197
/**
198
 * delete method
199
 *
200
 * @throws NotFoundException
201
 * @param string $id
202
 * @return void
203
 */
204
        public function withdraw_comp() {
205
            $this->User->id = $this->Auth->user('id');
206
            $this->User->saveField( 'status', 0);
207
            $this->Session->write('Auth', $user);
208
        }
209

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

    
248

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

    
256

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

    
286

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

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

    
320
        }
321

    
322

    
323

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

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

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

    
350
            }
351
        }
352

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

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

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

    
388
        }
389

    
390

    
391

    
392
}
393

    
394