X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModule%2FBaseApi.php;h=dc75b0dd262fc0da5a3ad206fc80e563a45323f2;hb=3bca4fe2a64671d09e08346456cdfa6c12f996e9;hp=c73f90aba62676a1eecd7f05c483d74b4bac9550;hpb=a2a1d852e9a17059dcbcf5e8bf5356d42a3998cf;p=friendica.git diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index c73f90aba6..dc75b0dd26 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -1,6 +1,6 @@ app = $app; + $this->app = $app; + $this->errorFactory = $errorFactory; } /** @@ -93,7 +97,7 @@ class BaseApi extends BaseModule case Router::PATCH: case Router::POST: case Router::PUT: - self::checkAllowedScope(self::SCOPE_WRITE); + $this->checkAllowedScope(self::SCOPE_WRITE); if (!self::getCurrentUserID()) { throw new HTTPException\ForbiddenException($this->t('Permission denied.')); @@ -414,23 +418,23 @@ class BaseApi extends BaseModule * * @param string $scope the requested scope (read, write, follow, push) */ - public static function checkAllowedScope(string $scope) + public function checkAllowedScope(string $scope) { $token = self::getCurrentApplication(); if (empty($token)) { - Logger::notice('Empty application token'); - DI::mstdnError()->Forbidden(); + $this->logger->notice('Empty application token'); + $this->logAndJsonError(403, $this->errorFactory->Forbidden()); } if (!isset($token[$scope])) { - Logger::warning('The requested scope does not exist', ['scope' => $scope, 'application' => $token]); - DI::mstdnError()->Forbidden(); + $this->logger->warning('The requested scope does not exist', ['scope' => $scope, 'application' => $token]); + $this->logAndJsonError(403, $this->errorFactory->Forbidden()); } if (empty($token[$scope])) { - Logger::warning('The requested scope is not allowed', ['scope' => $scope, 'application' => $token]); - DI::mstdnError()->Forbidden(); + $this->logger->warning('The requested scope is not allowed', ['scope' => $scope, 'application' => $token]); + $this->logAndJsonError(403, $this->errorFactory->Forbidden()); } } @@ -515,4 +519,16 @@ class BaseApi extends BaseModule return null; } + + /** + * @param int $errorno + * @param Error $error + * @return void + * @throws HTTPException\InternalServerErrorException + */ + protected function logAndJsonError(int $errorno, Error $error) + { + $this->logger->info('API Error', ['no' => $errorno, 'error' => $error->toArray(), 'method' => $this->args->getMethod(), 'command' => $this->args->getQueryString(), 'user-agent' => $this->server['HTTP_USER_AGENT'] ?? '']); + $this->jsonError(403, $error->toArray()); + } }