From 245979e5d7ed40fc7a29262fbf8f9c83d403d856 Mon Sep 17 00:00:00 2001 From: jureuk7 Date: Tue, 15 Sep 2026 20:52:08 +0900 Subject: [PATCH] Add optional logout prompt after updates --- Sources/GeulGuardInput/AppDelegate.swift | 3 + Sources/GeulGuardInput/UpdateController.swift | 18 +++++ .../GeulGuardInput/UpdateLogoutPrompt.swift | 70 +++++++++++++++++++ .../UpdateLogoutPromptTests.swift | 33 +++++++++ docs/updates.md | 14 ++++ 5 files changed, 138 insertions(+) create mode 100644 Sources/GeulGuardInput/UpdateLogoutPrompt.swift create mode 100644 Tests/GeulGuardInputTests/UpdateLogoutPromptTests.swift diff --git a/Sources/GeulGuardInput/AppDelegate.swift b/Sources/GeulGuardInput/AppDelegate.swift index d4b3811..30aace7 100644 --- a/Sources/GeulGuardInput/AppDelegate.swift +++ b/Sources/GeulGuardInput/AppDelegate.swift @@ -10,6 +10,9 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation { func applicationDidFinishLaunching(_ notification: Notification) { _ = updates configureStatusItem() + Task { @MainActor in + updates.presentPostUpdateLogoutIfNeeded() + } if ProcessInfo.processInfo.arguments.contains("--settings") { showSettings() } diff --git a/Sources/GeulGuardInput/UpdateController.swift b/Sources/GeulGuardInput/UpdateController.swift index 6692761..e151bca 100644 --- a/Sources/GeulGuardInput/UpdateController.swift +++ b/Sources/GeulGuardInput/UpdateController.swift @@ -34,12 +34,18 @@ final class UpdateController: NSObject, ObservableObject, SPUUpdaterDelegate { @Published private(set) var canCheckForUpdates = false @Published private(set) var checksOnLaunch = GeulGuardPreferences.checksForUpdatesOnLaunch @Published private(set) var status = "이 빌드에서는 업데이트를 사용할 수 없습니다." + private let logoutPrompt = UpdateLogoutPrompt() + private var shouldOfferLogout = false + private var installingVersion: String? private var controller: SPUStandardUpdaterController? private var observations: [NSKeyValueObservation] = [] private(set) var isInstallingUpdate = false override init() { super.init() + if let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String { + shouldOfferLogout = logoutPrompt.consumeInstalledUpdate(currentVersion: version) + } guard UpdateConfiguration.isValid(Bundle.main.infoDictionary ?? [:]) else { return } let controller = SPUStandardUpdaterController(startingUpdater: false, updaterDelegate: self, userDriverDelegate: nil) self.controller = controller @@ -64,6 +70,12 @@ final class UpdateController: NSObject, ObservableObject, SPUUpdaterDelegate { } } + func presentPostUpdateLogoutIfNeeded() { + guard shouldOfferLogout else { return } + shouldOfferLogout = false + logoutPrompt.present() + } + private func refreshState() { canCheckForUpdates = controller?.updater.canCheckForUpdates ?? false } @@ -91,11 +103,13 @@ final class UpdateController: NSObject, ObservableObject, SPUUpdaterDelegate { func allowedSystemProfileKeys(for updater: SPUUpdater) -> [String]? { [] } func updater(_ updater: SPUUpdater, willInstallUpdate item: SUAppcastItem) { + installingVersion = item.versionString isInstallingUpdate = true } func updater(_ updater: SPUUpdater, willInstallUpdateOnQuit item: SUAppcastItem, immediateInstallationBlock immediateInstallHandler: @escaping () -> Void) -> Bool { + installingVersion = item.versionString isInstallingUpdate = true return false } @@ -103,12 +117,15 @@ final class UpdateController: NSObject, ObservableObject, SPUUpdaterDelegate { func updater(_ updater: SPUUpdater, userDidMake choice: SPUUserUpdateChoice, forUpdate updateItem: SUAppcastItem, state: SPUUserUpdateState) { if state.stage == .installing { + installingVersion = updateItem.versionString isInstallingUpdate = true } } func updater(_ updater: SPUUpdater, didAbortWithError error: Error) { isInstallingUpdate = false + installingVersion = nil + logoutPrompt.cancelInstallation() } func confirmTermination() -> NSApplication.TerminateReply { @@ -130,6 +147,7 @@ final class UpdateController: NSObject, ObservableObject, SPUUpdaterDelegate { blocked.runModal() return .terminateCancel } + if let installingVersion { logoutPrompt.recordInstallation(version: installingVersion) } return .terminateNow } diff --git a/Sources/GeulGuardInput/UpdateLogoutPrompt.swift b/Sources/GeulGuardInput/UpdateLogoutPrompt.swift new file mode 100644 index 0000000..9a721a7 --- /dev/null +++ b/Sources/GeulGuardInput/UpdateLogoutPrompt.swift @@ -0,0 +1,70 @@ +import AppKit +import Carbon + +@MainActor +final class UpdateLogoutPrompt { + private static let pendingVersionKey = "updatePendingLogoutVersion" + private let defaults: UserDefaults + private let requestLogout: @MainActor () throws -> Void + + init(defaults: UserDefaults = .standard, + requestLogout: @escaping @MainActor () throws -> Void = UpdateLogoutPrompt.requestSystemLogout) { + self.defaults = defaults + self.requestLogout = requestLogout + } + + func recordInstallation(version: String) { + defaults.set(version, forKey: Self.pendingVersionKey) + } + + func cancelInstallation() { + defaults.removeObject(forKey: Self.pendingVersionKey) + } + + func consumeInstalledUpdate(currentVersion: String) -> Bool { + guard let expected = defaults.string(forKey: Self.pendingVersionKey) else { return false } + cancelInstallation() + return expected == currentVersion + } + + func present() { + NSApp.activate(ignoringOtherApps: true) + handleChoice(Self.makeAlert().runModal()) + } + + static func makeAlert() -> NSAlert { + let alert = NSAlert() + alert.messageText = "글가드 업데이트가 설치되었습니다" + alert.informativeText = "새 버전이 바로 적용되지 않았다면 로그아웃한 뒤 다시 로그인해 주세요. 지금 로그아웃하려면 다른 앱에서 작업 중인 내용을 먼저 저장하세요." + alert.addButton(withTitle: "나중에") + alert.addButton(withTitle: "지금 로그아웃") + alert.buttons[0].keyEquivalent = "\r" + alert.buttons[1].keyEquivalent = "" + return alert + } + + func handleChoice(_ choice: NSApplication.ModalResponse) { + guard choice == .alertSecondButtonReturn else { return } + do { + try requestLogout() + } catch { + let alert = NSAlert() + alert.messageText = "로그아웃을 요청하지 못했습니다" + alert.informativeText = "작업을 저장한 뒤 Apple 메뉴에서 로그아웃을 선택해 주세요." + alert.addButton(withTitle: "확인") + alert.runModal() + } + } + + static func logoutEvent() -> NSAppleEventDescriptor { + NSAppleEventDescriptor(eventClass: AEEventClass(kCoreEventClass), + eventID: AEEventID(kAELogOut), + targetDescriptor: NSAppleEventDescriptor(bundleIdentifier: "com.apple.loginwindow"), + returnID: AEReturnID(kAutoGenerateReturnID), + transactionID: AETransactionID(kAnyTransactionID)) + } + + private static func requestSystemLogout() throws { + _ = try logoutEvent().sendEvent(options: [.noReply, .canInteract], timeout: 10) + } +} diff --git a/Tests/GeulGuardInputTests/UpdateLogoutPromptTests.swift b/Tests/GeulGuardInputTests/UpdateLogoutPromptTests.swift new file mode 100644 index 0000000..e527488 --- /dev/null +++ b/Tests/GeulGuardInputTests/UpdateLogoutPromptTests.swift @@ -0,0 +1,33 @@ +import XCTest +import Carbon +@testable import GeulGuardInput + +@MainActor +final class UpdateLogoutPromptTests: XCTestCase { + func testPromptRequiresMatchingInstalledVersionAndIsConsumedOnce() { + let name = "GeulGuard.LogoutTest.\(UUID().uuidString)" + let defaults = UserDefaults(suiteName: name)! + defer { defaults.removePersistentDomain(forName: name) } + let prompt = UpdateLogoutPrompt(defaults: defaults, requestLogout: {}) + XCTAssertFalse(prompt.consumeInstalledUpdate(currentVersion: "7")) + prompt.recordInstallation(version: "8") + XCTAssertFalse(prompt.consumeInstalledUpdate(currentVersion: "7")) + prompt.recordInstallation(version: "8") + XCTAssertTrue(prompt.consumeInstalledUpdate(currentVersion: "8")) + XCTAssertFalse(prompt.consumeInstalledUpdate(currentVersion: "8")) + prompt.recordInstallation(version: "9") + prompt.cancelInstallation() + XCTAssertFalse(prompt.consumeInstalledUpdate(currentVersion: "9")) + } + + func testOnlyExplicitLogoutChoiceRequestsLogout() { + var requests = 0 + let prompt = UpdateLogoutPrompt(requestLogout: { requests += 1 }) + prompt.handleChoice(.alertFirstButtonReturn) + prompt.handleChoice(.abort) + XCTAssertEqual(requests, 0) + prompt.handleChoice(.alertSecondButtonReturn) + XCTAssertEqual(requests, 1) + XCTAssertEqual(UpdateLogoutPrompt.logoutEvent().eventID, AEEventID(kAELogOut)) + } +} diff --git a/docs/updates.md b/docs/updates.md index 0737e81..580cbbd 100644 --- a/docs/updates.md +++ b/docs/updates.md @@ -129,3 +129,17 @@ appcast는 빌드된 앱의 버전·최소 macOS, 최종 PKG의 크기·EdDSA 실제 입력 앱에서의 재활성화. 공개 피드·Sparkle 배포 키를 준비하고 실제 구버전→신버전으로 검증한 뒤 배포해야 한다. 현재 작업은 사용자 입력 소스나 Karabiner 설정을 변경하지 않았으며 배포 PKG 설치·공개 게시를 수행하지 않았다. + +## 업데이트 후 로그아웃 안내 + +이 기능이 포함된 버전에서 Sparkle 설치를 진행하면 대상 빌드 번호를 기록한다. +재실행한 앱의 빌드가 대상과 일치할 때 ‘나중에’와 ‘지금 로그아웃’ 안내를 한 번 +표시한다. ‘나중에’가 기본 선택이며, 새 버전이 바로 적용되었다면 로그아웃하지 +않고 사용할 수 있다. ‘지금 로그아웃’은 작업 저장을 안내한 뒤 macOS의 일반 +로그아웃 확인 절차를 요청한다. 강제 종료하거나 즉시 세션을 종료하지 않는다. +요청이 실패하면 Apple 메뉴를 통한 수동 로그아웃을 안내한다. + +설치 취소·실패 또는 대상 빌드와 다른 앱 실행에서는 설치 완료 안내를 표시하지 +않는다. 이전 버전에는 설치 대상 기록 기능이 없으므로, 이 기능을 처음 설치하는 +업데이트에서는 안내가 나오지 않을 수 있다. PKG를 직접 설치한 경우에도 별도 +안내를 표시하지 않는다. 안내를 미뤘다면 필요할 때 Apple 메뉴에서 로그아웃한다.