NCPermissions.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // NCPermissions.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 05/06/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import Foundation
  25. class NCPermissions: NSObject {
  26. let permissionShared = "S"
  27. let permissionCanShare = "R"
  28. let permissionMounted = "M"
  29. let permissionFileCanWrite = "W"
  30. let permissionCanCreateFile = "C"
  31. let permissionCanCreateFolder = "K"
  32. let permissionCanDelete = "D"
  33. let permissionCanRename = "N"
  34. let permissionCanMove = "V"
  35. // Share permission
  36. // permissions - (int) 1 = read; 2 = update; 4 = create; 8 = delete; 16 = share; 31 = all
  37. //
  38. let permissionReadShare: Int = 1
  39. let permissionUpdateShare: Int = 2
  40. let permissionCreateShare: Int = 4
  41. let permissionDeleteShare: Int = 8
  42. let permissionShareShare: Int = 16
  43. //
  44. let permissionMinFileShare: Int = 1
  45. let permissionMaxFileShare: Int = 19
  46. let permissionMinFolderShare: Int = 1
  47. let permissionMaxFolderShare: Int = 31
  48. let permissionDefaultFileRemoteShareNoSupportShareOption: Int = 3
  49. let permissionDefaultFolderRemoteShareNoSupportShareOption: Int = 15
  50. // ATTRIBUTES
  51. let permissionDownloadShare: Int = 0
  52. func isPermissionToRead(_ permission: Int) -> Bool {
  53. return ((permission & permissionReadShare) > 0)
  54. }
  55. func isPermissionToCanDelete(_ permission: Int) -> Bool {
  56. return ((permission & permissionDeleteShare) > 0)
  57. }
  58. func isPermissionToCanCreate(_ permission: Int) -> Bool {
  59. return ((permission & permissionCreateShare) > 0)
  60. }
  61. func isPermissionToCanChange(_ permission: Int) -> Bool {
  62. return ((permission & permissionUpdateShare) > 0)
  63. }
  64. func isPermissionToCanShare(_ permission: Int) -> Bool {
  65. return ((permission & permissionShareShare) > 0)
  66. }
  67. func isAnyPermissionToEdit(_ permission: Int) -> Bool {
  68. let canCreate = isPermissionToCanCreate(permission)
  69. let canChange = isPermissionToCanChange(permission)
  70. let canDelete = isPermissionToCanDelete(permission)
  71. return canCreate || canChange || canDelete
  72. }
  73. func isPermissionToReadCreateUpdate(_ permission: Int) -> Bool {
  74. let canRead = isPermissionToRead(permission)
  75. let canCreate = isPermissionToCanCreate(permission)
  76. let canChange = isPermissionToCanChange(permission)
  77. return canCreate && canChange && canRead
  78. }
  79. func getPermission(canEdit: Bool, canCreate: Bool, canChange: Bool, canDelete: Bool, canShare: Bool, isDirectory: Bool) -> Int {
  80. var permission = permissionReadShare
  81. if canEdit && !isDirectory {
  82. permission = permission + permissionUpdateShare
  83. }
  84. if canCreate && isDirectory {
  85. permission = permission + permissionCreateShare
  86. }
  87. if canChange && isDirectory {
  88. permission = permission + permissionUpdateShare
  89. }
  90. if canDelete && isDirectory {
  91. permission = permission + permissionDeleteShare
  92. }
  93. if canShare {
  94. permission = permission + permissionShareShare
  95. }
  96. return permission
  97. }
  98. }