-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Middleware): introduce InjectionMiddleware, with Table support f…
…irst - no more handcrafted Table instantiation and error handling on Controllers - switching to Attributes Signed-off-by: Arthur Schiwon <[email protected]>
- Loading branch information
Showing
4 changed files
with
174 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace OCA\Tables\Controller; | ||
|
||
use OCA\Tables\Db\Table; | ||
|
||
class AEnvironmentAwareOCSController extends AOCSController { | ||
protected ?Table $table; | ||
|
||
public function setTable(Table $table): void { | ||
$this->table = $table; | ||
} | ||
|
||
public function getTable(): ?Table { | ||
return $this->table; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Tables\Middleware\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD)] | ||
class RequireTable { | ||
public function __construct( | ||
protected bool $enhance = false, | ||
) { | ||
} | ||
|
||
public function enhance(): bool { | ||
return $this->enhance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace OCA\Tables\Middleware; | ||
|
||
use InvalidArgumentException; | ||
use OCA\Tables\Controller\AEnvironmentAwareOCSController; | ||
use OCA\Tables\Errors\InternalError; | ||
use OCA\Tables\Errors\NotFoundError; | ||
use OCA\Tables\Errors\PermissionError; | ||
use OCA\Tables\Middleware\Attribute\RequireTable; | ||
use OCA\Tables\Service\TableService; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\DataResponse; | ||
use OCP\AppFramework\Http\RedirectResponse; | ||
use OCP\AppFramework\Http\Response; | ||
use OCP\AppFramework\Middleware; | ||
use OCP\AppFramework\OCS\OCSException; | ||
use OCP\IL10N; | ||
use OCP\IRequest; | ||
use OCP\IURLGenerator; | ||
use OCP\IUserSession; | ||
use Psr\Log\LoggerInterface; | ||
use ReflectionAttribute; | ||
use ReflectionMethod; | ||
|
||
class InjectionMiddleware extends Middleware { | ||
public function __construct( | ||
protected IRequest $request, | ||
protected IURLGenerator $urlGenerator, | ||
protected IUserSession $userSession, | ||
protected LoggerInterface $logger, | ||
protected IL10N $l, | ||
protected TableService $tableService, | ||
) { | ||
} | ||
|
||
/** | ||
* @throws PermissionError | ||
* @throws \ReflectionException | ||
* @throws NotFoundError | ||
* @throws InternalError | ||
*/ | ||
public function beforeController($controller, $methodName): void { | ||
if (!$controller instanceof AEnvironmentAwareOCSController) { | ||
return; | ||
} | ||
|
||
$reflectionMethod = new ReflectionMethod($controller, $methodName); | ||
$attributes = $reflectionMethod->getAttributes(RequireTable::class); | ||
if (!empty($attributes)) { | ||
/** @var ReflectionAttribute $attribute */ | ||
$attribute = current($attributes); | ||
/** @var RequireTable $requireTableAttribute */ | ||
$requireTableAttribute = $attribute->newInstance(); | ||
$this->getTable($controller, $requireTableAttribute->enhance()); | ||
} | ||
} | ||
|
||
public function afterException($controller, $methodName, $exception): Response { | ||
if ($exception instanceof InvalidArgumentException) { | ||
throw new OCSException($exception->getMessage(), Http::STATUS_BAD_REQUEST, $exception); | ||
} | ||
|
||
$loggerOptions = [ | ||
'errorCode' => $exception->getCode(), | ||
'errorMessage' => $exception->getMessage(), | ||
'exception' => $exception, | ||
]; | ||
|
||
if ($exception instanceof NotFoundError) { | ||
$this->logger->warning('A not found error occurred: [{errorCode}] {errorMessage}', $loggerOptions); | ||
return new DataResponse(['message' => $this->l->t('A not found error occurred. More details can be found in the logs. Please reach out to your administration.')], Http::STATUS_NOT_FOUND); | ||
} | ||
if ($exception instanceof InternalError) { | ||
$this->logger->warning('An internal error or exception occurred: [{errorCode}] {errorMessage}', $loggerOptions); | ||
return new DataResponse(['message' => $this->l->t('An unexpected error occurred. More details can be found in the logs. Please reach out to your administration.')], Http::STATUS_INTERNAL_SERVER_ERROR); | ||
} | ||
if ($exception instanceof PermissionError) { | ||
$this->logger->warning('A permission error occurred: [{errorCode}] {errorMessage}', $loggerOptions); | ||
return new DataResponse(['message' => $this->l->t('A permission error occurred. More details can be found in the logs. Please reach out to your administration.')], Http::STATUS_FORBIDDEN); | ||
} | ||
|
||
$this->logger->warning('A unknown error occurred: [{errorCode}] {errorMessage}', $loggerOptions); | ||
return new RedirectResponse($this->urlGenerator->linkToDefaultPageUrl()); | ||
} | ||
|
||
/** | ||
* @throws NotFoundError | ||
* @throws PermissionError | ||
* @throws InternalError | ||
*/ | ||
protected function getTable(AEnvironmentAwareOCSController $controller, bool $enhance): void { | ||
$tableId = $this->request->getParam('id'); | ||
if ($tableId === null) { | ||
throw new InvalidArgumentException('Missing table ID.'); | ||
} | ||
|
||
$userId = $this->userSession->getUser()?->getUID(); | ||
|
||
$controller->setTable($this->tableService->find($tableId, !$enhance, $userId)); | ||
} | ||
} |