NCManageDatabase+Account.swift 17 KB

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