SafeSetupWork.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. @objc public class SafeSetupWork: NSObject {
  22. @objc static let backupEnable: Int = 2<<0
  23. @objc static let backupDisable: Int = 2<<1
  24. @objc static let backupForce: Int = 2<<2
  25. @objc static let restoreEnable: Int = 2<<3
  26. @objc static let restoreDisable: Int = 2<<4
  27. @objc static let restoreForce: Int = 2<<5
  28. @objc static let serverPreset: Int = 2<<6
  29. @objc static let passwordPreset: Int = 2<<7
  30. var safeBackupStatus: Int = 0
  31. var safeRestoreStatus: Int = 0
  32. private let mdmSetup: MDMSetup
  33. @objc init(mdmSetup: MDMSetup) {
  34. self.mdmSetup = mdmSetup
  35. super.init()
  36. self.initSafeStatus()
  37. }
  38. @objc public func isSafeBackupStatusSet(safeState: Int) -> Bool {
  39. return self.safeBackupStatus & safeState == safeState
  40. }
  41. @objc public func isSafeRestoreStatusSet(safeState: Int) -> Bool {
  42. return self.safeRestoreStatus & safeState != 0
  43. }
  44. private func initSafeStatus() {
  45. if self.mdmSetup.disableBackups() {
  46. self.safeBackupStatus |= SafeSetupWork.backupDisable
  47. } else {
  48. if self.mdmSetup.safeEnable() == nil {
  49. self.safeBackupStatus |= SafeSetupWork.backupEnable
  50. } else if !Bool(exactly: self.mdmSetup.safeEnable())! {
  51. self.safeBackupStatus |= SafeSetupWork.backupDisable
  52. } else {
  53. self.safeBackupStatus |= SafeSetupWork.backupForce
  54. if self.mdmSetup.safePassword() != nil {
  55. self.safeBackupStatus |= SafeSetupWork.passwordPreset
  56. }
  57. }
  58. if self.mdmSetup.safeServerUrl() != nil {
  59. self.safeBackupStatus |= SafeSetupWork.serverPreset
  60. }
  61. }
  62. if self.mdmSetup.disableBackups() || !self.mdmSetup.safeRestoreEnable() {
  63. self.safeRestoreStatus |= SafeSetupWork.restoreDisable
  64. } else {
  65. if self.mdmSetup.safeRestoreId() == nil || self.mdmSetup.safeRestoreId() == "" {
  66. self.safeRestoreStatus |= SafeSetupWork.restoreEnable
  67. } else {
  68. self.safeRestoreStatus |= SafeSetupWork.restoreForce
  69. if self.mdmSetup.safePassword() != nil {
  70. self.safeRestoreStatus |= SafeSetupWork.passwordPreset
  71. }
  72. }
  73. if self.mdmSetup.safeServerUrl() != nil {
  74. self.safeRestoreStatus |= SafeSetupWork.serverPreset
  75. }
  76. }
  77. }
  78. }