This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
class.AJXP_SerialUser.php
261 lines (238 loc) · 9.47 KB
/
class.AJXP_SerialUser.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
<?php
/*
* Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
defined('AJXP_EXEC') or die( 'Access not allowed');
/**
* Implementation of the AbstractUser for serial
* @package AjaXplorer_Plugins
* @subpackage Conf
*/
class AJXP_SerialUser extends AbstractAjxpUser
{
public $id;
public $hasAdmin = false;
public $rights;
public $prefs;
public $bookmarks;
public $version;
/**
* Conf Storage implementation
*
* @var AbstractConfDriver
*/
public $storage;
public $registerForSave = array();
public $create = true;
public $childrenPointer = null;
/**
* @param $id
* @param serialConfDriver $storage
*/
public function __construct($id, $storage=null)
{
parent::__construct($id, $storage);
$this->registerForSave = array();
}
public function setGroupPath($groupPath, $update = false)
{
if ($update && isSet($this->groupPath) && $this->groupPath != $groupPath) {
$children = $this->getChildrenPointer();
if (is_array($children)) {
foreach ($children as $userId) {
// UPDATE USER GROUP AND ROLES
$u = ConfService::getConfStorageImpl()->createUserObject($userId);
$u->setGroupPath($groupPath);
$r = $u->getRoles();
// REMOVE OLD GROUP ROLES
foreach (array_keys($r) as $role) {
if(strpos($role, "AJXP_GRP_/") === 0) $u->removeRole($role);
}
$u->recomputeMergedRole();
$u->save("superuser");
}
}
}
parent::setGroupPath($groupPath);
$groups = AJXP_Utils::loadSerialFile(AJXP_VarsFilter::filter($this->storage->getOption("USERS_DIRPATH"))."/groups.ser");
$groups[$this->getId()] = $groupPath;
AJXP_Utils::saveSerialFile(AJXP_VarsFilter::filter($this->storage->getOption("USERS_DIRPATH"))."/groups.ser", $groups);
}
public function __wakeup()
{
$this->registerForSave = array();
}
public function getStoragePath()
{
$subDir = trim($this->getGroupPath(), "/");
$id = $this->getId();
if(AuthService::ignoreUserCase()) $id = strtolower($id);
$res = AJXP_VarsFilter::filter($this->storage->getOption("USERS_DIRPATH"))."/".(empty($subDir)?"":$subDir."/").$id;
return $res;
}
public function storageExists()
{
return is_dir($this->getStoragePath());
}
public function load()
{
$groups = AJXP_Utils::loadSerialFile(AJXP_VarsFilter::filter($this->storage->getOption("USERS_DIRPATH"))."/groups.ser");
if(isSet($groups[$this->getId()])) $this->groupPath = $groups[$this->getId()];
$this->create = false;
$this->rights = AJXP_Utils::loadSerialFile($this->getStoragePath()."/rights.ser");
if(count($this->rights) == 0) $this->create = true;
$this->prefs = AJXP_Utils::loadSerialFile($this->getStoragePath()."/prefs.ser");
$this->bookmarks = AJXP_Utils::loadSerialFile($this->getStoragePath()."/bookmarks.ser");
if (isSet($this->rights["ajxp.admin"]) && $this->rights["ajxp.admin"] === true) {
$this->setAdmin(true);
}
if (isSet($this->rights["ajxp.parent_user"])) {
//$this->setParent($this->rights["ajxp.parent_user"]);
parent::setParent($this->rights["ajxp.parent_user"]);
}
if (isSet($this->rights["ajxp.group_path"])) {
$this->setGroupPath($this->rights["ajxp.group_path"]);
}
if (isSet($this->rights["ajxp.children_pointer"])) {
$this->childrenPointer = $this->rights["ajxp.children_pointer"];
}
// LOAD ROLES
$rolesToLoad = array();
if (isSet($this->rights["ajxp.roles"])) {
$rolesToLoad = array_keys($this->rights["ajxp.roles"]);
}
if ($this->groupPath != null) {
$base = "";
$exp = explode("/", $this->groupPath);
foreach ($exp as $pathPart) {
if(empty($pathPart)) continue;
$base = $base . "/" . $pathPart;
$rolesToLoad[] = "AJXP_GRP_".$base;
}
}
// Load roles
if (count($rolesToLoad)) {
$allRoles = AuthService::getRolesList($rolesToLoad);
foreach ($rolesToLoad as $roleId) {
if (isSet($allRoles[$roleId])) {
$this->roles[$roleId] = $allRoles[$roleId];
$this->rights["ajxp.roles"][$roleId] = true;
} else if (is_array($this->rights["ajxp.roles"]) && isSet($this->rights["ajxp.roles"][$roleId])) {
unset($this->rights["ajxp.roles"][$roleId]);
}
}
}
// LOAD USR ROLE LOCALLY
$personalRole = AJXP_Utils::loadSerialFile($this->getStoragePath()."/role.ser");
if (is_a($personalRole, "AJXP_Role")) {
$this->personalRole = $personalRole;
$this->roles["AJXP_USR_"."/".$this->id] = $personalRole;
} else {
// MIGRATE NOW !
$changes = $this->migrateRightsToPersonalRole();
if ($changes) {
AJXP_Utils::saveSerialFile($this->getStoragePath()."/role.ser", $this->personalRole, true);
AJXP_Utils::saveSerialFile($this->getStoragePath()."/rights.ser", $this->rights, true);
}
}
$this->recomputeMergedRole();
}
protected function _save($context = "superuser")
{
if ($this->isAdmin() === true) {
$this->rights["ajxp.admin"] = true;
} else {
$this->rights["ajxp.admin"] = false;
}
if ($this->hasParent()) {
$this->rights["ajxp.parent_user"] = $this->parentUser;
}
if (isSet($this->childrenPointer)) {
$this->rights["ajxp.children_pointer"] = $this->childrenPointer;
}
$this->rights["ajxp.group_path"] = $this->getGroupPath();
if ($context == "superuser") {
$this->registerForSave["rights"] = true;
}
$this->registerForSave["prefs"] = true;
$this->registerForSave["bookmarks"] = true;
}
public function __destruct()
{
if(count($this->registerForSave)==0) return;
$fastCheck = $this->storage->getOption("FAST_CHECKS");
$fastCheck = ($fastCheck == "true" || $fastCheck == true);
if (isSet($this->registerForSave["rights"]) || $this->create) {
$filteredRights = $this->rights;
if(isSet($filteredRights["ajxp.roles"])) $filteredRights["ajxp.roles"] = $this->filterRolesForSaving($filteredRights["ajxp.roles"]);
AJXP_Utils::saveSerialFile($this->getStoragePath()."/rights.ser", $this->rights, !$fastCheck);
AJXP_Utils::saveSerialFile($this->getStoragePath()."/role.ser", $this->personalRole, !$fastCheck);
}
if (isSet($this->registerForSave["prefs"])) {
AJXP_Utils::saveSerialFile($this->getStoragePath()."/prefs.ser", $this->prefs, !$fastCheck);
}
if (isSet($this->registerForSave["bookmarks"])) {
AJXP_Utils::saveSerialFile($this->getStoragePath()."/bookmarks.ser", $this->bookmarks, !$fastCheck);
}
$this->registerForSave = array();
}
public function getTemporaryData($key)
{
$fastCheck = $this->storage->getOption("FAST_CHECKS");
$fastCheck = ($fastCheck == "true" || $fastCheck == true);
return AJXP_Utils::loadSerialFile($this->getStoragePath()."/".$key.".ser",$fastCheck);
}
public function saveTemporaryData($key, $value)
{
$fastCheck = $this->storage->getOption("FAST_CHECKS");
$fastCheck = ($fastCheck == "true" || $fastCheck == true);
return AJXP_Utils::saveSerialFile($this->getStoragePath()."/".$key.".ser", $value, !$fastCheck);
}
/**
* Override parent method to keep a reference to the child users
* @param $parentId
*/
public function setParent($parentId)
{
$u = ConfService::getConfStorageImpl()->createUserObject($parentId);
$p = $u->getChildrenPointer();
if($p == null) $p = array();
$p[$this->getId()] = $this->getId();
$u->setChildrenPointer($p);
$u->save("superuser");
if (AuthService::getLoggedUser() != null && AuthService::getLoggedUser()->getId() == $parentId) {
AuthService::updateUser($u);
}
parent::setParent($parentId);
}
/**
* @return null|Array
*/
public function getChildrenPointer()
{
return $this->childrenPointer;
}
/**
* @param Array $array
*/
public function setChildrenPointer($array)
{
$this->childrenPointer = $array;
}
}