Skip to content

Commit

Permalink
Merge pull request #945 from nextcloud/backport/926/stable22
Browse files Browse the repository at this point in the history
[stable22] allow configuration of one single password per circle
  • Loading branch information
ArtificialOwl authored Mar 9, 2022
2 parents eb9cf95 + 400f47a commit 1f4babf
Show file tree
Hide file tree
Showing 16 changed files with 533 additions and 135 deletions.
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Those groups of users (or "circles") can then be used by any other app for shari
<command>OCA\Circles\Command\CirclesDestroy</command>
<command>OCA\Circles\Command\CirclesDetails</command>
<command>OCA\Circles\Command\CirclesConfig</command>
<command>OCA\Circles\Command\CirclesSetting</command>
<command>OCA\Circles\Command\CirclesSync</command>
<command>OCA\Circles\Command\CirclesCheck</command>
<command>OCA\Circles\Command\CirclesTest</command>
Expand Down
4 changes: 2 additions & 2 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

['name' => 'Local#editName', 'url' => '/circles/{circleId}/name', 'verb' => 'PUT'],
['name' => 'Local#editDescription', 'url' => '/circles/{circleId}/description', 'verb' => 'PUT'],
['name' => 'Local#editSettings', 'url' => '/circles/{circleId}/settings', 'verb' => 'PUT'],
['name' => 'Local#editSetting', 'url' => '/circles/{circleId}/setting', 'verb' => 'PUT'],
['name' => 'Local#editConfig', 'url' => '/circles/{circleId}/config', 'verb' => 'PUT'],
['name' => 'Local#link', 'url' => '/link/{circleId}/{singleId}', 'verb' => 'GET'],

Expand Down Expand Up @@ -95,7 +95,7 @@
['name' => 'Admin#circleLeave', 'url' => '/admin/{emulated}/circles/{circleId}/leave', 'verb' => 'PUT'],
['name' => 'Admin#editName', 'url' => '/admin/{emulated}/circles/{circleId}/name', 'verb' => 'PUT'],
['name' => 'Admin#editDescription', 'url' => '/admin/{emulated}/circles/{circleId}/description', 'verb' => 'PUT'],
['name' => 'Admin#editSettings', 'url' => '/admin/{emulated}/circles/{circleId}/settings', 'verb' => 'PUT'],
['name' => 'Admin#editSetting', 'url' => '/admin/{emulated}/circles/{circleId}/setting', 'verb' => 'PUT'],
['name' => 'Admin#editConfig', 'url' => '/admin/{emulated}/circles/{circleId}/config', 'verb' => 'PUT'],
['name' => 'Admin#link', 'url' => '/admin/{emulated}/link/{circleId}/{singleId}', 'verb' => 'GET']
],
Expand Down
34 changes: 17 additions & 17 deletions lib/Circles/FileSharingBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ public function createShareToMember(SharingFrame $frame, DeprecatedMember $membe

$password = '';
$sendPasswordByMail = true;
if ($this->configService->enforcePasswordProtection($circle)) {
if ($circle->getSetting('password_single_enabled') === 'true') {
$password = $circle->getPasswordSingle();
$sendPasswordByMail = false;
} else {
$password = $this->miscService->token(15);
}
}
// if ($this->configService->enforcePasswordProtection($circle)) {
// if ($circle->getSetting('password_single_enabled') === 'true') {
// $password = $circle->getPasswordSingle();
// $sendPasswordByMail = false;
// } else {
// $password = $this->miscService->token(15);
// }
// }

$sharesToken =
$this->tokensRequest->generateTokenForMember($member, $share->getId(), $password);
Expand Down Expand Up @@ -473,9 +473,9 @@ protected function sendMail($fileName, $link, $author, $circleName, $email) {
* @throws Exception
*/
protected function sendPasswordByMail(IShare $share, $circleName, $email, $password) {
if (!$this->configService->sendPasswordByMail() || $password === '') {
return;
}
// if (!$this->configService->sendPasswordByMail() || $password === '') {
// return;
// }

$message = $this->mailer->createMessage();

Expand Down Expand Up @@ -594,9 +594,9 @@ public function sendMailExitingShares(
$data = [];

$password = '';
if ($this->configService->enforcePasswordProtection($circle)) {
$password = $this->miscService->token(15);
}
// if ($this->configService->enforcePasswordProtection($circle)) {
// $password = $this->miscService->token(15);
// }

foreach ($unknownShares as $share) {
try {
Expand Down Expand Up @@ -628,9 +628,9 @@ public function sendMailExitingShares(
* @throws Exception
*/
protected function sendPasswordExistingShares(DeprecatedMember $author, string $email, string $password) {
if (!$this->configService->sendPasswordByMail() || $password === '') {
return;
}
// if (!$this->configService->sendPasswordByMail() || $password === '') {
// return;
// }

$message = $this->mailer->createMessage();

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

declare(strict_types=1);


/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2022
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/


namespace OCA\Circles\Command;

use OC\Core\Command\Base;
use OCA\Circles\Exceptions\CircleNotFoundException;
use OCA\Circles\Exceptions\FederatedEventException;
use OCA\Circles\Exceptions\FederatedItemException;
use OCA\Circles\Exceptions\FederatedUserException;
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
use OCA\Circles\Exceptions\InitiatorNotConfirmedException;
use OCA\Circles\Exceptions\InitiatorNotFoundException;
use OCA\Circles\Exceptions\InvalidIdException;
use OCA\Circles\Exceptions\MemberNotFoundException;
use OCA\Circles\Exceptions\OwnerNotFoundException;
use OCA\Circles\Exceptions\RemoteInstanceException;
use OCA\Circles\Exceptions\RemoteNotFoundException;
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
use OCA\Circles\Exceptions\RequestBuilderException;
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
use OCA\Circles\Exceptions\UnknownRemoteException;
use OCA\Circles\Exceptions\UserTypeNotFoundException;
use OCA\Circles\Model\Helpers\MemberHelper;
use OCA\Circles\Model\Member;
use OCA\Circles\Service\CircleService;
use OCA\Circles\Service\FederatedUserService;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class CirclesSetting extends Base {


/** @var FederatedUserService */
private $federatedUserService;

/** @var CircleService */
private $circleService;


/**
* @param FederatedUserService $federatedUserService
* @param CircleService $circlesService
*/
public function __construct(FederatedUserService $federatedUserService, CircleService $circlesService) {
parent::__construct();

$this->federatedUserService = $federatedUserService;
$this->circleService = $circlesService;
}


/**
*
*/
protected function configure() {
parent::configure();
$this->setName('circles:manage:setting')
->setDescription('edit setting for a Circle')
->addArgument('circle_id', InputArgument::REQUIRED, 'ID of the circle')
->addArgument('setting', InputArgument::OPTIONAL, 'setting to edit', '')
->addArgument('value', InputArgument::OPTIONAL, 'value', '')
->addOption('unset', '', InputOption::VALUE_NONE, 'unset the setting')
->addOption('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '')
->addOption('initiator-type', '', InputOption::VALUE_REQUIRED, 'set initiator type', '0')
->addOption('status-code', '', InputOption::VALUE_NONE, 'display status code on exception');
}


/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
* @throws FederatedEventException
* @throws FederatedItemException
* @throws InitiatorNotFoundException
* @throws RequestBuilderException
* @throws CircleNotFoundException
* @throws FederatedUserException
* @throws FederatedUserNotFoundException
* @throws InitiatorNotConfirmedException
* @throws InvalidIdException
* @throws MemberNotFoundException
* @throws OwnerNotFoundException
* @throws RemoteInstanceException
* @throws RemoteNotFoundException
* @throws RemoteResourceNotFoundException
* @throws SingleCircleNotFoundException
* @throws UnknownRemoteException
* @throws UserTypeNotFoundException
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$circleId = (string)$input->getArgument('circle_id');
$setting = (string)$input->getArgument('setting');
$value = (string)$input->getArgument('value');

try {
$this->federatedUserService->commandLineInitiator(
$input->getOption('initiator'),
Member::parseTypeString($input->getOption('initiator-type')),
$circleId,
false
);

if ($setting === '') {
$circle = $this->circleService->getCircle($circleId);
$initiatorHelper = new MemberHelper($circle->getInitiator());
$initiatorHelper->mustBeAdmin();
$output->writeln(json_encode($circle->getSettings(), JSON_PRETTY_PRINT));

return 0;
}

if (!$input->getOption('unset') && $value === '') {
throw new InvalidArgumentException('you need to specify a value');
}

$outcome = $this->circleService->updateSetting(
$circleId,
$setting,
($input->getOption('unset')) ? null : $value,
);
} catch (FederatedItemException $e) {
if ($input->getOption('status-code')) {
throw new FederatedItemException(
' [' . get_class($e) . ', ' . $e->getStatus() . ']' . "\n" . $e->getMessage()
);
}

throw $e;
}

if (strtolower($input->getOption('output')) === 'json') {
$output->writeln(json_encode($outcome, JSON_PRETTY_PRINT));
}

return 0;
}
}
12 changes: 8 additions & 4 deletions lib/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,25 +462,29 @@ public function editDescription(string $emulated, string $circleId, string $valu
/**
* @param string $emulated
* @param string $circleId
* @param array $value
* @param string $setting
* @param string|null $value
*
* @return DataResponse
* @throws OCSException
*/
public function editSettings(string $emulated, string $circleId, array $value): DataResponse {
public function editSetting(string $emulated, string $circleId, string $setting, ?string $value = null): DataResponse {
try {
$this->setLocalFederatedUser($emulated);

$outcome = $this->circleService->updateSettings($circleId, $value);
$outcome = $this->circleService->updateSetting($circleId, $setting, $value);

return new DataResponse($this->serializeArray($outcome));
} catch (Exception $e) {
$this->e($e, ['emulated' => $emulated, 'circleId' => $circleId, 'value' => $value]);
$this->e($e, ['circleId' => $circleId, 'setting' => $setting, 'value' => $value]);
throw new OCSException($e->getMessage(), $e->getCode());
}
}





/**
* @param string $emulated
* @param string $circleId
Expand Down
10 changes: 6 additions & 4 deletions lib/Controller/LocalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ public function memberRemove(string $circleId, string $memberId): DataResponse {
*
* @param int $limit
* @param int $offset
*
* @return DataResponse
* @throws OCSException
*/
Expand Down Expand Up @@ -505,20 +506,21 @@ public function editDescription(string $circleId, string $value): DataResponse {
* @NoAdminRequired
*
* @param string $circleId
* @param array $value
* @param string $setting
* @param string|null $value
*
* @return DataResponse
* @throws OCSException
*/
public function editSettings(string $circleId, array $value): DataResponse {
public function editSetting(string $circleId, string $setting, ?string $value = null): DataResponse {
try {
$this->setCurrentFederatedUser();

$outcome = $this->circleService->updateSettings($circleId, $value);
$outcome = $this->circleService->updateSetting($circleId, $setting, $value);

return new DataResponse($this->serializeArray($outcome));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId, 'value' => $value]);
$this->e($e, ['circleId' => $circleId, 'setting' => $setting, 'value' => $value]);
throw new OCSException($e->getMessage(), $e->getCode());
}
}
Expand Down
12 changes: 12 additions & 0 deletions lib/Db/CircleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ public function updateConfig(Circle $circle) {
}


/**
* @param Circle $circle
*/
public function updateSettings(Circle $circle) {
$qb = $this->getCircleUpdateSql();
$qb->set('settings', $qb->createNamedParameter(json_encode($circle->getSettings())));
$qb->limitToUniqueId($circle->getSingleId());

$qb->execute();
}


/**
* @param IFederatedUser|null $initiator
* @param CircleProbe $probe
Expand Down
Loading

0 comments on commit 1f4babf

Please sign in to comment.