Skip to content

Commit

Permalink
share with group members only
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Apr 12, 2022
1 parent c650524 commit 402e0ac
Show file tree
Hide file tree
Showing 15 changed files with 394 additions and 40 deletions.
38 changes: 37 additions & 1 deletion lib/Db/CoreQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,19 @@ class CoreQueryBuilder extends ExtendedQueryBuilder {
self::BASED_ON
]
],


self::MEMBERSHIPS => [
self::CONFIG
self::CONFIG,
self::INHERITED_BY,
self::CIRCLE => [
self::OWNER => [
self::BASED_ON
]
]
],


self::SHARE => [
self::SHARE,
self::TOKEN,
Expand Down Expand Up @@ -733,6 +743,32 @@ public function leftJoinCircle(
}


/**
* @param string $alias
* @param string $circleId
* @param string $singleId
*
* @throws RequestBuilderException
*/
public function leftJoinInheritedBy(
string $alias,
string $circleId = 'inheritance_last',
string $singleId = 'single_id'
): void {
$aliasInheritedBy = $this->generateAlias($alias, self::INHERITED_BY);
$expr = $this->expr();

$this->generateMemberSelectAlias($aliasInheritedBy)
->leftJoin(
$alias, CoreRequestBuilder::TABLE_MEMBER, $aliasInheritedBy,
$expr->andX(
$expr->eq($alias . '.' . $circleId, $aliasInheritedBy . '.circle_id'),
$expr->eq($alias . '.' . $singleId, $aliasInheritedBy . '.single_id')
)
);
}


/**
* @param string $aliasMember
*
Expand Down
21 changes: 19 additions & 2 deletions lib/Db/MembershipRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,33 @@ public function getMembership(string $circleId, string $singleId): Membership {
*
* @return Membership[]
*/
public function getMemberships(string $singleId): array {
public function getMemberships(string $singleId, bool $detailed = false, int $level = 0): array {
$qb = $this->getMembershipSelectSql();
$qb->limitToSingleId($singleId);
$qb->leftJoinCircleConfig(CoreQueryBuilder::MEMBERSHIPS);

if ($level > 1) {
$expr = $qb->expr();
$qb->andWhere($expr->gte('level', $qb->createNamedParameter($level, IQueryBuilder::PARAM_INT)));
}

if ($detailed) {
$qb->setOptions([CoreQueryBuilder::MEMBERSHIPS], ['getData' => true]);
$qb->leftJoinCircle(CoreQueryBuilder::MEMBERSHIPS);
}

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


/**
* @param string $singleId
* @param bool $detailed
* @param int $level
*
* @return Membership[]
*/
public function getInherited(string $singleId, int $level = 0): array {
public function getAccounted(string $singleId, bool $detailed = false, int $level = 0): array {
$qb = $this->getMembershipSelectSql();
$qb->limitToCircleId($singleId);
$qb->leftJoinCircleConfig(self::TABLE_MEMBERSHIP);
Expand All @@ -132,6 +143,12 @@ public function getInherited(string $singleId, int $level = 0): array {
$qb->andWhere($expr->gte('level', $qb->createNamedParameter($level, IQueryBuilder::PARAM_INT)));
}

if ($detailed) {
$qb->setOptions([CoreQueryBuilder::MEMBERSHIPS], ['getData' => true]);
$qb->leftJoinCircle(CoreQueryBuilder::MEMBERSHIPS);
$qb->leftJoinInheritedBy(CoreQueryBuilder::MEMBERSHIPS);
}

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

Expand Down
4 changes: 4 additions & 0 deletions lib/FederatedItems/CircleJoin.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ private function manageMemberStatus(Circle $circle, Member $member) {
throw new FederatedItemBadRequestException(StatusCode::$CIRCLE_JOIN[124], 124);
}

if ($this->membershipService->limitToGroupMembersOnly($member, $circle)) {
throw new FederatedItemBadRequestException(StatusCode::$CIRCLE_JOIN[125], 125);
}

$member->setId($this->token(ManagedModel::ID_LENGTH));

if ($circle->isConfig(Circle::CFG_REQUEST)) {
Expand Down
5 changes: 5 additions & 0 deletions lib/FederatedItems/SingleMemberAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ protected function generateMember(FederatedEvent $event, Circle $circle, Member
throw new FederatedItemBadRequestException(StatusCode::$MEMBER_ADD[128], 128);
}

if (!$event->getParams()->gBool('force_group_member')
&& $this->membershipService->limitToGroupMembersOnly($federatedUser, $circle)) {
throw new FederatedItemBadRequestException(StatusCode::$MEMBER_ADD[133], 133);
}

if (!$this->configService->isLocalInstance($member->getInstance())) {
if ($circle->isConfig(Circle::CFG_LOCAL)) {
throw new FederatedItemBadRequestException(StatusCode::$MEMBER_ADD[126], 126);
Expand Down
11 changes: 11 additions & 0 deletions lib/IEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,28 @@ public function getSingleId(): string;

/**
* @param Membership[] $memberships
* @deprecated - do not use outside of the app itself, will be removed.
*
* @return $this
*/
public function setMemberships(array $memberships): self;

/**
* TODO: might be better to have use abstract instead of interface
* @return Membership[]
*/
public function getMemberships(): array;

/**
* returns all memberships that can be hidden behind this entity, including this one.
* TODO: might be better to have use abstract instead of interface
*
* @return Membership[]
*/
public function getAccountedMemberships(bool $detailed): array;

/**
* TODO: might be better to have use abstract instead of interface
* @param string $singleId
* @param bool $detailed
*
Expand Down
18 changes: 11 additions & 7 deletions lib/Model/Circle.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,14 @@ public function getMembers(): array {
}


/**
* @return Membership[]
*/
public function getAccountedMemberships(bool $detailed = false): array {
return $this->getManager()->getAccountedMemberships($this, $detailed);
}


/**
* @param array $members
* @param bool $detailed
Expand Down Expand Up @@ -515,7 +523,7 @@ function (Member $member): string {
* @throws UnknownRemoteException
*/
public function getInheritedMembers(bool $detailed = false, bool $remote = false): array {
if (is_null($this->inheritedMembers)
if (is_null($this->inheritedMembers) // fix: array cannot be null.
|| ($detailed && !$this->detailedInheritedMember)) {
$this->getManager()->getInheritedMembers($this, $detailed);
}
Expand Down Expand Up @@ -549,12 +557,8 @@ public function setMemberships(array $memberships): IEntity {
/**
* @return Membership[]
*/
public function getMemberships(): array {
if (!$this->hasMemberships()) {
$this->getManager()->getMemberships($this);
}

return $this->memberships;
public function getMemberships(bool $detailed = false, int $level = 0): array {
return $this->getManager()->getMemberships($this, $detailed, $level);
}


Expand Down
6 changes: 6 additions & 0 deletions lib/Model/Federated/FederatedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ public function setParams(SimpleDataStore $params): self {
}

/**
* params are kept between master and slave in GS setup
*
* @return SimpleDataStore
*/
public function getParams(): SimpleDataStore {
Expand All @@ -396,6 +398,8 @@ public function getParams(): SimpleDataStore {


/**
* internal are only available to master in GS setup
*
* @param SimpleDataStore $internal
*
* @return self
Expand Down Expand Up @@ -424,6 +428,8 @@ public function resetInternal(): self {


/**
* data are generated within the FederatedEvent
*
* @param SimpleDataStore $data
*
* @return self
Expand Down
27 changes: 15 additions & 12 deletions lib/Model/FederatedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@

namespace OCA\Circles\Model;

use OCA\Circles\Tools\Db\IQueryRow;
use OCA\Circles\Tools\Exceptions\InvalidItemException;
use OCA\Circles\Tools\IDeserializable;
use OCA\Circles\Tools\Traits\TDeserialize;
use OCA\Circles\Tools\Traits\TArrayTools;
use JsonSerializable;
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
use OCA\Circles\Exceptions\MembershipNotFoundException;
use OCA\Circles\Exceptions\OwnerNotFoundException;
use OCA\Circles\Exceptions\RequestBuilderException;
use OCA\Circles\Exceptions\UnknownInterfaceException;
use OCA\Circles\IFederatedUser;
use OCA\Circles\IEntity;
use OCA\Circles\IFederatedUser;
use OCA\Circles\Tools\Db\IQueryRow;
use OCA\Circles\Tools\Exceptions\InvalidItemException;
use OCA\Circles\Tools\IDeserializable;
use OCA\Circles\Tools\Traits\TArrayTools;
use OCA\Circles\Tools\Traits\TDeserialize;

/**
* Class FederatedUser
Expand Down Expand Up @@ -309,6 +309,7 @@ public function hasMemberships(): bool {
* @param array $memberships
*
* @return self
* @deprecated - can be removed.
*/
public function setMemberships(array $memberships): IEntity {
$this->memberships = $memberships;
Expand All @@ -319,15 +320,17 @@ public function setMemberships(array $memberships): IEntity {
/**
* @return Membership[]
*/
public function getMemberships(): array {
if (!$this->hasMemberships()) {
$this->getManager()->getMemberships($this);
}

return $this->memberships;
public function getMemberships(bool $detailed = false, int $level = 0): array {
return $this->getManager()->getMemberships($this, $detailed, $level);
}


/**
* @return Membership[]
*/
public function getAccountedMemberships(bool $detailed = false, int $level = 0): array {
return $this->getManager()->getAccountedMemberships($this, $detailed, $level);
}

/**
* @param string $singleId
Expand Down
16 changes: 10 additions & 6 deletions lib/Model/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,12 +720,16 @@ public function setMemberships(array $memberships): IEntity {
/**
* @return Membership[]
*/
public function getMemberships(): array {
if (is_null($this->memberships)) {
$this->getManager()->getMemberships($this);
}
public function getMemberships(bool $detailed = false, int $level = 0): array {
return $this->getManager()->getMemberships($this, $detailed, $level);
}

return $this->memberships;

/**
* @return Membership[]
*/
public function getAccountedMemberships(bool $detailed = false): array {
return $this->getManager()->getAccountedMemberships($this, $detailed);
}


Expand All @@ -741,7 +745,7 @@ public function getLink(string $singleId, bool $detailed = false): Membership {
if ($singleId !== '') {
$this->getManager()->getLink($this, $singleId, $detailed);
}

throw new MembershipNotFoundException();
}

Expand Down
Loading

0 comments on commit 402e0ac

Please sign in to comment.