diff --git a/migrator/AppContext.swift b/migrator/AppContext.swift index f733a38..299160c 100644 --- a/migrator/AppContext.swift +++ b/migrator/AppContext.swift @@ -25,17 +25,39 @@ struct AppContext { // MARK: - Device Management Related Constants + /// UserDefaults key indicating the list of managed environments. + private static let mdmEnvironmentsUserDefaultsKey: String = "mdmEnvironments" + /// A list of managed environments to be tracked on the user's device. /// Each `ManagedEnvironment` includes: /// - `name`: The environment's name (e.g., Production, QA). /// - `serverURL`: The server URL used to identify the environment through the installed management profile. + /// The `serverURL` can optionally include the `/mdm/ServerURL` suffix, and will be added if missing. /// - `reconPolicyID`: The policy ID required to run an inventory update via Jamf Self Service. /// This workflow is specifically designed for devices managed by Jamf Pro. - static let mdmEnvironments: [ManagedEnvironment] = [ + static let fallbackMdmEnvironments: [ManagedEnvironment] = [ ManagedEnvironment(name: "TestExample", serverURL: "https://test.url/mdm/ServerURL", reconPolicyID: "022"), ManagedEnvironment(name: "ProdExample", serverURL: "https://prod.url/mdm/ServerURL", reconPolicyID: "033") ] + /// Read the list of managed environments from UserDefaults with the + static var mdmEnvironments: [ManagedEnvironment] { + let managedEnvironments = UserDefaults.standard.array(forKey: mdmEnvironmentsUserDefaultsKey) as? [[String: String]] ?? [] + + /// Use managedEnvironments if not empty, otherwise fallback to fallbackMdmEnvironments + return managedEnvironments.isEmpty ? fallbackMdmEnvironments : managedEnvironments.compactMap { dict in + guard + let name = dict["name"], + let serverURL = dict["serverURL"], + let reconPolicyID = dict["reconPolicyID"] + else { + return nil + } + + return ManagedEnvironment(name: name, serverURL: serverURL, reconPolicyID: reconPolicyID) + } + } + /// Path the Jamf Self Service .app static let fallbackStorePath: String = "/Applications/Company Self Service.app" diff --git a/migrator/Model/DeviceManagement/ManagedEnvironment.swift b/migrator/Model/DeviceManagement/ManagedEnvironment.swift index d7943b7..07d0111 100644 --- a/migrator/Model/DeviceManagement/ManagedEnvironment.swift +++ b/migrator/Model/DeviceManagement/ManagedEnvironment.swift @@ -17,4 +17,23 @@ struct ManagedEnvironment { var name: String var serverURL: String var reconPolicyID: String + + // MARK: - Initializers + + init(name: String, serverURL: String, reconPolicyID: String) { + self.name = name + self.reconPolicyID = reconPolicyID + + /// Allow for serverURL to be input without the "/mdm/ServerURL" suffix. + if !serverURL.hasSuffix("/mdm/ServerURL") { + if serverURL.hasSuffix("/") { + self.serverURL = serverURL + "mdm/ServerURL" + } else { + self.serverURL = serverURL + "/mdm/ServerURL" + } + } else { + /// The serverURL is already formatted correctly. + self.serverURL = serverURL + } + } }