NCManageDatabase+Account.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. //
  2. // NCManageDatabase+Account.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/11/23.
  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 UIKit
  25. import RealmSwift
  26. import NextcloudKit
  27. class tableAccount: Object {
  28. @objc dynamic var account = ""
  29. @objc dynamic var active: Bool = false
  30. @objc dynamic var address = ""
  31. @objc dynamic var alias = ""
  32. @objc dynamic var autoUpload: Bool = false
  33. @objc dynamic var autoUploadCreateSubfolder: Bool = false
  34. @objc dynamic var autoUploadSubfolderGranularity: Int = NCGlobal.shared.subfolderGranularityMonthly
  35. @objc dynamic var autoUploadDirectory = ""
  36. @objc dynamic var autoUploadFileName = ""
  37. @objc dynamic var autoUploadFull: Bool = false
  38. @objc dynamic var autoUploadImage: Bool = false
  39. @objc dynamic var autoUploadVideo: Bool = false
  40. @objc dynamic var autoUploadFavoritesOnly: Bool = false
  41. @objc dynamic var autoUploadWWAnPhoto: Bool = false
  42. @objc dynamic var autoUploadWWAnVideo: Bool = false
  43. @objc dynamic var backend = ""
  44. @objc dynamic var backendCapabilitiesSetDisplayName: Bool = false
  45. @objc dynamic var backendCapabilitiesSetPassword: Bool = false
  46. @objc dynamic var displayName = ""
  47. @objc dynamic var email = ""
  48. @objc dynamic var enabled: Bool = false
  49. @objc dynamic var groups = ""
  50. @objc dynamic var language = ""
  51. @objc dynamic var lastLogin: Int64 = 0
  52. @objc dynamic var locale = ""
  53. @objc dynamic var mediaPath = ""
  54. @objc dynamic var organisation = ""
  55. @objc dynamic var password = ""
  56. @objc dynamic var phone = ""
  57. @objc dynamic var quota: Int64 = 0
  58. @objc dynamic var quotaFree: Int64 = 0
  59. @objc dynamic var quotaRelative: Double = 0
  60. @objc dynamic var quotaTotal: Int64 = 0
  61. @objc dynamic var quotaUsed: Int64 = 0
  62. @objc dynamic var storageLocation = ""
  63. @objc dynamic var subadmin = ""
  64. @objc dynamic var twitter = ""
  65. @objc dynamic var urlBase = ""
  66. @objc dynamic var user = ""
  67. @objc dynamic var userId = ""
  68. @objc dynamic var userStatusClearAt: NSDate?
  69. @objc dynamic var userStatusIcon: String?
  70. @objc dynamic var userStatusMessage: String?
  71. @objc dynamic var userStatusMessageId: String?
  72. @objc dynamic var userStatusMessageIsPredefined: Bool = false
  73. @objc dynamic var userStatusStatus: String?
  74. @objc dynamic var userStatusStatusIsUserDefined: Bool = false
  75. @objc dynamic var website = ""
  76. override static func primaryKey() -> String {
  77. return "account"
  78. }
  79. }
  80. extension NCManageDatabase {
  81. func addAccount(_ account: String, urlBase: String, user: String, userId: String, password: String) {
  82. do {
  83. let realm = try Realm()
  84. try realm.write {
  85. if let result = realm.objects(tableAccount.self).filter("account == %@", account).first {
  86. realm.delete(result)
  87. }
  88. let tableAccount = tableAccount()
  89. tableAccount.account = account
  90. NCKeychain().setPassword(account: account, password: password)
  91. tableAccount.urlBase = urlBase
  92. tableAccount.user = user
  93. tableAccount.userId = userId
  94. realm.add(tableAccount, update: .all)
  95. }
  96. } catch let error {
  97. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  98. }
  99. }
  100. func updateAccount(_ account: tableAccount) {
  101. do {
  102. let realm = try Realm()
  103. try realm.write {
  104. realm.add(account, update: .all)
  105. }
  106. } catch let error {
  107. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  108. }
  109. }
  110. func getActiveTableAccount() -> tableAccount? {
  111. do {
  112. let realm = try Realm()
  113. guard let result = realm.objects(tableAccount.self).filter("active == true").first else { return nil }
  114. return tableAccount.init(value: result)
  115. } catch let error as NSError {
  116. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  117. }
  118. return nil
  119. }
  120. func getTableAccount(account: String) -> tableAccount? {
  121. do {
  122. let realm = try Realm()
  123. guard let result = realm.objects(tableAccount.self).filter("account == %@", account).first else { return nil }
  124. return tableAccount.init(value: result)
  125. } catch let error as NSError {
  126. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  127. }
  128. return nil
  129. }
  130. func getAccounts() -> [String]? {
  131. do {
  132. let realm = try Realm()
  133. let results = realm.objects(tableAccount.self).sorted(byKeyPath: "account", ascending: true)
  134. if !results.isEmpty {
  135. return Array(results.map { $0.account })
  136. }
  137. } catch let error as NSError {
  138. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  139. }
  140. return nil
  141. }
  142. func getTableAccount(predicate: NSPredicate) -> tableAccount? {
  143. do {
  144. let realm = try Realm()
  145. guard let result = realm.objects(tableAccount.self).filter(predicate).first else { return nil }
  146. return tableAccount.init(value: result)
  147. } catch let error as NSError {
  148. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  149. }
  150. return nil
  151. }
  152. func getAllTableAccount() -> [tableAccount] {
  153. do {
  154. let realm = try Realm()
  155. let sorted = [SortDescriptor(keyPath: "active", ascending: false), SortDescriptor(keyPath: "user", ascending: true)]
  156. let results = realm.objects(tableAccount.self).sorted(by: sorted)
  157. return Array(results.map { tableAccount.init(value: $0) })
  158. } catch let error as NSError {
  159. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  160. }
  161. return []
  162. }
  163. func getAllAccountOrderAlias() -> [tableAccount] {
  164. do {
  165. let realm = try Realm()
  166. let sorted = [SortDescriptor(keyPath: "active", ascending: false), SortDescriptor(keyPath: "alias", ascending: true), SortDescriptor(keyPath: "user", ascending: true)]
  167. let results = realm.objects(tableAccount.self).sorted(by: sorted)
  168. return Array(results.map { tableAccount.init(value: $0) })
  169. } catch let error as NSError {
  170. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  171. }
  172. return []
  173. }
  174. func getAccountAutoUploadFileName() -> String {
  175. do {
  176. let realm = try Realm()
  177. guard let result = realm.objects(tableAccount.self).filter("active == true").first else { return "" }
  178. if result.autoUploadFileName.isEmpty {
  179. return NCBrandOptions.shared.folderDefaultAutoUpload
  180. } else {
  181. return result.autoUploadFileName
  182. }
  183. } catch let error as NSError {
  184. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  185. }
  186. return ""
  187. }
  188. func getAccountAutoUploadDirectory(session: NCSession.Session) -> String {
  189. do {
  190. let realm = try Realm()
  191. guard let result = realm.objects(tableAccount.self).filter("active == true").first else { return "" }
  192. if result.autoUploadDirectory.isEmpty {
  193. return utilityFileSystem.getHomeServer(session: session)
  194. } else {
  195. // FIX change webdav -> /dav/files/
  196. if result.autoUploadDirectory.contains("/webdav") {
  197. return utilityFileSystem.getHomeServer(session: session)
  198. } else {
  199. return result.autoUploadDirectory
  200. }
  201. }
  202. } catch let error as NSError {
  203. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  204. }
  205. return ""
  206. }
  207. func getAccountAutoUploadPath(session: NCSession.Session) -> String {
  208. let cameraFileName = self.getAccountAutoUploadFileName()
  209. let cameraDirectory = self.getAccountAutoUploadDirectory(session: session)
  210. let folderPhotos = utilityFileSystem.stringAppendServerUrl(cameraDirectory, addFileName: cameraFileName)
  211. return folderPhotos
  212. }
  213. func getAccountAutoUploadSubfolderGranularity() -> Int {
  214. do {
  215. let realm = try Realm()
  216. guard let result = realm.objects(tableAccount.self).filter("active == true").first else { return NCGlobal.shared.subfolderGranularityMonthly }
  217. return result.autoUploadSubfolderGranularity
  218. } catch let error as NSError {
  219. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  220. }
  221. return NCGlobal.shared.subfolderGranularityMonthly
  222. }
  223. func setAccountActive(_ account: String) {
  224. do {
  225. let realm = try Realm()
  226. try realm.write {
  227. let results = realm.objects(tableAccount.self)
  228. for result in results {
  229. if result.account == account {
  230. result.active = true
  231. } else {
  232. result.active = false
  233. }
  234. }
  235. }
  236. } catch let error {
  237. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  238. }
  239. }
  240. func setAccountAutoUploadProperty(_ property: String, state: Bool) {
  241. do {
  242. let realm = try Realm()
  243. try realm.write {
  244. if let result = realm.objects(tableAccount.self).filter("active == true").first {
  245. if (tableAccount().objectSchema.properties.contains { $0.name == property }) {
  246. result[property] = state
  247. }
  248. }
  249. }
  250. } catch let error {
  251. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  252. }
  253. }
  254. func setAccountAutoUploadGranularity(_ property: String, state: Int) {
  255. do {
  256. let realm = try Realm()
  257. try realm.write {
  258. if let result = realm.objects(tableAccount.self).filter("active == true").first {
  259. result.autoUploadSubfolderGranularity = state
  260. }
  261. }
  262. } catch let error {
  263. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  264. }
  265. }
  266. func setAccountAutoUploadFileName(_ fileName: String) {
  267. do {
  268. let realm = try Realm()
  269. try realm.write {
  270. if let result = realm.objects(tableAccount.self).filter("active == true").first {
  271. result.autoUploadFileName = fileName
  272. }
  273. }
  274. } catch let error {
  275. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  276. }
  277. }
  278. func setAccountAutoUploadDirectory(_ serverUrl: String?, session: NCSession.Session) {
  279. do {
  280. let realm = try Realm()
  281. try realm.write {
  282. if let result = realm.objects(tableAccount.self).filter("active == true").first {
  283. if let serverUrl = serverUrl {
  284. result.autoUploadDirectory = serverUrl
  285. } else {
  286. result.autoUploadDirectory = self.getAccountAutoUploadDirectory(session: session)
  287. }
  288. }
  289. }
  290. } catch let error {
  291. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  292. }
  293. }
  294. func setAccountUserProfile(account: String, userProfile: NKUserProfile) {
  295. do {
  296. let realm = try Realm()
  297. try realm.write {
  298. if let result = realm.objects(tableAccount.self).filter("account == %@", account).first {
  299. result.address = userProfile.address
  300. result.backend = userProfile.backend
  301. result.backendCapabilitiesSetDisplayName = userProfile.backendCapabilitiesSetDisplayName
  302. result.backendCapabilitiesSetPassword = userProfile.backendCapabilitiesSetPassword
  303. result.displayName = userProfile.displayName
  304. result.email = userProfile.email
  305. result.enabled = userProfile.enabled
  306. result.groups = userProfile.groups.joined(separator: ",")
  307. result.language = userProfile.language
  308. result.lastLogin = userProfile.lastLogin
  309. result.locale = userProfile.locale
  310. result.organisation = userProfile.organisation
  311. result.phone = userProfile.phone
  312. result.quota = userProfile.quota
  313. result.quotaFree = userProfile.quotaFree
  314. result.quotaRelative = userProfile.quotaRelative
  315. result.quotaTotal = userProfile.quotaTotal
  316. result.quotaUsed = userProfile.quotaUsed
  317. result.storageLocation = userProfile.storageLocation
  318. result.subadmin = userProfile.subadmin.joined(separator: ",")
  319. result.twitter = userProfile.twitter
  320. result.userId = userProfile.userId
  321. result.website = userProfile.website
  322. }
  323. }
  324. } catch let error {
  325. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  326. }
  327. }
  328. func setAccountMediaPath(_ path: String, account: String) {
  329. do {
  330. let realm = try Realm()
  331. try realm.write {
  332. if let result = realm.objects(tableAccount.self).filter("account == %@", account).first {
  333. result.mediaPath = path
  334. }
  335. }
  336. } catch let error {
  337. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  338. }
  339. }
  340. func setAccountUserStatus(userStatusClearAt: Date?, userStatusIcon: String?, userStatusMessage: String?, userStatusMessageId: String?, userStatusMessageIsPredefined: Bool, userStatusStatus: String?, userStatusStatusIsUserDefined: Bool, account: String) {
  341. do {
  342. let realm = try Realm()
  343. try realm.write {
  344. if let result = realm.objects(tableAccount.self).filter("account == %@", account).first {
  345. result.userStatusClearAt = userStatusClearAt as? NSDate
  346. result.userStatusIcon = userStatusIcon
  347. result.userStatusMessage = userStatusMessage
  348. result.userStatusMessageId = userStatusMessageId
  349. result.userStatusMessageIsPredefined = userStatusMessageIsPredefined
  350. result.userStatusStatus = userStatusStatus
  351. result.userStatusStatusIsUserDefined = userStatusStatusIsUserDefined
  352. }
  353. }
  354. } catch let error {
  355. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  356. }
  357. }
  358. func setAccountAlias(_ account: String, alias: String) {
  359. let alias = alias.trimmingCharacters(in: .whitespacesAndNewlines)
  360. do {
  361. let realm = try Realm()
  362. try realm.write {
  363. if let result = realm.objects(tableAccount.self).filter("account == %@", account).first {
  364. result.alias = alias
  365. }
  366. }
  367. } catch let error {
  368. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  369. }
  370. }
  371. func getAccountGroups(account: String) -> [String] {
  372. do {
  373. let realm = try Realm()
  374. if let result = realm.objects(tableAccount.self).filter("account == %@", account).first {
  375. return result.groups.components(separatedBy: ",")
  376. }
  377. } catch let error as NSError {
  378. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  379. }
  380. return []
  381. }
  382. }