OSDN Git Service

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