Skip to content

Commit

Permalink
Excluded callable methods cannot be called from an ajax request.
Browse files Browse the repository at this point in the history
  • Loading branch information
feuzeu committed Jan 25, 2025
1 parent 0004e66 commit 693652b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Plugin/Request/CallableClass/CallableClassPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,14 @@ public function processRequest()
$xCallableObject = $this->getCallable($sClassName);

$sError = 'errors.objects.call';
$xCallableObject->call($this->xTarget);
if(!$xCallableObject->excluded($sMethodName))
{
$xCallableObject->call($this->xTarget);
return;
}

// Unable to find the requested class or method
$this->throwException('', 'errors.objects.excluded', $aErrorParams);
}
catch(ReflectionException|SetupException $e)
{
Expand Down
8 changes: 7 additions & 1 deletion src/Plugin/Request/CallableClass/CallableObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,16 @@ public function getPublicMethods(bool $bTakeAll): array
}

/**
* @param string|null $sMethod
*
* @return bool
*/
public function excluded(): bool
public function excluded(?string $sMethod = null): bool
{
if($sMethod !== null && $this->isProtectedMethod($sMethod, false))
{
return true;
}
return $this->xOptions->excluded();
}

Expand Down
68 changes: 68 additions & 0 deletions tests/TestRequestHandler/ClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,72 @@ public function testRequestWithIncorrectMethodName()
$this->expectException(RequestException::class);
jaxon()->processRequest();
}

/**
* @throws SetupException
* @throws RequestException
*/
public function testRequestToExcludedClass()
{
jaxon()->app()->setOption('', true);
jaxon()->register(Jaxon::CALLABLE_CLASS, 'Excluded', [
'include' => __DIR__ . '/../src/excluded.php',
'functions' => [
'*' => [
'excluded' => true,
],
],
]);
// The server request
jaxon()->di()->set(ServerRequestInterface::class, function($c) {
return $c->g(ServerRequestCreator::class)
->fromGlobals()
->withParsedBody([
'jxncall' => json_encode([
'type' => 'class',
'name' => 'Excluded',
'method' => 'action',
'args' => [],
]),
]);
});

$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
$this->expectException(RequestException::class);
jaxon()->processRequest();
}

/**
* @throws SetupException
* @throws RequestException
*/
public function testRequestToExcludedMethod()
{
jaxon()->app()->setOption('', true);
jaxon()->register(Jaxon::CALLABLE_CLASS, 'Excluded', [
'include' => __DIR__ . '/../src/excluded.php',
'functions' => [
'action' => [
'excluded' => true,
],
],
]);
// The server request
jaxon()->di()->set(ServerRequestInterface::class, function($c) {
return $c->g(ServerRequestCreator::class)
->fromGlobals()
->withParsedBody([
'jxncall' => json_encode([
'type' => 'class',
'name' => 'Excluded',
'method' => 'action',
'args' => [],
]),
]);
});

$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
$this->expectException(RequestException::class);
jaxon()->processRequest();
}
}
1 change: 1 addition & 0 deletions translations/en/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'objects' => [
'call' => "An error occured during the call of method :method in of class :class.",
'invalid' => "Invalid object request received; no object :class or method :method found.",
'excluded' => "Trying to call the excluded method :method of class :class.",
'instance' => "To register a callable object, please provide an instance of the desired class.",
'invalid-declaration' => "Invalid object declaration.",
],
Expand Down
1 change: 1 addition & 0 deletions translations/es/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'objects' => [
'call' => "An error occured during the call of method :method in of class :class",
'invalid' => "Solicitud de objeto invalida recibida; Sin objeto :class o metodo :method encontrado.",
'excluded' => "Trying to call the excluded method :method of class :class.",
'instance' => "Para registrar un objeto, por favor de proveer una instancia de la clase deseada.",
'invalid-declaration' => "Declaración de objeto invalida.",
],
Expand Down
1 change: 1 addition & 0 deletions translations/fr/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'objects' => [
'call' => "Une erreur s'est produite à l'appel de la méthode :method de la classe :class.",
'invalid' => "La requête indique un objet invalide; il n'existe pas de classe :class ou de méthode :method.",
'excluded' => "La requête a essayé d'appeler la méthode :method de la classe :class, qui est exclue.",
'instance' => "Pour enregistrer un objet, vous devez fournir une instance de la classe correspondante.",
'invalid-declaration' => "La déclaration d'objet est invalide.",
],
Expand Down

0 comments on commit 693652b

Please sign in to comment.