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

Commit

Permalink
JS UI: handle Prompt exception and dialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdujeu committed Nov 7, 2016
1 parent 2a90fa2 commit 0510045
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 290 deletions.
@@ -0,0 +1,105 @@
<?php
/*
* Copyright 2007-2015 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\Http\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Pydio\Auth\Core\MemorySafe;
use Pydio\Core\Exception\PydioException;

use Pydio\Core\Exception\PydioPromptException;
use Pydio\Core\Exception\WorkspaceAuthRequired;
use Pydio\Core\Http\Server;
use Pydio\Core\Model\ContextInterface;
use Pydio\Core\Services\SessionService;
use Pydio\Core\Utils\Vars\StringHelper;

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

/**
* Class WorkspaceAuthMiddleware
* PSR7 Middleware that handle Workspace "prompted" authentication
* @package Pydio\Core\Http\Middleware
*/
class WorkspaceAuthMiddleware
{

const RESUBMIT_AUTH_VARS = "PYDIO_WORKSPACE_AUTH_VARS";
const RESUBMIT_AUTH_KEY = "PYDIO_WORKSPACE_AUTH_RESUBMIT";
const RESUBMIT_AUTH_COUNT = "PYDIO_WORKSPACE_AUTH_RESUBMIT_COUNT";

const FORM_RESUBMIT_KEY = "workspace-auth-submission-id";
const FORM_RESUBMIT_PASS = "workspace-auth-password";

/**
* @param ServerRequestInterface $requestInterface
* @param ResponseInterface $responseInterface
* @return ResponseInterface
* @param callable|null $next
* @throws PydioException
*/
public static function handleRequest(ServerRequestInterface $requestInterface, ResponseInterface $responseInterface, callable $next = null){

$vars = $requestInterface->getParsedBody();
if(isSet($vars[self::FORM_RESUBMIT_KEY]) && SessionService::has(self::RESUBMIT_AUTH_VARS."-".$vars[self::FORM_RESUBMIT_KEY]) && !empty($vars[self::FORM_RESUBMIT_PASS])){
$submittedId = $vars[self::FORM_RESUBMIT_KEY];
// Count a max number of submission?
/** @var ContextInterface $ctx */
$ctx = $requestInterface->getAttribute("ctx");
if($ctx->hasUser()){
$password = $vars[self::FORM_RESUBMIT_PASS];
MemorySafe::storeCredentials($ctx->getUser()->getId(), $password);
}

$newVars = SessionService::fetch(self::RESUBMIT_AUTH_VARS."-".$submittedId);
SessionService::delete(self::RESUBMIT_AUTH_VARS."-".$submittedId);

$vars = $newVars;
$requestInterface = $requestInterface->withParsedBody($newVars);
if(isSet($vars["get_action"])){
$requestInterface = $requestInterface->withAttribute("action", $vars["get_action"]);
}
}

try{

return Server::callNextMiddleWare($requestInterface, $responseInterface, $next);

} catch (WorkspaceAuthRequired $ex){

// Generate a random ID.
$submissionId = StringHelper::generateRandomString(24);
SessionService::save(self::RESUBMIT_AUTH_VARS."-".$submissionId, $vars);
$parameters = [self::FORM_RESUBMIT_KEY => $submissionId];
$postSubmitCallback = "";
if($requestInterface->getAttribute("action") === "switch_repository"){
$postSubmitCallback = "ajaxplorer.loadXmlRegistry();";
}
// Will throw a prompt exception with all current values
return PydioPromptException::promptForWorkspaceCredentials($parameters, self::FORM_RESUBMIT_PASS, $postSubmitCallback);

}


}

}
46 changes: 8 additions & 38 deletions core/src/plugins/gui.ajax/res/js/es6/http/PydioApi.es6
Expand Up @@ -382,7 +382,7 @@ class PydioApi{

for(var i=0; i<childs.length;i++)
{
if(childs[i].tagName == "message")
if(childs[i].tagName === "message")
{
var messageTxt = "No message";
if(childs[i].firstChild) messageTxt = childs[i].firstChild.nodeValue;
Expand All @@ -393,43 +393,13 @@ class PydioApi{
Logger.log(messageTxt);
}

}else if(childs[i].tagName == "prompt"){

var message = XMLUtils.XPathSelectSingleNode(childs[i], "message").firstChild.nodeValue;
var jsonData = XMLUtils.XPathSelectSingleNode(childs[i], "data").firstChild.nodeValue;
var json = JSON.parse(jsonData);
// TODO: DELEGATE TO UI
/*
var dialogContent = new Element('div').update(json["DIALOG"]);
modal.showSimpleModal(modal.messageBox?modal.messageBox:document.body, dialogContent, function(){
// ok callback;
if(json["OK"]["GET_FIELDS"]){
var params = $H();
$A(json["OK"]["GET_FIELDS"]).each(function(fName){
params.set(fName, dialogContent.down('input[name="'+fName+'"]').getValue());
});
var conn = new Connexion();
conn.setParameters(params);
if(json["OK"]["EVAL"]){
conn.onComplete = function(){
eval(json["OK"]["EVAL"]);
};
}
conn.sendAsync();
}else{
if(json["OK"]["EVAL"]){
eval(json["OK"]["EVAL"]);
}
}
return true;
}, function(){
// cancel callback
if(json["CANCEL"]["EVAL"]){
eval(json["CANCEL"]["EVAL"]);
}
return true;
});
*/
}else if(childs[i].tagName === "prompt"){

if(pydio && pydio.UI && pydio.UI.openPromptDialog){
let jsonData = XMLUtils.XPathSelectSingleNode(childs[i], "data").firstChild.nodeValue;
pydio.UI.openPromptDialog(JSON.parse(jsonData));
}
// Interrupt further process
throw new Error();

}
Expand Down
Expand Up @@ -217,6 +217,14 @@ class RemoteNodeProvider{
}
}

// CHECK FOR AUTH PROMPT REQUIRED
var authNode = XMLUtils.XPathSelectSingleNode(rootNode, "prompt");
if(authNode && pydio && pydio.UI && pydio.UI.openPromptDialog){
let jsonData = XMLUtils.XPathSelectSingleNode(authNode, "data").firstChild.nodeValue;
pydio.UI.openPromptDialog(JSON.parse(jsonData));
throw new Error();
}

// CHECK FOR PAGINATION DATA
var paginationNode = XMLUtils.XPathSelectSingleNode(rootNode, "pagination");
if(paginationNode){
Expand Down
90 changes: 51 additions & 39 deletions core/src/plugins/gui.ajax/res/js/ui/prototype/class.Modal.js
Expand Up @@ -340,11 +340,21 @@ Class.create("Modal", {
this.showDialogForm('', editorData.formId, loadFunc, null, null, true, true);
},

showSimpleModal : function(element, content, okCallback, cancelCallback, position){
var box = new Element("div", {className:"dialogBox css_boxshadow", style:'display:block;'});
showSimpleModal : function(element, content, okCallback, cancelCallback, position, additionalClass, skipEffect){
if(!additionalClass) additionalClass = '';
var box = new Element("div", {className:"dialogBox css_boxshadow" + ' ' + additionalClass, style:'display:block;'});
box.insert(content);
content.addClassName("dialogContent");
addLightboxMarkupToElement(element);
var overlay;
if(element === document.body){
overlay = $(document.body).down('#overlay');
overlay.setStyle({display:'block'});
pydio.UI.disableShortcuts();
}else{
addLightboxMarkupToElement(element);
overlay = element.down("#element_overlay");
overlay.setStyle({opacity:0.9});
}
if(Prototype.Browser.IE && !Prototype.Browser.IE10plus){
$("all_forms").insert(box);
var outBox = element.up(".dialogBox");
Expand All @@ -364,60 +374,62 @@ Class.create("Modal", {
});
}
}else{
$(element).down("#element_overlay").insert({after:box});
$(element).down("#element_overlay").setStyle({opacity:0.9});
if(element.up('div.dialogBox')){
//Effect.BlindDown(box, {
// duration:0.6,
// transition:Effect.Transitions.sinoidal
//});
}
overlay.insert({after:box});
}
this.currentLightBoxElement = $(element);
this.currentLightBoxModal = box;
this.addSubmitCancel(content, cancelCallback, (cancelCallback==null), position);

var finish = function(){
content.down('div.dialogButtons').remove();
box.remove();
if(element !== document.body){
overlay.setStyle({opacity:0});
removeLightboxFromElement(element);
}else{
overlay.setStyle({display:null});
pydio.UI.enableShortcuts();
}
this.currentLightBoxElement = null;
this.currentLightBoxModal = null;
}.bind(this);
var finishWrapped = function(){
if(skipEffect){
finish();
}else{
Effect.BlindUp(box, {
duration:0.4,
afterFinish:finish
});
}
};

content.down(".dialogButtons").select("input").each(function(button){
if(((cancelCallback==null) && button.getAttribute("name") == "close") || button.getAttribute("name") == "ok"){
button.observe("click", function(event){
Event.stop(event);
var res = okCallback();
if(res){
Effect.BlindUp(box, {
duration:0.4,
afterFinish:function(){
content.down('div.dialogButtons').remove();
$(element).down("#element_overlay").setStyle({opacity:0});
box.remove();
removeLightboxFromElement(element);
this.currentLightBoxElement = null;
this.currentLightBoxModal = null;
}.bind(this)
});
if(okCallback()){
finishWrapped();
}
}.bind(this));
}else{
button.stopObserving("click");
button.observe("click", function(event){
Event.stop(event);
var res = cancelCallback();
if(res){
Effect.BlindUp(box, {
duration:0.4,
afterFinish:function(){
content.down('div.dialogButtons').remove();
if($(element).down("#element_overlay")) {
$(element).down("#element_overlay").setStyle({opacity:0});
}
box.remove();
removeLightboxFromElement(element);
this.currentLightBoxElement = null;
this.currentLightBoxModal = null;
}.bind(this)
});
if(cancelCallback()){
finishWrapped();
}
}.bind(this));
}
});
content.select('input[type="password"],input[type="text"]').invoke("observe", "keydown", function(event){
if(event.keyCode === Event.KEY_RETURN){
Event.stop(event);
if(okCallback()){
finishWrapped();
}
}
});
},


Expand Down
32 changes: 32 additions & 0 deletions core/src/plugins/gui.ajax/res/js/ui/prototype/class.PydioUI.js
Expand Up @@ -771,6 +771,38 @@ Class.create("PydioUI", {

enableAllKeyBindings : function(){
this.blockNavigation = this.blockShortcuts = this.blockEditorShortcuts = false;
},

openPromptDialog: function(json){
var dialogContent = new Element('div').update(json["DIALOG"]);
modal.showSimpleModal(document.body, dialogContent, function(){
// ok callback;
if(json["OK"]["GET_FIELDS"]){
var params = $H();
$A(json["OK"]["GET_FIELDS"]).each(function(fName){
params.set(fName, dialogContent.down('input[name="'+fName+'"]').getValue());
});
var conn = new Connexion();
conn.setParameters(params);
if(json["OK"]["EVAL"]){
conn.onComplete = function(){
eval(json["OK"]["EVAL"]);
};
}
conn.sendAsync();
}else{
if(json["OK"]["EVAL"]){
eval(json["OK"]["EVAL"]);
}
}
return true;
}, function(){
// cancel callback
if(json["CANCEL"]["EVAL"]){
eval(json["CANCEL"]["EVAL"]);
}
return true;
}, 'bottom', 'simple-modal-auth-prompt', true);
}

});

0 comments on commit 0510045

Please sign in to comment.