Skip to content

Commit

Permalink
Merge pull request #2535 from nextcloud/sharingAttributes
Browse files Browse the repository at this point in the history
Sharing attributes - Allow download folder
  • Loading branch information
marinofaggiana authored Jul 17, 2023
2 parents 441cacd + 95b58a6 commit 6866d9e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Brand/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ import Foundation
// Database Realm
//
let databaseName = "nextcloud.realm"
let databaseSchemaVersion: UInt64 = 299
let databaseSchemaVersion: UInt64 = 300
4 changes: 2 additions & 2 deletions Nextcloud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" */ = {
Expand Down
30 changes: 30 additions & 0 deletions iOSClient/Data/NCManageDatabase+Share.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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 changes: 2 additions & 0 deletions iOSClient/NCGlobal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
29 changes: 25 additions & 4 deletions iOSClient/Share/Advanced/NCShareCells.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -101,6 +118,10 @@ enum NCLinkPermission: NCPermission {
permissionValue & parentPermission == permissionValue
}

func hasDownload() -> Bool {
return false
}

var permissionValue: Int {
switch self {
case .allowEdit:
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions iOSClient/Share/NCShare+Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -62,6 +64,7 @@ extension NCTableShareable {
}

class NCTableShareOptions: NCTableShareable {

var shareType: Int
var permissions: Int

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions iOSClient/Share/NCShareNetworking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions iOSClient/Supporting Files/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 6866d9e

Please sign in to comment.