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

Commit

Permalink
Hunting AuthService::getLoggedUser to replace with Context
Browse files Browse the repository at this point in the history
  • Loading branch information
cdujeu committed Jun 6, 2016
1 parent baf6bd0 commit 2cf2a73
Show file tree
Hide file tree
Showing 29 changed files with 138 additions and 124 deletions.
12 changes: 7 additions & 5 deletions core/src/core/src/pydio/Core/Controller/XMLWriter.php
Expand Up @@ -22,10 +22,10 @@

use Pydio\Access\Core\Model\AJXP_Node;
use Pydio\Access\Core\IAjxpWrapperProvider;
use Pydio\Core\Model\ContextInterface;
use Pydio\Core\Model\RepositoryInterface;
use Pydio\Core\Utils\Utils;
use Pydio\Core\Services;
use Pydio\Core\Services\AuthService;
use Pydio\Conf\Core\AbstractAjxpUser;
use Pydio\Core\Services\ConfService;
use Pydio\Core\PluginFramework\PluginsService;
Expand Down Expand Up @@ -498,24 +498,26 @@ public static function sendMessage($logMessage, $errorMessage, $print = true)
/**
* Extract all the user data and put it in XML
* @static
* @param ContextInterface $ctx
* @param AbstractAjxpUser|null $userObject
* @return string
*/
public static function getUserXML($userObject = null)
public static function getUserXML(ContextInterface $ctx, $userObject = null)
{
$buffer = "";
$loggedUser = AuthService::getLoggedUser();
$loggedUser = $ctx->getUser();
$currentRepoId = $ctx->getRepositoryId();
$confDriver = ConfService::getConfStorageImpl();
if($userObject != null) $loggedUser = $userObject;
if (!Services\AuthService::usersEnabled()) {
$buffer.="<user id=\"shared\">";
$buffer.="<active_repo id=\"".ConfService::getCurrentRepositoryId()."\" write=\"1\" read=\"1\"/>";
$buffer.="<active_repo id=\"".$currentRepoId."\" write=\"1\" read=\"1\"/>";
$buffer.= XMLWriter::writeRepositoriesData(null);
$buffer.="</user>";
} else if ($loggedUser != null) {
$lock = $loggedUser->getLock();
$buffer.="<user id=\"".$loggedUser->id."\">";
$buffer.="<active_repo id=\"".ConfService::getCurrentRepositoryId()."\" write=\"".($loggedUser->canWrite(ConfService::getCurrentRepositoryId())?"1":"0")."\" read=\"".($loggedUser->canRead(ConfService::getCurrentRepositoryId())?"1":"0")."\"/>";
$buffer.="<active_repo id=\"".$currentRepoId."\" write=\"".($loggedUser->canWrite($currentRepoId)?"1":"0")."\" read=\"".($loggedUser->canRead($currentRepoId)?"1":"0")."\"/>";
$buffer.= XMLWriter::writeRepositoriesData($loggedUser);
$buffer.="<preferences>";
$preferences = $confDriver->getExposedPreferences($loggedUser);
Expand Down
8 changes: 3 additions & 5 deletions core/src/core/src/pydio/Core/Services/AuthService.php
Expand Up @@ -21,14 +21,12 @@
namespace Pydio\Core\Services;
use Pydio\Access\Core\Model\Repository;
use Pydio\Auth\Core\AJXP_Safe;
use Pydio\Authfront\Core\AbstractAuthFrontend;
use Pydio\Conf\Core\AbstractAjxpUser;
use Pydio\Conf\Core\AJXP_Role;
use Pydio\Conf\Core\AjxpGroupPathProvider;
use Pydio\Conf\Core\AjxpRole;
use Pydio\Core\Model\Context;
use Pydio\Core\Model\ContextInterface;
use Pydio\Core\Services\ConfService;
use Pydio\Core\Model\UserInterface;
use Pydio\Core\Controller\Controller;
use Pydio\Core\Utils\CookiesHelper;
use Pydio\Core\Utils\Utils;
Expand Down Expand Up @@ -189,7 +187,7 @@ public static function ignoreUserCase()

/**
* @static
* @param AbstractAjxpUser $user
* @param UserInterface $user
*/
public static function refreshRememberCookie($user)
{
Expand All @@ -199,7 +197,7 @@ public static function refreshRememberCookie($user)
}
$rememberPass = CookiesHelper::getCookieString($user);
if(self::$useSession) {
setcookie("AjaXplorer-remember", $user->id.":".$rememberPass, time()+3600*24*10, null, null, (isSet($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"]) == "on"), true);
setcookie("AjaXplorer-remember", $user->getId().":".$rememberPass, time()+3600*24*10, null, null, (isSet($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"]) == "on"), true);
}
}

Expand Down
12 changes: 6 additions & 6 deletions core/src/core/src/pydio/Core/Services/ConfService.php
Expand Up @@ -475,7 +475,7 @@ public static function getRepositoriesList($scope = "user", $includeShared = tru
if ($scope == "user") {
return self::getInstance()->getLoadedRepositories();
} else {
return self::getInstance()->initRepositoriesListInst("all", $includeShared);
return self::getInstance()->initRepositoriesListInst("all", null, $includeShared);
}
}

Expand Down Expand Up @@ -510,7 +510,7 @@ private function getLoadedRepositories()
unset($this->configs["REPOSITORIES"]);
}
}
$this->configs["REPOSITORIES"] = $this->initRepositoriesListInst();
$this->configs["REPOSITORIES"] = $this->initRepositoriesListInst("user", AuthService::getLoggedUser());
$_SESSION["REPOSITORIES"] = $this->configs["REPOSITORIES"];
return $this->configs["REPOSITORIES"];
}
Expand Down Expand Up @@ -762,13 +762,13 @@ public static function listRepositoriesWithCriteria($criteria, &$count){

/**
* @param $scope String "user", "all"
* @param UserInterface|null $loggedUser
* @param bool $includeShared
* @return array
*/
protected function initRepositoriesListInst($scope = "user", $includeShared = true)
protected function initRepositoriesListInst($scope = "user", $loggedUser = null, $includeShared = true)
{
// APPEND CONF FILE REPOSITORIES
$loggedUser = AuthService::getLoggedUser();
$objList = array();
if($loggedUser != null){
$l = $loggedUser->getLock();
Expand All @@ -786,8 +786,8 @@ protected function initRepositoriesListInst($scope = "user", $includeShared = tr
$confDriver = self::getConfStorageImpl();
if($scope == "user"){
$acls = array();
if(AuthService::getLoggedUser() != null){
$acls = AuthService::getLoggedUser()->mergedRole->listAcls(true);
if($loggedUser != null){
$acls = $loggedUser->getMergedRole()->listAcls(true);
}
if(!count($acls)) {
$drvList = array();
Expand Down
14 changes: 8 additions & 6 deletions core/src/core/src/pydio/Core/Utils/Utils.php
Expand Up @@ -22,6 +22,7 @@

use Psr\Http\Message\UploadedFileInterface;
use Pydio\Access\Core\Model\Repository;
use Pydio\Core\Model\ContextInterface;
use Pydio\Core\Services\AuthService;
use Pydio\Core\Services\ConfService;
use Pydio\Core\PluginFramework\Plugin;
Expand Down Expand Up @@ -384,12 +385,13 @@ public static function parseFileDataErrors($boxData, $throwException=false)
* + skipIOS
* + gui
* @static
* @param ContextInterface $ctx
* @param $parameters
* @param $output
* @param $session
* @return void
*/
public static function parseApplicationGetParameters($parameters, &$output, &$session)
public static function parseApplicationGetParameters(ContextInterface $ctx, $parameters, &$output, &$session)
{
$output["EXT_REP"] = "/";

Expand All @@ -411,7 +413,7 @@ public static function parseApplicationGetParameters($parameters, &$output, &$se
$parameters["repository_id"] = $repository->getId();
}
if (AuthService::usersEnabled()) {
$loggedUser = AuthService::getLoggedUser();
$loggedUser = $ctx->getUser();
if ($loggedUser != null && $loggedUser->canSwitchTo($parameters["repository_id"])) {
$output["FORCE_REGISTRY_RELOAD"] = true;
$output["EXT_REP"] = TextEncoder::toUTF8(urldecode($parameters["folder"]));
Expand Down Expand Up @@ -1731,10 +1733,10 @@ public static function filterFormElementsFromMeta($metadata, &$nestedData, $user
}
}

public static function parseStandardFormParameters(&$repDef, &$options, $userId = null, $prefix = "DRIVER_OPTION_", $binariesContext = null, $cypheredPassPrefix = "")
public static function parseStandardFormParameters(ContextInterface $ctx, &$repDef, &$options, $prefix = "DRIVER_OPTION_", $binariesContext = null, $cypheredPassPrefix = "")
{
if ($binariesContext === null) {
$binariesContext = array("USER" => (AuthService::getLoggedUser()!= null)?AuthService::getLoggedUser()->getId():"shared");
$binariesContext = array("USER" => ($ctx->hasUser())?$ctx->getUser()->getId():"shared");
}
$replicationGroups = array();
$switchesGroups = array();
Expand All @@ -1752,10 +1754,10 @@ public static function parseStandardFormParameters(&$repDef, &$options, $userId
$value = intval($value);
} else if ($type == "array") {
$value = explode(",", $value);
} else if ($type == "password" && $userId!=null) {
} else if ($type == "password" && $ctx->hasUser()) {
if (trim($value) != "" && $value != "__AJXP_VALUE_SET__" && function_exists('mcrypt_encrypt')) {
// We encode as base64 so if we need to store the result in a database, it can be stored in text column
$value = $cypheredPassPrefix . base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($userId."\1CDAFx¨op#"), $value, MCRYPT_MODE_ECB));
$value = $cypheredPassPrefix . base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($ctx->getUser()->getId()."\1CDAFx¨op#"), $value, MCRYPT_MODE_ECB));
}
} else if ($type == "binary" && $binariesContext !== null) {
if (!empty($value)) {
Expand Down
37 changes: 25 additions & 12 deletions core/src/plugins/access.ajxp_conf/class.ajxp_confAccessDriver.php
Expand Up @@ -28,6 +28,7 @@
use Pydio\Access\Core\Filter\AJXP_PermissionMask;
use Pydio\Access\Core\Model\Repository;
use Pydio\Access\Core\Model\UserSelection;
use Pydio\Core\Model\Context;
use Pydio\Core\Model\ContextInterface;
use Pydio\Core\Model\UserInterface;
use Pydio\Core\Services\AuthService;
Expand Down Expand Up @@ -871,7 +872,10 @@ public function switchAction($action, $httpVars, $fileVars, ContextInterface $ct
$data = json_decode($jsonData, true);
$roleData = $data["ROLE"];
$binariesContext = array();
$parseContext = $ctx;
if (isset($userObject)) {
$parseContext = new Context(null, $ctx->getRepositoryId());
$parseContext->setUserObject($userObject);
$binariesContext = array("USER" => $userObject->getId());
}
if(isSet($data["FORMS"])){
Expand All @@ -880,9 +884,9 @@ public function switchAction($action, $httpVars, $fileVars, ContextInterface $ct
foreach ($plugData as $plugId => $formsData) {
$parsed = array();
Utils::parseStandardFormParameters(
$parseContext,
$formsData,
$parsed,
($userObject!=null?$usrId:null),
"ROLE_PARAM_",
$binariesContext,
AJXP_Role::$cypheredPassPrefix
Expand Down Expand Up @@ -1284,6 +1288,7 @@ public function switchAction($action, $httpVars, $fileVars, ContextInterface $ct
break;

case "save_custom_user_params" :

$userId = Utils::sanitize($httpVars["user_id"], AJXP_SANITIZE_EMAILCHARS);
if ($userId == $loggedUser->getId()) {
$user = $loggedUser;
Expand All @@ -1299,7 +1304,8 @@ public function switchAction($action, $httpVars, $fileVars, ContextInterface $ct
if(!is_array($custom)) $custom = array();

$options = $custom;
$this->parseParameters($httpVars, $options, $userId, false, $custom);
$newCtx = new Context($userId, $ctx->getRepositoryId());
$this->parseParameters($newCtx, $httpVars, $options, false, $custom);
$custom = $options;
$user->setPref("CUSTOM_PARAMS", $custom);
$user->save();
Expand Down Expand Up @@ -1333,7 +1339,8 @@ public function switchAction($action, $httpVars, $fileVars, ContextInterface $ct
}
$options = $wallet[$repoID];
$existing = $options;
$this->parseParameters($httpVars, $options, $userId, false, $existing);
$newCtx = new Context($userId, $ctx->getRepositoryId());
$this->parseParameters($newCtx, $httpVars, $options, false, $existing);
$wallet[$repoID] = $options;
$user->setPref("AJXP_WALLET", $wallet);
$user->save();
Expand Down Expand Up @@ -1448,7 +1455,7 @@ public function switchAction($action, $httpVars, $fileVars, ContextInterface $ct
$options = $repDef["DRIVER_OPTIONS"];
} else {
$options = array();
$this->parseParameters($repDef, $options, null, true);
$this->parseParameters($ctx, $repDef, $options, true);
}
if (count($options)) {
$repDef["DRIVER_OPTIONS"] = $options;
Expand Down Expand Up @@ -1717,7 +1724,7 @@ public function switchAction($action, $httpVars, $fileVars, ContextInterface $ct
if(!$repo->isTemplate){
foreach($existing as $exK) $existingValues[$exK] = $repo->getOption($exK, true);
}
$this->parseParameters($httpVars, $options, null, true, $existingValues);
$this->parseParameters($ctx, $httpVars, $options, true, $existingValues);
if (count($options)) {
foreach ($options as $key=>$value) {
if ($key == "AJXP_SLUG") {
Expand Down Expand Up @@ -1822,7 +1829,7 @@ public function switchAction($action, $httpVars, $fileVars, ContextInterface $ct
$options = json_decode(TextEncoder::magicDequote($httpVars["json_data"]), true);
} else {
$options = array();
$this->parseParameters($httpVars, $options, null, true);
$this->parseParameters($ctx, $httpVars, $options, true);
}
$repoOptions = $repo->getOption("META_SOURCES");
if (is_array($repoOptions) && isSet($repoOptions[$metaSourceType])) {
Expand Down Expand Up @@ -1879,7 +1886,7 @@ public function switchAction($action, $httpVars, $fileVars, ContextInterface $ct
$options = json_decode(TextEncoder::magicDequote($httpVars["json_data"]), true);
} else {
$options = array();
$this->parseParameters($httpVars, $options, null, true);
$this->parseParameters($ctx, $httpVars, $options, true);
}
if(isset($repoOptions[$metaSourceId])){
$this->mergeExistingParameters($options, $repoOptions[$metaSourceId]);
Expand Down Expand Up @@ -2094,7 +2101,7 @@ public function switchAction($action, $httpVars, $fileVars, ContextInterface $ct
case "run_plugin_action":

$options = array();
$this->parseParameters($httpVars, $options, null, true);
$this->parseParameters($ctx, $httpVars, $options, true);
$pluginId = $httpVars["action_plugin_id"];
if (isSet($httpVars["button_key"])) {
$options = $options[$httpVars["button_key"]];
Expand All @@ -2117,7 +2124,7 @@ public function switchAction($action, $httpVars, $fileVars, ContextInterface $ct
case "edit_plugin_options":

$options = array();
$this->parseParameters($httpVars, $options, null, true);
$this->parseParameters($ctx, $httpVars, $options, true);
$confStorage = ConfService::getConfStorageImpl();
$pluginId = Utils::sanitize($httpVars["plugin_id"], AJXP_SANITIZE_ALPHANUM);
list($pType, $pName) = explode(".", $pluginId);
Expand Down Expand Up @@ -2911,10 +2918,16 @@ public function updateUserRole(UserInterface $ctxUser, $userId, $roleId, $addOrR

}


protected function parseParameters(&$repDef, &$options, $userId = null, $globalBinaries = false, $existingValues = array())
/**
* @param ContextInterface $ctx
* @param $repDef
* @param $options
* @param bool $globalBinaries
* @param array $existingValues
*/
protected function parseParameters(ContextInterface $ctx, &$repDef, &$options, $globalBinaries = false, $existingValues = array())
{
Utils::parseStandardFormParameters($repDef, $options, $userId, "DRIVER_OPTION_", ($globalBinaries?array():null));
Utils::parseStandardFormParameters($ctx, $repDef, $options, "DRIVER_OPTION_", ($globalBinaries?array():null));
if(!count($existingValues)){
return;
}
Expand Down
23 changes: 12 additions & 11 deletions core/src/plugins/access.dropbox/class.dropboxAccessDriver.php
Expand Up @@ -74,7 +74,7 @@ protected function initRepository(ContextInterface $contextInterface)
if(!empty($_SESSION["OAUTH_DROPBOX_TOKENS"])) return;

// TOKENS IN FILE ?
$tokens = $this->getTokens();
$tokens = $this->getTokens($contextInterface);
if (!empty($tokens)) {
$_SESSION["OAUTH_DROPBOX_TOKENS"] = $tokens;
return;
Expand Down Expand Up @@ -108,7 +108,7 @@ protected function initRepository(ContextInterface $contextInterface)
}
$_SESSION['DROPBOX_NEGOCIATION_STATE'] = 3;
$_SESSION['OAUTH_DROPBOX_TOKENS'] = $tokens;
$this->setTokens($tokens);
$this->setTokens($contextInterface, $tokens);
return;
}

Expand All @@ -128,14 +128,15 @@ public function isWriteable($dir, $type = "dir")
return true;
}

public function getTokens()
public function getTokens(ContextInterface $ctx)
{
if($this->repository->getOption("DROPBOX_OAUTH_TOKENS") !== null && is_array($this->repository->getOption("DROPBOX_OAUTH_TOKENS"))){
return $this->repository->getOption("DROPBOX_OAUTH_TOKENS");
$repo = $ctx->getRepository();
if($repo->getOption("DROPBOX_OAUTH_TOKENS") !== null && is_array($repo->getOption("DROPBOX_OAUTH_TOKENS"))){
return $repo->getOption("DROPBOX_OAUTH_TOKENS");
}
$repositoryId = $this->repository->getId();
$repositoryId = $repo->getId();
if(AuthService::usersEnabled()) {
$u = AuthService::getLoggedUser();
$u = $ctx->getUser();
$userId = $u->getId();
if($u->getResolveAsParent()){
$userId = $u->getParent();
Expand All @@ -146,18 +147,18 @@ public function getTokens()
return Utils::loadSerialFile(AJXP_DATA_PATH."/plugins/access.dropbox/".$repositoryId."_".$userId."_tokens");
}

public function setTokens($oauth_tokens)
public function setTokens(ContextInterface $ctx, $oauth_tokens)
{
$repositoryId = $this->repository->getId();
if(AuthService::usersEnabled()) $userId = AuthService::getLoggedUser()->getId();
$repositoryId = $ctx->getUser()->getId();
if(AuthService::usersEnabled()) $userId = $ctx->getUser()->getId();
else $userId = "shared";
Utils::saveSerialFile(AJXP_DATA_PATH."/plugins/access.dropbox/".$repositoryId."_".$userId."_tokens", $oauth_tokens, true);
}

public function makeSharedRepositoryOptions(ContextInterface $ctx, $httpVars)
{
$newOptions = parent::makeSharedRepositoryOptions($ctx, $httpVars);
$newOptions["DROPBOX_OAUTH_TOKENS"] = $this->getTokens();
$newOptions["DROPBOX_OAUTH_TOKENS"] = $this->getTokens($ctx);
return $newOptions;
}

Expand Down

0 comments on commit 2cf2a73

Please sign in to comment.