/
User.php
304 lines (263 loc) · 8.29 KB
/
User.php
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
require_once 'model/om/BaseUser.php';
/**
* @package model
*/
class User extends BaseUser {
public static $ALL_ROLES = null;
private static $CACHED_ADMIN_SETTINGS = null;
private static $ADMIN_SETTINGS_SET = array();
const IS_BACKEND_LOGIN_ENABLED = 'user-backend';
const IS_ADMIN_LOGIN_ENABLED = 'user-admin_area';
const IS_FRONTEND_USER = 'user-frontend';
const IS_ADMIN_USER = 'user-admin';
public function getFullName() {
return implode(' ', $this->getFullNameArray());
}
public function getFullNameInverted($sSeparator=', ') {
return implode($sSeparator=', ', array_reverse($this->getFullNameArray()));
}
public function getFullNameArray() {
$aResult = array();
if($this->getFirstName()) {
$aResult[] = $this->getFirstName();
}
if($this->getLastName()) {
$aResult[] = $this->getLastName();
}
return $aResult;
}
public function getInitials() {
return strtolower(substr($this->getFirstName(),0,1).substr($this->getLastName(),0,1));
}
public function getUserKind() {
if($this->getIsAdmin()) {
return self::IS_ADMIN_USER;
}
if(!$this->getIsBackendLoginEnabled()) {
return self::IS_FRONTEND_USER;
}
if($this->getIsAdminLoginEnabled()) {
return self::IS_ADMIN_LOGIN_ENABLED;
}
return self::IS_BACKEND_LOGIN_ENABLED;
}
public function isFirstAdministrator() {
return (UserPeer::doCount(new Criteria()) === 1)
&& $this->getIsAdmin()
&& !$this->getIsInactive()
&& $this->getIsBackendLoginEnabled();
}
public function requiresUserName() {
return trim($this->getFullName()) === '';
}
public function isSessionUser() {
return $this->getId() === Session::getSession()->getUserId();
}
public function may($mPage, $sRightName) {
if($this->getIsAdmin()) {
return true;
}
foreach($this->allRoles() as $oRole) {
if($oRole->may($mPage, $sRightName)) {
return true;
}
}
return false;
}
public function mayEditPageDetails($mPage) {
return $this->may($mPage, 'edit_page_details');
}
public function mayEditPageContents($mPage) {
return $this->may($mPage, 'edit_page_contents');
}
public function mayEditPageStructure($mPage) {
return $this->may($mPage, 'edit_page_structure');
}
public function mayCreateChildren($mPage) {
return $this->may($mPage, 'create_children');
}
public function mayDelete($mPage) {
return $this->may($mPage, 'delete');
}
public function mayViewPage($mPage) {
return $this->may($mPage, 'view_page');
}
public function mayUseAdminModule($sAdminModuleName, $bCheckEnabled = true) {
return $this->mayUseModuleOfTypeAndName('admin', $sAdminModuleName, $bCheckEnabled);
}
public function mayUseModuleOfTypeAndName($sModuleType, $sModuleName, $bCheckEnabled = true) {
//Case 1: Module is disabled (this check is not mandatory): deny
if($bCheckEnabled && !Module::isModuleEnabled($sModuleType, $sModuleName)) {
return false;
}
//Case 2: User is admin: allow
if($this->getIsAdmin()) {
return true;
}
$aModuleInfo = Module::getModuleInfoByTypeAndName($sModuleType, $sModuleName);
//Case 3: Access to module is unrestricted: allow
if(!isset($aModuleInfo['allowed_roles']) || !is_array($aModuleInfo['allowed_roles'])) {
return true;
}
$aRoleKeys = $aModuleInfo['allowed_roles'];
//Case 4: Access to module is restricted to admins: deny (because the user is not one of them)
if(count($aRoleKeys) === 0) {
return false;
}
//Case 5: Access is restricted to certain roles: allow if in role
$aUserRoles = $this->allRoles();
foreach($aRoleKeys as $sRoleKey) {
if(isset($aUserRoles[$sRoleKey])) {
return true;
}
}
//Case 6: Access is restricted to certain roles: allow if roles with page_rights grant limited access
if($sModuleName === 'pages') {
foreach($aUserRoles as $oRole) {
if($oRole->countRights() > 0) {
return true;
}
}
}
//Case 7: User is not in allowed roles
return false;
}
public function getAdminSettings($sSection, $mDefaultResult = array()) {
if(self::$CACHED_ADMIN_SETTINGS === null) {
self::$CACHED_ADMIN_SETTINGS = $this->allAdminSettings();
}
if(!isset(self::$CACHED_ADMIN_SETTINGS[$sSection])) {
self::$CACHED_ADMIN_SETTINGS[$sSection] = Settings::getSetting(null, $sSection, $mDefaultResult, 'user_defaults');
}
return self::$CACHED_ADMIN_SETTINGS[$sSection];
}
public function setAdminSettings($sSection, $mValue) {
if(self::$CACHED_ADMIN_SETTINGS === null) {
self::$CACHED_ADMIN_SETTINGS = $this->allAdminSettings();
}
self::$CACHED_ADMIN_SETTINGS[$sSection] = $mValue;
self::$ADMIN_SETTINGS_SET[$sSection] = true;
$aToSave = array();
foreach(self::$ADMIN_SETTINGS_SET as $sSetSection => $bTrue) {
$aToSave[$sSetSection] = self::$CACHED_ADMIN_SETTINGS[$sSetSection];
}
parent::setBackendSettings(serialize($aToSave));
}
public function resetBackendSettings() {
return parent::setBackendSettings(null);
}
public function getBackendSettings() {
// Never call getBackendSettings directly!
return null;
}
public function setBackendSettings($mSettings) {
throw new Exception('Never call setBackendSettings directly!');
}
private function allAdminSettings() {
$mSettings = parent::getBackendSettings();
if($mSettings === null) {
return array();
}
if(is_resource($mSettings)) {
$mSettings = stream_get_contents($mSettings);
}
return unserialize($mSettings);
}
public function getGroups($bReturnNamesOnly = false) {
$aResult = array();
foreach($this->getUserGroupsRelatedByUserIdJoinGroup() as $oGroupUser) {
if(!$bReturnNamesOnly) {
$aResult[] = $oGroupUser->getGroup();
} else {
$aResult[] = $oGroupUser->getGroup()->getName();
}
}
return $aResult;
}
public function getRoles($bReturnNamesOnly = false) {
$aResult = array();
$aUserRoles = $bReturnNamesOnly ? $this->getUserRolesRelatedByUserId() : $this->getUserRolesRelatedByUserIdJoinRole();
foreach($aUserRoles as $oUserRole) {
if($bReturnNamesOnly) {
$aResult[] = $oUserRole->getRoleKey();
} else {
$aResult[] = $oUserRole->getRole();
}
}
return $aResult;
}
public function allRoles() {
if(self::$ALL_ROLES === null) {
self::$ALL_ROLES = array();
foreach($this->getRoles() as $oRole) {
self::$ALL_ROLES[$oRole->getRoleKey()] = $oRole;
}
foreach($this->getGroups() as $oGroup) {
foreach($oGroup->getRoles() as $oRole) {
self::$ALL_ROLES[$oRole->getRoleKey()] = $oRole;
}
}
}
return self::$ALL_ROLES;
}
public function mayEditUser($oUser = null) {
return UserPeer::mayOperateOn($this, $oUser, 'update');
}
public function getMissingRights($mPage, $bInheritedOnly = false) {
$aResult = null;
foreach($this->allRoles() as $oRole) {
if($aResult === null) {
$aResult = $oGroup->getMissingRights($mPage, $bInheritedOnly);
} else {
$aResult = array_diff($aResult, $oGroup->getMissingRights($mPage, $bInheritedOnly));
}
}
return $aResult;
}
public function getActiveUserGroupIds($bAsString = false) {
$aResult = array();
foreach($this->getUserGroupsRelatedByUserId() as $oUserGroup) {
$aResult[] = $bAsString ? (string) $oUserGroup->getGroupId() : $oUserGroup->getGroupId();
}
return $aResult;
}
public function getActiveUserRoleKeys() {
return $this->getRoles(true);
}
public function hasGroup($mGroup) {
if($mGroup instanceof Group) {
$mGroup = $mGroup->getId();
}
foreach($this->getUserGroupsRelatedByUserId() as $oUserGroup) {
if($oUserGroup->getGroupId() === $mGroup) {
return true;
}
}
return false;
}
public function hasRole($mRole) {
if($mRole instanceof Role) {
$mRole = $mRole->getRoleKey();
}
$aRoles = $this->allRoles();
return isset($aRoles[$mRole]);
}
public function setPassword($sPassword, $cPasswordHashMethod = null) {
if(Settings::getSetting('security', 'generate_digest_secrets', false) === true) {
$this->setDigestHA1(md5($this->getUsername().':'.Session::getRealm().':'.$sPassword));
} else {
$this->setDigestHA1(null);
}
if($cPasswordHashMethod === null) {
$cPasswordHashMethod = array('PasswordHash', 'hashPassword');
}
if($cPasswordHashMethod !== false) {
$sPassword = call_user_func($cPasswordHashMethod, $sPassword);
}
return parent::setPassword($sPassword);
}
public function getLanguageName() {
return StringPeer::getString('language.'.$this->getLanguageId(), null, $this->getLanguageId());
}
}