Skip to content

Commit

Permalink
async and more
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed Feb 7, 2021
1 parent affc876 commit ce45b71
Show file tree
Hide file tree
Showing 25 changed files with 756 additions and 255 deletions.
10 changes: 4 additions & 6 deletions lib/Command/MembersLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OC\Core\Command\Base;
use OC\User\NoUserException;
use OCA\Circles\Db\MemberRequest;
use OCA\Circles\Model\Member;
use OCA\Circles\Service\FederatedUserService;
use OCA\Circles\Service\MemberService;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -104,13 +105,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$input->getOption('initiator'), $member->getCircleId()
);

$level = $this->memberService->parseLevelString($input->getArgument('level'));
$this->memberService->memberLevel($memberId, $level);
$level = Member::parseLevelString($input->getArgument('level'));
$outcome = $this->memberService->memberLevel($memberId, $level);

// $member = $this->membersRequest->forceGetMember(
// $member->getCircleId(), $member->getUserId(), DeprecatedMember::TYPE_USER, $member->getInstance()
// );
echo json_encode($member, JSON_PRETTY_PRINT) . "\n";
echo json_encode($outcome, JSON_PRETTY_PRINT) . "\n";

return 0;
}
Expand Down
45 changes: 32 additions & 13 deletions lib/Command/MembersRemove.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);


/**
Expand Down Expand Up @@ -31,11 +33,14 @@

use Exception;
use OC\Core\Command\Base;
use OCA\Circles\Db\MemberRequest;
use OCA\Circles\Exceptions\MemberDoesNotExistException;
use OCA\Circles\Service\FederatedUserService;
use OCA\Circles\Service\MemberService;
use OCA\Circles\Service\MembersService;
use OCP\IL10N;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;


Expand All @@ -47,31 +52,39 @@
class MembersRemove extends Base {


/** @var IL10N */
private $l10n;
/** @var MemberRequest */
private $memberRequest;

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

/** @var MembersService */
private $membersService;
private $memberService;


/**
* MembersRemove constructor.
*
* @param IL10N $l10n
* @param MembersService $membersService
* @param MemberRequest $memberRequest
* @param FederatedUserService $federatedUserService
* @param MemberService $memberService
*/
public function __construct(IL10N $l10n, MembersService $membersService) {
public function __construct(
MemberRequest $memberRequest, FederatedUserService $federatedUserService, MemberService $memberService
) {
parent::__construct();
$this->l10n = $l10n;
$this->membersService = $membersService;
$this->memberRequest = $memberRequest;
$this->federatedUserService = $federatedUserService;
$this->memberService = $memberService;
}


protected function configure() {
parent::configure();
$this->setName('circles:members:remove')
->setDescription('remove a member from a circle')
->addArgument('member_id', InputArgument::REQUIRED, 'ID of the member to be expel');
->addArgument('member_id', InputArgument::REQUIRED, 'ID of the member from the Circle')
->addOption('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '');
}


Expand All @@ -86,8 +99,14 @@ protected function configure() {
protected function execute(InputInterface $input, OutputInterface $output): int {
$memberId = $input->getArgument('member_id');

$member = $this->membersService->getMemberById($memberId);
$this->membersService->removeMember($member->getCircleId(), $member->getUserId(), $member->getType(), $member->getInstance(), true);
$member = $this->memberRequest->getMember($memberId);
$this->federatedUserService->commandLineInitiator(
$input->getOption('initiator'), $member->getCircleId()
);

$outcome = $this->memberService->removeMember($memberId);

echo json_encode($outcome, JSON_PRETTY_PRINT) . "\n";

return 0;
}
Expand Down
10 changes: 8 additions & 2 deletions lib/Controller/RemoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

namespace OCA\Circles\Controller;

use daita\MySmallPhpTools\Exceptions\InvalidItemException;
use daita\MySmallPhpTools\Exceptions\InvalidOriginException;
use daita\MySmallPhpTools\Exceptions\MalformedArrayException;
use daita\MySmallPhpTools\Exceptions\SignatoryException;
Expand All @@ -39,6 +40,7 @@
use daita\MySmallPhpTools\Traits\Nextcloud\nc21\TNC21Controller;
use Exception;
use OCA\Circles\Db\CircleRequest;
use OCA\Circles\Exceptions\FederatedEventDSyncException;
use OCA\Circles\Exceptions\FederatedItemException;
use OCA\Circles\Model\Federated\FederatedEvent;
use OCA\Circles\Model\Federated\RemoteInstance;
Expand Down Expand Up @@ -116,7 +118,9 @@ public function event(): DataResponse {
$this->remoteDownstreamService->requestedEvent($event);
} catch (FederatedItemException $e) {
$this->e($e, ['event' => $event]);
$event->setReadingOutcome($e->getMessage(), $e->getParams(), false);
$event->setReadingOutcome($e->getMessage(), $e->getParams(), true);
} catch (FederatedEventDSyncException $e) {
return $this->fail($e, [], Http::STATUS_CONFLICT);
} catch (Exception $e) {
$this->e($e);

Expand Down Expand Up @@ -183,7 +187,8 @@ public function circles(): DataResponse {
private function confirmRemoteInstance(NC21SignedRequest $signedRequest) {
$signatory = $signedRequest->getSignatory();
if (!$signatory instanceof RemoteInstance
|| $signatory->getType() === RemoteInstance::TYPE_UNKNOWN) {
|| ((!$this->configService->isLocalInstance($signedRequest->getOrigin())
&& $signatory->getType() === RemoteInstance::TYPE_UNKNOWN))) {
$this->debug('Could not confirm identity', ['signedRequest' => $signedRequest]);
throw new SignatoryException('could not confirm identity');
}
Expand All @@ -195,6 +200,7 @@ private function confirmRemoteInstance(NC21SignedRequest $signedRequest) {
* @throws MalformedArrayException
* @throws SignatoryException
* @throws SignatureException
* @throws InvalidItemException
*/
private function extractEventFromRequest(): FederatedEvent {
$signed = $this->remoteService->incomingSignedRequest($this->configService->getLocalInstance());
Expand Down
8 changes: 8 additions & 0 deletions lib/Db/CoreQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ public function limitToInstance(string $host): void {
}


/**
* @param int $userType
*/
public function limitToUserType(int $userType): void {
$this->limitToDBFieldInt('user_type', $userType);
}


/**
* @param IFederatedUser $initiator
* @param string $alias
Expand Down
60 changes: 59 additions & 1 deletion lib/Db/MemberRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class MemberRequest extends MemberRequestBuilder {
* @throws InvalidIdException
*/
public function save(Member $member): void {
$this->confirmValidIds([$member->getCircleId(), $member->getSingleId(), $member->getId()]);
// TODO: check singleId is not empty
// $this->confirmValidIds([$member->getCircleId(), $member->getSingleId(), $member->getId()]);
$this->confirmValidIds([$member->getCircleId(), $member->getId()]);

$qb = $this->getMemberInsertSql();
$qb->setValue('circle_id', $qb->createNamedParameter($member->getCircleId()))
Expand All @@ -73,6 +75,39 @@ public function save(Member $member): void {
}


/**
* @param Member $member
*/
public function delete(Member $member) {
$qb = $this->getMemberDeleteSql();
$qb->limitToCircleId($member->getCircleId());
$qb->limitToUserId($member->getUserId());
$qb->limitToUserType($member->getUserType());
$qb->limitToInstance($qb->getInstance($member));
// $qb->limitToSingleId($federatedUser->getSingleId());

$qb->execute();
}


/**
* @param Member $member
*/
public function updateLevel(Member $member): void {
$qb = $this->getMemberUpdateSql();
$qb->set('level', $qb->createNamedParameter($member->getLevel()));

$qb->limitToMemberId($member->getId());
$qb->limitToCircleId($member->getCircleId());
$qb->limitToUserId($member->getUserId());
$qb->limitToUserType($member->getUserType());
$qb->limitToInstance($qb->getInstance($member));
// $qb->limitToSingleId($member->getSingleId());

$qb->execute();
}


/**
* @param string $circleId
*
Expand Down Expand Up @@ -105,5 +140,28 @@ public function getMember(string $memberId, ?FederatedUser $initiator = null): M
return $this->getItemFromRequest($qb);
}


/**
* @param Member $member
* @param FederatedUser|null $initiator
*
* @return Member
* @throws MemberNotFoundException
*/
public function searchMember(Member $member, ?FederatedUser $initiator = null): Member {
$qb = $this->getMemberSelectSql();
$qb->limitToCircleId($member->getCircleId());
$qb->limitToUserId($member->getUserId());
$qb->limitToUserType($member->getUserType());
$qb->limitToInstance($qb->getInstance($member));
// $qb->limitToSingleId($federatedUser->getSingleId());

if (!is_null($initiator)) {
$qb->leftJoinCircle($initiator);
}

return $this->getItemFromRequest($qb);
}

}

13 changes: 12 additions & 1 deletion lib/Exceptions/MemberAlreadyExistsException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php

declare(strict_types=1);


/**
* Circles - Bring cloud-users closer together.
*
Expand All @@ -24,9 +28,16 @@
*
*/


namespace OCA\Circles\Exceptions;

class MemberAlreadyExistsException extends \Exception {

/**
* Class MemberAlreadyExistsException
*
* @package OCA\Circles\Exceptions
*/
class MemberAlreadyExistsException extends FederatedItemException {

}

43 changes: 43 additions & 0 deletions lib/Exceptions/MemberHelperException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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 <[email protected]>
* @copyright 2021
* @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\Exceptions;


/**
* Class MemberLevelException
*
* @package OCA\Circles\Exceptions
*/
class MemberHelperException extends FederatedItemException {

}

8 changes: 6 additions & 2 deletions lib/Exceptions/MemberLevelException.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
*
*/

namespace OCA\Circles\Exceptions;

use Exception;
namespace OCA\Circles\Exceptions;


/**
* Class MemberLevelException
*
* @package OCA\Circles\Exceptions
*/
class MemberLevelException extends FederatedItemException {

}
Expand Down
2 changes: 2 additions & 0 deletions lib/FederatedItems/CircleCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public function manage(FederatedEvent $event): void {
$circle = $event->getCircle();
$owner = $circle->getOwner();

// TODO: confirm CircleId is unique
// TODO: confirm MemberId is unique
$this->circleRequest->save($circle);
$this->memberRequest->save($owner);

Expand Down
Loading

0 comments on commit ce45b71

Please sign in to comment.