Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Let only an administrator change Peel's helper
Țugui Dragoșf9a80e9b058c
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swiftindex 4c02d61e..6df81460 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift@@ -118,6 +118,24 @@ public enum PrivilegedHelper {UserAuthorization.isAdministrator(getuid())}+ /// `isAvailableToThisAccount`, asked away from the caller's actor: the answer comes from opendirectoryd.+ @concurrent+ public static func currentAvailability() async -> Bool {+ isAvailableToThisAccount+ }++ /// Runs `change` only while this account is an administrator, asked again now: macOS lets any account unregister+ /// the helper, which serves every administrator. Answers whether it ran.+ @discardableResult+ public static func changeAsAnAdministrator(+ _ change: () async throws -> Void,+ isAdministrator: () async -> Bool = { await currentAvailability() }+ ) async throws -> Bool {+ guard await isAdministrator() else { return false }+ try await change()+ return true+ }+public static func openLoginItemsSettings() {SMAppService.openSystemSettingsLoginItems()}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HelperStandingTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HelperStandingTests.swiftindex 2dc71446..8bf5a06a 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/HelperStandingTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/HelperStandingTests.swift@@ -16,6 +16,36 @@ struct HelperStandingTests {#expect(turn.taken)}+ @MainActor @Test func asksWhetherThisAccountIsAnAdministratorAwayFromTheMainActor() async {+ final class Turn {+ var taken = false+ }+ let turn = Turn()+ Task { @MainActor in turn.taken = true }++ _ = await PrivilegedHelper.currentAvailability()++ #expect(turn.taken)+ }++ @Test func anAccountThatCantUseTheHelperNeverChangesIt() async throws {+ var changed = false++ let ran = try await PrivilegedHelper.changeAsAnAdministrator({ changed = true }, isAdministrator: { false })++ #expect(!ran)+ #expect(!changed)+ }++ @Test func anAdministratorChangesTheHelper() async throws {+ var changed = false++ let ran = try await PrivilegedHelper.changeAsAnAdministrator({ changed = true }, isAdministrator: { true })++ #expect(ran)+ #expect(changed)+ }+/// The helper serves administrators only, so a standard account is never offered to install, approve or repair/// it, whatever its registration says.@Test func aStandardAccountNeedsAnAdministratorWhateverTheRegistration() {diff --git a/Peel/Model/HelperModel.swift b/Peel/Model/HelperModel.swiftindex b926caa5..f16fe55d 100644--- a/Peel/Model/HelperModel.swift+++ b/Peel/Model/HelperModel.swift@@ -25,17 +25,13 @@ final class HelperModel {standing == .ready}- var isEnabled: Bool {- status == .enabled- }-var standing: PrivilegedHelper.Standing {PrivilegedHelper.Standing(status: status, isAvailableToThisAccount: isAvailableToThisAccount, isResponding: isResponding)}- let isAvailableToThisAccount = PrivilegedHelper.isAvailableToThisAccount+ private(set) var isAvailableToThisAccount = PrivilegedHelper.isAvailableToThisAccount/// True when a Peel helper is registered, but not by this copy of Peel. Registration belongs to a bundle,/// so a second copy (in a build folder, or a download beside the one in Applications) reads "not installed"@@ -48,12 +44,15 @@ final class HelperModel {func checkConnection() async {let check = checks.start()let status = await PrivilegedHelper.currentStatus()- let isResponding = status == .enabled ? await PrivilegedHelper.isResponding() : nil+ let isAvailableToThisAccount = await PrivilegedHelper.currentAvailability()+ // The helper answers administrators only, so another account doesn't ask it.+ let isResponding = status == .enabled && isAvailableToThisAccount ? await PrivilegedHelper.isResponding() : nillet isRegisteredByAnotherCopy = status == .notRegistered? await PrivilegedHelper.isRegisteredByAnyCopy(): falseguard checks.mayShow(check) else { return }self.status = status+ self.isAvailableToThisAccount = isAvailableToThisAccountself.isResponding = isRespondingself.isRegisteredByAnotherCopy = isRegisteredByAnotherCopyhasChecked = true@@ -64,8 +63,8 @@ final class HelperModel {/// throws an error. Neither is reported as a failure, since the status says what happened.func install() async -> Failure? {guard !isChanging else { return nil }- let error = await change(.install) { try await PrivilegedHelper.register() }- if status == .requiresApproval {+ let (wasAllowed, error) = await change(.install) { try await PrivilegedHelper.register() }+ if wasAllowed, status == .requiresApproval {PrivilegedHelper.openLoginItemsSettings()}guard let error, status != .requiresApproval, status != .enabled else { return nil }@@ -74,8 +73,8 @@ final class HelperModel {func repair() async -> Failure? {guard !isChanging else { return nil }- let error = await change(.repair) { try await PrivilegedHelper.repair() }- if status == .requiresApproval {+ let (wasAllowed, error) = await change(.repair) { try await PrivilegedHelper.repair() }+ if wasAllowed, status == .requiresApproval {PrivilegedHelper.openLoginItemsSettings()}return error.map { Failure(action: .repair, reason: $0.localizedDescription) }@@ -83,24 +82,28 @@ final class HelperModel {func uninstall() async -> Failure? {guard !isChanging else { return nil }- let error = await change(.uninstall) { try await PrivilegedHelper.unregister() }+ let (_, error) = await change(.uninstall) { try await PrivilegedHelper.unregister() }return error.map { Failure(action: .uninstall, reason: $0.localizedDescription) }}- /// Runs a change of the helper's registration and answers the error it threw, if any. Nothing about the helper- /// shows while it runs, and what it leaves shows only through the check that follows it.- private func change(_ action: Action, _ body: () async throws -> Void) async -> (any Error)? {+ /// Runs a change of the helper's registration, for an administrator only, and answers whether it was allowed and+ /// its error. Nothing about the helper shows meanwhile; what it leaves shows through the check that follows.+ private func change(+ _ action: Action,+ _ body: () async throws -> Void+ ) async -> (wasAllowed: Bool, error: (any Error)?) {changing = actiondefer { changing = nil }checks.changeStarts()+ var wasAllowed = truevar error: (any Error)?do {- try await body()+ wasAllowed = try await PrivilegedHelper.changeAsAnAdministrator(body)} catch let thrown {error = thrown}checks.changeEnds()await checkConnection()- return error+ return (wasAllowed, error)}}diff --git a/Peel/Model/SelfUninstall.swift b/Peel/Model/SelfUninstall.swiftindex c8da05b2..c71d7a5d 100644--- a/Peel/Model/SelfUninstall.swift+++ b/Peel/Model/SelfUninstall.swift@@ -65,8 +65,14 @@ final class SelfUninstall {return}- work.pause()let isRegistered = { [helper] in helper.status == .enabled || helper.status == .requiresApproval }+ // The helper serves every administrator of this Mac, so an account that can't use it leaves it, and Peel stays.+ if isRegistered(), await !PrivilegedHelper.currentAvailability() {+ failure = String(localized: "The helper couldn’t be removed, so Peel stayed where it was.")+ return+ }++ work.pause()var ledger = PrivilegedHelper.LedgerMove.nonevar links = TrashResult()if isRegistered() {diff --git a/Peel/Views/Settings/SettingsView.swift b/Peel/Views/Settings/SettingsView.swiftindex 3e4bc012..cad4f92a 100644--- a/Peel/Views/Settings/SettingsView.swift+++ b/Peel/Views/Settings/SettingsView.swift@@ -665,9 +665,6 @@ private struct HelperSettingsView: View {case .notThisAccount:Text("Only an administrator can use the helper. Log in as an administrator to remove items that need one.").font(.callout)- if helper.isEnabled {- uninstallButton- }case .notAnswering:// Several causes share this message: an older or newer helper, a signature that doesn't// match, or a helper that didn't start.