Skip to content

Commit 2f4cc3a

Browse files
authored
Merge pull request #8989 from nextcloud/backport/8981/stable-4.0
[stable-4.0] Fix (File Provider): Share Permissions
2 parents 6bd093b + 5357f2e commit 2f4cc3a

3 files changed

Lines changed: 56 additions & 73 deletions

File tree

shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/Extensions/NKShare+Extensions.swift

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,6 @@ import AppKit
1010
import NextcloudKit
1111

1212
extension NKShare {
13-
enum ShareType: Int {
14-
case internalLink = -1
15-
case user = 0
16-
case group = 1
17-
case publicLink = 3
18-
case email = 4
19-
case federatedCloud = 6
20-
case team = 7
21-
case talkConversation = 10
22-
}
23-
24-
enum PermissionValues: Int {
25-
case readShare = 1
26-
case updateShare = 2
27-
case createShare = 4
28-
case deleteShare = 8
29-
case shareShare = 16
30-
case all = 31
31-
}
32-
3313
var typeImage: NSImage? {
3414
var image: NSImage?
3515
switch shareType {
@@ -121,12 +101,15 @@ extension NKShare {
121101
}
122102

123103
var shareesCanEdit: Bool {
124-
get { (permissions & PermissionValues.updateShare.rawValue) != 0 }
104+
get {
105+
NKShare.Permission(rawValue: permissions).contains(.update)
106+
}
107+
125108
set {
126109
if newValue {
127-
permissions |= NKShare.PermissionValues.updateShare.rawValue
110+
permissions = NKShare.Permission(rawValue: permissions).union(.update).rawValue
128111
} else {
129-
permissions &= ~NKShare.PermissionValues.updateShare.rawValue
112+
permissions = NKShare.Permission(rawValue: permissions).subtracting(.update).rawValue
130113
}
131114
}
132115
}

shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/Sharing/ShareController.swift

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,49 +27,37 @@ class ShareController: ObservableObject {
2727
shareWith: String?,
2828
password: String? = nil,
2929
expireDate: String? = nil,
30-
permissions: Int = 1,
31-
publicUpload: Bool = false,
30+
permissions: NKShare.Permission,
31+
publicUpload: Bool,
3232
note: String? = nil,
3333
label: String? = nil,
3434
hideDownload: Bool,
3535
attributes: String? = nil,
3636
options: NKRequestOptions = NKRequestOptions()
3737
) async -> NKError? {
38-
return await withCheckedContinuation { continuation in
39-
if shareType == .publicLink {
40-
kit.createShare(
41-
path: itemServerRelativePath,
42-
shareType: ShareType.publicLink.rawValue,
43-
shareWith: nil,
44-
publicUpload: publicUpload,
45-
hideDownload: hideDownload,
46-
password: password,
47-
permissions: permissions,
48-
account: account.ncKitAccount,
49-
options: options
50-
) { account, share, data, error in
51-
continuation.resume(returning: error)
52-
}
53-
} else {
54-
guard let shareWith = shareWith else {
55-
let error = NKError(statusCode: 0, fallbackDescription: "No recipient for share!")
56-
continuation.resume(returning: error)
57-
return
58-
}
38+
let sharee: String? = if shareType == .publicLink {
39+
nil
40+
} else {
41+
shareWith
42+
}
5943

60-
kit.createShare(
61-
path: itemServerRelativePath,
62-
shareType: shareType.rawValue,
63-
shareWith: shareWith,
64-
password: password,
65-
permissions: permissions,
66-
attributes: attributes,
67-
account: account.ncKitAccount
68-
) { account, share, data, error in
69-
continuation.resume(returning: error)
70-
}
71-
}
44+
if shareType != .publicLink, sharee == nil {
45+
// Any share requires a recipient, except public links.
46+
return NKError(statusCode: 0, fallbackDescription: "No recipient for share!")
7247
}
48+
49+
let (_, _, _, error) = await kit.createShareAsync(
50+
path: itemServerRelativePath,
51+
shareType: shareType.rawValue,
52+
shareWith: sharee,
53+
hideDownload: hideDownload,
54+
password: password,
55+
permissions: permissions.rawValue,
56+
account: account.ncKitAccount,
57+
options: options
58+
)
59+
60+
return error
7361
}
7462

7563
init(share: NKShare, account: Account, kit: NextcloudKit, log: any FileProviderLogging) {

shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/Sharing/ShareOptionsView.swift

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ class ShareOptionsView: NSView {
5656

5757
suggestionsTextFieldDelegate.suggestionsDataSource = ShareeSuggestionsDataSource(account: account, kit: kit, log: controller.log)
5858

59-
suggestionsTextFieldDelegate.confirmationHandler = { suggestion in
60-
guard let sharee = suggestion?.data as? NKSharee else {
61-
return
59+
suggestionsTextFieldDelegate.confirmationHandler = { [weak self] suggestion in
60+
Task { @MainActor in
61+
guard let sharee = suggestion?.data as? NKSharee else {
62+
return
63+
}
64+
self?.shareRecipientTextField.stringValue = sharee.shareWith
65+
self?.logger?.debug("Chose sharee \(sharee.shareWith)")
6266
}
63-
64-
self.shareRecipientTextField.stringValue = sharee.shareWith
65-
self.logger?.debug("Chose sharee \(sharee.shareWith)")
6667
}
6768

6869
suggestionsTextFieldDelegate.targetTextField = shareRecipientTextField
@@ -190,7 +191,7 @@ class ShareOptionsView: NSView {
190191

191192
if let caps = dataSource?.capabilities?.filesSharing {
192193
uploadEditPermissionCheckbox.state =
193-
caps.defaultPermissions & NKShare.PermissionValues.updateShare.rawValue != 0
194+
caps.defaultPermissions & NKShare.Permission.update.rawValue != 0
194195
? .on : .off
195196

196197
switch type {
@@ -201,7 +202,7 @@ class ShareOptionsView: NSView {
201202
expirationDateCheckbox.state = caps.publicLink?.expireDateEnforced == true ? .on : .off
202203
expirationDateCheckbox.isEnabled = caps.publicLink?.expireDateEnforced == false
203204
expirationDatePicker.dateValue = Date(
204-
timeIntervalSinceNow:
205+
timeIntervalSinceNow:
205206
TimeInterval((caps.publicLink?.expireDateDays ?? 1) * 24 * 60 * 60)
206207
)
207208
if caps.publicLink?.expireDateEnforced == true {
@@ -281,12 +282,15 @@ class ShareOptionsView: NSView {
281282
let password = passwordProtectCheckbox.state == .on
282283
? passwordSecureField.stringValue
283284
: ""
285+
284286
let expireDate = expirationDateCheckbox.state == .on
285287
? NKShare.formattedDateString(date: expirationDatePicker.dateValue)
286288
: ""
289+
287290
let note = noteForRecipientCheckbox.state == .on
288291
? noteTextField.stringValue
289292
: ""
293+
290294
let label = labelTextField.stringValue
291295
let hideDownload = hideDownloadCheckbox.state == .on
292296
let uploadAndEdit = uploadEditPermissionCheckbox.state == .on
@@ -305,14 +309,17 @@ class ShareOptionsView: NSView {
305309
let selectedShareType = pickedShareType()
306310
let shareWith = shareRecipientTextField.stringValue
307311

308-
var permissions = NKShare.PermissionValues.all.rawValue
309-
permissions = uploadAndEdit
310-
? permissions | NKShare.PermissionValues.updateShare.rawValue
311-
: permissions & ~NKShare.PermissionValues.updateShare.rawValue
312+
var permissions = NKShare.Permission.defaultPermission(for: selectedShareType)
313+
314+
if uploadAndEdit {
315+
permissions.formUnion(.create)
316+
permissions.formUnion(.update)
317+
}
312318

313319
setAllFields(enabled: false)
314320
deleteButton.isEnabled = false
315321
saveButton.isEnabled = false
322+
316323
let error = await ShareController.create(
317324
account: account,
318325
kit: kit,
@@ -322,17 +329,20 @@ class ShareOptionsView: NSView {
322329
password: password,
323330
expireDate: expireDate,
324331
permissions: permissions,
332+
publicUpload: permissions.contains(.create),
325333
note: note,
326334
label: label,
327335
hideDownload: hideDownload
328336
)
337+
329338
if let error = error, error != .success {
330339
dataSource.uiDelegate?.showError(String(localized: "Error creating: \(error.errorDescription)"))
331340
setAllFields(enabled: true)
332341
} else {
333342
dataSource.uiDelegate?.hideOptions(self)
334343
await dataSource.reload()
335344
}
345+
336346
return
337347
}
338348

@@ -342,14 +352,16 @@ class ShareOptionsView: NSView {
342352
logger?.error("No valid share controller, cannot edit share.")
343353
return
344354
}
355+
345356
let share = controller.share
346357
let permissions = uploadAndEdit
347-
? share.permissions | NKShare.PermissionValues.updateShare.rawValue
348-
: share.permissions & ~NKShare.PermissionValues.updateShare.rawValue
358+
? share.permissions | NKShare.Permission.update.rawValue
359+
: share.permissions & ~NKShare.Permission.update.rawValue
349360

350361
setAllFields(enabled: false)
351362
deleteButton.isEnabled = false
352363
saveButton.isEnabled = false
364+
353365
let error = await controller.save(
354366
password: password,
355367
expireDate: expireDate,
@@ -358,6 +370,7 @@ class ShareOptionsView: NSView {
358370
label: label,
359371
hideDownload: hideDownload
360372
)
373+
361374
if let error = error, error != .success {
362375
dataSource?.uiDelegate?.showError("Error updating share: \(error.errorDescription)")
363376
setAllFields(enabled: true)
@@ -387,4 +400,3 @@ class ShareOptionsView: NSView {
387400
}
388401
}
389402
}
390-

0 commit comments

Comments
 (0)