/**
* Load initial user data (Rights, Preferences and Bookmarks).
*
* @see AbstractAjxpUser#load()
*/
public function load()
{
$this->log('Loading all user data..');
// update group
$res = dibi::query('SELECT [groupPath] FROM [ajxp_users] WHERE [login] = %s', $this->getId());
$this->groupPath = $res->fetchSingle();
if (empty($this->groupPath)) {
// Auto migrate from old version
$this->setGroupPath("/");
}
$result_rights = dibi::query('SELECT [repo_uuid], [rights] FROM [ajxp_user_rights] WHERE [login] = %s', $this->getId());
$this->rights = $result_rights->fetchPairs('repo_uuid', 'rights');
// Db field returns integer or string so we are required to cast it in order to make the comparison
if (isset($this->rights["ajxp.admin"]) && (bool) $this->rights["ajxp.admin"] === true) {
$this->setAdmin(true);
}
if (isset($this->rights["ajxp.parent_user"])) {
$this->setParent($this->rights["ajxp.parent_user"]);
}
if (isset($this->rights["ajxp.hidden"])) {
$this->setHidden(true);
}
if ("postgre" == $this->storage->sqlDriver["driver"]) {
dibi::nativeQuery('SET bytea_output = escape');
}
$result_prefs = dibi::query('SELECT [name], [val] FROM [ajxp_user_prefs] WHERE [login] = %s', $this->getId());
$this->prefs = $result_prefs->fetchPairs('name', 'val');
$result_bookmarks = dibi::query('SELECT [repo_uuid], [path], [title] FROM [ajxp_user_bookmarks] WHERE [login] = %s', $this->getId());
$all_bookmarks = $result_bookmarks->fetchAll();
if (!is_array($this->bookmarks)) {
$this->bookmarks = array();
}
$this->bookmarks = array();
foreach ($all_bookmarks as $b) {
if (!is_array($this->bookmarks[$b['repo_uuid']])) {
$this->bookmarks[$b['repo_uuid']] = array();
}
$this->bookmarks[$b['repo_uuid']][] = array('PATH' => $b['path'], 'TITLE' => $b['title']);
}
// COLLECT ROLES TO LOAD
$rolesToLoad = array();
if (isset($this->rights["ajxp.roles"])) {
if (is_string($this->rights["ajxp.roles"])) {
if (strpos($this->rights["ajxp.roles"], '$phpserial$') === 0) {
$this->rights["ajxp.roles"] = unserialize(str_replace('$phpserial$', '', $this->rights["ajxp.roles"]));
} else {
if (strpos($this->rights["ajxp.roles"], '$json$') === 0) {
$this->rights["ajxp.roles"] = json_decode(str_replace('$json$', '', $this->rights["ajxp.roles"]), true);
} else {
$this->rights["ajxp.roles"] = unserialize($this->rights["ajxp.roles"]);
}
}
}
if (is_array($this->rights["ajxp.roles"])) {
$rolesToLoad = array_keys($this->rights["ajxp.roles"]);
}
}
$rolesToLoad[] = "AJXP_GRP_/";
if ($this->groupPath != null) {
$base = "";
$exp = explode("/", $this->groupPath);
foreach ($exp as $pathPart) {
if (empty($pathPart)) {
continue;
}
$base = $base . "/" . $pathPart;
$rolesToLoad[] = "AJXP_GRP_" . $base;
}
}
$rolesToLoad[] = "AJXP_USR_/" . $this->id;
// NOW LOAD THEM
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;
$roleObject = $allRoles[$roleId];
if ($roleObject->alwaysOverrides()) {
if (!isset($this->rights["ajxp.roles.sticky"]) || !is_array($this->rights["ajxp.roles.sticky"])) {
$this->rights["ajxp.roles.sticky"] = array();
}
$this->rights["ajxp.roles.sticky"][$roleId] = true;
}
} else {
if (is_array($this->rights["ajxp.roles"]) && isset($this->rights["ajxp.roles"][$roleId])) {
unset($this->rights["ajxp.roles"][$roleId]);
}
}
}
}
if (!isset($this->rights["ajxp.roles.order"]) && is_array($this->rights["ajxp.roles"])) {
// Create sample order
$this->rights["ajxp.roles.order"] = array();
$index = 0;
foreach ($this->rights["ajxp.roles"] as $id => $rBool) {
$this->rights["ajxp.roles.order"][$id] = $index;
$index++;
}
} else {
$this->rights["ajxp.roles.order"] = unserialize(str_replace('$phpserial$', '', $this->rights["ajxp.roles.order"]));
}
// CHECK USER PERSONAL ROLE
if (isset($this->roles["AJXP_USR_" . "/" . $this->id]) && is_a($this->roles["AJXP_USR_" . "/" . $this->id], "AJXP_Role")) {
$this->personalRole = $this->roles["AJXP_USR_" . "/" . $this->id];
} else {
// MIGRATE NOW !
$originalRights = $this->rights;
$changes = $this->migrateRightsToPersonalRole();
// SAVE RIGHT AND ROLE
if ($changes > 0) {
// There was an actual migration, let's save the changes now.
$removedRights = array_keys(array_diff($originalRights, $this->rights));
if (count($removedRights)) {
// We use (%s) instead of %in to pass everything as string ('1' instead of 1)
dibi::query("DELETE FROM [ajxp_user_rights] WHERE [login] = %s AND [repo_uuid] IN (%s)", $this->getId(), $removedRights);
}
AuthService::updateRole($this->personalRole);
} else {
$this->personalRole = new AJXP_Role("AJXP_USR_" . "/" . $this->id);
}
$this->roles["AJXP_USR_" . "/" . $this->id] = $this->personalRole;
}
$this->recomputeMergedRole();
}