Commit 53ad72a1 authored by Jordan Breton's avatar Jordan Breton

ActionRouter now try to find modules action and reject not imported packages

parent f6ba38a5
......@@ -12,7 +12,7 @@ use wfw\daemons\modelSupervisor\server\MSServerPool;
use wfw\daemons\modelSupervisor\server\requestHandler\MSServerRequestHandlerManager;
use wfw\daemons\modelSupervisor\socket\protocol\MSServerSocketProtocol;
use wfw\engine\core\conf\WFWModulesCollector;
use wfw\engine\core\conf\WFW;
use wfw\engine\core\data\DBAccess\NOSQLDB\kvs\KVSAccess;
use wfw\engine\core\data\model\loaders\KVStoreBasedModelLoader;
use wfw\engine\lib\cli\argv\ArgvOpt;
......@@ -66,7 +66,7 @@ try{
if(!is_null($pPath = $confs->getProjectPath($name)))
(new Autoloader([],$pPath))->register(false,true);
WFWModulesCollector::collectModules();
WFW::collectModules();
if($oldPID){
sleep(10);//ugly but let the time for childs to die;...
......@@ -105,7 +105,7 @@ try{
$confs->getKVSContainer($name),
$confs->getKVSDefaultStorage($name) ?? null
),
WFWModulesCollector::models()
WFW::models()
),
$confs->getUsers($name),
$confs->getGroups($name),
......
......@@ -4,6 +4,7 @@
"core" : {
"app" : {
"ACTION_HANDLER_NOT_FOUND" : "ActionHandler [[$]] doesn't exists",
"DISABLED_MODULE" : "Trying to access to a disabled module/package.",
"INTERNAL_ERROR" : "Internal error : [$]",
"MUST_BE_LOGGED" : "You must be connected.",
"ACCESS_DENIED" : "Access denied",
......
......@@ -4,8 +4,9 @@
"core" : {
"app" : {
"ACTION_HANDLER_NOT_FOUND" : "L'ActionHandler [[$]] n'existe pas !",
"DISABLED_MODULE" : "Le package et/ou module auquel vous tentez d'accéder n'est pas activé.",
"INTERNAL_ERROR" : "Erreur interne : [$]",
"MUST_BE_LOGGED" : "Vous devez être connecté pour effectuer cette action !",
"MUST_BE_LOGGED" : "Vous devez être connecté pour effectuer cette action.",
"ACCESS_DENIED" : "Accès refusé",
"INSUFFICIENT_PRIVILEGIES":"Privilèges utilisateur insuffisants.",
"BAD_CSRF_TOKEN" : "Token CSRF invalide.",
......
<?php
namespace wfw\engine\core\action;
use wfw\engine\core\action\errors\ActionHandlerNotEnabled;
use wfw\engine\core\action\errors\ActionHandlerNotFound;
use wfw\engine\core\action\errors\InvalidActionHandler;
use wfw\engine\core\conf\IConf;
/**
* Router d'actions de base.
*/
final class ActionRouter implements IActionRouter {
/** @var string[] $_enabledPackages */
private $_enabledPackages;
/** @var IActionHandlerFactory $_factory */
private $_factory;
/** @var int $_foldingLimit */
......@@ -16,42 +20,71 @@ final class ActionRouter implements IActionRouter {
/**
* ActionRouter constructor.
*
* @param IActionHandlerFactory $factory Factory pour les ActionHandlers
* @param int $foldingLimit (optionnel defaut : 5) Limite du nombre de sous
* repertoirs de recherche. Si la limite est atteinte, et que le
* handler n'est pas trouvé, l'exception HandlerNotFound sera levée.
* @param IConf $conf
* @param IActionHandlerFactory $factory Factory pour les ActionHandlers
* @param int $foldingLimit (optionnel defaut : 5) Limite du nombre de sous
* repertoirs de recherche. Si la limite est atteinte, et que le
* handler n'est pas trouvé, l'exception HandlerNotFound sera levée.
*/
public function __construct(IActionHandlerFactory $factory,int $foldingLimit = 5) {
public function __construct(IConf $conf,IActionHandlerFactory $factory,int $foldingLimit = 5) {
$this->_enabledPackages = array_flip($conf->getArray("server/packages") ?? []);
$this->_factory = $factory;
$this->_foldingLimit = $foldingLimit;
}
/**
* @param string $package
* @param null|string $location
* @return bool
*/
private function enabledPackage(string $package, ?string $location = null):bool{
if(is_null($location)) return isset($this->_enabledPackages["site/$package"])
|| isset($this->_enabledPackages["modules/$package"])
|| isset($this->_enabledPackages["engine/$package"]);
else return isset($this->_enabledPackages["$location/$package"]);
}
/**
* @param IAction $action Action à router
* @return IActionHandler Handler destinataire de l'action
*/
public function findActionHandler(IAction $action): IActionHandler {
$path = explode('/',$action->getInternalPath());
if(!is_null($package = array_shift($path))){
$handlerClass = "package\\$package\\handlers\\action";
if(!empty($this->_enabledPackages) && !is_null($package = array_shift($path))){
$handlerClass = "handlers\\action";
$modulePackage = $package;
$moduleHandlerClass = $handlerClass;
$handlerFound = false;
$folding = 0;
while(!is_null($part = array_shift($path)) && $folding<$this->_foldingLimit){
$handlerClass.="\\";
$tmpName = ucfirst($part)."Handler";
if(class_exists("wfw\\site\\".$handlerClass.$tmpName)){
$handlerClass = "wfw\\site\\".$handlerClass.$tmpName;
if(class_exists("wfw\\site\\package\\$package\\".$handlerClass.$tmpName)){
$handlerClass = "wfw\\site\\package\\$package\\".$handlerClass.$tmpName;
$handlerFound = true;
break;
}else if(class_exists("wfw\\engine\\".$handlerClass.$tmpName)){
$handlerClass = "wfw\\engine\\".$handlerClass.$tmpName;
}else if(class_exists("wfw\\modules\\$modulePackage\\".$moduleHandlerClass.$tmpName)){
$handlerClass = "wfw\\modules\\$modulePackage\\".$moduleHandlerClass.$tmpName;
$handlerFound = true;
break;
}else if(class_exists("wfw\\engine\\package\\$package\\".$handlerClass.$tmpName)){
$handlerClass = "wfw\\engine\\package\\$package\\".$handlerClass.$tmpName;
$handlerFound = true;
break;
}else{
$handlerClass.=$part;
if($this->enabledPackage($modulePackage,"modules"))
$moduleHandlerClass.="\\$part\\";
else $modulePackage.="\\$part";
}
}
if($handlerFound && !(new \ReflectionClass($handlerClass))->isAbstract()){
if(!$this->enabledPackage($package) && !$this->enabledPackage($modulePackage,"modules")) {
throw new ActionHandlerNotEnabled(
"Action found : $handlerClass, but the '$package' package or the module '$modulePackage' haven't been enabled. "
."Please check your project configuration and add this package to the 'server/packages' list."
);
}
try{
return $this->_factory->create(
$handlerClass,
......
<?php
namespace wfw\engine\core\action\errors;
/**
* Thrown when an action have been found, but it's package were not enabled.
*/
final class ActionHandlerNotEnabled extends ActionResolutionFailure {}
\ No newline at end of file
<?php
namespace wfw\engine\core\app;
use wfw\engine\core\action\errors\ActionHandlerNotEnabled;
use wfw\engine\core\action\errors\ActionResolutionFailure;
use wfw\engine\core\notifier\Message;
use wfw\engine\core\response\responses\ErrorResponse;
......@@ -50,26 +51,25 @@ final class WebApp {
$actionRouter = $this->_context->getActionRouter();
$handler = $actionRouter->findActionHandler($action);
$response = $handler->handle($action);
}catch(ActionHandlerNotEnabled $e){
$response = new ErrorResponse(
HTTPStatus::FORBIDDEN,
$translator->get("server/engine/core/app/DISABLED_MODULE")
);
}catch(ActionResolutionFailure $e){
if($action->getRequest()->isAjax()){
$response = new ErrorResponse(
500,
$translator->getTranslateAndReplace(
"server/engine/core/app/ACTION_HANDLER_NOT_FOUND",
null,
$action->getInternalPath()
)
);
}else{
$response = new StaticResponse($action);
}
if($action->getRequest()->isAjax()) $response = new ErrorResponse(
HTTPStatus::INTERNAL_SERVER_ERROR,
$translator->getAndReplace(
"server/engine/core/app/ACTION_HANDLER_NOT_FOUND",
$action->getInternalPath()
)
);
else $response = new StaticResponse($action);
}catch(\Error $e){
$response = new ErrorResponse(
500,
$translator->getTranslateAndReplace(
"server/engine/core/app/INTERNAL_ERROR",
null,
$e
HTTPStatus::INTERNAL_SERVER_ERROR,
$translator->getAndReplace(
"server/engine/core/app/INTERNAL_ERROR",$e
)
);
}
......@@ -77,7 +77,7 @@ final class WebApp {
if(is_null($permission->getResponse())){
if($action->getRequest()->isAjax()){
$response = new ErrorResponse(
100,
HTTPStatus::FORBIDDEN,
$translator->getAndTranslate(
"server/engine/core/app/MUST_BE_LOGGED"
)
......@@ -109,10 +109,9 @@ final class WebApp {
$handler = $responseRouter->findResponseHandler($action,$response);
}catch(ResponseResolutionFailure $e){
$response = new ErrorResponse(
404,
$translator->getTranslateAndReplace(
HTTPStatus::NOT_FOUND,
$translator->getAndReplace(
"server/engine/core/app/404_NOT_FOUND",
null,
$action->getInternalPath()
)
);
......
......@@ -24,7 +24,7 @@ use wfw\engine\core\command\security\CommandSecurityCenter;
use wfw\engine\core\command\security\ICommandSecurityCenter;
use wfw\engine\core\command\security\rules\CommandAccessRulesCollector;
use wfw\engine\core\command\SynchroneCommandBus;
use wfw\engine\core\conf\WFWModulesCollector;
use wfw\engine\core\conf\WFW;
use wfw\engine\core\data\DBAccess\NOSQLDB\msServer\IMSServerAccess;
use wfw\engine\core\data\DBAccess\NOSQLDB\msServer\MSServerWriterAccess;
use wfw\engine\core\data\DBAccess\SQLDB\IDBAccess;
......@@ -520,19 +520,19 @@ class DefaultContext implements IWebAppContext {
protected function loadModules():void{
$modules = $this->getCacheSystem()->get(self::CACHE_KEYS[self::MODULES]);
if(is_null($modules)){
WFWModulesCollector::collectModules();
WFW::collectModules();
$this->getCacheSystem()->set(
self::CACHE_KEYS[self::MODULES],
WFWModulesCollector::modules()
WFW::modules()
);
}else WFWModulesCollector::restoreModulesFromCache($modules);
}else WFW::restoreModulesFromCache($modules);
}
/**
* @return array
*/
protected function getDi():array{
return WFWModulesCollector::di();
return WFW::di();
}
/**
......@@ -541,7 +541,7 @@ class DefaultContext implements IWebAppContext {
protected function getDomainEventListeners():array{
$listeners = $this->getCacheSystem()->get(self::CACHE_KEYS[self::DOMAIN_EVENT_LISTENERS]);
if(is_null($listeners)){
$listeners = WFWModulesCollector::domainEventListeners();
$listeners = WFW::domainEventListeners();
$this->getCacheSystem()->set(self::CACHE_KEYS[self::DOMAIN_EVENT_LISTENERS],$listeners);
}
return $listeners;
......@@ -553,7 +553,7 @@ class DefaultContext implements IWebAppContext {
protected function getCommandHandlers():array{
$handlers = $this->getCacheSystem()->get(self::CACHE_KEYS[self::COMMAND_HANDLERS]);
if(is_null($handlers)){
$handlers = WFWModulesCollector::commandHandlers();
$handlers = WFW::commandHandlers();
$this->getCacheSystem()->set(self::CACHE_KEYS[self::DOMAIN_EVENT_LISTENERS],$handlers);
}
return $handlers;
......@@ -565,7 +565,7 @@ class DefaultContext implements IWebAppContext {
protected function getConfs():array{
$confFiles = $this->getCacheSystem()->get(self::CACHE_KEYS[self::CONF_FILES]);
if(is_null($confFiles)){
$confFiles = WFWModulesCollector::confs();
$confFiles = WFW::confs();
$this->getCacheSystem()->set(self::CACHE_KEYS[self::CONF_FILES],$confFiles);
}
return $confFiles;
......@@ -578,7 +578,7 @@ class DefaultContext implements IWebAppContext {
protected function getAccessRules(?array $access=null):array{
$rules = $this->getCacheSystem()->get(self::CACHE_KEYS[self::ACCESS_RULES]);
if(is_null($rules)){
$rules = WFWModulesCollector::accessPolicy(!empty($access) ? $access : null);
$rules = WFW::accessPolicy(!empty($access) ? $access : null);
$this->getCacheSystem()->set(self::CACHE_KEYS[self::ACCESS_RULES],$rules);
}
return $rules;
......@@ -591,7 +591,7 @@ class DefaultContext implements IWebAppContext {
protected function getCommandRules(?array $commands=null):array{
$rules = $this->getCacheSystem()->get(self::CACHE_KEYS[self::COMMAND_RULES]);
if(is_null($rules)){
$rules = WFWModulesCollector::commandsPolicy(!empty($commands) ? $commands : null);
$rules = WFW::commandsPolicy(!empty($commands) ? $commands : null);
$this->getCacheSystem()->set(self::CACHE_KEYS[self::COMMAND_RULES],$rules);
}
return $rules;
......@@ -604,7 +604,7 @@ class DefaultContext implements IWebAppContext {
protected function getQueryRules(?array $queries=null):array{
$rules = $this->getCacheSystem()->get(self::CACHE_KEYS[self::QUERY_RULES]);
if(is_null($rules)){
$rules = WFWModulesCollector::queriesPolicy(!empty($queries) ? $queries : null);
$rules = WFW::queriesPolicy(!empty($queries) ? $queries : null);
$this->getCacheSystem()->set(self::CACHE_KEYS[self::QUERY_RULES],$rules);
}
return $rules;
......@@ -617,7 +617,7 @@ class DefaultContext implements IWebAppContext {
protected function getHooks(?array $hooks=null):array{
$hooksPolicy = $this->getCacheSystem()->get(self::CACHE_KEYS[self::HOOKS]);
if(is_null($hooksPolicy)){
$hooksPolicy = WFWModulesCollector::hooksPolicy(!empty($hooks) ? $hooks : null);
$hooksPolicy = WFW::hooksPolicy(!empty($hooks) ? $hooks : null);
$this->getCacheSystem()->set(self::CACHE_KEYS[self::HOOKS],$hooksPolicy);
}
return $hooksPolicy;
......@@ -630,7 +630,7 @@ class DefaultContext implements IWebAppContext {
protected function getLangs(array $langs):array{
$lc = $this->getCacheSystem()->get(self::CACHE_KEYS[self::LANGS]);
if(is_null($lc)){
$langs = WFWModulesCollector::langs($langs);
$langs = WFW::langs($langs);
$this->getCacheSystem()->set(self::CACHE_KEYS[self::LANGS],$langs);
}else $langs = $lc;
return $langs;
......
......@@ -8,7 +8,7 @@ use wfw\engine\core\security\WFWDefaultSecurityPolicy;
/**
* Base descriptor
*/
final class WFWModulesCollector extends ModuleDescriptor implements IAppModulesCollector, ISecurityPolicy {
final class WFW extends ModuleDescriptor implements IAppModulesCollector, ISecurityPolicy {
private static $_modules = [];
private static $_collected = false;
......
<?php
namespace wfw\engine\core\response\errors;
/**
* Thrown when the module/package that owns a response havn't been enabled in configurations.
*/
final class ResponseHandlerNotEnabled extends ResponseResolutionFailure {}
\ No newline at end of file
......@@ -24,6 +24,9 @@ final class HTTPStatus extends PHPEnum {
public const MULTI_STATUS = 207;
public const ALREADY_REPORTED = 208;
public const IM_USED = 226;
//non-standard
public const DONE = 249;
public const DATA = 250;
//300
public const MUTLIPLE_CHOICES = 300;
......
......@@ -48,7 +48,7 @@ wfw.define("plugins/miel/medias",function($params){
['title','description'].forEach($e=>{
if(!($e in $params.editable)) $params.editable[$e] = false;
});
let $uniques = {};
wfw.dom.appendTo($window,
wfw.dom.appendTo(wfw.dom.create("div",{className:"head"}),
wfw.dom.appendTo(wfw.dom.create("div",{className:"title"}),
......@@ -71,9 +71,15 @@ wfw.define("plugins/miel/medias",function($params){
wfw.dom.create("button",{innerHTML:$lstr('SAVE'),on:{click:()=>{
$update(
$node.getAttribute('data-miel_key'),
Array.from($o.list.querySelectorAll(".item")).map($n=>$compileListNode($n)),
Array.from($o.list.querySelectorAll(".item")).map($n=>$compileListNode($n)).filter($i=>{
if(!($i.file in $uniques)){
$uniques[$i.file]=true;
return true;
} else return false;
}),
$window
);
$uniques = {};
$window.parentNode.removeChild($window)
}}}),
wfw.dom.create("button",{innerHTML:$lstr('CANCEL'),on:{
......@@ -236,6 +242,7 @@ wfw.define("plugins/miel/medias",function($params){
);
};
let $update = ($k,$arr,$window)=>{
console.log($arr);
let $loader = $displayLoader($lstr("WAIT_UPDATE"),$window.querySelector(".body"));
wfw.network.wfwAPI(wfw.webroot+"miel/update",{
type : "POST",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment