Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ This app allows generating reports of shares on the system.

## Usage

### Command
### Commands

#### List

```sh
./occ sharing:list [-u|--user [USER]] [-p|--path [PATH]] [-t|--token [TOKEN]] [-f|--filter [FILTER]] [-o|--output FORMAT]
Expand All @@ -13,7 +15,7 @@ This app allows generating reports of shares on the system.
Without options, the command yields the unfiltered list of all shares.\
With options, the list is narrowed down using the filters set.

### Options
##### Options

* `-u [USER]` or `--user [USER]`\
List only shares of the given user.
Expand All @@ -27,6 +29,33 @@ With options, the list is narrowed down using the filters set.
* `-o FORMAT` or `--output FORMAT`\
Set the output format (json or csv, default is json).

#### Send

```sh
./occ sharing:send [-u|--user USER] [-p|--path PATH] [-t|--token TOKEN] [-f|--filter FILTER] [-o|--output FORMAT]
```

Without options, the command yields the unfiltered list of all shares.\
With options, the list is narrowed down using the filters set.

##### Options

* `-r` or `--recipients`\
Recipients users of generated reports.
* `-x` or `--target-path`\
Generated reports will be stored on this path.
* `-d` or `--diff`\
Create a differential report in json format from the last available report.
* `-u [USER]` or `--user [USER]`\
List only shares of the given user.
* `-p [PATH]` or `--path [PATH]`\
List only shares within the given path.
* `-t [TOKEN]` or `--token [TOKEN]`\
List only shares that use a token that (at least partly) matches the argument.
* `-f [FILTER]` or `--filter [FILTER]`\
List only shares where the TYPE matches the argument.\
Possible values for the filter argument: {owner, initiator, recipient}

## Examples

To better illustrate how the app work see the examples below:
Expand Down
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@

<commands>
<command>OCA\ShareListing\Command\ListShares</command>
<command>OCA\ShareListing\Command\SendShares</command>
</commands>
</info>
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"require": {
"nikic/iter": "^2.4",
"symfony/serializer": "^5.4",
"bamarni/composer-bin-plugin": "^1.8"
"bamarni/composer-bin-plugin": "^1.8",
"swaggest/json-diff": "^3.12"
}
}
46 changes: 45 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions lib/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

// SPDX-FileCopyrightText: 2018 Roeland Jago Douma <roeland@famdouma.nl>
// SPDX-FileCopyrightText: 2022 Solution Libre SAS
// SPDX-FileContributor: Florent Poinsaut <florent@solution-libre.fr>
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace OCA\ShareListing\Command;

use OC\Core\Command\Base;
use OCA\ShareListing\Service\SharesList;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

abstract class AbstractCommand extends Base {
public function __construct(
protected readonly SharesList $sharesList,
) {
parent::__construct();
}

public function configure(): void {
$this->addOption(
'user',
'u',
InputOption::VALUE_OPTIONAL,
'Will list shares of the given user'
)->addOption(
'path',
'p',
InputOption::VALUE_OPTIONAL,
'Will only consider the given path'
)->addOption(
'token',
't',
InputOption::VALUE_OPTIONAL,
'Will only consider the given token'
)->addOption(
'filter',
'f',
InputOption::VALUE_OPTIONAL,
'Filter shares, possible values: owner, initiator, recipient, token, has-expiration, no-expiration'
);
}

protected function getOptions(InputInterface $input): array {
$user = $input->getOption('user');
$path = $input->getOption('path');
$token = $input->getOption('token');
$filter = $this->sharesList->filterStringToInt($input->getOption('filter'));

return [$user, $path, $token, $filter];
}
}
50 changes: 6 additions & 44 deletions lib/Command/ListShares.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,18 @@

namespace OCA\ShareListing\Command;

use OC\Core\Command\Base;
use OCA\ShareListing\Service\SharesList;
use OCP\Files\IRootFolder;
use OCP\IUserManager;
use OCP\Share\IManager as ShareManager;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function iter\toArray;

class ListShares extends Base {

public function __construct(
private ShareManager $shareManager,
private IUserManager $userManager,
private IRootFolder $rootFolder,
private SharesList $sharesList,
) {
parent::__construct();

}

public function configure() {
/**
* @psalm-api
*/
class ListShares extends AbstractCommand {
public function configure(): void {
$this->setName('sharing:list')
->setDescription('List who has access to shares by owner')
->addOption(
'user',
'u',
InputOption::VALUE_OPTIONAL,
'Will list shares of the given user'
)
->addOption(
'path',
'p',
InputOption::VALUE_OPTIONAL,
'Will only consider the given path'
)->addOption(
'token',
't',
InputOption::VALUE_OPTIONAL,
'Will only consider the given token'
)->addOption(
'filter',
'f',
InputOption::VALUE_OPTIONAL,
'Filter shares, possible values: owner, initiator, recipient, token, has-expiration, no-expiration'
)
->addOption(
'output',
'o',
Expand All @@ -84,10 +49,7 @@ public function configure() {
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$user = $input->getOption('user');
$path = $input->getOption('path');
$token = $input->getOption('token');
$filter = $this->sharesList->filterStringToInt($input->getOption('filter'));
[$user, $path, $token, $filter] = $this->getOptions($input);
$outputOpt = $input->getOption('output');

$shares = toArray($this->sharesList->getFormattedShares($user, $filter, $path, $token));
Expand Down
115 changes: 115 additions & 0 deletions lib/Command/SendShares.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

// SPDX-FileCopyrightText: 2018 Roeland Jago Douma <roeland@famdouma.nl>
// SPDX-FileCopyrightText: 2022 Solution Libre SAS
// SPDX-FileContributor: Florent Poinsaut <florent@solution-libre.fr>
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace OCA\ShareListing\Command;

use OCA\ShareListing\Service\ReportSender;
use OCA\ShareListing\Service\SharesList;
use OCP\IUserManager;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @psalm-api
*/
class SendShares extends AbstractCommand {
public function __construct(
private readonly IUserManager $userManager,
private readonly ReportSender $reportSender,
SharesList $sharesList,
) {
parent::__construct($sharesList);
}

public function configure(): void {
parent::configure();

$this->setName('sharing:send')
->setDescription('Send list who has access to shares by owner')
->addOption(
'diff',
'd',
InputOption::VALUE_NONE,
'Create a differential report in json format from the last available report'
)
->addOption(
'recipients',
'r',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Recipients users of generated reports'
)
->addOption(
'target-path',
'x',
InputOption::VALUE_REQUIRED,
'Generated reports will be stored on this path'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$this->checkAllRequiredOptionsAreNotEmpty($input);

[$user, $path, $token, $filter] = $this->getOptions($input);
$diff = $input->getOption('diff');
$recipients = $input->getOption('recipients');
$targetPath = $input->getOption('target-path');

$dateTime = new \DateTimeImmutable();

foreach ($recipients as $recipient) {
$this->reportSender->createReport(
$recipient,
$targetPath,
$dateTime,
$user,
$filter,
$path,
$token
);

if ($diff) {
$this->reportSender->diff($recipient, $targetPath);
}

$this->reportSender->sendReport($recipient, $dateTime);
}
return 0;
}

protected function checkAllRequiredOptionsAreNotEmpty(InputInterface $input): void {
$errors = [];

if (!$input->getOption('target-path')) {
$errors[] = 'The required option --target-path is not set or is empty.';
}

$recipients = $this->getDefinition()->getOption('recipients');

/** @var InputOption $option */
foreach ([$recipients] as $option) {
$name = $option->getName();
$values = $input->getOption($name);

if ($values === null || $values === '' || ($option->isArray() && empty($values))) {
$errors[] = sprintf('The required option --%s is not set or is empty.', $name);
}

foreach ($values as $value) {
if (!$this->userManager->userExists($value)) {
$errors[] = sprintf('The recipient user %s does not exist.', $value);
}
}
}

if (count($errors)) {
throw new \InvalidArgumentException(implode("\n\n", $errors));
}
}
}
Loading
Loading