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

Commit 8ce3909

Browse files
committed
New Middleware WorkspaceAuthRequired catches specific exception, sends JS prompt message and re-initialize request when form is submitted.
1 parent 0510045 commit 8ce3909

File tree

7 files changed

+104
-34
lines changed

7 files changed

+104
-34
lines changed

core/src/core/src/pydio/Core/Exception/PydioPromptException.php

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,36 +74,42 @@ public function __construct($promptType, $data, $messageString, $messageId = fal
7474
parent::__construct($messageString, $messageId);
7575
}
7676

77+
7778
/**
7879
* Prompt user for credentials
79-
* @param $sessionVariable
80-
* @param $switchToRepositoryId
80+
* @param array $parameters
81+
* @param string $passFieldName
82+
* @param string $postSubmitCallback
8183
* @throws PydioPromptException
8284
*/
83-
public static function testOrPromptForCredentials($sessionVariable, $switchToRepositoryId){
84-
if(isSet($_GET["prompt_passed_data"]) && isSet($_GET["variable_name"]) && $_GET["variable_name"] == $sessionVariable){
85-
$_SESSION[$sessionVariable] = true;
85+
public static function promptForWorkspaceCredentials($parameters, $passFieldName, $postSubmitCallback = ""){
86+
$hiddens = [];
87+
$getFields = [$passFieldName];
88+
foreach($parameters as $key => $value){
89+
$hiddens[] = "<input type='hidden' name='$key' value='$value'>";
90+
$getFields[] = $key;
8691
}
87-
if(!isSet($_SESSION[$sessionVariable])){
88-
throw new PydioPromptException(
89-
"confirm",
90-
array(
91-
"DIALOG" => "Please enter your credentials for this workspace
92-
<input type='hidden' name='get_action' value='switch_repository'>
93-
<input type='hidden' name='repository_id' value='".$switchToRepositoryId."'>
94-
<input type='hidden' name='prompt_passed_data' value='true'>
95-
<input type='hidden' name='variable_name' value='".$sessionVariable."'>
96-
",
97-
"OK" => array(
98-
"GET_FIELDS" => array("get_action", "repository_id", "prompt_passed_data", "variable_name"),
99-
"EVAL" => "ajaxplorer.loadXmlRegistry();"
100-
),
101-
"CANCEL" => array(
102-
"EVAL" => "ajaxplorer.loadXmlRegistry();"
103-
)
92+
throw new PydioPromptException(
93+
"confirm",
94+
array(
95+
"DIALOG" => "<div>
96+
<h3>Credentials Required</h3>
97+
<div class='dialogLegend'>Please provide a password to enter this workspace. You may have to manually redo the action you were currently trying to achieve.</div>
98+
<form autocomplete='off'>
99+
".implode("\n", $hiddens)."
100+
<input style='width: 200px;' type='password' autocomplete='off' name='$passFieldName' value='' placeholder='Password'>
101+
</form>
102+
</div>
103+
",
104+
"OK" => array(
105+
"GET_FIELDS" => $getFields,
106+
"EVAL" => $postSubmitCallback
104107
),
105-
"Credentials Needed");
106-
}
108+
"CANCEL" => array(
109+
"EVAL" => ""
110+
)
111+
),
112+
"Credentials Needed");
107113

108114
}
109115

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/*
3+
* Copyright 2007-2016 Abstrium <contact (at) pydio.com>
4+
* This file is part of Pydio.
5+
*
6+
* Pydio is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Pydio is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* The latest code can be found at <https://pydio.com/>.
20+
*/
21+
namespace Pydio\Core\Exception;
22+
23+
use Pydio\Access\Core\Model\Repository;
24+
use Pydio\Auth\Core\MemorySafe;
25+
use Pydio\Core\Model\Context;
26+
use Pydio\Core\Model\UserInterface;
27+
28+
defined('AJXP_EXEC') or die('Access not allowed');
29+
30+
/**
31+
* Class WorkspaceAuthRequired - Extend exception to trigger an authentication error
32+
* if workspace requires a specific authentication and it cannot be found.
33+
* @package Pydio\Core\Exception
34+
*/
35+
class WorkspaceAuthRequired extends PydioException {
36+
37+
private $repositoryId;
38+
39+
/**
40+
* WorkspaceAuthRequired constructor.
41+
* @param string $repositoryId
42+
* @param string $message
43+
*/
44+
public function __construct($repositoryId, $message = "Authentication required for this workspace")
45+
{
46+
$this->repositoryId = $repositoryId;
47+
parent::__construct($message, false, null);
48+
}
49+
50+
/**
51+
* @param Repository $workspaceObject
52+
* @param UserInterface $userObject
53+
* @throws WorkspaceAuthRequired
54+
*/
55+
public static function testWorkspace($workspaceObject, $userObject){
56+
if($workspaceObject->getContextOption(Context::contextWithObjects($userObject, $workspaceObject), "USE_SESSION_CREDENTIALS") !== true){
57+
return;
58+
}
59+
if(MemorySafe::loadCredentials() !== false){
60+
return;
61+
}
62+
throw new WorkspaceAuthRequired($workspaceObject->getId());
63+
}
64+
65+
}

core/src/core/src/pydio/Core/Http/Server.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ protected function stackMiddleWares(){
9595

9696
$this->middleWares->push(array("Pydio\\Core\\Controller\\Controller", "registryActionMiddleware"));
9797
$this->middleWares->push(array("Pydio\\Core\\Http\\Middleware\\SessionRepositoryMiddleware", "handleRequest"));
98+
$this->middleWares->push(array("Pydio\\Core\\Http\\Middleware\\WorkspaceAuthMiddleware", "handleRequest"));
9899
$this->middleWares->push(array("Pydio\\Core\\Http\\Middleware\\AuthMiddleware", "handleRequest"));
99100
$this->middleWares->push(array("Pydio\\Core\\Http\\Middleware\\SecureTokenMiddleware", "handleRequest"));
100101
$this->middleWares->push(array("Pydio\\Core\\Http\\Middleware\\SessionMiddleware", "handleRequest"));

core/src/core/src/pydio/Core/Services/SessionService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public static function saveRepositoryId($repoId){
178178
/**
179179
* @param $repoId
180180
*/
181-
public static function switchSessionRepositoriId($repoId){
181+
public static function switchSessionRepositoryId($repoId){
182182
if(self::has(self::CTX_REPOSITORY_ID)) {
183183
self::save(self::PREVIOUS_REPOSITORY, self::fetch(self::CTX_REPOSITORY_ID));
184184
}

core/src/core/src/pydio/Core/Services/UsersService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Pydio\Conf\Core\AbstractUser;
2424
use Pydio\Core\Controller\Controller;
2525
use Pydio\Core\Exception\UserNotFoundException;
26+
use Pydio\Core\Exception\WorkspaceAuthRequired;
2627
use Pydio\Core\Exception\WorkspaceForbiddenException;
2728
use Pydio\Core\Exception\WorkspaceNotFoundException;
2829
use Pydio\Core\Http\Message\ReloadRepoListMessage;
@@ -145,6 +146,7 @@ public static function getRepositoryWithPermission($user, $repositoryId){
145146
if(!RepositoryService::repositoryIsAccessible($repo, $user)){
146147
throw new WorkspaceForbiddenException($repositoryId);
147148
}
149+
WorkspaceAuthRequired::testWorkspace($repo, $user);
148150
return $repo;
149151
}
150152

core/src/core/src/pydio/Core/Utils/Vars/UrlUtils.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ class UrlUtils
3131
{
3232
/**
3333
* UTF8 support for parseUrl
34-
* @param $url
35-
* @return mixed
34+
* @param string $url
35+
* @param int $part one of PHP_URL_** variable
36+
* @return array|string
3637
*/
3738
public static function mbParseUrl($url, $part = -1){
3839
$enc_url = preg_replace_callback(

core/src/plugins/core.conf/AbstractConfDriver.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -611,13 +611,8 @@ public function switchAction(ServerRequestInterface $requestInterface, ResponseI
611611
if (!isSet($repository_id)) {
612612
break;
613613
}
614-
$dirList = UsersService::getRepositoriesForUser($ctx->getUser());
615-
/** @var $repository_id string */
616-
if (!isSet($dirList[$repository_id])) {
617-
throw new PydioException("Trying to switch to an unkown repository!");
618-
}
619-
//ConfService::switchRootDir($repository_id);
620-
SessionService::switchSessionRepositoriId($repository_id);
614+
UsersService::getRepositoryWithPermission($ctx->getUser(), $repository_id);
615+
SessionService::switchSessionRepositoryId($repository_id);
621616
PluginsService::getInstance($ctx->withRepositoryId($repository_id));
622617
if (UsersService::usersEnabled() && $loggedUser !== null) {
623618
$loggedUser->setArrayPref("repository_last_connected", $repository_id, time());

0 commit comments

Comments
 (0)