Browse Source

Fix share permission, disable parent share permissions

related to dc3ba9a4e6c40bbf827c0b05c50707394c52b3cd

Signed-off-by: Henrik Storch <henrik.storch@nextcloud.com>
Henrik Storch 3 years ago
parent
commit
a427bceb85

+ 9 - 5
iOSClient/Share/Advanced/NCShareAdvancePermission.swift

@@ -45,7 +45,7 @@ class NCShareAdvancePermission: UITableViewController, NCShareAdvanceFotterDeleg
 
     override func viewDidLoad() {
         super.viewDidLoad()
-        self.shareConfig = NCShareConfig(isDirectory: metadata.directory, share: share)
+        self.shareConfig = NCShareConfig(parentMetadata: metadata, share: share)
         self.setNavigationTitle()
         if #available(iOS 13.0, *) {
             // disbale pull to dimiss
@@ -99,17 +99,21 @@ class NCShareAdvancePermission: UITableViewController, NCShareAdvanceFotterDeleg
 
     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         if section == 0 {
-            return shareConfig.permissions.count
+            return shareConfig.parentPermission != 31 ? shareConfig.permissions.count + 1 : shareConfig.permissions.count
         } else if section == 1 {
             return shareConfig.advanced.count
         } else { return 0 }
     }
 
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
-        guard let cell = shareConfig.cellFor(indexPath: indexPath) else { return UITableViewCell() }
-        if let cell = cell as? NCShareDateCell {
-            cell.onReload = tableView.reloadData
+        guard let cell = shareConfig.cellFor(indexPath: indexPath) else {
+            let noteCell = UITableViewCell(style: .subtitle, reuseIdentifier: "noteCell")
+            noteCell.detailTextLabel?.text = "Reshare permissions restricted"
+            noteCell.detailTextLabel?.isEnabled = false
+            noteCell.isUserInteractionEnabled = false
+            return noteCell
         }
+        if let cell = cell as? NCShareDateCell { cell.onReload = tableView.reloadData }
         return cell
     }
 

+ 34 - 17
iOSClient/Share/Advanced/NCShareCells.swift

@@ -47,9 +47,14 @@ extension NCToggleCellConfig {
 protocol NCPermission: NCToggleCellConfig {
     static var forDirectory: [Self] { get }
     static var forFile: [Self] { get }
+    func hasResharePermission(for parentPermission: Int) -> Bool
 }
 
 enum NCUserPermission: CaseIterable, NCPermission {
+    func hasResharePermission(for parentPermission: Int) -> Bool {
+        return ((permissionBitFlag & parentPermission) != 0)
+    }
+
     var permissionBitFlag: Int {
         switch self {
         case .reshare: return NCGlobal.shared.permissionShareShare
@@ -83,23 +88,29 @@ enum NCUserPermission: CaseIterable, NCPermission {
 
 enum NCLinkPermission: NCPermission {
     func didChange(_ share: NCTableShareable, to newValue: Bool) {
-        guard self != .allowEdit else {
-            // file
-            share.permissions = CCUtility.getPermissionsValue(
-                byCanEdit: newValue,
-                andCanCreate: newValue,
-                andCanChange: newValue,
-                andCanDelete: newValue,
-                andCanShare: false,
-                andIsFolder: false)
+        guard self != .allowEdit || newValue else {
+            share.permissions = NCGlobal.shared.permissionReadShare
             return
         }
-        // can't deselect, only change
-        guard newValue == true else { return }
+        share.permissions = permissionValue
+    }
+
+    func hasResharePermission(for parentPermission: Int) -> Bool {
+        permissionValue & parentPermission == permissionValue
+    }
+
+    var permissionValue: Int {
         switch self {
-        case .allowEdit: return
+        case .allowEdit:
+            return CCUtility.getPermissionsValue(
+                byCanEdit: true,
+                andCanCreate: true,
+                andCanChange: true,
+                andCanDelete: true,
+                andCanShare: false,
+                andIsFolder: false)
         case .viewOnly:
-            share.permissions = CCUtility.getPermissionsValue(
+            return CCUtility.getPermissionsValue(
                 byCanEdit: false,
                 andCanCreate: false,
                 andCanChange: false,
@@ -107,7 +118,7 @@ enum NCLinkPermission: NCPermission {
                 andCanShare: false,
                 andIsFolder: true)
         case .uploadEdit:
-            share.permissions = CCUtility.getPermissionsValue(
+            return CCUtility.getPermissionsValue(
                 byCanEdit: true,
                 andCanCreate: true,
                 andCanChange: true,
@@ -115,7 +126,7 @@ enum NCLinkPermission: NCPermission {
                 andCanShare: false,
                 andIsFolder: true)
         case .fileDrop:
-            share.permissions = NCGlobal.shared.permissionCreateShare
+            return NCGlobal.shared.permissionCreateShare
         }
     }
 
@@ -191,11 +202,13 @@ struct NCShareConfig {
     let permissions: [NCPermission]
     let advanced: [NCShareDetails]
     let share: NCTableShareable
+    let parentPermission: Int
 
-    init(isDirectory: Bool, share: NCTableShareable) {
+    init(parentMetadata: tableMetadata, share: NCTableShareable) {
         self.share = share
+        self.parentPermission = parentMetadata.sharePermissionsCollaborationServices
         let type: NCPermission.Type = share.shareType == NCShareCommon.shared.SHARE_TYPE_LINK ? NCLinkPermission.self : NCUserPermission.self
-        self.permissions = isDirectory ? type.forDirectory : type.forFile
+        self.permissions = parentMetadata.directory ? type.forDirectory : type.forFile
         self.advanced = share.shareType == NCShareCommon.shared.SHARE_TYPE_LINK ? NCShareDetails.forLink : NCShareDetails.forUser
     }
 
@@ -203,6 +216,10 @@ struct NCShareConfig {
         let cellConfig = config(for: indexPath)
         let cell = cellConfig?.getCell(for: share)
         cell?.textLabel?.text = cellConfig?.title
+        if let cellConfig = cellConfig as? NCPermission, !cellConfig.hasResharePermission(for: parentPermission) {
+            cell?.isUserInteractionEnabled = false
+            cell?.textLabel?.isEnabled = false
+        }
         return cell
     }