-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCurrentLibUser.php
More file actions
187 lines (169 loc) · 4.25 KB
/
CurrentLibUser.php
File metadata and controls
187 lines (169 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* NetCommonsの機能に必要な情報を取得する内容をまとめたUtility
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('LibAppObject', 'NetCommons.Lib');
App::uses('AuthComponent', 'Controller/Component');
App::uses('UserAttributeChoice', 'UserAttributes.Model');
/**
* NetCommonsの機能に必要な情報(ユーザ)を取得する内容をまとめたUtility
*
* @property Controller $_controller コントローラ
* @property Language $Language Languageモデル
* @property PluginsRole $PluginsRole PluginsRoleモデル
* @property Plugin $Plugin Pluginモデル
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\NetCommons\Utility
*/
class CurrentLibUser extends LibAppObject {
/**
* 使用するモデル
*
* @var array
*/
public $uses = [
'User' => 'Users.User'
];
/**
* 一度取得したユーザデータを保持
*
* @var array|null
*/
private $__user = null;
/**
* インスタンスの取得
*
* @return CurrentLibUser
*/
public static function getInstance() {
return parent::_getInstance(__CLASS__);
}
/**
* インスタンスのクリア
*
* @return void
*/
public static function resetInstance() {
parent::_resetInstance(__CLASS__);
}
/**
* コントローラのセット
*
* @param Controller $controller コントローラ
* @return void
*/
public function initialize($controller = null) {
parent::initialize($controller);
if (! empty($this->_controller->Auth)) {
$this->__user = $this->_controller->Auth->user();
} else {
$this->__user = $this->_controller->Session->read(AuthComponent::$sessionKey);
}
if ($this->isLoginChanged()) {
$this->renewSessionUser();
}
}
/**
* ログイン情報が変わっているか否か
*
* @return bool
*/
public function isLoginChanged() {
$sessionUser = $this->__user;
if (! $sessionUser || !isset($sessionUser['modified'])) {
return false;
}
$latestUser = $this->User->find('first', array(
'recursive' => -1,
'fields' => ['modified'],
'conditions' => array(
'User.id' => $sessionUser['id'],
),
'callbacks' => false,
));
if ($latestUser &&
$latestUser[$this->User->alias]['modified'] !== $sessionUser['modified']) {
return true;
} else {
return false;
}
}
/**
* セッションのログイン情報を再登録する
*
* @return void
*/
public function renewSessionUser() {
$sessionUser = $this->_controller->Session->read(AuthComponent::$sessionKey);
$changeUser = $this->User->find('first', array(
'recursive' => 0,
'conditions' => array(
'User.id' => $sessionUser['id'],
//'User.modified !=' => $sessionUser['modified'],
),
'callbacks' => false,
));
if ($changeUser) {
$status = (string)$changeUser['User']['status'];
if ($status === UserAttributeChoice::STATUS_CODE_ACTIVE) {
foreach ($changeUser['User'] as $key => $value) {
$this->_controller->Session->write(AuthComponent::$sessionKey . '.' . $key, $value);
}
unset($changeUser['User']);
foreach ($changeUser as $key => $value) {
$this->_controller->Session->write(AuthComponent::$sessionKey . '.' . $key, $value);
}
} else {
$this->_controller->Session->delete(AuthComponent::$sessionKey);
}
}
$this->__user = $this->_controller->Session->read(AuthComponent::$sessionKey);
}
/**
* ログイン情報の取得
*
* @return array
*/
public function getLoginUser() {
return $this->__user;
}
/**
* ログインしているユーザIDの取得
*
* @return string|null 数値の文字列
*/
public function getLoginUserId() {
if (isset($this->__user['id'])) {
return $this->__user['id'];
} else {
return null;
}
}
/**
* ログインしているユーザのロールキーの取得
*
* @return string|null
*/
public function getLoginUserRoleKey() {
if (isset($this->__user['role_key'])) {
return $this->__user['role_key'];
} else {
return null;
}
}
/**
* ログインしているか否か
*
* @return bool
*/
public function isLogined() {
return isset($this->__user['id']);
}
}