-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #861 from nextcloud/enh/noid/contexts-api
Contexts API
- Loading branch information
Showing
24 changed files
with
3,501 additions
and
29 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
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
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,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Tables\Command; | ||
|
||
use OC\Core\Command\Base; | ||
use OCA\Tables\Errors\InternalError; | ||
use OCA\Tables\Service\ContextService; | ||
use OCP\DB\Exception; | ||
use OCP\IConfig; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use function json_decode; | ||
use function json_encode; | ||
|
||
class ListContexts extends Base { | ||
protected ContextService $contextService; | ||
protected LoggerInterface $logger; | ||
private IConfig $config; | ||
|
||
public function __construct( | ||
ContextService $contextService, | ||
LoggerInterface $logger, | ||
IConfig $config, | ||
) { | ||
parent::__construct(); | ||
$this->contextService = $contextService; | ||
$this->logger = $logger; | ||
$this->config = $config; | ||
} | ||
|
||
protected function configure(): void { | ||
parent::configure(); | ||
$this | ||
->setName('tables:contexts:list') | ||
->setDescription('Get all contexts or contexts available to a specified user') | ||
->addArgument( | ||
'user-id', | ||
InputArgument::OPTIONAL, | ||
'User ID of the user' | ||
) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int { | ||
$userId = trim($input->getArgument('user-id')); | ||
if ($userId === '') { | ||
$userId = null; | ||
} | ||
|
||
try { | ||
$contexts = $this->contextService->findAll($userId); | ||
} catch (InternalError|Exception $e) { | ||
$output->writeln('Error while reading contexts from DB.'); | ||
$this->logger->warning('Following error occurred during executing occ command "{class}"', | ||
[ | ||
'app' => 'tables', | ||
'class' => self::class, | ||
'exception' => $e, | ||
] | ||
); | ||
if ($this->config->getSystemValueBool('debug', false)) { | ||
$output->writeln(sprintf('<warning>%s</warning>', $e->getMessage())); | ||
$output->writeln('<error>'); | ||
debug_print_backtrace(); | ||
$output->writeln('</error>'); | ||
} | ||
return 1; | ||
} | ||
|
||
foreach ($contexts as $context) { | ||
$contextArray = json_decode(json_encode($context), true); | ||
|
||
$contextArray['ownerType'] = match ($contextArray['ownerType']) { | ||
1 => 'group', | ||
default => 'user', | ||
}; | ||
|
||
$out = ['ID ' . $contextArray['id'] => $contextArray]; | ||
unset($out[$contextArray['id']]['id']); | ||
$this->writeArrayInOutputFormat($input, $output, $out); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
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,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Tables\Command; | ||
|
||
use OC\Core\Command\Base; | ||
use OCA\Tables\Errors\InternalError; | ||
use OCA\Tables\Service\ContextService; | ||
use OCP\DB\Exception; | ||
use OCP\IConfig; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use function json_decode; | ||
use function json_encode; | ||
|
||
class ShowContext extends Base { | ||
protected ContextService $contextService; | ||
protected LoggerInterface $logger; | ||
private IConfig $config; | ||
|
||
public function __construct( | ||
ContextService $contextService, | ||
LoggerInterface $logger, | ||
IConfig $config, | ||
) { | ||
parent::__construct(); | ||
$this->contextService = $contextService; | ||
$this->logger = $logger; | ||
$this->config = $config; | ||
} | ||
|
||
protected function configure(): void { | ||
parent::configure(); | ||
$this | ||
->setName('tables:contexts:show') | ||
->setDescription('Get all contexts or contexts available to a specified user') | ||
->addArgument( | ||
'context-id', | ||
InputArgument::REQUIRED, | ||
'The ID of the context to show' | ||
) | ||
->addArgument( | ||
'user-id', | ||
InputArgument::OPTIONAL, | ||
'Optionally, showing the context from the perspective of the given user' | ||
) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int { | ||
$contextId = trim($input->getArgument('context-id')); | ||
if ($contextId === '' || !is_numeric($contextId)) { | ||
$output->writeln('<error>Invalid Context ID</error>'); | ||
return 1; | ||
} | ||
|
||
$userId = trim($input->getArgument('user-id')); | ||
if ($userId === '') { | ||
$userId = null; | ||
} | ||
|
||
try { | ||
$context = $this->contextService->findById($contextId, $userId); | ||
} catch (InternalError|Exception $e) { | ||
$output->writeln('Error while reading contexts from DB.'); | ||
$this->logger->warning('Following error occurred during executing occ command "{class}"', | ||
[ | ||
'app' => 'tables', | ||
'class' => self::class, | ||
'exception' => $e, | ||
] | ||
); | ||
if ($this->config->getSystemValueBool('debug', false)) { | ||
$output->writeln(sprintf('<warning>%s</warning>', $e->getMessage())); | ||
$output->writeln('<error>'); | ||
debug_print_backtrace(); | ||
$output->writeln('</error>'); | ||
} | ||
return 1; | ||
} | ||
|
||
$contextArray = json_decode(json_encode($context), true); | ||
|
||
$contextArray['ownerType'] = match ($contextArray['ownerType']) { | ||
1 => 'group', | ||
default => 'user', | ||
}; | ||
|
||
$out = ['ID ' . $contextArray['id'] => $contextArray]; | ||
unset($out[$contextArray['id']]['id']); | ||
$this->writeArrayInOutputFormat($input, $output, $out); | ||
|
||
return 0; | ||
} | ||
} |
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
Oops, something went wrong.