Skip to content

Commit 689a67c

Browse files
committed
refactor(external-share): Port more code to string as type for the id
Signed-off-by: Carl Schwan <carl.schwan@nextcloud.com>
1 parent 0494e0b commit 689a67c

File tree

13 files changed

+210
-489
lines changed

13 files changed

+210
-489
lines changed

apps/federatedfilesharing/lib/Controller/RequestHandlerController.php

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ public function __construct(
4444
IRequest $request,
4545
private FederatedShareProvider $federatedShareProvider,
4646
private IDBConnection $connection,
47-
private Share\IManager $shareManager,
48-
private Notifications $notifications,
49-
private AddressHandler $addressHandler,
50-
private IUserManager $userManager,
5147
private ICloudIdManager $cloudIdManager,
5248
private LoggerInterface $logger,
5349
private ICloudFederationFactory $cloudFederationFactory,
@@ -66,10 +62,10 @@ public function __construct(
6662
* @param string|null $owner Display name of the receiver
6763
* @param string|null $sharedBy Display name of the sender
6864
* @param string|null $shareWith ID of the user that receives the share
69-
* @param int|null $remoteId ID of the remote
65+
* @param string|null $remoteId ID of the remote
7066
* @param string|null $sharedByFederatedId Federated ID of the sender
7167
* @param string|null $ownerFederatedId Federated ID of the receiver
72-
* @return Http\DataResponse<Http::STATUS_OK, list<empty>, array{}>
68+
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
7369
* @throws OCSException
7470
*
7571
* 200: Share created successfully
@@ -83,10 +79,10 @@ public function createShare(
8379
?string $owner = null,
8480
?string $sharedBy = null,
8581
?string $shareWith = null,
86-
?int $remoteId = null,
82+
?string $remoteId = null,
8783
?string $sharedByFederatedId = null,
8884
?string $ownerFederatedId = null,
89-
) {
85+
): DataResponse {
9086
if ($ownerFederatedId === null) {
9187
$ownerFederatedId = $this->cloudIdManager->getCloudId($owner, $this->cleanupRemote($remote))->getId();
9288
}
@@ -132,19 +128,19 @@ public function createShare(
132128
/**
133129
* create re-share on behalf of another user
134130
*
135-
* @param int $id ID of the share
131+
* @param string $id ID of the share
136132
* @param string|null $token Shared secret between servers
137133
* @param string|null $shareWith ID of the user that receives the share
138134
* @param int|null $remoteId ID of the remote
139-
* @return Http\DataResponse<Http::STATUS_OK, array{token: string, remoteId: string}, array{}>
135+
* @return DataResponse<Http::STATUS_OK, array{token: string, remoteId: string}, array{}>
140136
* @throws OCSBadRequestException Re-sharing is not possible
141137
* @throws OCSException
142138
*
143139
* 200: Remote share returned
144140
*/
145141
#[NoCSRFRequired]
146142
#[PublicPage]
147-
public function reShare(int $id, ?string $token = null, ?string $shareWith = null, ?int $remoteId = 0) {
143+
public function reShare(string $id, ?string $token = null, ?string $shareWith = null, ?int $remoteId = 0): DataResponse {
148144
if ($token === null
149145
|| $shareWith === null
150146
|| $remoteId === null
@@ -181,9 +177,9 @@ public function reShare(int $id, ?string $token = null, ?string $shareWith = nul
181177
/**
182178
* accept server-to-server share
183179
*
184-
* @param int $id ID of the remote share
180+
* @param string $id ID of the remote share
185181
* @param string|null $token Shared secret between servers
186-
* @return Http\DataResponse<Http::STATUS_OK, list<empty>, array{}>
182+
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
187183
* @throws OCSException
188184
* @throws ShareNotFound
189185
* @throws HintException
@@ -192,7 +188,7 @@ public function reShare(int $id, ?string $token = null, ?string $shareWith = nul
192188
*/
193189
#[NoCSRFRequired]
194190
#[PublicPage]
195-
public function acceptShare(int $id, ?string $token = null) {
191+
public function acceptShare(string $id, ?string $token = null): DataResponse {
196192
$notification = [
197193
'sharedSecret' => $token,
198194
'message' => 'Recipient accept the share'
@@ -216,16 +212,16 @@ public function acceptShare(int $id, ?string $token = null) {
216212
/**
217213
* decline server-to-server share
218214
*
219-
* @param int $id ID of the remote share
215+
* @param string $id ID of the remote share
220216
* @param string|null $token Shared secret between servers
221-
* @return Http\DataResponse<Http::STATUS_OK, list<empty>, array{}>
217+
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
222218
* @throws OCSException
223219
*
224220
* 200: Share declined successfully
225221
*/
226222
#[NoCSRFRequired]
227223
#[PublicPage]
228-
public function declineShare(int $id, ?string $token = null) {
224+
public function declineShare(string $id, ?string $token = null) {
229225
$notification = [
230226
'sharedSecret' => $token,
231227
'message' => 'Recipient declined the share'
@@ -249,16 +245,16 @@ public function declineShare(int $id, ?string $token = null) {
249245
/**
250246
* remove server-to-server share if it was unshared by the owner
251247
*
252-
* @param int $id ID of the share
248+
* @param string $id ID of the share
253249
* @param string|null $token Shared secret between servers
254-
* @return Http\DataResponse<Http::STATUS_OK, list<empty>, array{}>
250+
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
255251
* @throws OCSException
256252
*
257253
* 200: Share unshared successfully
258254
*/
259255
#[NoCSRFRequired]
260256
#[PublicPage]
261-
public function unshare(int $id, ?string $token = null) {
257+
public function unshare(string $id, ?string $token = null) {
262258
if (!$this->isS2SEnabled()) {
263259
throw new OCSException('Server does not support federated cloud sharing', 503);
264260
}
@@ -275,7 +271,7 @@ public function unshare(int $id, ?string $token = null) {
275271
return new DataResponse();
276272
}
277273

278-
private function cleanupRemote($remote) {
274+
private function cleanupRemote(string $remote): string {
279275
$remote = substr($remote, strpos($remote, '://') + 3);
280276

281277
return rtrim($remote, '/');
@@ -285,16 +281,16 @@ private function cleanupRemote($remote) {
285281
/**
286282
* federated share was revoked, either by the owner or the re-sharer
287283
*
288-
* @param int $id ID of the share
284+
* @param string $id ID of the share
289285
* @param string|null $token Shared secret between servers
290-
* @return Http\DataResponse<Http::STATUS_OK, list<empty>, array{}>
286+
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
291287
* @throws OCSBadRequestException Revoking the share is not possible
292288
*
293289
* 200: Share revoked successfully
294290
*/
295291
#[NoCSRFRequired]
296292
#[PublicPage]
297-
public function revoke(int $id, ?string $token = null) {
293+
public function revoke(string $id, ?string $token = null) {
298294
try {
299295
$provider = $this->cloudFederationProviderManager->getCloudFederationProvider('file');
300296
$notification = ['sharedSecret' => $token];
@@ -324,19 +320,19 @@ private function isS2SEnabled($incoming = false) {
324320
}
325321

326322
/**
327-
* update share information to keep federated re-shares in sync
323+
* Update share information to keep federated re-shares in sync.
328324
*
329-
* @param int $id ID of the share
325+
* @param string $id ID of the share
330326
* @param string|null $token Shared secret between servers
331327
* @param int|null $permissions New permissions
332-
* @return Http\DataResponse<Http::STATUS_OK, list<empty>, array{}>
328+
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
333329
* @throws OCSBadRequestException Updating permissions is not possible
334330
*
335331
* 200: Permissions updated successfully
336332
*/
337333
#[NoCSRFRequired]
338334
#[PublicPage]
339-
public function updatePermissions(int $id, ?string $token = null, ?int $permissions = null) {
335+
public function updatePermissions(string $id, ?string $token = null, ?int $permissions = null) {
340336
$ncPermissions = $permissions;
341337

342338
try {
@@ -385,7 +381,7 @@ protected function ncPermissions2ocmPermissions($ncPermissions) {
385381
* @param string|null $token Shared secret between servers
386382
* @param string|null $remote Address of the remote
387383
* @param string|null $remote_id ID of the remote
388-
* @return Http\DataResponse<Http::STATUS_OK, array{remote: string, owner: string}, array{}>
384+
* @return DataResponse<Http::STATUS_OK, array{remote: string, owner: string}, array{}>
389385
* @throws OCSBadRequestException Moving share is not possible
390386
*
391387
* 200: Share moved successfully

apps/federatedfilesharing/lib/FederatedShareProvider.php

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use OCP\Share\IShare;
2828
use OCP\Share\IShareProvider;
2929
use OCP\Share\IShareProviderSupportsAllSharesInFolder;
30+
use Override;
3031
use Psr\Log\LoggerInterface;
3132

3233
/**
@@ -140,7 +141,7 @@ public function create(IShare $share) {
140141
if ($remoteShare) {
141142
try {
142143
$ownerCloudId = $this->cloudIdManager->getCloudId($remoteShare['owner'], $remoteShare['remote']);
143-
$shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_' . time(), $shareType, $expirationDate);
144+
$shareId = (string)$this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_' . time(), $shareType, $expirationDate);
144145
$share->setId($shareId);
145146
[$token, $remoteId] = $this->askOwnerToReShare($shareWith, $share, $shareId);
146147
// remote share was create successfully if we get a valid token as return
@@ -168,16 +169,14 @@ public function create(IShare $share) {
168169
}
169170

170171
/**
171-
* create federated share and inform the recipient
172+
* Create federated share and inform the recipient.
172173
*
173-
* @param IShare $share
174-
* @return int
175174
* @throws ShareNotFound
176175
* @throws \Exception
177176
*/
178-
protected function createFederatedShare(IShare $share) {
177+
protected function createFederatedShare(IShare $share): string {
179178
$token = $this->tokenHandler->generateToken();
180-
$shareId = $this->addShareToDB(
179+
$shareId = (string)$this->addShareToDB(
181180
$share->getNodeId(),
182181
$share->getNodeType(),
183182
$share->getSharedWith(),
@@ -321,12 +320,9 @@ private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ui
321320
}
322321

323322
/**
324-
* Update a share
325-
*
326-
* @param IShare $share
327-
* @return IShare The share object
323+
* Update a share.
328324
*/
329-
public function update(IShare $share) {
325+
public function update(IShare $share): IShare {
330326
/*
331327
* We allow updating the permissions of federated shares
332328
*/
@@ -348,13 +344,12 @@ public function update(IShare $share) {
348344
}
349345

350346
/**
351-
* send the updated permission to the owner/initiator, if they are not the same
347+
* Send the updated permission to the owner/initiator, if they are not the same.
352348
*
353-
* @param IShare $share
354349
* @throws ShareNotFound
355350
* @throws HintException
356351
*/
357-
protected function sendPermissionUpdate(IShare $share) {
352+
protected function sendPermissionUpdate(IShare $share): void {
358353
$remoteId = $this->getRemoteId($share);
359354
// if the local user is the owner we send the permission change to the initiator
360355
if ($this->userManager->userExists($share->getShareOwner())) {
@@ -367,12 +362,9 @@ protected function sendPermissionUpdate(IShare $share) {
367362

368363

369364
/**
370-
* update successful reShare with the correct token
371-
*
372-
* @param int $shareId
373-
* @param string $token
365+
* Update successful reShare with the correct token.
374366
*/
375-
protected function updateSuccessfulReShare($shareId, $token) {
367+
protected function updateSuccessfulReShare(string $shareId, string $token): void {
376368
$query = $this->dbConnection->getQueryBuilder();
377369
$query->update('share')
378370
->where($query->expr()->eq('id', $query->createNamedParameter($shareId)))
@@ -381,12 +373,9 @@ protected function updateSuccessfulReShare($shareId, $token) {
381373
}
382374

383375
/**
384-
* store remote ID in federated reShare table
385-
*
386-
* @param $shareId
387-
* @param $remoteId
376+
* Store remote ID in federated reShare table.
388377
*/
389-
public function storeRemoteId(int $shareId, string $remoteId): void {
378+
public function storeRemoteId(string $shareId, string $remoteId): void {
390379
$query = $this->dbConnection->getQueryBuilder();
391380
$query->insert('federated_reshares')
392381
->values(
@@ -399,10 +388,8 @@ public function storeRemoteId(int $shareId, string $remoteId): void {
399388
}
400389

401390
/**
402-
* get share ID on remote server for federated re-shares
391+
* Get share ID on remote server for federated re-shares.
403392
*
404-
* @param IShare $share
405-
* @return string
406393
* @throws ShareNotFound
407394
*/
408395
public function getRemoteId(IShare $share): string {
@@ -512,11 +499,9 @@ public function removeShareFromTable(IShare $share) {
512499
}
513500

514501
/**
515-
* remove share from table
516-
*
517-
* @param string $shareId
502+
* Remove share from table.
518503
*/
519-
private function removeShareFromTableById($shareId) {
504+
private function removeShareFromTableById(string $shareId): void {
520505
$qb = $this->dbConnection->getQueryBuilder();
521506
$qb->delete('share')
522507
->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)))
@@ -748,13 +733,8 @@ public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
748733
return $shares;
749734
}
750735

751-
/**
752-
* Get a share by token
753-
*
754-
* @param string $token
755-
* @throws ShareNotFound
756-
*/
757-
public function getShareByToken($token): IShare {
736+
#[Override]
737+
public function getShareByToken(string $token): IShare {
758738
$qb = $this->dbConnection->getQueryBuilder();
759739

760740
$cursor = $qb->select('*')

apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ protected function reshareRequested($id, array $notification) {
618618
$share->setSharedBy($share->getSharedWith());
619619
$share->setSharedWith($shareWith);
620620
$result = $this->federatedShareProvider->create($share);
621-
$this->federatedShareProvider->storeRemoteId((int)$result->getId(), $senderId);
621+
$this->federatedShareProvider->storeRemoteId($result->getId(), $senderId);
622622
return ['token' => $result->getToken(), 'providerId' => $result->getId()];
623623
} else {
624624
throw new ProviderCouldNotAddShareException('resharing not allowed for share: ' . $id);

0 commit comments

Comments
 (0)