Skip to content

Commit

Permalink
Add controller different req var differnet methods
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-schindler committed May 20, 2022
1 parent 064c953 commit f1f3ce2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
11 changes: 7 additions & 4 deletions Backend/Core/System/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ abstract class Controller
protected array $methods = ['GET'];

/**
* @var string[] Required variables in the body of the request
* @var array<string,string[]> Required variables in the body of the request
*
* defined like this: ['POST' => ['param1', 'param2'], 'PATCH' => ['param3']]
*/
protected array $reqVar = [];

Expand Down Expand Up @@ -86,14 +88,15 @@ protected function redirect(string $url): never {
* @return int HTTP status code (200 === OK!)
*/
private function checkAccess(): int {
$method = IO::method();
header('Access-Control-Allow-Methods: ' . implode(', ', array_merge(['OPTIONS', 'HEAD'], $this->methods)));
if (empty(array_intersect(['*', 'OPTIONS', 'HEAD', IO::method()], $this->methods)))
if (empty(array_intersect(['*', 'OPTIONS', 'HEAD', $method], $this->methods)))
return 405;
if ($this->userRequired)
if (!Auth::validateToken())
return 401;
if (!in_array(IO::method(), ['GET', 'HEAD', 'DELETE', 'OPTIONS']))
foreach ($this->reqVar as $var)
if (!in_array($method, ['GET', 'HEAD', 'DELETE', 'OPTIONS']))
foreach ($this->reqVar[$method] as $var)
if (IO::body($var) === null)
return 400;
return 200;
Expand Down
12 changes: 6 additions & 6 deletions Backend/Libraries/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f1f3ce2

Please sign in to comment.