NCManageDatabase+Account.swift 14 KB

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