OSDN Git Service

バージョン記載追加
[trpgtools-onweb/cake-frame.git] / app / controllers / components / auth_plus.php
1 <?php
2 /**
3  * PHP version 5
4  *
5  * @category Component
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 /*
13  * 認証コンポーネント拡張
14  */
15
16 /**
17  * The parent component
18  */
19 App::import('Component', 'Auth');
20
21 class AuthPlusComponent extends AuthComponent {
22
23 /**
24  * The name of the model that represents users which will be authenticated.  Defaults to 'User'.
25  *
26  * @var string
27  * @access public
28  */
29         var $userModel = 'User';
30
31         function initialize(&$controller)
32         {
33                 parent::initialize($controller);
34
35                 // ログイン後リダイレクト設定
36                 if (isset($controller->params["url"]["url"])) {
37                         $this->loginRedirect = $controller->params["url"]["url"];
38                 } else {
39                         $this->loginRedirect = Configure::read('Routing.basePath');
40                 }
41                 if ("/".$this->loginRedirect == Router::normalize(Configure::read('Routing.loginPath'))) {
42                         $this->loginRedirect = Router::normalize(Configure::read('Routing.basePath'));
43                         if (Configure::read('mobileUserAgent')) {
44                                 $this->loginRedirect .= "m/";
45                         }
46                 }
47         }
48
49         function deleteAuth()
50         {
51                 $this->__setDefaults();
52                 $this->Session->del($this->sessionKey);
53                 $this->Session->destroy();
54
55                 $this->_loggedIn = false;
56                 return Router::normalize($this->logoutRedirect);
57         }
58
59         function isAuthorized($type = null, $object = null, $user = null) {
60                 if ($type == 'orig') {
61                         $user = $this->user();
62
63                         // 未ログイン
64                         if (empty($user)) {
65                                 $valid = false;
66                         // 管理者
67                         } elseif (in_array($this->user('group_id'), array(
68                                 Configure::read('Group.admin'), 
69                                 Configure::read('Group.subadmin'), 
70                                 Configure::read('Group.watcher'))
71                         )) {
72                                 $valid = true;
73                         // 一般ユーザ
74                         } elseif ($this->user('group_id') == Configure::read('Group.member')) {
75                                 if (isset($this->params['prefix']) 
76                                         && $this->params['prefix'] == Configure::read('Routing.admin')) {
77                                         $valid = false;
78                                 } else {
79                                         $valid = true;
80
81                                 }
82                         } else {
83                                 $valid = false;
84                         }
85                 
86                 } else {
87                         $valid = parent::isAuthorized($type, $object, $user);
88                 }
89
90                 return $valid;
91         }
92
93         function login($data = null) {
94                 parent::login($data);
95
96                 if ($this->_loggedIn) {
97
98                         $user = $this->user(); 
99
100                         // 二重ログイン対策
101                         if (Configure::read('Session.destroyOldAuth')) {
102                                 $before_sid = $user['User']['sid'];
103                                 $new_sid = $this->Session->id();
104
105                                 if ($before_sid != $new_sid) {
106                                         // 新セッションclose
107                                         session_write_close();
108
109                                         // 前セッション破棄
110                                         $this->Session->__initSession();
111                                         session_id($before_sid);
112                                         session_start();
113                                         session_destroy();
114
115                                         if (Configure::read('Session.save') == 'database') {
116                                                 // session Lib.の__destroy()処理
117                                                 $db =& ConnectionManager::getDataSource(Configure::read('Session.database'));
118                                                 $table = $db->fullTableName(Configure::read('Session.table'));
119                                                 $db->execute("DELETE FROM " . $db->name($table) . " WHERE " . $db->name($table.'.id') . " = " . $before_sid);
120                                         } else {
121                                                 $file = $sessionpath . DS . "sess_$before_sid";
122                                                 @unlink($file);
123                                         }
124
125                                         // 新セッション再Open
126                                         $this->Session->__initSession();
127                                         session_id($new_sid);
128                                         session_start();
129                                 }
130                         }
131
132                         // ログイン時間更新
133                         $model =& $this->getModel();
134                         $model->id = $user['User']['id'];
135                         $model->save(array(
136                                 'useragent' => $_SERVER["HTTP_USER_AGENT"],
137                                 'host' => gethostbyaddr($_SERVER["REMOTE_ADDR"]),
138                                 'sid' => $this->Session->id(),
139                                 )
140                         );
141                 }
142
143                 return $this->_loggedIn;
144         }
145
146         function logout() {
147                 $this->Session->destroy();
148
149                 return parent::logout();
150         }
151
152 }
153
154