Skip to content
Open
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
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Those groups of people can then be used by any other app for sharing purpose.
]]>
</description>
<version>35.0.0-dev.0</version>
<version>35.0.0-dev.1</version>
<licence>agpl</licence>
<author>Maxence Lange</author>
<types>
Expand Down
4 changes: 4 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
['name' => 'Local#editName', 'url' => '/circles/{circleId}/name', 'verb' => 'PUT'],
['name' => 'Local#editDescription', 'url' => '/circles/{circleId}/description', 'verb' => 'PUT'],
['name' => 'Local#editSetting', 'url' => '/circles/{circleId}/setting', 'verb' => 'PUT'],
['name' => 'Local#createInvitation', 'url' => '/circles/{circleId}/invitation', 'verb' => 'PUT'],
['name' => 'Local#revokeInvitation', 'url' => '/circles/{circleId}/invitation', 'verb' => 'DELETE'],
['name' => 'Local#getInvitation', 'url' => '/invitations/{invitationCode}', 'verb' => 'GET'],
['name' => 'Local#joinInvitation', 'url' => '/invitations/{invitationCode}', 'verb' => 'POST'],
['name' => 'Local#editConfig', 'url' => '/circles/{circleId}/config', 'verb' => 'PUT'],
['name' => 'Local#circleAvatar', 'url' => '/circles/{circleId}/avatar', 'verb' => 'GET'],
['name' => 'Local#uploadAvatar', 'url' => '/circles/{circleId}/avatar', 'verb' => 'POST'],
Expand Down
91 changes: 91 additions & 0 deletions lib/Controller/LocalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,97 @@ public function link(string $circleId, string $singleId): DataResponse {
}
}

#[NoAdminRequired]
public function createInvitation(string $circleId): DataResponse {
try {
$this->setCurrentFederatedUser();

$outcome = $this->circleService->createInvitation($circleId);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this only be reachable by members with certain levels (e.g. admin, owner)? as it is right now, seems like anyone can create invitations

you can see on other methods of this controller how permissions are handled, for example here:
https://github.com/nextcloud/circles/blob/master/lib/Controller/LocalController.php#L423


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

#[NoAdminRequired]
public function revokeInvitation(string $circleId): DataResponse {
try {
$this->setCurrentFederatedUser();

$outcome = $this->circleService->revokeInvitation($circleId);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as createInvitation()


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

#[NoAdminRequired]
#[BruteForceProtection(action: 'getInvitation')]
public function getInvitation(string $invitationCode): DataResponse {
try {
$this->setCurrentFederatedUser();

$circleProbe = (new CircleProbe())
->includeSystemCircles()
->includeHiddenCircles()
->filterByInvitationCode($invitationCode);

$circles = $this->circleService->getCircles($circleProbe);
if (empty($circles)) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
$circle = reset($circles);

$membershipStatus = 'NOT_A_MEMBER';
if ($circle->hasInitiator()) {
if ($circle->getInitiator()->getLevel() > Member::LEVEL_NONE) {
$membershipStatus = 'MEMBER';
} elseif ($circle->getInitiator()->getStatus() === Member::STATUS_REQUEST) {
$membershipStatus = 'REQUESTED_MEMBERSHIP';
}
}

return new DataResponse([
'circleId' => $circle->getSingleId(),
'circleName' => $circle->getName(),
'membershipStatus' => $membershipStatus,
]);
} catch (\Exception $e) {
$this->e($e, ['circleId' => $invitationCode]);
throw new OCSException($e->getMessage(), (int)$e->getCode(), $e);
}
}

#[NoAdminRequired]
#[BruteForceProtection(action: 'joinInvitation')]
public function joinInvitation(string $invitationCode): DataResponse {
try {
$this->setCurrentFederatedUser();

$circleProbe = (new CircleProbe())
->includeSystemCircles()
->includeHiddenCircles()
->filterByInvitationCode($invitationCode);

$circles = $this->circleService->getCircles($circleProbe);
if (empty($circles)) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
$circle = reset($circles);

$result = $this->circleService->circleJoin($circle->getSingleId(), $invitationCode);

return new DataResponse($this->serializeArray($result));
} catch (\Exception $e) {
$this->e($e, ['circleId' => $invitationCode]);
throw new OCSException($e->getMessage(), (int)$e->getCode(), $e);
}
}

/**
* @throws FederatedUserException
* @throws FederatedUserNotFoundException
Expand Down
54 changes: 54 additions & 0 deletions lib/Db/CircleInvitationRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Circles\Db;

use OCA\Circles\Exceptions\InvalidIdException;
use OCA\Circles\Model\CircleInvitation;

/**
* Class CircleInvitationRequest
*
* @package OCA\Circles\Db
*/
class CircleInvitationRequest extends CircleRequestBuilder {
/**
* @throws InvalidIdException
*/
public function save(CircleInvitation $circleInvitation): void {
$this->confirmValidId($circleInvitation->getCircleId());

$qb = $this->getQueryBuilder();
$qb->insert(self::TABLE_INVITATIONS)
->setValue('created', $qb->createNamedParameter($this->timezoneService->getUTCDate()));
$qb->setValue('circle_id', $qb->createNamedParameter($circleInvitation->getCircleId()))
->setValue('invitation_code', $qb->createNamedParameter($circleInvitation->getInvitationCode()))
->setValue('created_by', $qb->createNamedParameter($circleInvitation->getCreatedBy()));
$qb->executeStatement();
}

/**
* @throws InvalidIdException
*/
public function replace(CircleInvitation $circleInvitation): void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could there be multiple invitation per circle? If so, shouldn't we delete a specific invitation, and not all of them?

@Koc Koc Jun 28, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not now. In theory we can add that, but in this case we need rework UI and introduce something like "invitation link name" to distinguish links.

I would like to start with something really simple and improve it later

$qb = $this->getQueryBuilder();
$qb->delete(self::TABLE_INVITATIONS);
$qb->limitToCircleId($circleInvitation->getCircleId());
$qb->executeStatement();

$this->save($circleInvitation);
}

public function delete(string $circleId): void {
$qb = $this->getQueryBuilder();
$qb->delete(self::TABLE_INVITATIONS);
$qb->limitToCircleId($circleId);
$qb->executeStatement();
}
}
5 changes: 5 additions & 0 deletions lib/Db/CircleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,17 @@ public function getCircles(?IFederatedUser $initiator, CircleProbe $probe): arra
$qb->limitToInitiator(CoreQueryBuilder::CIRCLE, $initiator);
$qb->orderBy($qb->generateAlias(CoreQueryBuilder::CIRCLE, CoreQueryBuilder::INITIATOR) . '.level', 'desc');
$qb->addOrderBy(CoreQueryBuilder::CIRCLE . '.display_name', 'asc');
$qb->leftJoinCircleInvitation(CoreQueryBuilder::CIRCLE);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we join only if $probe->hasInvitationCode() === true?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, because we need to respond with invitationCode in GET https://nextcloud.test/ocs/v2.php/apps/circles/circles endpoint

image

}
if ($probe->hasFilterMember()) {
$qb->limitToDirectMembership(CoreQueryBuilder::CIRCLE, $probe->getFilterMember());
}
if ($probe->hasFilterCircle()) {
$qb->filterCircleDetails($probe->getFilterCircle());
}
if ($probe->hasInvitationCode()) {
$qb->filterInvitationCode(CoreQueryBuilder::CIRCLE, $probe->getInvitationCode());
}
if ($probe->hasFilterRemoteInstance()) {
$qb->limitToRemoteInstance(CoreQueryBuilder::CIRCLE, $probe->getFilterRemoteInstance(), false);
}
Expand Down Expand Up @@ -375,6 +379,7 @@ public function getCircle(
$qb->limitToUniqueId($id);
$qb->filterCircles(CoreQueryBuilder::CIRCLE, $probe);
$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE);
$qb->leftJoinCircleInvitation(CoreQueryBuilder::CIRCLE);
// $qb->setOptions(
// [CoreRequestBuilder::CIRCLE, CoreRequestBuilder::INITIATOR], [
// 'mustBeMember' => false,
Expand Down
14 changes: 11 additions & 3 deletions lib/Db/CircleRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ protected function getCircleInsertSql(): CoreQueryBuilder {
return $qb;
}


/**
* @return CoreQueryBuilder&IQueryBuilder
*/
Expand All @@ -44,7 +43,6 @@ protected function getCircleUpdateSql(): CoreQueryBuilder {
return $qb;
}


/**
* @param string $alias
* @param bool $single
Expand All @@ -65,7 +63,6 @@ protected function getCircleSelectSql(
return $qb;
}


/**
* Base of the Sql Delete request
*
Expand All @@ -78,6 +75,17 @@ protected function getCircleDeleteSql(): CoreQueryBuilder {
return $qb;
}

/**
* Base of the Sql Delete request
*
* @return CoreQueryBuilder&IQueryBuilder
*/
protected function getCircleInvitationDeleteSql(): CoreQueryBuilder {
$qb = $this->getQueryBuilder();
$qb->delete(self::TABLE_INVITATIONS);

return $qb;
}

/**
* @param CoreQueryBuilder&IQueryBuilder $qb
Expand Down
57 changes: 56 additions & 1 deletion lib/Db/CoreQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class CoreQueryBuilder extends ExtendedQueryBuilder {
public const TOKEN = 'u';
public const OPTIONS = 'v';
public const HELPER = 'w';

public const INVITATION = 'x';

public static $SQL_PATH = [
self::SINGLE => [
Expand All @@ -69,6 +69,7 @@ class CoreQueryBuilder extends ExtendedQueryBuilder {
self::OPTIONS => [
],
self::MEMBER,
self::INVITATION,
self::OWNER => [
self::BASED_ON
],
Expand Down Expand Up @@ -865,6 +866,27 @@ public function leftJoinOwner(string $alias, string $field = 'unique_id'): void
$this->leftJoinBasedOn($aliasMember);
}

/**
* @throws RequestBuilderException
*/
public function leftJoinCircleInvitation(string $alias, string $field = 'unique_id'): void {
if ($this->getType() !== QueryBuilder::SELECT) {
return;
}

try {
$aliasInvitation = $this->generateAlias($alias, self::INVITATION, $options);
} catch (RequestBuilderException $e) {
return;
}

$expr = $this->expr();
$this->generateCircleInvitationSelectAlias($aliasInvitation)
->leftJoin(
$alias, CoreRequestBuilder::TABLE_INVITATIONS, $aliasInvitation,
$expr->eq($aliasInvitation . '.circle_id', $alias . '.' . $field),
);
}

/**
* @param CircleProbe $probe
Expand Down Expand Up @@ -1332,6 +1354,12 @@ protected function limitInitiatorVisibility(string $alias): ICompositeExpression
$aliasMembershipCircle = $this->generateAlias($aliasMembership, self::CONFIG, $options);
$levelCheck = [$aliasMembership];

// no need to check anything, we are filtering by invitation code
$invitationCode = $this->get('filterInvitationCode', $options, '');
if ($invitationCode) {
return $this->expr()->andX($this->expr()->eq('1', '1'));
}

$directMember = '';
if ($this->getBool('initiatorDirectMember', $options, false)) {
$directMember = $this->generateAlias($alias, self::DIRECT_INITIATOR, $options);
Expand Down Expand Up @@ -1547,6 +1575,22 @@ public function limitToShareOwner(
}
}

public function filterInvitationCode(string $alias, string $invitationCode): void {
if ($this->getType() !== QueryBuilder::SELECT) {
return;
}

try {
$aliasInvitation = $this->generateAlias($alias, self::INVITATION, $options);
} catch (RequestBuilderException $e) {
return;
}

$expr = $this->expr();
$this->andWhere(
$expr->eq($aliasInvitation . '.invitation_code', $this->createNamedParameter($invitationCode))
);
}

/**
* @param string $aliasMount
Expand Down Expand Up @@ -1635,6 +1679,17 @@ private function generateMemberSelectAlias(string $alias, array $default = []):
return $this;
}

private function generateCircleInvitationSelectAlias(string $alias, array $default = []): self {
$this->generateSelectAlias(
CoreRequestBuilder::$tables[CoreRequestBuilder::TABLE_INVITATIONS],
$alias,
$alias,
$default
);

return $this;
}


/**
* @param string $alias
Expand Down
7 changes: 7 additions & 0 deletions lib/Db/CoreRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class CoreRequestBuilder {
public const TABLE_STORAGES = 'storages';

public const TABLE_CIRCLE = 'circles_circle';
public const TABLE_INVITATIONS = 'circles_invitations';
public const TABLE_MEMBER = 'circles_member';
public const TABLE_MEMBERSHIP = 'circles_membership';
public const TABLE_REMOTE = 'circles_remote';
Expand Down Expand Up @@ -64,6 +65,12 @@ class CoreRequestBuilder {
'contact_groupname',
'creation'
],
self::TABLE_INVITATIONS => [
'circle_id',
'invitation_code',
'created_by',
'created',
],
self::TABLE_MEMBER => [
'circle_id',
'member_id',
Expand Down
14 changes: 14 additions & 0 deletions lib/Exceptions/CircleInvitationNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);


/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Circles\Exceptions;

class CircleInvitationNotFoundException extends FederatedItemNotFoundException {
}
Loading
Loading