Sfoglia il codice sorgente

Merge pull request #2535 from nextcloud/sharingAttributes

Sharing attributes - Allow download folder
Marino Faggiana 1 anno fa
parent
commit
6866d9e284

+ 1 - 1
Brand/Database.swift

@@ -26,4 +26,4 @@ import Foundation
 // Database Realm
 //
 let databaseName                    = "nextcloud.realm"
-let databaseSchemaVersion: UInt64   = 299
+let databaseSchemaVersion: UInt64   = 300

+ 2 - 2
Nextcloud.xcodeproj/project.pbxproj

@@ -4937,8 +4937,8 @@
 			isa = XCRemoteSwiftPackageReference;
 			repositoryURL = "https://github.com/nextcloud/NextcloudKit";
 			requirement = {
-				kind = exactVersion;
-				version = 2.6.0;
+				branch = "2425-missing-sharing-options";
+				kind = branch;
 			};
 		};
 		F788ECC5263AAAF900ADC67F /* XCRemoteSwiftPackageReference "MarkdownKit" */ = {

+ 30 - 0
iOSClient/Data/NCManageDatabase+Share.swift

@@ -67,6 +67,7 @@ class tableShareV2: Object {
     @objc dynamic var userIcon = ""
     @objc dynamic var userMessage = ""
     @objc dynamic var userStatus = ""
+    @objc dynamic var attributes: String?
 
     override static func primaryKey() -> String {
         return "primaryKey"
@@ -127,6 +128,7 @@ extension NCManageDatabase {
                     object.userIcon = share.userIcon
                     object.userMessage = share.userMessage
                     object.userStatus = share.userStatus
+                    object.attributes = share.attributes
                     realm.add(object, update: .all)
                 }
             }
@@ -236,4 +238,32 @@ extension NCManageDatabase {
             NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
         }
     }
+
+    // There is currently only one share attribute “download” from the scope “permissions”. This attribute is only valid for user and group shares, not for public link shares.
+    func setAttibuteDownload(state: Bool) -> String? {
+        if state {
+            return nil
+        } else {
+            return "[{\"scope\":\"permissions\",\"key\":\"download\",\"enabled\":false}]"
+        }
+    }
+
+    func isAttributeDownloadEnabled(attributes: String?) -> Bool {
+        if let attributes = attributes, let data = attributes.data(using: .utf8) {
+            do {
+                if let json = try JSONSerialization.jsonObject(with: data) as? [Dictionary<String, Any>] {
+                    for sub in json {
+                        let key = sub["key"] as? String
+                        let enabled = sub["enabled"] as? Bool
+                        let scope = sub["scope"] as? String
+                        if key == "download", scope == "permissions", let enabled = enabled {
+                            return enabled
+                        }
+                    }
+                }
+            } catch let error as NSError { print(error) }
+        }
+        return true
+    }
+
 }

+ 2 - 0
iOSClient/NCGlobal.swift

@@ -257,6 +257,8 @@ class NCGlobal: NSObject {
     @objc let permissionMaxFolderShare: Int         = 31
     @objc let permissionDefaultFileRemoteShareNoSupportShareOption: Int     = 3
     @objc let permissionDefaultFolderRemoteShareNoSupportShareOption: Int   = 15
+    // ATTRIBUTES
+    @objc let permissionDownloadShare: Int          = 0
 
     // Filename Mask and Type
     //

+ 25 - 4
iOSClient/Share/Advanced/NCShareCells.swift

@@ -49,31 +49,46 @@ protocol NCPermission: NCToggleCellConfig {
     static var forDirectoryE2EE: [Self] { get }
     static var forFile: [Self] { get }
     func hasResharePermission(for parentPermission: Int) -> Bool
+    func hasDownload() -> Bool
 }
 
 enum NCUserPermission: CaseIterable, NCPermission {
     func hasResharePermission(for parentPermission: Int) -> Bool {
+        if self == .download { return true }
         return ((permissionBitFlag & parentPermission) != 0)
     }
 
+    func hasDownload() -> Bool {
+        return self == .download
+    }
+
     var permissionBitFlag: Int {
         switch self {
         case .reshare: return NCGlobal.shared.permissionShareShare
         case .edit: return NCGlobal.shared.permissionUpdateShare
         case .create: return NCGlobal.shared.permissionCreateShare
         case .delete: return NCGlobal.shared.permissionDeleteShare
+        case .download: return NCGlobal.shared.permissionDownloadShare
         }
     }
 
     func didChange(_ share: NCTableShareable, to newValue: Bool) {
-        share.permissions ^= permissionBitFlag
+        if self == .download {
+            share.attributes = NCManageDatabase.shared.setAttibuteDownload(state: newValue)
+        } else {
+            share.permissions ^= permissionBitFlag
+        }
     }
 
     func isOn(for share: NCTableShareable) -> Bool {
-        return (share.permissions & permissionBitFlag) != 0
+        if self == .download {
+            return NCManageDatabase.shared.isAttributeDownloadEnabled(attributes: share.attributes)
+        } else {
+            return (share.permissions & permissionBitFlag) != 0
+        }
     }
 
-    case reshare, edit, create, delete
+    case reshare, edit, create, delete, download
     static let forDirectory: [NCUserPermission] = NCUserPermission.allCases
     static let forDirectoryE2EE: [NCUserPermission] = []
     static let forFile: [NCUserPermission] = [.reshare, .edit]
@@ -84,11 +99,13 @@ enum NCUserPermission: CaseIterable, NCPermission {
         case .edit: return NSLocalizedString("_share_can_change_", comment: "")
         case .create: return NSLocalizedString("_share_can_create_", comment: "")
         case .delete: return NSLocalizedString("_share_can_delete_", comment: "")
+        case .download: return NSLocalizedString("_share_can_download_", comment: "")
         }
     }
 }
 
 enum NCLinkPermission: NCPermission {
+
     func didChange(_ share: NCTableShareable, to newValue: Bool) {
         guard self != .allowEdit || newValue else {
             share.permissions = NCGlobal.shared.permissionReadShare
@@ -101,6 +118,10 @@ enum NCLinkPermission: NCPermission {
         permissionValue & parentPermission == permissionValue
     }
 
+    func hasDownload() -> Bool {
+        return false
+    }
+
     var permissionValue: Int {
         switch self {
         case .allowEdit:
@@ -225,7 +246,7 @@ 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: resharePermission) {
+        if let cellConfig = cellConfig as? NCPermission, !cellConfig.hasResharePermission(for: resharePermission), !cellConfig.hasDownload() {
             cell?.isUserInteractionEnabled = false
             cell?.textLabel?.isEnabled = false
         }

+ 5 - 0
iOSClient/Share/NCShare+Helper.swift

@@ -40,6 +40,8 @@ protocol NCTableShareable: AnyObject {
     var note: String { get set }
     var expirationDate: NSDate? { get set }
     var shareWithDisplayname: String { get set }
+
+    var attributes: String? { get set }
 }
 
 extension NCTableShareable {
@@ -62,6 +64,7 @@ extension NCTableShareable {
 }
 
 class NCTableShareOptions: NCTableShareable {
+
     var shareType: Int
     var permissions: Int
 
@@ -75,6 +78,8 @@ class NCTableShareOptions: NCTableShareable {
     var expirationDate: NSDate?
     var shareWithDisplayname: String = ""
 
+    var attributes: String?
+
     private init(shareType: Int, metadata: tableMetadata, password: String?) {
         if metadata.e2eEncrypted {
             self.permissions = NCGlobal.shared.permissionCreateShare

+ 2 - 2
iOSClient/Share/NCShareNetworking.swift

@@ -82,7 +82,7 @@ class NCShareNetworking: NSObject {
         NCActivityIndicator.shared.start(backgroundView: view)
         let filenamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId, account: metadata.account)!
 
-        NextcloudKit.shared.createShare(path: filenamePath, shareType: option.shareType, shareWith: option.shareWith, password: option.password, permissions: option.permissions) { (account, share, data, error) in
+        NextcloudKit.shared.createShare(path: filenamePath, shareType: option.shareType, shareWith: option.shareWith, password: option.password, permissions: option.permissions, attributes: option.attributes) { (account, share, data, error) in
             NCActivityIndicator.shared.stop()
             if error == .success, let share = share {
                 option.idShare = share.idShare
@@ -113,7 +113,7 @@ class NCShareNetworking: NSObject {
 
     func updateShare(option: NCTableShareable) {
         NCActivityIndicator.shared.start(backgroundView: view)
-        NextcloudKit.shared.updateShare(idShare: option.idShare, password: option.password, expireDate: option.expDateString, permissions: option.permissions, note: option.note, label: option.label, hideDownload: option.hideDownload) { account, share, data, error in
+        NextcloudKit.shared.updateShare(idShare: option.idShare, password: option.password, expireDate: option.expDateString, permissions: option.permissions, note: option.note, label: option.label, hideDownload: option.hideDownload, attributes: option.attributes) { account, share, data, error in
             NCActivityIndicator.shared.stop()
             if error == .success, let share = share {
                 let home = NCUtilityFileSystem.shared.getHomeServer(urlBase: self.metadata.urlBase, userId: self.metadata.userId)

+ 1 - 0
iOSClient/Supporting Files/en.lproj/Localizable.strings

@@ -650,6 +650,7 @@
 "_share_can_create_"            = "Allow creating";
 "_share_can_change_"            = "Allow editing";
 "_share_can_delete_"            = "Allow deleting";
+"_share_can_download_"          = "Allow download";
 "_share_unshare_"               = "Unshare";
 "_share_internal_link_"         = "Internal link";
 "_share_internal_link_des_"     = "Only works for users with access to this folder";