-
Notifications
You must be signed in to change notification settings - Fork 323
Feat/assign users on federated boards #7703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,12 +8,16 @@ | |
| namespace OCA\Deck\Service; | ||
|
|
||
| use OCA\Deck\Db\Acl; | ||
| use OCA\Deck\Db\Assignment; | ||
| use OCA\Deck\Db\Board; | ||
| use OCA\Deck\Db\BoardMapper; | ||
| use OCA\Deck\Db\FederatedUser; | ||
| use OCA\Deck\Db\User; | ||
| use OCA\Deck\Federation\DeckFederationProxy; | ||
| use OCA\Deck\Model\OptionalNullableValue; | ||
| use OCP\AppFramework\Http\DataResponse; | ||
| use OCP\Federation\ICloudIdManager; | ||
| use OCP\IURLGenerator; | ||
| use OCP\IUserManager; | ||
|
|
||
| class ExternalBoardService { | ||
|
|
@@ -25,6 +29,7 @@ public function __construct( | |
| private BoardService $boardService, | ||
| private PermissionService $permissionService, | ||
| private BoardMapper $boardMapper, | ||
| private IURLGenerator $urlGenerator, | ||
| private ?string $userId, | ||
| ) { | ||
| } | ||
|
|
@@ -50,9 +55,37 @@ public function getExternalStacksFromRemote(Board $localBoard):DataResponse { | |
| return new DataResponse($this->LocalizeRemoteStacks($ocs, $localBoard)); | ||
| } | ||
|
|
||
| public function localizeRemoteUser(Board $localBoard, array $user): array|User|FederatedUser|null { | ||
| // skip invalid users | ||
| if (!$user['uid']) { | ||
| return null; | ||
| } | ||
| // if it's already a valid cloud id the user originates from a third instance and we pass it as is | ||
| if ($this->cloudIdManager->isValidCloudId($user['uid'])) { | ||
| if ($user['remote'] == $this->urlGenerator->getBaseUrl()) { | ||
| // local user from remote: return as local user | ||
| $localuid = $this->cloudIdManager->resolveCloudId($user['uid'])->getUser(); | ||
| return new User($localuid, $this->userManager); | ||
| } | ||
| return new FederatedUser($this->cloudIdManager->resolveCloudId($user['uid'])); | ||
| } | ||
| // if it's not a valid cloud id: it originates from the remote instance and we send it out as a federated user | ||
| $owner = $localBoard->resolveOwner(); // retrieve owner to get the remote | ||
| if ($owner instanceof FederatedUser) { | ||
| return new FederatedUser($this->cloudIdManager->getCloudId($user['uid'], $owner->getCloudId()->getRemote())); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public function LocalizeRemoteStacks(array $stacks, Board $localBoard) { | ||
| foreach ($stacks as $i => $stack) { | ||
| $stack['boardId'] = $localBoard->getId(); | ||
| foreach ($stack['cards'] as $j => $card) { | ||
| $stack['cards'][$j]['assignedUsers'] = array_map(function ($assignment) use ($localBoard) { | ||
| $assignment['participant'] = $this->localizeRemoteUser($localBoard, $assignment['participant']); | ||
| return $assignment; | ||
| }, $card['assignedUsers']); | ||
| } | ||
| $stacks[$i] = $stack; | ||
| } | ||
| return $stacks; | ||
|
|
@@ -63,9 +96,19 @@ public function LocalizeRemoteBoard(array $remoteBoard, Board $localBoard) { | |
| $remoteBoard['owner'] = $localBoard->resolveOwner(); | ||
| $remoteBoard['acl'] = $localBoard->getAcl(); | ||
| $remoteBoard['permissions'] = $localBoard->getPermissions(); | ||
| $remoteBoard['users'] = $this->localizeRemoteUsers($remoteBoard['users'], $localBoard); | ||
| return $remoteBoard; | ||
| } | ||
|
|
||
| public function localizeRemoteUsers(array $users, Board $localBoard) { | ||
| $localizedUsers = []; | ||
| foreach ($users as $i => $user) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $i is not used here, was there a reason for keeping it? |
||
| $localizedUsers[] = $this->localizeRemoteUser($localBoard, $user); | ||
| } | ||
|
|
||
| return $localizedUsers; | ||
| } | ||
|
|
||
| public function createCardOnRemote( | ||
| Board $localBoard, | ||
| string $title, | ||
|
|
@@ -159,6 +202,64 @@ public function removeLabelOnRemote(Board $localBoard, int $cardId, int $labelId | |
| return $this->proxy->getOcsData($resp); | ||
| } | ||
|
|
||
| public function assignUserOnRemote(Board $localBoard, int $cardId, string $userId, int $type = 0): array { | ||
| $this->configService->ensureFederationEnabled(); | ||
|
|
||
| $ownerCloudId = $this->cloudIdManager->resolveCloudId($localBoard->getOwner()); | ||
|
|
||
| if ($this->cloudIdManager->isValidCloudId($userId)) { | ||
| $cloudId = $this->cloudIdManager->resolveCloudId($userId); | ||
| // assignee's origin is the same as the board owner's origin: send as local user | ||
| if ($cloudId->getRemote() === $ownerCloudId->getRemote()) { | ||
| $userId = $cloudId->getUser(); | ||
| $type = Assignment::TYPE_USER; | ||
| } | ||
| } else { | ||
| // local user for us = remote user for remote | ||
| $userId = $this->cloudIdManager->getCloudId($userId, null)->getId(); | ||
| $type = Assignment::TYPE_REMOTE; | ||
| } | ||
| $shareToken = $localBoard->getShareToken(); | ||
| $url = $ownerCloudId->getRemote() . '/ocs/v2.php/apps/deck/api/v1.0/cards/' . $cardId . '/assign'; | ||
| $resp = $this->proxy->post($ownerCloudId->getId(), $shareToken, $url, [ | ||
| 'userId' => $userId, | ||
| 'type' => $type, | ||
| 'boardId' => $localBoard->getExternalId(), | ||
| ]); | ||
| $result = $this->proxy->getOcsData($resp); | ||
| if (isset($result['participant'])) { | ||
| $result['participant'] = $this->localizeRemoteUser($localBoard, $result['participant']); | ||
| } | ||
| return $result; | ||
| } | ||
|
|
||
| public function unAssignUserOnRemote(Board $localBoard, int $cardId, string $userId, int $type = 0): array { | ||
| $this->configService->ensureFederationEnabled(); | ||
|
|
||
| $ownerCloudId = $this->cloudIdManager->resolveCloudId($localBoard->getOwner()); | ||
|
|
||
| if ($this->cloudIdManager->isValidCloudId($userId)) { | ||
| $cloudId = $this->cloudIdManager->resolveCloudId($userId); | ||
| // assignee's origin is the same as the board owner's origin: send as local user | ||
| if ($cloudId->getRemote() === $ownerCloudId->getRemote()) { | ||
| $userId = $cloudId->getUser(); | ||
| $type = Assignment::TYPE_USER; | ||
| } | ||
| } else { | ||
| // local user for us = remote user for remote | ||
| $userId = $this->cloudIdManager->getCloudId($userId, null)->getId(); | ||
| $type = Assignment::TYPE_REMOTE; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
this code block is duplicated in both assignUserOnRemote and unAssignUserOnRemote. maybe extract it into a private helper like resolveUserIdForRemote() to avoid the duplication and make maintenance easier for later? |
||
| $shareToken = $localBoard->getShareToken(); | ||
| $url = $ownerCloudId->getRemote() . '/ocs/v2.php/apps/deck/api/v1.0/cards/' . $cardId . '/unassign'; | ||
| $resp = $this->proxy->put($ownerCloudId->getId(), $shareToken, $url, [ | ||
| 'userId' => $userId, | ||
| 'type' => $type, | ||
| 'boardId' => $localBoard->getExternalId(), | ||
| ]); | ||
| return $this->proxy->getOcsData($resp); | ||
| } | ||
|
|
||
| public function createStackOnRemote( | ||
| Board $localBoard, | ||
| string $title, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe throw an exception instead of return null? if the owner isnt federated here.