Skip to content

Commit a01b5bb

Browse files
feat: manage saved folder access separately from Finder menus
Add File Access settings for saved directory grants and check current authorization when file operations run.
1 parent e86f5fc commit a01b5bb

47 files changed

Lines changed: 763 additions & 361 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎App/FileMint/BatchRenameAccess.swift‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import FileMintCore
77
@MainActor
88
final class BatchRenameAccess {
99
private var active: [URL] = []
10+
private(set) var shouldRememberParentGrant = false
1011
private let store: BatchRenameAccessStore?
1112
private let chooseDirectory: @MainActor (URL, AppLanguage) -> URL?
1213

@@ -28,7 +29,10 @@ final class BatchRenameAccess {
2829
let saved = try await Task.detached(priority: .userInitiated) { try store.load() }.value
2930
for candidate in BatchRenameAccessStore.candidates(for: parent, identity: identity, grants: saved) {
3031
if let grant = await restore(candidate.grant.bookmark, parent: parent, identity: identity,
31-
allowRelocation: allowRelocation, savedGrant: candidate.grant) { return grant }
32+
allowRelocation: allowRelocation, savedGrant: candidate.grant) {
33+
shouldRememberParentGrant = true
34+
return grant
35+
}
3236
renewalRequired = true
3337
}
3438
}
@@ -54,6 +58,7 @@ final class BatchRenameAccess {
5458
guard let held = try await retain(grant, identity: identity) else {
5559
throw BatchRenameError.invalidBookmark
5660
}
61+
shouldRememberParentGrant = true
5762
return held
5863
}
5964

‎App/FileMint/BatchRenameOperations.swift‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,19 @@ final class BatchRenameOperations: ObservableObject {
6868
try await perform(action, preferencesFile: preferencesFile)
6969
return
7070
}
71-
let operationStore = store
72-
let previousAuthority = try await Task.detached(priority: .userInitiated) {
73-
try operationStore.undoAuthorization()
74-
}.value
75-
let previousBookmark = previousAuthority?.parentIdentity == selection.parentIdentity
76-
? previousAuthority?.parentBookmark : nil
7771
let sourceParent = selection.items.first?.source.deletingLastPathComponent() ?? selection.parent
7872
let configuredAccess = FolderAccess.startSavedAccess(to: sourceParent, resolved: selection.parent, in: preferences)
7973
+ RememberedFileAccess(preferencesFile: preferencesFile).start(for: selection.parent, folder: true, mode: .write,
8074
roots: preferences.monitoredFolderURLs)
8175
defer { configuredAccess.forEach { $0.stopAccessingSecurityScopedResource() } }
8276
guard let (_, bookmark) = try await access.authorize(parent: selection.parent,
83-
identity: selection.parentIdentity,
84-
bookmark: preferences.monitoredFolderBookmarks[selection.parent.path] ?? previousBookmark,
77+
identity: selection.parentIdentity, bookmark: nil,
8578
allowRelocation: false, language: language, hasIndependentAccess: !configuredAccess.isEmpty) else { return }
8679
let service = BatchRenameService(store: store)
8780
let roots = observationRoots(preferences)
8881
let locations = privateLocations
8982
let accessStore = self.accessStore
83+
let shouldRememberParentGrant = access.shouldRememberParentGrant
9084
let session = try await Task.detached(priority: .userInitiated) {
9185
guard Self.allowsForward(parent: selection.parent, preferencesFile: preferencesFile) else {
9286
throw BatchRenameError.disabled
@@ -96,7 +90,9 @@ final class BatchRenameOperations: ObservableObject {
9690
guard Self.allowsForward(parent: session.parentURL, preferencesFile: preferencesFile) else {
9791
throw BatchRenameError.disabled
9892
}
99-
try accessStore.remember(parent: session.parentURL, identity: selection.parentIdentity, bookmark: bookmark)
93+
if shouldRememberParentGrant {
94+
try accessStore.remember(parent: session.parentURL, identity: selection.parentIdentity, bookmark: bookmark)
95+
}
10096
return session
10197
}.value
10298
let controlStore = store

‎App/FileMint/ContentView.swift‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ struct ContentView: View {
178178
case .creation: CreationSettingsPane()
179179
case .fileTypes: TypesPane()
180180
case .folders: FoldersPane()
181+
case .fileAccess: FileAccessPane()
181182
case .fileTools: FileToolsPane()
182183
case .resourceTools: ResourceToolsPane(launchTool: launchResourceTool)
183184
case .openWith: OpenWithPane()
@@ -259,6 +260,7 @@ extension PreferencesModel.Pane {
259260
case .creation: InterfaceText.creationSubtitle.text(language)
260261
case .fileTypes: InterfaceText.typesSubtitle.text(language)
261262
case .folders: InterfaceText.foldersSubtitle.text(language)
263+
case .fileAccess: FileMintStrings.text(.fileAccessHint, language: language)
262264
case .fileTools: InterfaceText.filesSubtitle.text(language)
263265
case .resourceTools: InterfaceText.resourceSubtitle.text(language)
264266
case .openWith: FileMintStrings.text(.openWithAppsHint, language: language)
@@ -274,6 +276,7 @@ extension PreferencesModel.Pane {
274276
case .creation: .creationSettings
275277
case .fileTypes: .templatesAndTypes
276278
case .folders: .finderAndFolders
279+
case .fileAccess: .fileAccess
277280
case .fileTools: .fileTools
278281
case .resourceTools: .resourceTools
279282
case .openWith: .openWithApps
@@ -289,6 +292,7 @@ extension PreferencesModel.Pane {
289292
case .creation: .creationSettingsHint
290293
case .fileTypes: .fileTypeHint
291294
case .folders: .finderFoldersHint
295+
case .fileAccess: .fileAccessHint
292296
case .fileTools: .fileToolsHint
293297
case .resourceTools: .resourceToolsHint
294298
case .openWith: .openWithAppsHint
@@ -305,6 +309,7 @@ extension PreferencesModel.Pane {
305309
case .creation: "doc.badge.plus"
306310
case .fileTypes: "doc.on.doc"
307311
case .folders: "folder"
312+
case .fileAccess: "lock.shield"
308313
case .fileTools: "wrench.and.screwdriver"
309314
case .resourceTools: "photo.on.rectangle"
310315
case .openWith: FileToolAppearance.openWithSymbol

‎App/FileMint/DesignSystem.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ enum InterfaceText {
173173
case .generalSubtitle: ("Quietly at work, just the way you like.", "按你的习惯,安静地工作。")
174174
case .creationSubtitle: ("A few thoughtful details for every new file.", "照顾每一次新建的小细节。")
175175
case .typesSubtitle: ("Keep your everyday starting points close.", "把常用的起点,留在手边。")
176-
case .foldersSubtitle: ("You decide where FileMint can work.", "你决定 FileMint 可以在哪里工作。")
176+
case .foldersSubtitle: ("Choose where FileMint appears in Finder.", "选择 FileMint 菜单在 Finder 中的显示位置。")
177177
case .filesSubtitle: ("Just the tools you need in the context menu.", "让右键菜单,恰好够用。")
178178
case .interface: ("Interface", "界面")
179179
case .enabledFinder: ("Finder extension enabled", "Finder 扩展已启用")

‎App/FileMint/FileAccessPane.swift‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import FileMintCore
2+
import SwiftUI
3+
4+
struct FileAccessPane: View {
5+
private struct Grant: Hashable {
6+
let path: String
7+
let legacy: Bool
8+
}
9+
10+
@EnvironmentObject private var model: PreferencesModel
11+
@State private var writePaths: [String] = []
12+
@State private var couldNotLoad = false
13+
@State private var selection: Grant?
14+
15+
private var grants: [Grant] {
16+
writePaths.map { Grant(path: $0, legacy: false) }
17+
+ model.preferences.monitoredFolderBookmarks.keys.sorted().map { Grant(path: $0, legacy: true) }
18+
}
19+
20+
var body: some View {
21+
ScrollView {
22+
VStack(alignment: .leading, spacing: 12) {
23+
SettingsSectionTitle(title: model.text(.directoryWriteAccess))
24+
Text(model.text(.directoryWriteAuthorizationHint))
25+
.font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
26+
if couldNotLoad {
27+
Label(model.text(.directoryAccessLoadFailed), systemImage: "exclamationmark.circle")
28+
.font(.caption).foregroundStyle(.secondary)
29+
}
30+
List(grants, id: \.self, selection: $selection) { grant in
31+
Label {
32+
VStack(alignment: .leading, spacing: 4) {
33+
Text((grant.path as NSString).abbreviatingWithTildeInPath)
34+
.lineLimit(1).truncationMode(.middle)
35+
Text(model.text(grant.legacy ? .legacyDirectoryAccess : .folderAccessSaved))
36+
.font(.caption).foregroundStyle(.secondary)
37+
}
38+
} icon: {
39+
Image(systemName: "folder.badge.person.crop").foregroundStyle(FileMintStyle.accent)
40+
}.padding(.vertical, 6).tag(grant)
41+
}.listStyle(.plain).scrollContentBackground(.hidden).frame(height: 210)
42+
.background(FileMintStyle.surface, in: RoundedRectangle(cornerRadius: 12))
43+
.clipShape(RoundedRectangle(cornerRadius: 12))
44+
.overlay(RoundedRectangle(cornerRadius: 12).strokeBorder(FileMintStyle.line, lineWidth: 0.7))
45+
.overlay {
46+
if grants.isEmpty && !couldNotLoad {
47+
Text(model.text(.noDirectoryWriteAccess)).font(.caption).foregroundStyle(.secondary)
48+
}
49+
}
50+
.accessibilityIdentifier("fileAccess.savedFolders")
51+
HStack {
52+
Button(model.text(.authorizeHomeDirectory)) { model.chooseDirectoryWriteAccess(home: true) }
53+
.accessibilityIdentifier("fileAccess.authorizeHome")
54+
Button(model.text(.authorizeOtherDirectory)) { model.chooseDirectoryWriteAccess(home: false) }
55+
.accessibilityIdentifier("fileAccess.authorizeOther")
56+
Spacer()
57+
Button(model.text(.removeDirectoryAccess)) {
58+
if let selection, model.forgetDirectoryWriteAccess(path: selection.path, legacy: selection.legacy) {
59+
self.selection = nil
60+
}
61+
}.disabled(selection == nil).accessibilityIdentifier("fileAccess.remove")
62+
}
63+
Text(model.text(.directoryAccessRemovalHint))
64+
.font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
65+
}.padding(1)
66+
}
67+
.onAppear(perform: refresh)
68+
.onReceive(NotificationCenter.default.publisher(for: RememberedFileAccess.directoryWritesChanged)) { _ in refresh() }
69+
}
70+
71+
private func refresh() {
72+
do {
73+
writePaths = try model.rememberedFileAccess.directoryWritePaths()
74+
couldNotLoad = false
75+
} catch { couldNotLoad = true }
76+
}
77+
}

‎App/FileMint/FileInfoReadAccess.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ final class FileInfoReadAccess {
3333
defer { panel = nil }
3434
NSApp.activate(ignoringOtherApps: true)
3535
guard withExtendedLifetime(delegate, { picker.runModal() }) == .OK, let chosen = picker.url else { return false }
36+
defer { chosen.stopAccessingSecurityScopedResource() }
3637
try remember(chosen, for: selection, in: currentRoots())
3738
return true
3839
}

‎App/FileMint/FileOperationCoordinator.swift‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,9 @@ final class FileOperationCoordinator: NSObject, NSSharingServiceDelegate {
393393
try requireResolvedSelection(selection)
394394
guard FileToolsPolicy.availableTools(selection: selection, isItemMenu: true,
395395
preferences: currentPreferences).contains(.move) else { throw FileMoveError.disabled }
396-
let savedBookmarks = bookmarks
397396
let store = self.store
398397
try await Task.detached(priority: .userInitiated) {
399-
let pending = try PendingFileMove.capture(selection: selection, bookmarks: savedBookmarks)
398+
let pending = try PendingFileMove.capture(selection: selection)
400399
try store.save(pending)
401400
}.value
402401

@@ -480,18 +479,15 @@ final class FileOperationCoordinator: NSObject, NSSharingServiceDelegate {
480479
}.value
481480

482481
case .perform(let batchID, let destination):
483-
guard var pending = try store.load(), pending.id == batchID else { throw FileMoveError.staleRequest }
482+
guard let pending = try store.load(), pending.id == batchID else { throw FileMoveError.staleRequest }
484483
try validate(pending, destination: destination)
485-
var bookmarks = pending.bookmarks
484+
var bookmarks: [String: Data] = [:]
486485
guard try authorizeParents(of: pending.items.map(\.source), bookmarks: &bookmarks) else { return }
487486
guard try authorize(destination, bookmarks: &bookmarks) else { return }
488487
try requireResolvedSelection(pending.items.map(\.source))
489488
guard FolderScope.containsResolvedDirectory(destination, in: currentPreferences.monitoredFolderURLs) else {
490489
throw FileMoveError.disabled
491490
}
492-
// Preserve new folder grants even if a later item cannot be moved.
493-
pending.bookmarks = bookmarks
494-
try store.save(pending)
495491
let store = self.store
496492
let preferencesFile = self.preferencesFile
497493
try await Task.detached(priority: .userInitiated) {
@@ -564,11 +560,12 @@ final class FileOperationCoordinator: NSObject, NSSharingServiceDelegate {
564560
private func authorize(_ directory: URL, bookmarks: inout [String: Data], reading items: [URL] = [],
565561
message: FileMintTextKey = .fileOperationAuthorize) throws -> Bool {
566562
let isReadOnly = !items.isEmpty
563+
let requiresFinderScope = message != .desktopAliasAuthorize
567564
let preferences = currentPreferences
568-
access += FolderAccess.startSavedAccess(to: directory, in: preferences)
565+
access += FolderAccess.startSavedAccess(to: directory, in: preferences, requiresFinderScope: requiresFinderScope)
569566
let mode: RememberedFileAccess.Mode = isReadOnly ? .read : .write
570567
access += rememberedAccess.start(for: items.isEmpty ? [directory] : items, folders: items.isEmpty,
571-
mode: mode, roots: preferences.monitoredFolderURLs)
568+
mode: mode, roots: requiresFinderScope ? preferences.monitoredFolderURLs : nil)
572569
let savedAncestors = bookmarks.keys.filter { $0 != directory.path }.map { URL(fileURLWithPath: $0, isDirectory: true) }
573570
access += FolderAccess.startSavedAccess(to: directory, roots: savedAncestors, bookmarks: bookmarks)
574571
if let data = bookmarks[directory.path] {
@@ -596,10 +593,13 @@ final class FileOperationCoordinator: NSObject, NSSharingServiceDelegate {
596593
}
597594
}
598595
let language = preferences.language
596+
let detail = isReadOnly ? FileMintStrings.text(message, language: language)
597+
: FileMintStrings.text(message, language: language) + "\n" + FileMintStrings.text(.directoryWriteAuthorizationHint, language: language)
599598
let panel = FolderAccess.authorizationPanel(for: directory, selection: .folder, language: language,
600-
detail: FileMintStrings.text(message, language: language))
599+
detail: detail)
601600
NSApp.activate(ignoringOtherApps: true)
602601
guard panel.runModal() == .OK, let chosen = panel.url else { return false }
602+
defer { chosen.stopAccessingSecurityScopedResource() }
603603
guard chosen.resolvingSymlinksInPath().standardizedFileURL.path ==
604604
directory.resolvingSymlinksInPath().standardizedFileURL.path else {
605605
showMessage(.moveChooseExactFolder)
@@ -610,7 +610,7 @@ final class FileOperationCoordinator: NSObject, NSSharingServiceDelegate {
610610
try check()
611611
let roots = currentPreferences.monitoredFolderURLs
612612
let canonical = directory.resolvingSymlinksInPath().standardizedFileURL
613-
if FolderScope.containsResolvedDirectory(canonical, in: roots) {
613+
if requiresFinderScope, FolderScope.containsResolvedDirectory(canonical, in: roots) {
614614
access.append(try rememberedAccess.remember(chosen, target: canonical, folder: true, mode: mode, roots: roots))
615615
}
616616
let options: URL.BookmarkCreationOptions = isReadOnly ? [.withSecurityScope, .securityScopeAllowOnlyReadAccess] : .withSecurityScope

‎App/FileMint/FoldersPane.swift‎

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import SwiftUI
44
struct FoldersPane: View {
55
@EnvironmentObject private var model: PreferencesModel
66
@State private var selection: URL?
7-
@State private var showsPrivacyGuide = false
87

98
var body: some View {
109
ScrollView {
@@ -31,38 +30,22 @@ struct FoldersPane: View {
3130
List(model.preferences.monitoredFolderURLs, id: \.self, selection: $selection) { url in
3231
HStack(spacing: 12) {
3332
Image(systemName: "folder").font(.system(size: 18)).foregroundStyle(FileMintStyle.accent)
34-
VStack(alignment: .leading, spacing: 4) {
35-
Text((url.path as NSString).abbreviatingWithTildeInPath)
36-
.lineLimit(1).truncationMode(.middle)
37-
Text(model.preferences.monitoredFolderBookmarks[url.path] == nil ? model.text(.needsAccess) : model.text(.folderAccessSaved))
38-
.font(.caption).foregroundStyle(.secondary)
39-
}
33+
Text((url.path as NSString).abbreviatingWithTildeInPath)
34+
.lineLimit(1).truncationMode(.middle)
4035
}.padding(.vertical, 8).tag(url)
4136
}.listStyle(.plain).scrollContentBackground(.hidden).frame(height: 210)
4237
.background(FileMintStyle.surface, in: RoundedRectangle(cornerRadius: 12))
4338
.clipShape(RoundedRectangle(cornerRadius: 12))
4439
.overlay(RoundedRectangle(cornerRadius: 12).strokeBorder(FileMintStyle.line, lineWidth: 0.7))
4540
HStack {
4641
Button(model.text(.addFolder)) { model.addMonitoredFolder() }
47-
Button(model.text(.authorize)) { model.addMonitoredFolder(initial: selection) }.disabled(selection == nil)
4842
Button(model.text(.remove)) {
4943
if let selection { model.removeFolder(selection); self.selection = nil }
5044
}.disabled(selection == nil)
45+
Spacer()
46+
Button(model.text(.openWithFolderSettings)) { model.selectedPane = .fileAccess }
47+
.accessibilityIdentifier("settings.manageFileAccess")
5148
}
52-
Text(model.text(.folderAccessReminder)).font(.caption).foregroundStyle(.secondary)
53-
.fixedSize(horizontal: false, vertical: true)
54-
}
55-
DisclosureGroup(isExpanded: $showsPrivacyGuide) {
56-
VStack(alignment: .leading, spacing: 10) {
57-
Text(model.text(.fullDiskAccessStatus)).font(.callout.weight(.medium))
58-
Text(model.text(.fullDiskAccessStatusHint)).foregroundStyle(.secondary)
59-
Text(model.text(.fullDiskAccessEnabledHint))
60-
Text(model.text(.fullDiskAccessHint)).foregroundStyle(.secondary)
61-
Button(model.text(.openFullDiskAccess)) { model.openFullDiskAccessSettings() }
62-
}.font(.caption).fixedSize(horizontal: false, vertical: true)
63-
.frame(maxWidth: .infinity, alignment: .leading).padding(.top, 12)
64-
} label: {
65-
Text(model.text(.fullDiskAccess)).font(.system(size: 13, weight: .medium))
6649
}
6750
}.padding(1)
6851
}

0 commit comments

Comments
 (0)