From 03454c8e2f18b0ea600fafb0dc2406baab034b28 Mon Sep 17 00:00:00 2001 From: Andrey Borysenko Date: Tue, 25 Jul 2023 18:28:11 +0300 Subject: [PATCH 1/3] config command fixes, remove deprecated annotation reflector --- lib/Command/ExAppConfig/SetConfig.php | 64 ++++++++++--------- lib/Db/ExAppConfigMapper.php | 15 ----- lib/Middleware/AppEcosystemAuthMiddleware.php | 28 +------- lib/Service/ExAppConfigService.php | 18 +++--- 4 files changed, 46 insertions(+), 79 deletions(-) diff --git a/lib/Command/ExAppConfig/SetConfig.php b/lib/Command/ExAppConfig/SetConfig.php index 2ab14604..e9d18504 100644 --- a/lib/Command/ExAppConfig/SetConfig.php +++ b/lib/Command/ExAppConfig/SetConfig.php @@ -32,7 +32,7 @@ protected function configure() { $this->addArgument('configkey', InputArgument::REQUIRED); $this->addOption('value', null, InputOption::VALUE_REQUIRED); - $this->addOption('sensitive', null, InputOption::VALUE_NONE, 'Sensitive config value'); + $this->addOption('sensitive', null, InputOption::VALUE_OPTIONAL, 'Sensitive config value', null); $this->addOption('update-only', null, InputOption::VALUE_NONE, 'Only update config, if not exists - do not create'); } @@ -40,41 +40,45 @@ protected function execute(InputInterface $input, OutputInterface $output): int $appId = $input->getArgument('appid'); $exApp = $this->service->getExApp($appId); if ($exApp === null) { - $output->writeln('ExApp ' . $appId . ' not found'); + $output->writeln(sprintf('ExApp %s not found', $appId)); return 1; } - if ($exApp->getEnabled()) { - $configKey = $input->getArgument('configkey'); - $value = $input->getOption('value'); - $sensitive = $input->getOption('sensitive'); - $updateOnly = $input->getOption('update-only'); + $configKey = $input->getArgument('configkey'); + $value = $input->getOption('value'); + $isSensitive = $input->getOption('sensitive'); + $sensitive = (int) filter_var($isSensitive, FILTER_VALIDATE_BOOLEAN); + $updateOnly = $input->getOption('update-only'); - if (!$updateOnly) { - $exAppConfig = $this->exAppConfigService->setAppConfigValue($appId, $configKey, $value, (int) $sensitive); - if ($exAppConfig === null) { - $output->writeln('ExApp ' . $appId . ' config ' . $configKey . ' not found'); - return 1; - } - } else { - $exAppConfig = $this->exAppConfigService->getAppConfig($appId, $configKey); - if ($exAppConfig === null) { - $output->writeln('ExApp ' . $appId . ' config ' . $configKey . ' not found'); - return 1; - } - $exAppConfig->setConfigvalue($value); - $exAppConfig->setSensitive((int) $sensitive); - if ($this->exAppConfigService->updateAppConfigValue($exAppConfig) !== 1) { - $output->writeln('ExApp ' . $appId . ' config ' . $configKey . ' not updated'); - return 1; - } + $exAppConfig = $this->exAppConfigService->getAppConfig($appId, $configKey); + if (!$updateOnly) { + if ($exAppConfig !== null) { + $output->writeln(sprintf('ExApp %s config %s already exists. Use --update-only flag.', $appId, $configKey)); + return 1; + } + + $exAppConfig = $this->exAppConfigService->setAppConfigValue($appId, $configKey, $value, $sensitive); + if ($exAppConfig === null) { + $output->writeln(sprintf('ExApp %s config %s not found', $appId, $configKey)); + return 1; + } + } else { + if ($exAppConfig === null) { + $output->writeln(sprintf('ExApp %s config %s not found', $appId, $configKey)); + return 1; + } + $exAppConfig->setConfigvalue($value); + if ($isSensitive !== null) { + $exAppConfig->setSensitive($sensitive); + } + if ($this->exAppConfigService->updateAppConfigValue($exAppConfig) === null) { + $output->writeln(sprintf('ExApp %s config %s not updated', $appId, $configKey)); + return 1; } - $sensitiveMsg = $sensitive ? '[sensitive]' : ''; - $output->writeln('ExApp ' . $appId . ' config ' . $configKey . ' set to ' . $value . ' ' . $sensitiveMsg); - return 0; } - $output->writeln('ExApp ' . $appId . ' is disabled'); - return 1; + $sensitiveMsg = $exAppConfig->getSensitive() === 1 ? '[sensitive]' : ''; + $output->writeln(sprintf('ExApp %s config %s set to %s %s', $appId, $configKey, $value, $sensitiveMsg)); + return 0; } } diff --git a/lib/Db/ExAppConfigMapper.php b/lib/Db/ExAppConfigMapper.php index 3c8cc97b..a1400280 100644 --- a/lib/Db/ExAppConfigMapper.php +++ b/lib/Db/ExAppConfigMapper.php @@ -71,21 +71,6 @@ public function findByAppConfigKeys(string $appId, array $configKeys): array { return $this->findEntities($qb); } - /** - * @throws Exception - */ - public function updateAppConfigValue(ExAppConfig $appConfigEx): int { - $qb = $this->db->getQueryBuilder(); - return $qb->update($this->tableName) - ->set('configvalue', $qb->createNamedParameter($appConfigEx->getConfigvalue(), IQueryBuilder::PARAM_STR)) - ->set('sensitive', $qb->createNamedParameter($appConfigEx->getSensitive(), IQueryBuilder::PARAM_INT)) - ->where( - $qb->expr()->eq('appid', $qb->createNamedParameter($appConfigEx->getAppid(), IQueryBuilder::PARAM_STR)), - $qb->expr()->eq('configkey', $qb->createNamedParameter($appConfigEx->getConfigkey(), IQueryBuilder::PARAM_STR)) - ) - ->executeStatement(); - } - /** * @throws Exception */ diff --git a/lib/Middleware/AppEcosystemAuthMiddleware.php b/lib/Middleware/AppEcosystemAuthMiddleware.php index a9fed935..19128c15 100644 --- a/lib/Middleware/AppEcosystemAuthMiddleware.php +++ b/lib/Middleware/AppEcosystemAuthMiddleware.php @@ -13,27 +13,23 @@ use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\Response; use OCP\AppFramework\Middleware; -use OCP\AppFramework\Utility\IControllerMethodReflector; use OCP\IL10N; use OCP\IRequest; use Psr\Log\LoggerInterface; use ReflectionMethod; class AppEcosystemAuthMiddleware extends Middleware { - private IControllerMethodReflector $reflector; private AppEcosystemV2Service $service; protected IRequest $request; private IL10N $l; private LoggerInterface $logger; public function __construct( - IControllerMethodReflector $reflector, - AppEcosystemV2Service $service, + AppEcosystemV2Service $service, IRequest $request, IL10N $l, LoggerInterface $logger, ) { - $this->reflector = $reflector; $this->service = $service; $this->request = $request; $this->l = $l; @@ -43,7 +39,7 @@ public function __construct( public function beforeController($controller, $methodName) { $reflectionMethod = new ReflectionMethod($controller, $methodName); - $isAppEcosystemAuth = $this->hasAnnotationOrAttribute($reflectionMethod, 'AppEcosystemAuth', AppEcosystemAuth::class); + $isAppEcosystemAuth = !empty($reflectionMethod->getAttributes(AppEcosystemAuth::class)); if ($isAppEcosystemAuth) { if (!$this->service->validateExAppRequestToNC($this->request)) { @@ -52,26 +48,6 @@ public function beforeController($controller, $methodName) { } } - /** - * @template T - * - * @param ReflectionMethod $reflectionMethod - * @param string $annotationName - * @param class-string $attributeClass - * @return boolean - */ - protected function hasAnnotationOrAttribute(ReflectionMethod $reflectionMethod, string $annotationName, string $attributeClass): bool { - if (!empty($reflectionMethod->getAttributes($attributeClass))) { - return true; - } - - if ($this->reflector->hasAnnotation($annotationName)) { - return true; - } - - return false; - } - /** * If an AEAuthNotValidException is being caught * diff --git a/lib/Service/ExAppConfigService.php b/lib/Service/ExAppConfigService.php index 5ce67296..25a72661 100644 --- a/lib/Service/ExAppConfigService.php +++ b/lib/Service/ExAppConfigService.php @@ -54,11 +54,11 @@ public function getAppConfigValues(string $appId, array $configKeys): ?array { * @param string $appId * @param string $configKey * @param mixed $configValue - * @param int $sensitive + * @param int|null $sensitive * * @return ExAppConfig|null */ - public function setAppConfigValue(string $appId, string $configKey, mixed $configValue, int $sensitive = 0): ?ExAppConfig { + public function setAppConfigValue(string $appId, string $configKey, mixed $configValue, ?int $sensitive = null): ?ExAppConfig { $appConfigEx = $this->getAppConfig($appId, $configKey); if ($appConfigEx === null) { try { @@ -66,7 +66,7 @@ public function setAppConfigValue(string $appId, string $configKey, mixed $confi 'appid' => $appId, 'configkey' => $configKey, 'configvalue' => $configValue ?? '', - 'sensitive' => $sensitive, + 'sensitive' => $sensitive ?? 0, ])); } catch (Exception $e) { $this->logger->error(sprintf('Failed to insert appconfig_ex value. Error: %s', $e->getMessage()), ['exception' => $e]); @@ -74,8 +74,10 @@ public function setAppConfigValue(string $appId, string $configKey, mixed $confi } } else { $appConfigEx->setConfigvalue($configValue); - $appConfigEx->setSensitive($sensitive); - if ($this->updateAppConfigValue($appConfigEx) !== 1) { + if ($sensitive !== null) { + $appConfigEx->setSensitive($sensitive); + } + if ($this->updateAppConfigValue($appConfigEx) === null) { $this->logger->error(sprintf('Error while updating appconfig_ex %s value.', $configKey)); return null; } @@ -129,11 +131,11 @@ public function getAppConfig(mixed $appId, mixed $configKey): ?ExAppConfig { /** * @param ExAppConfig $exAppConfig * - * @return int|null + * @return ExAppConfig|null */ - public function updateAppConfigValue(ExAppConfig $exAppConfig): ?int { + public function updateAppConfigValue(ExAppConfig $exAppConfig): ?ExAppConfig { try { - return $this->mapper->updateAppConfigValue($exAppConfig); + return $this->mapper->update($exAppConfig); } catch (Exception) { return null; } From e255d8aa1f355188bbbf3e165578381c50750471 Mon Sep 17 00:00:00 2001 From: Andrey Borysenko Date: Tue, 25 Jul 2023 18:33:54 +0300 Subject: [PATCH 2/3] cs fix --- lib/Middleware/AppEcosystemAuthMiddleware.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Middleware/AppEcosystemAuthMiddleware.php b/lib/Middleware/AppEcosystemAuthMiddleware.php index 19128c15..cb0e7a24 100644 --- a/lib/Middleware/AppEcosystemAuthMiddleware.php +++ b/lib/Middleware/AppEcosystemAuthMiddleware.php @@ -25,7 +25,7 @@ class AppEcosystemAuthMiddleware extends Middleware { private LoggerInterface $logger; public function __construct( - AppEcosystemV2Service $service, + AppEcosystemV2Service $service, IRequest $request, IL10N $l, LoggerInterface $logger, From e5b4cb1e9ff13e09711760549d573fc1287024ea Mon Sep 17 00:00:00 2001 From: Andrey Borysenko Date: Tue, 25 Jul 2023 21:23:00 +0300 Subject: [PATCH 3/3] fix docs-check ci --- .github/workflows/docs-check.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/docs-check.yml b/.github/workflows/docs-check.yml index 45e99f83..113c8588 100644 --- a/.github/workflows/docs-check.yml +++ b/.github/workflows/docs-check.yml @@ -1,9 +1,6 @@ name: Docs check on: pull_request: - paths: - - '.github/workflows/docs-check.yml' - - 'docs/**' permissions: contents: read