NCManageDatabase+Account.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //
  2. // NCManageDatabase+Account.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 30.11.21.
  6. // Copyright © 2021 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 Foundation
  24. import RealmSwift
  25. import NextcloudKit
  26. extension NCManageDatabase {
  27. @objc func addAccount(_ account: String, urlBase: String, user: String, password: String) {
  28. let realm = try! Realm()
  29. do {
  30. try realm.write {
  31. let addObject = tableAccount()
  32. addObject.account = account
  33. // Brand
  34. if NCBrandOptions.shared.use_default_auto_upload {
  35. addObject.autoUpload = true
  36. addObject.autoUploadImage = true
  37. addObject.autoUploadVideo = true
  38. addObject.autoUploadWWAnVideo = true
  39. }
  40. CCUtility.setPassword(account, password: password)
  41. addObject.urlBase = urlBase
  42. addObject.user = user
  43. addObject.userId = user
  44. realm.add(addObject, update: .all)
  45. }
  46. } catch let error {
  47. NKCommon.shared.writeLog("Could not write to database: \(error)")
  48. }
  49. }
  50. @objc func updateAccount(_ account: tableAccount) {
  51. let realm = try! Realm()
  52. do {
  53. try realm.write {
  54. realm.add(account, update: .all)
  55. }
  56. } catch let error {
  57. NKCommon.shared.writeLog("Could not write to database: \(error)")
  58. }
  59. }
  60. @objc func deleteAccount(_ account: String) {
  61. let realm = try! Realm()
  62. do {
  63. try realm.write {
  64. let result = realm.objects(tableAccount.self).filter("account == %@", account)
  65. realm.delete(result)
  66. }
  67. } catch let error {
  68. NKCommon.shared.writeLog("Could not write to database: \(error)")
  69. }
  70. }
  71. @objc func getActiveAccount() -> tableAccount? {
  72. let realm = try! Realm()
  73. guard let result = realm.objects(tableAccount.self).filter("active == true").first else {
  74. return nil
  75. }
  76. return tableAccount.init(value: result)
  77. }
  78. @objc func getAccounts() -> [String]? {
  79. let realm = try! Realm()
  80. let results = realm.objects(tableAccount.self).sorted(byKeyPath: "account", ascending: true)
  81. if results.count > 0 {
  82. return Array(results.map { $0.account })
  83. }
  84. return nil
  85. }
  86. @objc func getAccount(predicate: NSPredicate) -> tableAccount? {
  87. let realm = try! Realm()
  88. guard let result = realm.objects(tableAccount.self).filter(predicate).first else {
  89. return nil
  90. }
  91. return tableAccount.init(value: result)
  92. }
  93. @objc func getAllAccount() -> [tableAccount] {
  94. let realm = try! Realm()
  95. let sorted = [SortDescriptor(keyPath: "active", ascending: false), SortDescriptor(keyPath: "user", ascending: true)]
  96. let results = realm.objects(tableAccount.self).sorted(by: sorted)
  97. return Array(results.map { tableAccount.init(value: $0) })
  98. }
  99. @objc func getAllAccountOrderAlias() -> [tableAccount] {
  100. let realm = try! Realm()
  101. let sorted = [SortDescriptor(keyPath: "active", ascending: false), SortDescriptor(keyPath: "alias", ascending: true), SortDescriptor(keyPath: "user", ascending: true)]
  102. let results = realm.objects(tableAccount.self).sorted(by: sorted)
  103. return Array(results.map { tableAccount.init(value: $0) })
  104. }
  105. @objc func getAccountAutoUploadFileName() -> String {
  106. let realm = try! Realm()
  107. guard let result = realm.objects(tableAccount.self).filter("active == true").first else {
  108. return ""
  109. }
  110. if result.autoUploadFileName.count > 0 {
  111. return result.autoUploadFileName
  112. } else {
  113. return NCBrandOptions.shared.folderDefaultAutoUpload
  114. }
  115. }
  116. @objc func getAccountAutoUploadDirectory(urlBase: String, userId: String, account: String) -> String {
  117. let realm = try! Realm()
  118. guard let result = realm.objects(tableAccount.self).filter("active == true").first else {
  119. return ""
  120. }
  121. if result.autoUploadDirectory.count > 0 {
  122. // FIX change webdav -> /dav/files/
  123. if result.autoUploadDirectory.contains("/webdav") {
  124. return NCUtilityFileSystem.shared.getHomeServer(urlBase: urlBase, userId: userId)
  125. } else {
  126. return result.autoUploadDirectory
  127. }
  128. } else {
  129. return NCUtilityFileSystem.shared.getHomeServer(urlBase: urlBase, userId: userId)
  130. }
  131. }
  132. @objc func getAccountAutoUploadPath(urlBase: String, userId: String, account: String) -> String {
  133. let cameraFileName = self.getAccountAutoUploadFileName()
  134. let cameraDirectory = self.getAccountAutoUploadDirectory(urlBase: urlBase, userId: userId, account: account)
  135. let folderPhotos = CCUtility.stringAppendServerUrl(cameraDirectory, addFileName: cameraFileName)!
  136. return folderPhotos
  137. }
  138. @discardableResult
  139. @objc func setAccountActive(_ account: String) -> tableAccount? {
  140. let realm = try! Realm()
  141. var accountReturn = tableAccount()
  142. do {
  143. try realm.write {
  144. let results = realm.objects(tableAccount.self)
  145. for result in results {
  146. if result.account == account {
  147. result.active = true
  148. accountReturn = result
  149. } else {
  150. result.active = false
  151. }
  152. }
  153. }
  154. } catch let error {
  155. NKCommon.shared.writeLog("Could not write to database: \(error)")
  156. return nil
  157. }
  158. return tableAccount.init(value: accountReturn)
  159. }
  160. @objc func removePasswordAccount(_ account: String) {
  161. let realm = try! Realm()
  162. do {
  163. try realm.write {
  164. if let result = realm.objects(tableAccount.self).filter("account == %@", account).first {
  165. result.password = "********"
  166. }
  167. }
  168. } catch let error {
  169. NKCommon.shared.writeLog("Could not write to database: \(error)")
  170. }
  171. }
  172. @objc func setAccountAutoUploadProperty(_ property: String, state: Bool) {
  173. let realm = try! Realm()
  174. do {
  175. try realm.write {
  176. if let result = realm.objects(tableAccount.self).filter("active == true").first {
  177. if (tableAccount().objectSchema.properties.contains { $0.name == property }) {
  178. result[property] = state
  179. }
  180. }
  181. }
  182. } catch let error {
  183. NKCommon.shared.writeLog("Could not write to database: \(error)")
  184. }
  185. }
  186. @objc func setAccountAutoUploadFileName(_ fileName: String?) {
  187. let realm = try! Realm()
  188. do {
  189. try realm.write {
  190. if let result = realm.objects(tableAccount.self).filter("active == true").first {
  191. if let fileName = fileName {
  192. result.autoUploadFileName = fileName
  193. } else {
  194. result.autoUploadFileName = self.getAccountAutoUploadFileName()
  195. }
  196. }
  197. }
  198. } catch let error {
  199. NKCommon.shared.writeLog("Could not write to database: \(error)")
  200. }
  201. }
  202. @objc func setAccountAutoUploadDirectory(_ serverUrl: String?, urlBase: String, userId: String, account: String) {
  203. let realm = try! Realm()
  204. do {
  205. try realm.write {
  206. if let result = realm.objects(tableAccount.self).filter("active == true").first {
  207. if let serverUrl = serverUrl {
  208. result.autoUploadDirectory = serverUrl
  209. } else {
  210. result.autoUploadDirectory = self.getAccountAutoUploadDirectory(urlBase: urlBase, userId: userId, account: account)
  211. }
  212. }
  213. }
  214. } catch let error {
  215. NKCommon.shared.writeLog("Could not write to database: \(error)")
  216. }
  217. }
  218. @objc func setAccountUserProfile(account: String, userProfile: NKUserProfile) -> tableAccount? {
  219. let realm = try! Realm()
  220. do {
  221. try realm.write {
  222. if let result = realm.objects(tableAccount.self).filter("account == %@", account).first {
  223. result.address = userProfile.address
  224. result.backend = userProfile.backend
  225. result.backendCapabilitiesSetDisplayName = userProfile.backendCapabilitiesSetDisplayName
  226. result.backendCapabilitiesSetPassword = userProfile.backendCapabilitiesSetPassword
  227. result.displayName = userProfile.displayName
  228. result.email = userProfile.email
  229. result.enabled = userProfile.enabled
  230. result.groups = userProfile.groups.joined(separator: ",")
  231. result.language = userProfile.language
  232. result.lastLogin = userProfile.lastLogin
  233. result.locale = userProfile.locale
  234. result.organisation = userProfile.organisation
  235. result.phone = userProfile.phone
  236. result.quota = userProfile.quota
  237. result.quotaFree = userProfile.quotaFree
  238. result.quotaRelative = userProfile.quotaRelative
  239. result.quotaTotal = userProfile.quotaTotal
  240. result.quotaUsed = userProfile.quotaUsed
  241. result.storageLocation = userProfile.storageLocation
  242. result.subadmin = userProfile.subadmin.joined(separator: ",")
  243. result.twitter = userProfile.twitter
  244. result.userId = userProfile.userId
  245. result.website = userProfile.website
  246. }
  247. }
  248. } catch let error {
  249. NKCommon.shared.writeLog("Could not write to database: \(error)")
  250. }
  251. if let result = realm.objects(tableAccount.self).filter("account == %@", account).first {
  252. return tableAccount.init(value: result)
  253. } else {
  254. return nil
  255. }
  256. }
  257. @objc func setAccountMediaPath(_ path: String, account: String) {
  258. let realm = try! Realm()
  259. do {
  260. try realm.write {
  261. if let result = realm.objects(tableAccount.self).filter("account == %@", account).first {
  262. result.mediaPath = path
  263. }
  264. }
  265. } catch let error {
  266. NKCommon.shared.writeLog("Could not write to database: \(error)")
  267. }
  268. }
  269. @objc func setAccountUserStatus(userStatusClearAt: NSDate?, userStatusIcon: String?, userStatusMessage: String?, userStatusMessageId: String?, userStatusMessageIsPredefined: Bool, userStatusStatus: String?, userStatusStatusIsUserDefined: Bool, account: String) {
  270. let realm = try! Realm()
  271. do {
  272. try realm.write {
  273. if let result = realm.objects(tableAccount.self).filter("account == %@", account).first {
  274. result.userStatusClearAt = userStatusClearAt
  275. result.userStatusIcon = userStatusIcon
  276. result.userStatusMessage = userStatusMessage
  277. result.userStatusMessageId = userStatusMessageId
  278. result.userStatusMessageIsPredefined = userStatusMessageIsPredefined
  279. result.userStatusStatus = userStatusStatus
  280. result.userStatusStatusIsUserDefined = userStatusStatusIsUserDefined
  281. }
  282. }
  283. } catch let error {
  284. NKCommon.shared.writeLog("Could not write to database: \(error)")
  285. }
  286. }
  287. @objc func setAccountAlias(_ alias: String?) {
  288. let realm = try! Realm()
  289. let alias = alias?.trimmingCharacters(in: .whitespacesAndNewlines)
  290. do {
  291. try realm.write {
  292. if let result = realm.objects(tableAccount.self).filter("active == true").first {
  293. if let alias = alias {
  294. result.alias = alias
  295. } else {
  296. result.alias = ""
  297. }
  298. }
  299. }
  300. } catch let error {
  301. NKCommon.shared.writeLog("Could not write to database: \(error)")
  302. }
  303. }
  304. }