Restore password policy editing in Web Console

This commit is contained in:
SantaSpeen committed 2026-11-22 03:34:20 +03:00
1 parent bf3b40d9a5
commit 4801cfbe77
6 files changed
+174 -25

No files matched your search

+57
View File
@@ -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() }