NCManageDatabase+SecurityGuard.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // NCManageDatabase+SecurityGuard.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 17/01/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 Foundation
  24. import UIKit
  25. import RealmSwift
  26. import NextcloudKit
  27. class TableSecurityGuardDiagnostics: Object {
  28. @Persisted var account = ""
  29. @Persisted(primaryKey: true) var primaryKey = ""
  30. @Persisted var issue: String = ""
  31. @Persisted var error: String?
  32. @Persisted var counter: Int = 0
  33. @Persisted var oldest: TimeInterval
  34. @Persisted var id: ObjectId
  35. convenience init(account: String, issue: String, error: String?, date: Date) {
  36. self.init()
  37. self.account = account
  38. self.primaryKey = account + issue + (error ?? "")
  39. self.issue = issue
  40. self.error = error
  41. self.counter = 1
  42. self.oldest = date.timeIntervalSince1970
  43. }
  44. }
  45. extension NCManageDatabase {
  46. func addDiagnostic(account: String, issue: String, error: String? = nil) {
  47. do {
  48. let realm = try Realm()
  49. try realm.write {
  50. let primaryKey = account + issue + (error ?? "")
  51. if let result = realm.object(ofType: TableSecurityGuardDiagnostics.self, forPrimaryKey: primaryKey) {
  52. result.counter += 1
  53. result.oldest = Date().timeIntervalSince1970
  54. } else {
  55. let table = TableSecurityGuardDiagnostics(account: account, issue: issue, error: error, date: Date())
  56. realm.add(table)
  57. }
  58. }
  59. } catch let error {
  60. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  61. }
  62. }
  63. func existsDiagnostics(account: String) -> Bool {
  64. do {
  65. let realm = try Realm()
  66. let results = realm.objects(TableSecurityGuardDiagnostics.self).where({
  67. $0.account == account
  68. })
  69. if !results.isEmpty { return true }
  70. } catch let error as NSError {
  71. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  72. }
  73. return false
  74. }
  75. func getDiagnostics(account: String, issue: String) -> Results<TableSecurityGuardDiagnostics>? {
  76. do {
  77. let realm = try Realm()
  78. let results = realm.objects(TableSecurityGuardDiagnostics.self).where({
  79. $0.account == account && $0.issue == issue
  80. })
  81. return results
  82. } catch let error as NSError {
  83. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  84. }
  85. return nil
  86. }
  87. func deleteDiagnostics(account: String, ids: [ObjectId]) {
  88. do {
  89. let realm = try Realm()
  90. try realm.write {
  91. let results = realm.objects(TableSecurityGuardDiagnostics.self).where({
  92. $0.account == account
  93. })
  94. for result in results where ids.contains(result.id) {
  95. realm.delete(result)
  96. }
  97. }
  98. } catch let error {
  99. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  100. }
  101. }
  102. }