OSDN Git Service

バージョン記載追加
[trpgtools-onweb/cake-frame.git] / app / controllers / users_controller.php
1 <?php
2 /**
3  * PHP version 5
4  *
5  * @category Controller
6  * @package  Chara-Shee!
7  * @version  beta
8  * @author   Cake <cake_67@users.sourceforge.jp>
9  * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
10  * @link     http://trpgtools-onweb.sourceforge.jp/
11  */
12 class UsersController extends ModuleController {
13
14         var $name = 'Users';
15         var $helpers = array(
16                 'Group',
17                 'Select',
18                 'Time',
19         );
20
21         var $components = array(
22                 'Mail',
23         );
24
25         var $disableTokenActions = array('add','mobile_add');
26
27         // listView用のpagenate設定
28         var $paginate = array(
29                 'conditions' => array(
30                 ),
31                 'fields' => array(
32                         'User.id',
33                         'User.group_id',
34                         'User.name',
35                         'User.modified',
36                 ),
37                 'recursive' => 1,
38                 'contain' => array(
39                         'Character',
40                 ),
41                 'limit' => 20,
42                 'order' => array(
43                         'User.id' => 'asc'
44                 ),
45         );
46
47         function beforeFilter() {
48
49                 parent::beforeFilter();
50
51                 // Cache
52                 $this->cacheAction = array(
53                         'view/' => Configure::read('Cache.expire'),
54                         'listview' => Configure::read('Cache.expire'),
55                 );
56
57                 // 認証なしアクセス可
58                 $this->AuthPlus->allow('index');
59                 $this->AuthPlus->allow('view');
60                 $this->AuthPlus->allow('add');
61                 $this->AuthPlus->allow('regist_end');
62                 $this->AuthPlus->allow('logout');
63                 $this->AuthPlus->allow('mobile_login');
64                 $this->AuthPlus->allow('mobile_add'); //todo:メール認証etc
65                 $this->AuthPlus->allow('mobile_logout');
66                 $this->AuthPlus->allow('get_user');
67         }
68
69         /* PC */
70         function login() {
71                 // 現在のセッション削除
72                 $this->AuthPlus->deleteAuth();
73
74                 $this->pageTitle .= " - ". __('Login', true);
75         }
76
77         function logout() {
78                 self::_logout();
79         }
80
81         function regist_end() {
82                 $this->pageTitle .= " - ". __('Regist Mail', true);
83         }
84
85         function index() {
86                 // ユーザ情報表示
87                 if ($this->AuthPlus->user()) {
88                         $name = $this->user['User']['name'];
89                         self::_index();
90                 } else {
91                         $name = __('Guest', true);
92                 }
93                 $this->pageTitle .= " - ". sprintf(__("%s's Home", true), $name);
94
95                 // 全体の情報
96                 self::_view($this->user_id);
97         }
98
99         function view($id = null) {
100                 if ($id && $id == $this->AuthPlus->user("id")) {
101                         $this->redirect(array('action'=>'index'));
102                 }
103
104                 $user = $this->getUser($id);
105                 if (!$user) {
106                         $this->redirect(array('action'=>'listview'));
107                 }
108                 $this->set('target_user', $user);
109                 $this->pageTitle .= " - ". sprintf(__("%s's Home", true), $user['User']['name']);
110
111                 if (!empty($this->user_id) && $this->user_id == $id) {
112                         $this->set('isOwner', true);
113                 } else {
114                         $this->set('isOwner', false);
115                 }
116
117                 self::_view($id);
118         }
119
120         function listview() {
121
122                 self::_listview();
123         }
124
125         function add($code = null) {
126                 // 新規登録停止
127                 if (!$this->site_configs['Site.newUserRegist']['value']) {
128                         $this->Session->setFlash(__('NO AVAILABLE NOW.', true));
129                         $this->redirect(array('action'=>'index'));
130                 }
131
132                 // メールアドレス登録必須
133                 if ($this->site_configs['Site.registMail']['value']) {
134                         if (empty($code)) {
135                                 $this->redirect(array('controller' => 'regist_mails', 'action'=>'add'));
136                         }
137
138                         $registData = $this->{$this->modelClass}->getRegistData4code($code);
139                         // $codeに該当するデータなし、user_idに紐づいている
140                         if (empty($registData) || !empty($registData['RegistMail']['user_id'])) {
141                                 $this->Session->setFlash(__('Invalid URL.', true));
142                                 $this->redirect(array('controller' => 'users', 'action'=>'login'));
143                         }
144                 }
145
146                 if (!empty($this->data)) {
147                         self::_add($registData);
148                 }
149
150                 $this->set('code', $code);
151                 $this->set('idLength', Configure::read('User.UserId.Length'));
152                 $this->set('passwordLength', Configure::read('User.Password.Length'));
153
154                 $this->pageTitle .= " - ". __('Add New User', true);
155         }
156
157         function edit() {
158                 // デモモードチェック
159                 $this->_check_demo();
160
161                 $id = $this->AuthPlus->user("id");
162
163                 self::_edit($id);
164
165                 $this->pageTitle .= " - ". __('Edit User', true);
166         }
167
168         function edit_image() {
169                 $id = $this->AuthPlus->user("id");
170                 $user = $this->getUser($id);
171
172                 if (!empty($this->data)) {
173                         // 重複アップロードチェック
174                         if (count($this->data['Attachment']) > 2) {
175                                 $this->Session->setFlash(__('Invalid data.', true));
176                                 $this->redirect(array('action'=>'edit_image'));
177                         }
178                         if (count($user['Attachment']) == 1) {
179                                 if (!empty($this->data['Attachment'][0])) {
180                                         $this->Session->setFlash(__('Invalid data.', true));
181                                         $this->redirect(array('action'=>'edit_image'));
182                                 }
183                         }
184
185                         $this->data['User']['id'] = $id;
186
187                         $fieldList = array(
188                                 'user_id',
189                                 'model',
190                                 'foreign_key',
191                                 'dirname',
192                                 'basename',
193                                 'checksum',
194                                 'size',
195                                 'group',
196                                 'alternative',
197                                 'file',
198                         );
199
200                         if ($this->User->saveAll($this->data, array(
201                                 'validate' => 'first',
202                                 'fieldList' => $fieldList
203                         ))) {
204                                 $this->Session->setFlash(__('The User image has been saved', true));
205                                 $this->redirect(array('action'=>'edit_image'));
206                         } else {
207                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
208                                 $this->data['User']['id'] = $id;
209                         }
210                 } else {
211                         $this->data = $user;
212                 }
213
214                 $this->pageTitle .= " - ". __('Edit Image', true);
215         }
216
217         function edit_mail($code = null) {
218                 if (empty($code)) {
219                                 $this->Session->setFlash(__('Invalid URL.', true));
220                         $this->redirect(array('controller' => 'users', 'action'=>'login'));
221                 }
222                 $registData = $this->{$this->modelClass}->getRegistData4code($code);
223                 // $codeに該当するデータなし、user_idが異なる
224                 if (empty($registData) || empty($registData['RegistMail']['user_id']) || $registData['RegistMail']['user_id'] != $this->user_id) {
225                         $this->Session->setFlash(__('Invalid URL.', true));
226                         $this->redirect(array('controller' => 'users', 'action'=>'login'));
227                 }
228
229                 $this->pageTitle .= " - ". __('Regist Mail', true);
230                 self::_edit_mail($registData);
231         }
232
233         function change_password() {
234                 // デモモードチェック
235                 $this->_check_demo();
236
237                 $id = $this->AuthPlus->user("id");
238                 self::_change_password($id);
239
240                 $this->set('passwordLength', Configure::read('User.Password.Length'));
241
242                 $this->pageTitle .= " - ". __('Change Password', true);
243         }
244
245         function delete() {
246                 // デモモードチェック
247                 $this->_check_demo();
248
249                 $id = $this->AuthPlus->user("id");
250
251                 $this->pageTitle .= " - ". __('Delete Your Account', true);
252
253                 self::_delete($id);
254         }
255
256         /* 携帯側 */
257         function mobile_login() {
258                 // 現在のセッション削除
259                 $this->AuthPlus->deleteAuth();
260         }
261
262         function mobile_logout() {
263                 self::_logout();
264         }
265         
266         function mobile_index() {
267                 // ユーザ情報表示
268                 if ($this->AuthPlus->user()) {
269                         $name = $this->user['User']['name'];
270
271                         self::_index();
272                 } else {
273                         $name = __('Guest', true);
274                 }
275                 $this->pageTitle .= " - ". sprintf(__("%s's Home", true), $name);
276         }
277         function mobile_view($id = null) {
278                 $user = $this->getUser($id);
279                 if (!$user) {
280                         $this->redirect(array('action'=>'listview'));
281                 }
282                 $this->set('target_user', $user);
283
284                 self::_view($id);
285         }
286
287         function mobile_listview() {
288
289                 self::_listview();
290         }
291
292         function mobile_add() {
293                 if (!empty($this->data)) {
294                         self::_add();
295                 }
296
297                 $this->set('idLength', Configure::read('User.UserId.Length'));
298                 $this->set('passwordLength', Configure::read('User.Password.Length'));
299         }
300
301         function mobile_edit() {
302                 $id = $this->AuthPlus->user("id");
303
304                 self::_edit($id);
305         }
306
307         function mobile_change_password() {
308                 $id = $this->AuthPlus->user("id");
309
310                 self::_change_password($id);
311
312                 $this->set('passwordLength', Configure::read('User.Password.Length'));
313         }
314
315         function mobile_delete() {
316                 $id = $this->AuthPlus->user("id");
317
318                 self::_delete($id);
319         }
320
321         /* 管理画面 */
322         function admin_index() {
323                 $this->redirect(array('controller' => 'users', 'action'=>'admin_listview'));
324         }
325
326         function admin_listview() {
327
328                 // 検索処理
329                 $searchword = array();
330                 if (!empty($this->data)) {
331                         if (isset($this->data['User']['name'])) {
332                                 $name = $this->data['User']['name'];
333                         }
334                 } else {
335                         if (isset($this->passedArgs['name'])) {
336                                 $name = urldecode($this->passedArgs['name']);
337                         }
338                 }
339
340                 if (isset($name)) {
341                         $this->data['User']['name'] = $name;
342                         $this->paginate['conditions']['User.name LIKE'] = '%'.Sanitize::html($name).'%';
343                         $searchword['name'] = urlencode($name);
344                 }
345                 $this->set('searchword', $searchword);
346
347                 // 非公開キャラクター取得
348                 unset($this->User->hasMany['Character']['conditions']['Character.public_flag']);
349
350                 // group設定
351                 unset($this->User->hasMany['Character']['conditions']['Character.public_flag']);
352
353                 $this->paginate['fields'] = array_merge($this->paginate['fields'], array(
354                         'User.username',
355                         'User.url',
356                         'User.pcmail',
357                         'User.mobile_mail',
358                         'User.useragent',
359                         'User.host',
360                         'User.created',
361                 ));
362
363                 self::_listview(true);
364
365         }
366
367         function admin_view($id = null) {
368                 if (!$id) {
369                         $this->Session->setFlash(__('Invalid User.', true));
370                         $this->redirect(array('controller' => 'users', 'action'=>'index'));
371                 }
372
373                 $user = $this->getUser($id, true);
374                 if (!$user) {
375                         $this->redirect(array('action'=>'listview'));
376                 }
377                 $user['User'] = $this->decrypt_mail($user['User']);
378
379                 $this->set('target_user', $user);
380
381                 $this->pageTitle .= " - ". $user['User']['name'];
382         }
383
384         function admin_add() {
385                 if (!empty($this->data)) {
386                         self::_add();
387                 }
388
389                 $this->set('idLength', Configure::read('User.UserId.Length'));
390                 $this->set('passwordLength', Configure::read('User.Password.Length'));
391
392                 $this->pageTitle .= " - ". __('Add New User', true);
393         }
394
395         function admin_change_password($id = null) {
396
397                 if (!$id && empty($this->data)) {
398                         $this->Session->setFlash(__('Invalid Id.', true));
399                         $this->redirect(array('controller' => 'users', 'action'=>'index'));
400                 }
401                 if (!empty($this->data)) {
402                         $result = self::_exe_change_password($id);
403                         if ($result) {
404                                 $this->Session->setFlash(__('The password has been changed.', true));
405                                 $this->redirect(array('controller' => 'users', 'action'=>'index'));
406                         }
407                 }
408                 if (empty($this->data)) {
409                         $this->data = $this->User->read(null, $id);
410                 }
411
412                 $this->set('passwordLength', Configure::read('User.Password.Length'));
413                 $this->set('target_user', $this->User->read(null, $id));
414
415                 $this->pageTitle .= " - ". __('Change Password', true);
416         }
417
418         function admin_delete($id = null) {
419                 if (!$id) {
420                         $this->Session->setFlash(__('Invalid Id.', true));
421                         $this->redirect(array('controller' => 'users', 'action'=>'index'));
422                 }
423                 if ($this->data) {
424                         $result = self::_exe_delete($id, true);
425                         if ($result) {
426                                 $this->redirect(array('controller' => 'users', 'action'=>'index'));
427                         }
428                 }
429
430                 $user = $this->getUser($id, true, true);
431                 if (!$user) {
432                         $this->Session->setFlash(__('No User', true));
433                         $this->redirect(array('controller' => 'users', 'action'=>'listview'));
434                 }
435                 $this->set('target_user', $user);
436
437                 $this->data['User']['id'] = $id;
438
439                 $this->pageTitle .= " - ". __('Delete User', true);
440         }
441
442         /* Users共通メソッド */
443         function _logout() {
444                 // キャッシュ削除
445                 $this->User->deleteCache4User();
446
447                 $this->Session->setFlash(__('Logout.', true));
448                 $this->redirect($this->AuthPlus->logout());
449         }
450
451         function _index() {
452                 $user = $this->getUser($this->User->id);
453                 $this->set('target_user', $user);
454         }
455
456         function _view($id = null) {
457                 // 指定ユーザの情報
458                 // characters
459                 $target_user_characters = array();
460                 if (!empty($id)) {
461                         // characters
462                         $target_user_characters = $this->_get_characters_list4user_id($id, array(), 5, array(), array('System'));
463                 }
464                 $this->set('target_user_characters', $target_user_characters);
465
466                 // 全体の情報
467                 $public_characters = array();
468                 if (empty($id) || ($id == $this->user_id)) {
469                         // characters
470                         $public_characters = $this->_get_characters_list4user_id(null, array(), 5, array(), array('System', 'User'));
471                 }
472                 $this->set('public_characters', $public_characters);
473
474                 $this->set_public_flag4view();
475         }
476
477         function _listview($isAdmin = false) {
478                 if ($isAdmin === true) {
479                         $this->paginate['conditions']['User.group_id'] = array(
480                                 Configure::read('Group.admin'),
481                                 Configure::read('Group.subadmin'),
482                                 Configure::read('Group.watcher'),
483                                 Configure::read('Group.member'),
484                                 Configure::read('Group.locked'),
485                                 Configure::read('Group.pre'),
486                         );
487                 } else {
488                         $this->paginate['conditions']['User.group_id'] = array(
489                                 Configure::read('Group.admin'),
490                                 Configure::read('Group.subadmin'),
491                                 Configure::read('Group.watcher'),
492                                 Configure::read('Group.member'),
493                                 Configure::read('Group.locked'),
494                         );
495                 }
496                 $users = $this->paginate();
497
498                 if (!empty($users)) {
499                         foreach ($users as $k => $v) {
500                                 // アドレス復号化
501                                 if (isset($v['User']['pcmail']) && !empty($v['User']['pcmail'])) {
502                                         $users[$k]['User']['pcmail'] = $this->Crypt->decrypt($v['User']['pcmail']);
503                                 }
504
505                                 // 全キャラクター数取得
506                                 if (empty($v['Character'])) {
507
508                                         $users[$k]['User']['character_num'] = 0;
509                                 } else {
510                                         $users[$k]['User']['character_num'] = $this->User->Character->find('count', array(
511                                                 'conditions' => array_merge($this->User->hasMany['Character']['conditions'],
512                                                         array('Character.user_id' => $v['User']['id'])),
513                                                 'recursive' => -1,
514                                         ));
515                                 }
516                         }
517
518                         $users = $this->HtmlEscape->nl_unescape($users);
519                 }
520                 $this->set('users', $users);
521
522                 $this->pageTitle .= " - ". __('List Users', true);
523         }
524
525         function _add($registData = array()) {
526                 if (!$this->isAdmin) {
527                         $this->data['User']['group_id'] = Configure::read('Group.member');
528                         $this->AuthPlus->logout();
529                 }
530
531                 // アドレス登録必須設定
532                 if ($this->site_configs['Site.registMail']['value'] && !$this->isAdmin) {
533                         if (empty($registData)) {
534                                 $this->redirect(array('controller' => 'regist_mails', 'action'=>'add'));
535                         }
536
537                         if (!empty($registData)) {
538                                 $this->data['User']['pcmail'] = $this->Crypt->decrypt($registData['RegistMail']['mail']);
539                         }
540                 }
541
542                 // バリデーション
543                 $this->User->set($this->data);
544                 if ($this->User->validates()) {
545                         $fieldList = $this->User->fields['add']; 
546
547                         // ACL設定(デフォルト:一般ユーザ)
548                         if (!isset($this->data['User']['group_id'])) {
549                                 $this->data['User']['group_id'] = Configure::read('Group.member');
550                         }
551
552                         // passwordセット
553                         $this->data['User']['password'] = $this->AuthPlus->password($this->data['User']['password1']);
554
555                         // pcmail
556                         if (!$this->isMobile) {
557                                 $fieldList = array_merge($fieldList, array('pcmail'));
558                                 $this->data['User']['pcmail'] = $this->Crypt->crypt($this->data['User']['pcmail']);
559                         }
560
561                         // 他データ
562                         if (!$this->isAdmin) {
563                                 $this->data['User']['useragent'] = $_SERVER["HTTP_USER_AGENT"];
564                                 $this->data['User']['host'] = gethostbyaddr($_SERVER["REMOTE_ADDR"]);
565                         }
566
567                         // save
568                         $this->User->create();
569                         if ($this->User->save($this->data, array('fieldList' => $fieldList, 'validate' => false))) {
570                                 if (!$this->isAdmin) {
571                                         if ($this->site_configs['Site.registMail']['value']) {
572                                                 if (!empty($registData)) {
573                                                         $this->{$this->modelClass}->deleteRegistData4mail($registData['RegistMail']['mail']);
574                                                 }
575                                         }
576
577                                         $this->Session->setFlash(__('The User has been saved. Please Login.', true));
578                                         $this->redirect(array('controller' => 'users', 'action'=>'index'));
579                                 } else {
580                                         $this->Session->setFlash(__('The User has been saved', true));
581                                         
582                                         $this->redirect(array('controller' => 'users', 'action'=>'view', $this->User->id));
583                                 }
584                         } else {
585                                 $this->Session->setFlash(__('The User cannot be saved.', true));
586                         }
587                 }
588                 if (isset($this->User->validationErrors['pcmail'])) {
589                         $this->Session->setFlash($this->validationErrors['pcmail']);
590                         if (!empty($registData)) {
591                                 $this->{$this->modelClass}->deleteRegistData4mail($registData['RegistMail']['mail']);
592                         }
593
594
595                         $this->redirect(array('controller' => 'users', 'action'=>'index'));
596                 }
597         }
598
599         function _edit($id) {
600                 if (!empty($this->data)) {
601                         $this->data['User']['id'] = $id;
602                         if ($this->User->save($this->data)) {
603                                 $this->Session->setFlash(__('The User has been saved', true));
604                                 $this->redirect(array('action'=>'index'));
605                         } else {
606                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
607                         }
608                 }
609                 if (empty($this->data)) {
610                         $this->data = $this->User->read(null, $id);
611
612                         $this->data['User'] = $this->_restore_html_user($this->data['User'], true);
613                 }
614         }
615
616         function _edit_mail($registData = array()) {
617                 if ($this->isMobile) {
618                         exit;
619                 } else {
620                         $data['User']['pcmail'] = $registData['RegistMail']['mail'];
621                         $fields = array('pcmail');
622                 }
623
624                 // バリデーション
625                 $data['User']['id'] = $this->user_id;
626                 if ($this->User->save($data, array('fieldList' => $fields, 'validate' => false))) {
627                                 $this->{$this->modelClass}->deleteRegistData4mail($registData['RegistMail']['mail']);
628
629                                 $this->Session->setFlash(__('The User has been saved', true));
630                                 $this->redirect(array('action'=>'index'));
631                 }
632
633                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
634                 $this->redirect(array('action'=>'index'));
635         }
636
637         function _change_password($id) {
638                 if (!empty($this->data)) {
639                         $data = $this->User->read('password', $id);
640                         if ($data['User']['password'] != $this->AuthPlus->password($this->data['User']['password'])) {
641                                 $this->Session->setFlash(__('Old Password is wrong.', true));
642                         } else {
643                                 $result = self::_exe_change_password($id);
644                                 if ($result) {
645                                         $this->Session->setFlash(__('The password has been changed. Please login at new password.', true));
646                                         $this->redirect($this->AuthPlus->logout());
647                                 }
648                         }
649                 }
650         }
651         function _exe_change_password($id) {
652                 // バリデーション
653                 $this->User->set($this->data);
654                 if ($this->User->validates()) {
655                         $this->data['User']['id'] = $id;
656
657                         // passwordセット
658                         $this->data[$this->AuthPlus->userModel]['password'] = $this->AuthPlus->password($this->data[$this->AuthPlus->userModel]['password1']);
659
660                         if ($this->User->save($this->data, array('fieldList' => array('password')))) {
661                                 return true;
662                         } else {
663                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
664                                 return false;
665                         }
666                 }
667         }
668
669         function _delete($id) {
670                 if ($this->data) {
671                         $data = $this->User->read('password', $id);
672                         if ($data['User']['password'] != $this->AuthPlus->password($this->data['User']['password'])) {
673                                 $this->Session->setFlash(__('Password is wrong.', true));
674                         } else {
675                                 $result = self::_exe_delete($id, false);
676                                 if ($result) {
677                                         $this->redirect($this->AuthPlus->logout());
678                                 } else {
679                                         $this->redirect(array('action'=>'index'));
680                                 }
681                         }
682                 }
683         }
684         function _exe_delete($id, $isAdmin=false) {
685                 $user = $this->getUser($id, $isAdmin, true);
686                 if (!$user) {
687                         return false;
688                 }
689                 if ($user['User']['group_id'] == Configure::read('Group.admin')) {
690                         $this->Session->setFlash(__('Super Administrator can not be deleted.', true));
691                         return false;
692                 }
693
694                 if ($isAdmin) {
695                         $this->data['User']['id'] = $id;
696                         $this->data['User']['group_id'] = Configure::read('Group.deleted');
697
698                         $this->User->create();
699                         $result = $this->User->save(
700                                         $this->data,
701                                         array(
702                                                 'validate' => false,
703                                                 'fieldList' => array(
704                                                         'group_id',
705                                                 ),
706                                         )
707                         );
708                 } else {
709                         $result = $this->User->del($id);
710                 }
711
712                 if ($result) {
713                         if (!empty($user['Character'])) {
714                                 App::import('Controller', 'Characters');
715                                 $this->CharactersController = new CharactersController;
716                                 $this->CharactersController->Character = CorePlus::set_model('Character');
717                                 $this->CharactersController->Session = new SessionComponent;
718                                 $this->CharactersController->data['Character'] = $this->data['Character'];
719
720                                 foreach ($user['Character'] as $character) {
721                                         $this->CharactersController->_delete($character['id'], array(), true);
722                                 }
723                         }
724                         $this->Session->setFlash(sprintf(__('%s was deleted.', true), $user['User']['name']));
725                         return true;
726                 } else {
727                         $this->Session->setFlash(__('The account could not be deleted.', true));
728                         return false;
729                 }
730         }
731
732         // ログインユーザ取得
733         function get_user() {
734                 return $this->user;
735         }
736 }
737