-
Notifications
You must be signed in to change notification settings - Fork 36
/
MPFullDiskAccessAuthorizer.m
121 lines (105 loc) · 2.81 KB
/
MPFullDiskAccessAuthorizer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// MPFullDiskAccessAuthorizer.m
// PermissionsKit
//
// Created by Sergii Kryvoblotskyi on 9/12/18.
// Copyright © 2018 MacPaw. All rights reserved.
//
#import "MPFullDiskAccessAuthorizer.h"
#import <pwd.h>
@interface MPFullDiskAccessAuthorizer()
@property (nonatomic, strong) NSFileManager *fileManager;
@property (nonatomic, strong) NSWorkspace *workspace;
@property (nonatomic, copy) NSString *userHomeFolderPath;
@end
@implementation MPFullDiskAccessAuthorizer
- (instancetype)initWithFileManager:(NSFileManager *)fileManager workspace:(NSWorkspace *)workspace
{
self = [super init];
if (self)
{
_fileManager = fileManager;
_workspace = workspace;
}
return self;
}
- (instancetype)init
{
return [self initWithFileManager:[NSFileManager defaultManager] workspace:[NSWorkspace sharedWorkspace]];
}
#pragma mark - Public
- (MPAuthorizationStatus)authorizationStatus
{
if (@available(macOS 10.14, *))
{
return [self _fullDiskAuthorizationStatus];
}
else
{
return MPAuthorizationStatusAuthorized;
}
}
- (void)requestAuthorizationWithCompletion:(nonnull void (^)(MPAuthorizationStatus))completionHandler
{
if (@available(macOS 10.14, *))
{
[self _openPreferences];
}
else
{
completionHandler(MPAuthorizationStatusAuthorized);
}
}
#pragma mark - Private
- (MPAuthorizationStatus)_fullDiskAuthorizationStatus
{
NSString *path;
if (@available(macOS 10.15, *))
{
path = [self.userHomeFolderPath stringByAppendingPathComponent:@"Library/Safari/CloudTabs.db"];
}
else
{
path = [self.userHomeFolderPath stringByAppendingPathComponent:@"Library/Safari/Bookmarks.plist"];
}
BOOL fileExists = [self.fileManager fileExistsAtPath:path];
NSData *data = [NSData dataWithContentsOfFile:path];
if (data == nil && fileExists)
{
return MPAuthorizationStatusDenied;
}
else if (fileExists)
{
return MPAuthorizationStatusAuthorized;
}
else
{
return MPAuthorizationStatusNotDetermined;
}
}
- (NSString *)userHomeFolderPath
{
@synchronized (self)
{
if (!_userHomeFolderPath)
{
BOOL isSandboxed = (nil != NSProcessInfo.processInfo.environment[@"APP_SANDBOX_CONTAINER_ID"]);
if (isSandboxed)
{
struct passwd *pw = getpwuid(getuid());
assert(pw);
_userHomeFolderPath = [NSString stringWithUTF8String:pw->pw_dir];
}
else
{
_userHomeFolderPath = NSHomeDirectory();
}
}
}
return _userHomeFolderPath;
}
- (void)_openPreferences
{
[self.workspace openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles"]];
}
@end