Restore password policy editing in Web Console
This commit is contained in:
1 parent
bf3b40d9a5
commit
4801cfbe77
6 files changed
+174
-25
No files matched your search
@@ -55,12 +55,69 @@ final class ServerStore {
|
||||
PRAGMA user_version = 1;
|
||||
""")
|
||||
}
|
||||
try execute("""
|
||||
CREATE TABLE IF NOT EXISTS password_policy (
|
||||
key TEXT PRIMARY KEY NOT NULL,
|
||||
value TEXT NOT NULL
|
||||
);
|
||||
""")
|
||||
try recoverPendingDeletes()
|
||||
}
|
||||
}
|
||||
|
||||
deinit { sqlite3_close(db) }
|
||||
|
||||
private static let passwordPolicyDefaults = [
|
||||
"minimumLength": "10",
|
||||
"isUpperAlphabetsEnabled": "true",
|
||||
"isLowerAlphabetsEnabled": "true",
|
||||
"isNumericDigitsEnabled": "true",
|
||||
"isSpecialCharactersEnabled": "true",
|
||||
"isExpirationEnabled": "false",
|
||||
"expirationDaysAfter": "180",
|
||||
"expirationReminderDaysBefore": "10",
|
||||
]
|
||||
|
||||
private static let characterClasses = [
|
||||
"isUpperAlphabetsEnabled", "isLowerAlphabetsEnabled",
|
||||
"isNumericDigitsEnabled", "isSpecialCharactersEnabled",
|
||||
]
|
||||
|
||||
func passwordPolicy() throws -> [String: String] {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
let rows = try query("SELECT key, value FROM password_policy") { row in
|
||||
(self.text(row, 0), self.text(row, 1))
|
||||
}
|
||||
let saved = Dictionary(uniqueKeysWithValues: rows)
|
||||
return Self.passwordPolicyDefaults.merging(saved) { _, value in value }
|
||||
}
|
||||
|
||||
func updatePasswordPolicy(_ changes: [String: String]) throws {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
guard !changes.isEmpty, changes.keys.allSatisfy({ Self.passwordPolicyDefaults[$0] != nil }) else {
|
||||
throw StoreError.invalid
|
||||
}
|
||||
try transaction {
|
||||
let policy = try passwordPolicy().merging(changes) { _, value in value }
|
||||
let booleanKeys = Self.characterClasses + ["isExpirationEnabled"]
|
||||
guard let length = policy["minimumLength"].flatMap(Int.init), (8...20).contains(length),
|
||||
let days = policy["expirationDaysAfter"].flatMap(Int.init), (30...180).contains(days), days % 30 == 0,
|
||||
let reminder = policy["expirationReminderDaysBefore"].flatMap(Int.init), (1...10).contains(reminder),
|
||||
booleanKeys.allSatisfy({ policy[$0] == "true" || policy[$0] == "false" }),
|
||||
Self.characterClasses.filter({ policy[$0] == "true" }).count >= 2 else {
|
||||
throw StoreError.invalid
|
||||
}
|
||||
for (key, value) in changes {
|
||||
try run("""
|
||||
INSERT INTO password_policy(key, value) VALUES (?, ?)
|
||||
ON CONFLICT(key) DO UPDATE SET value = excluded.value
|
||||
""", [.text(key), .text(value)])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func copyImages(to parent: URL) throws {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
|
||||
@@ -87,20 +87,35 @@ final class WebConsoleAPI {
|
||||
guard path == ["settings", "settingvalues", "section", "passwordpolicy"] else {
|
||||
return Reply(["data": []], errorCode: -100037)
|
||||
}
|
||||
// Display the defaults from CreateSETTINGDBTable.sql.
|
||||
let policy = [
|
||||
"minimumLength": "10",
|
||||
"isUpperAlphabetsEnabled": "true",
|
||||
"isLowerAlphabetsEnabled": "true",
|
||||
"isNumericDigitsEnabled": "true",
|
||||
"isSpecialCharactersEnabled": "true",
|
||||
"isExpirationEnabled": "false",
|
||||
"expirationDaysAfter": "180",
|
||||
"expirationReminderDaysBefore": "10",
|
||||
]
|
||||
return Reply(["data": policy.sorted { $0.key < $1.key }.map {
|
||||
["section": "PasswordPolicy", "key": $0.key, "value": $0.value]
|
||||
}])
|
||||
do {
|
||||
let policy = try store.passwordPolicy()
|
||||
return Reply(["data": policy.sorted { $0.key < $1.key }.map {
|
||||
["section": "PasswordPolicy", "key": $0.key, "value": $0.value]
|
||||
}])
|
||||
} catch {
|
||||
return Reply(status: "503 Service Unavailable", errorCode: -1005)
|
||||
}
|
||||
case ("settings", "settingvalues", "POST") where path.count == 2:
|
||||
guard let rows = input["data"] as? [[String: String]], (1...8).contains(rows.count) else {
|
||||
return Reply(status: "400 Bad Request", errorCode: -1021)
|
||||
}
|
||||
var changes: [String: String] = [:]
|
||||
for row in rows {
|
||||
guard row["section"] == "PasswordPolicy", let key = row["key"],
|
||||
let value = row["value"], changes[key] == nil else {
|
||||
return Reply(status: "400 Bad Request", errorCode: -1021)
|
||||
}
|
||||
changes[key] = value
|
||||
}
|
||||
do {
|
||||
try store.updatePasswordPolicy(changes)
|
||||
event(type: "SETTINGS", message: "Updated password policy.")
|
||||
return Reply()
|
||||
} catch StoreError.invalid {
|
||||
return Reply(status: "400 Bad Request", errorCode: -1021)
|
||||
} catch {
|
||||
return Reply(status: "503 Service Unavailable", errorCode: -1005)
|
||||
}
|
||||
case ("settings", "settingvaluesp2", "GET"):
|
||||
guard path == ["settings", "settingvaluesp2", "section", "protectpatientsocialids", "key", "isenabled"] else {
|
||||
return Reply(["data": []], errorCode: -100037)
|
||||
|
||||
Reference in new issue
Block a user