diff --git a/lib/Service/AppAPIService.php b/lib/Service/AppAPIService.php index ba4654ac..b2266e60 100644 --- a/lib/Service/AppAPIService.php +++ b/lib/Service/AppAPIService.php @@ -213,7 +213,7 @@ public function enableExApp(ExApp $exApp): bool { $exApp->setEnabled(1); $this->cache->set($cacheKey, $exApp, self::CACHE_TTL); - $exAppEnabled = $this->requestToExApp(null, null, $exApp, '/enabled?enabled=1', 'PUT'); + $exAppEnabled = $this->requestToExApp($exApp, '/enabled?enabled=1', null, 'PUT'); if ($exAppEnabled instanceof IResponse) { $response = json_decode($exAppEnabled->getBody(), true); if (isset($response['error']) && strlen($response['error']) === 0) { @@ -249,7 +249,7 @@ public function enableExApp(ExApp $exApp): bool { */ public function disableExApp(ExApp $exApp): bool { try { - $exAppDisabled = $this->requestToExApp(null, null, $exApp, '/enabled?enabled=0', 'PUT'); + $exAppDisabled = $this->requestToExApp($exApp, '/enabled?enabled=0', null, 'PUT'); if ($exAppDisabled instanceof IResponse) { $response = json_decode($exAppDisabled->getBody(), true); if (isset($response['error']) && strlen($response['error']) !== 0) { @@ -416,8 +416,6 @@ public function dispatchExAppInit(ExApp $exApp): void { ) . '/init'; $options = [ - 'Accept' => 'application/json', - 'Content-Type' => 'application/json', 'headers' => $this->buildAppAPIAuthHeaders(null, null, $exApp), 'nextcloud' => [ 'allow_local_address' => true, @@ -505,22 +503,24 @@ public function getLatestExAppInfoFromAppstore(string $appId): ?\SimpleXMLElemen /** * Request to ExApp with AppAPI auth headers and ExApp user initialization * - * @param IRequest|null $request - * @param string $userId * @param ExApp $exApp * @param string $route + * @param string $userId * @param string $method * @param array $params + * @param array $options + * @param IRequest|null $request * * @return array|IResponse */ public function aeRequestToExApp( - ?IRequest $request, - string $userId, ExApp $exApp, string $route, + string $userId, string $method = 'POST', - array $params = [] + array $params = [], + array $options = [], + ?IRequest $request = null, ): array|IResponse { try { $this->exAppUsersService->setupExAppUser($exApp, $userId); @@ -528,28 +528,88 @@ public function aeRequestToExApp( $this->logger->error(sprintf('Error while inserting ExApp %s user. Error: %s', $exApp->getAppid(), $e->getMessage()), ['exception' => $e]); return ['error' => 'Error while inserting ExApp user: ' . $e->getMessage()]; } - return $this->requestToExApp($request, $userId, $exApp, $route, $method, $params); + return $this->requestToExApp($exApp, $userId, $route, $method, $params, $options, $request); } /** - * Request to ExApp with AppAPI auth headers + * Request to ExApp by appId with AppAPI auth headers and ExApp user initialization * + * @param string $appId + * @param string $route + * @param string $userId + * @param string $method + * @param array $params + * @param array $options * @param IRequest|null $request + * + * @return array|IResponse + */ + public function aeRequestToExAppById( + string $appId, + string $route, + string $userId, + string $method = 'POST', + array $params = [], + array $options = [], + ?IRequest $request = null, + ): array|IResponse { + $exApp = $this->getExApp($appId); + if ($exApp === null) { + return ['error' => 'ExApp not found']; + } + return $this->aeRequestToExApp($exApp, $route, $userId, $method, $params, $options, $request); + } + + /** + * Request to ExApp by appId with AppAPI auth headers + * + * @param string $appId + * @param string $route * @param string|null $userId + * @param string $method + * @param array $params + * @param array $options + * @param IRequest|null $request + * + * @return array|IResponse + */ + public function requestToExAppById( + string $appId, + string $route, + ?string $userId = null, + string $method = 'POST', + array $params = [], + array $options = [], + ?IRequest $request = null, + ): array|IResponse { + $exApp = $this->getExApp($appId); + if ($exApp === null) { + return ['error' => 'ExApp not found']; + } + return $this->requestToExApp($exApp, $route, $userId, $method, $params, $options, $request); + } + + /** + * Request to ExApp with AppAPI auth headers + * * @param ExApp $exApp * @param string $route + * @param string|null $userId * @param string $method * @param array $params + * @param array $options + * @param IRequest|null $request * * @return array|IResponse */ public function requestToExApp( - ?IRequest $request, - ?string $userId, ExApp $exApp, string $route, + ?string $userId = null, string $method = 'POST', - array $params = [] + array $params = [], + array $options = [], + ?IRequest $request = null, ): array|IResponse { $this->handleExAppDebug($exApp, $request, true); try { @@ -558,11 +618,13 @@ public function requestToExApp( $exApp->getHost(), $exApp->getPort()) . $route; - $options = [ - 'headers' => $this->buildAppAPIAuthHeaders($request, $userId, $exApp), - 'nextcloud' => [ - 'allow_local_address' => true, // it's required as we are using ExApp appid as hostname (usually local) - ], + if (is_array($options['headers'])) { + $options['headers'] = [...$options['headers'], ...$this->buildAppAPIAuthHeaders($request, $userId, $exApp)]; + } else { + $options['headers'] = $this->buildAppAPIAuthHeaders($request, $userId, $exApp); + } + $options['nextcloud'] = [ + 'allow_local_address' => true, // it's required as we are using ExApp appid as hostname (usually local) ]; if (count($params) > 0) { diff --git a/lib/Service/ExFilesActionsMenuService.php b/lib/Service/ExFilesActionsMenuService.php index 75663ce3..880f5815 100644 --- a/lib/Service/ExFilesActionsMenuService.php +++ b/lib/Service/ExFilesActionsMenuService.php @@ -174,7 +174,7 @@ public function handleFileAction(string $userId, string $appId, string $fileActi ]; $exApp = $this->appAPIService->getExApp($appId); if ($exApp !== null) { - $result = $this->appAPIService->aeRequestToExApp($this->request, $userId, $exApp, $handler, 'POST', $params); + $result = $this->appAPIService->aeRequestToExApp($exApp, $handler, $userId, 'POST', $params, [], $this->request); if ($result instanceof IResponse) { return $result->getStatusCode() === 200; }