Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Encapsulate users objects loading / caching inside UsersService
Browse files Browse the repository at this point in the history
  • Loading branch information
cdujeu committed Jun 17, 2016
1 parent 1e6cc40 commit 86591d4
Show file tree
Hide file tree
Showing 36 changed files with 379 additions and 279 deletions.
1 change: 1 addition & 0 deletions core/src/.gitignore
@@ -1,3 +1,4 @@
/plugins/index.elasticsearch/vendor
/plugins/access.ajxp_home/res/build
/plugins/access.inbox/res/build
/plugins/core.mailer/js/build
Expand Down
2 changes: 1 addition & 1 deletion core/src/core/src/pydio/Core/Controller/XMLWriter.php
Expand Up @@ -663,7 +663,7 @@ public static function repositoryToXML($repoId, $repoObject, $exposed, $streams,
if($loggedUser != null && $loggedUser->getId() == $uId){
$currentUserIsOwner = true;
}
$label = ConfService::getUserPersonalParameter("USER_DISPLAY_NAME", $uId, "core.conf", $uId);
$label = UsersService::getUserPersonalParameter("USER_DISPLAY_NAME", $uId, "core.conf", $uId);
$ownerLabel = $label;
$isSharedString = 'owner="'.Utils::xmlEntities($label).'"';
}
Expand Down
39 changes: 39 additions & 0 deletions core/src/core/src/pydio/Core/Exception/UserNotFoundException.php
@@ -0,0 +1,39 @@
<?php
/*
* Copyright 2007-2016 Abstrium <contact (at) pydio.com>
* 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 <https://pydio.com/>.
*/
namespace Pydio\Core\Exception;

defined('AJXP_EXEC') or die('Access not allowed');

/**
* Class UserNotFoundException
* @package Pydio\Core\Exception
*/
class UserNotFoundException extends PydioException
{
/**
* UserNotFoundException constructor.
* @param string $userId
*/
public function __construct($userId)
{
parent::__construct("Cannot find user ".$userId, null, 50014);
}
}
9 changes: 7 additions & 2 deletions core/src/core/src/pydio/Core/Http/Dav/AuthBackendBasic.php
Expand Up @@ -24,6 +24,7 @@
use Pydio\Auth\Core\AJXP_Safe;
use Pydio\Core\Exception\LoginException;
use Pydio\Core\Exception\RepositoryLoadException;
use Pydio\Core\Exception\UserNotFoundException;
use Pydio\Core\Exception\WorkspaceForbiddenException;
use Pydio\Core\Exception\WorkspaceNotFoundException;
use Pydio\Core\Model\ContextInterface;
Expand Down Expand Up @@ -87,8 +88,12 @@ public function authenticate(Sabre\DAV\Server $server, $realm)
// Authenticates the user
//AJXP_Logger::info(__CLASS__,"authenticate",$userpass[0]);

$confDriver = ConfService::getConfStorageImpl();
$userObject = $confDriver->createUserObject($userpass[0]);
try{
$userObject = UsersService::getUserById($userpass[0]);
}catch (UserNotFoundException $e){
throw new Sabre\DAV\Exception\NotAuthenticated();
}

$webdavData = $userObject->getPref("AJXP_WEBDAV_DATA");
if (empty($webdavData) || !isset($webdavData["ACTIVE"]) || $webdavData["ACTIVE"] !== true) {
AJXP_Logger::warning(__CLASS__, "Login failed", array("user" => $userpass[0], "error" => "WebDAV user not found or disabled"));
Expand Down
9 changes: 5 additions & 4 deletions core/src/core/src/pydio/Core/Http/Dav/AuthBackendDigest.php
Expand Up @@ -22,6 +22,7 @@

use Pydio\Core\Exception\LoginException;
use Pydio\Core\Exception\RepositoryLoadException;
use Pydio\Core\Exception\UserNotFoundException;
use Pydio\Core\Exception\WorkspaceForbiddenException;
use Pydio\Core\Exception\WorkspaceNotFoundException;

Expand Down Expand Up @@ -62,11 +63,11 @@ public function __construct($context)

public function getDigestHash($realm, $username)
{
if (!UsersService::userExists($username)) {
return false;
try{
$user = UsersService::getUserById($username);
}catch (UserNotFoundException $e){
throw new Sabre\DAV\Exception\NotAuthenticated();
}
$confDriver = ConfService::getConfStorageImpl();
$user = $confDriver->createUserObject($username);
$webdavData = $user->getPref("AJXP_WEBDAV_DATA");
if (empty($webdavData) || !isset($webdavData["ACTIVE"]) || $webdavData["ACTIVE"] !== true || (!isSet($webdavData["PASS"]) && !isset($webdavData["HA1"]) ) ) {
return false;
Expand Down
8 changes: 3 additions & 5 deletions core/src/core/src/pydio/Core/Model/Context.php
Expand Up @@ -21,8 +21,8 @@
namespace Pydio\Core\Model;


use Pydio\Core\Services\ConfService;
use Pydio\Core\Services\RepositoryService;
use Pydio\Core\Services\UsersService;

defined('AJXP_EXEC') or die('Access not allowed');

Expand Down Expand Up @@ -123,7 +123,7 @@ public function getUser()
return $this->userObject;
}
if(isSet($this->userId)){
$this->userObject = ConfService::getConfStorageImpl()->createUserObject($this->userId);
$this->userObject = UsersService::getUserById($this->userId, false);
return $this->userObject;
}
return null;
Expand Down Expand Up @@ -215,15 +215,13 @@ public function resetRepository()

public function getStringIdentifier()
{
$logged = $this->getUser();
$u = $logged == null ? "shared" : $logged->getId();
$u = $this->userId == null ? "shared" : $this->userId;
$a = "norepository";
$r = $this->getRepository();
if($r !== null){
$a = $r->getSlug();
}
return $u.":".$a;

}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/src/core/src/pydio/Core/Services/AuthService.php
Expand Up @@ -120,7 +120,7 @@ public static function logUser($user_id, $pwd, $bypass_pwd = false, $cookieLogin
AJXP_Safe::storeCredentials($authId, $authPwd);
}

$user = $confDriver->createUserObject($user_id);
$user = UsersService::getUserById($user_id, false);

if ($user->getLock() === "logout") {
AJXP_Logger::warning(__CLASS__, "Login failed", array("user" => Utils::sanitize($user_id, AJXP_SANITIZE_EMAILCHARS), "error" => "Locked user"));
Expand Down
51 changes: 0 additions & 51 deletions core/src/core/src/pydio/Core/Services/ConfService.php
Expand Up @@ -479,57 +479,6 @@ public static function getCoreConf($varName, $coreType = "ajaxplorer")
}

/**
* @var array Keep loaded labels in memory
*/
private static $usersParametersCache = array();

/**
* @param string $parameterName Plugin parameter name
* @param AbstractAjxpUser|string $userIdOrObject
* @param string $pluginId Plugin name, core.conf by default
* @param null $defaultValue
* @return mixed
*/
public static function getUserPersonalParameter($parameterName, $userIdOrObject, $pluginId="core.conf", $defaultValue=null){

$cacheId = $pluginId."-".$parameterName;
if(!isSet(self::$usersParametersCache[$cacheId])){
self::$usersParametersCache[$cacheId] = array();
}
// Passed an already loaded object
if($userIdOrObject instanceof AbstractAjxpUser){
$value = $userIdOrObject->personalRole->filterParameterValue($pluginId, $parameterName, AJXP_REPO_SCOPE_ALL, $defaultValue);
self::$usersParametersCache[$cacheId][$userIdOrObject->getId()] = $value;
if(empty($value) && !empty($defaultValue)) $value = $defaultValue;
return $value;
}
// Already in memory cache
if(isSet(self::$usersParametersCache[$cacheId][$userIdOrObject])){
return self::$usersParametersCache[$cacheId][$userIdOrObject];
}

// Try to load personal role if it was already loaded.
$uRole = RolesService::getRole("AJXP_USR_/" . $userIdOrObject);
if($uRole === false){
$uObject = self::getConfStorageImpl()->createUserObject($userIdOrObject);
if(isSet($uObject)){
$uRole = $uObject->personalRole;
}
}
if(empty($uRole)){
return $defaultValue;
}
$value = $uRole->filterParameterValue($pluginId, $parameterName, AJXP_REPO_SCOPE_ALL, $defaultValue);
if(empty($value) && !empty($defaultValue)) {
$value = $userIdOrObject;
}
self::$usersParametersCache[$cacheId][$userIdOrObject] = $value;
return $value;

}


/**
* Singleton method
*
* @return ConfService the service instance
Expand Down
3 changes: 3 additions & 0 deletions core/src/core/src/pydio/Core/Services/RepositoryService.php
Expand Up @@ -354,17 +354,20 @@ private function getRepositoryByIdInst($repoId)
}
$test = CacheService::fetch(AJXP_CACHE_SERVICE_NS_SHARED, "repository:" . $repoId);
if($test !== false){
$this->cache["REPOSITORIES"][$repoId] = $test;
return $test;
}
$test = ConfService::getConfStorageImpl()->getRepositoryById($repoId);
if($test != null) {
$this->cache["REPOSITORIES"][$repoId] = $test;
CacheService::save(AJXP_CACHE_SERVICE_NS_SHARED, "repository:" . $repoId, $test);
return $test;
}
// Finally try to search in default repositories
$statics = self::getStaticRepositories();
if (isSet($statics[$repoId])) {
$repo = $statics[$repoId];
$this->cache["REPOSITORIES"][$repoId] = $test;
CacheService::save(AJXP_CACHE_SERVICE_NS_SHARED, "repository:" . $repoId, $repo);
return $repo;
}
Expand Down
6 changes: 2 additions & 4 deletions core/src/core/src/pydio/Core/Services/RolesService.php
Expand Up @@ -354,9 +354,8 @@ public static function bootSequence()
if (!$authDriver->getOptionAsBool("TRANSMIT_CLEAR_PASS")) {
$adminPass = md5(ADMIN_PASSWORD);
}
UsersService::createUser("admin", $adminPass, true);
$userObject = UsersService::createUser("admin", $adminPass, true);
if (ADMIN_PASSWORD == INITIAL_ADMIN_PASSWORD) {
$userObject = ConfService::getConfStorageImpl()->createUserObject("admin");
$userObject->setAdmin(true);
RolesService::updateAdminRights($userObject);
if (UsersService::changePasswordEnabled()) {
Expand All @@ -369,8 +368,7 @@ public static function bootSequence()
}
} else if ($adminCount == -1) {
// Here we may come from a previous version! Check the "admin" user and set its right as admin.
$confStorage = ConfService::getConfStorageImpl();
$adminUser = $confStorage->createUserObject("admin");
$adminUser = UsersService::getUserById("admin");
$adminUser->setAdmin(true);
$adminUser->save("superuser");
file_put_contents(AJXP_CACHE_DIR . "/admin_counted", "true");
Expand Down

0 comments on commit 86591d4

Please sign in to comment.