Skip to content

Commit e0e1356

Browse files
committed
feat: re-add access to Photos auth status
1 parent 4c26a6f commit e0e1356

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This native Node.js module allows you to manage an app's access to:
1515
* Reminders
1616
* Camera
1717
* Microphone
18+
* Photos
1819
* Accessibility
1920
* Location
2021
* Screen Capture
@@ -23,7 +24,7 @@ This native Node.js module allows you to manage an app's access to:
2324

2425
## `permissions.getAuthStatus(type)`
2526

26-
* `type` String - The type of system component to which you are requesting access. Can be one of `accessibility`, `calendar`, `camera`, `contacts`, `full-disk-access`, `location`, `microphone`, `screen`, or `reminders`.
27+
* `type` String - The type of system component to which you are requesting access. Can be one of `accessibility`, `calendar`, `camera`, `contacts`, `full-disk-access`, `location`, `microphone`, `photos`, `screen`, or `reminders`.
2728

2829
Returns `String` - Can be one of `not determined`, `denied`, `authorized`, or `restricted`.
2930

@@ -39,6 +40,7 @@ Return Value Descriptions:
3940
* Access to `contacts` will always return a status of `authorized` prior to macOS 10.11, as access to contacts was unilaterally allowed until that version.
4041
* Access to `camera` and `microphone` will always return a status of `authorized` prior to macOS 10.14, as access to contacts was unilaterally allowed until that version.
4142
* Access to `screen` will always return a status of `authorized` prior to macOS 10.15, as access to screen capture was unilaterally allowed until that version.
43+
* Access to `photos` will always return a status of `authorized` prior to macOS 10.13, as access to screen capture was unilaterally allowed until that version.
4244

4345
Example:
4446
```js
@@ -48,12 +50,12 @@ const types = [
4850
'reminders',
4951
'full-disk-access',
5052
'camera',
53+
'photos',
5154
'microphone',
5255
'accessibility',
5356
'location'
5457
]
5558

56-
const statuses = ['not determined', 'denied', 'authorized', 'restricted']
5759
for (const type of types) {
5860
const status = getAuthStatus(type)
5961
console.log(`Access to ${type} is ${status}`)
@@ -242,4 +244,7 @@ $ tccutil reset Camera
242244

243245
# Reset Microphone access permissions
244246
$ tccutil reset Microphone
247+
248+
# Reset Photos access permissions
249+
$ tccutil reset Photos
245250
```

binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
2323
"xcode_settings": {
2424
"OTHER_CPLUSPLUSFLAGS": ["-std=c++14", "-stdlib=libc++"],
25-
"OTHER_LDFLAGS": ["-framework CoreFoundation -framework CoreLocation -framework AppKit -framework Contacts -framework EventKit -framework AVFoundation"]
25+
"OTHER_LDFLAGS": ["-framework CoreFoundation -framework CoreLocation -framework AppKit -framework Contacts -framework Photos -framework EventKit -framework AVFoundation"]
2626
}
2727
}]
2828
}

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function getAuthStatus(type) {
77
'reminders',
88
'full-disk-access',
99
'camera',
10+
'photos',
1011
'microphone',
1112
'accessibility',
1213
'location',

permissions.mm

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
#import <Contacts/Contacts.h>
77
#import <CoreLocation/CoreLocation.h>
88
#import <EventKit/EventKit.h>
9+
#import <Photos/Photos.h>
910
#import <Foundation/Foundation.h>
1011
#import <pwd.h>
1112

1213
/***** HELPER FUNCTIONS *****/
1314

14-
// Dummy value to pass into function parameter for ThreadSafeFunction
15+
// Dummy value to pass into function parameter for ThreadSafeFunction.
1516
Napi::Value NoOp(const Napi::CallbackInfo &info) {
1617
return info.Env().Undefined();
1718
}
1819

19-
// Returns the user's home folder path
20+
// Returns the user's home folder path.
2021
NSString *GetUserHomeFolderPath() {
2122
NSString *path;
2223
BOOL isSandboxed =
@@ -35,7 +36,7 @@
3536
}
3637

3738
// Returns a status indicating whether the user has authorized Contacts
38-
// access
39+
// access.
3940
std::string ContactAuthStatus() {
4041
std::string auth_status = "not determined";
4142

@@ -54,7 +55,7 @@
5455
}
5556

5657
// Returns a status indicating whether the user has authorized
57-
// Calendar/Reminders access
58+
// Calendar/Reminders access.
5859
std::string EventAuthStatus(const std::string &type) {
5960
std::string auth_status = "not determined";
6061

@@ -73,7 +74,7 @@
7374
return auth_status;
7475
}
7576

76-
// Returns a status indicating whether the user has Full Disk Access
77+
// Returns a status indicating whether the user has Full Disk Access.
7778
std::string FDAAuthStatus() {
7879
std::string auth_status = "not determined";
7980
NSString *path;
@@ -100,7 +101,7 @@
100101
}
101102

102103
// Returns a status indicating whether the user has authorized
103-
// Screen Capture access
104+
// Screen Capture access.
104105
std::string ScreenAuthStatus() {
105106
std::string auth_status = "not determined";
106107
if (@available(macOS 10.15, *)) {
@@ -147,7 +148,7 @@
147148
}
148149

149150
// Returns a status indicating whether the user has authorized
150-
// Camera/Microphone access
151+
// Camera/Microphone access.
151152
std::string MediaAuthStatus(const std::string &type) {
152153
std::string auth_status = "not determined";
153154

@@ -171,7 +172,7 @@
171172
}
172173

173174
// Returns a status indicating whether the user has authorized location
174-
// access
175+
// access.
175176
std::string LocationAuthStatus() {
176177
std::string auth_status = "not determined";
177178

@@ -187,9 +188,30 @@
187188
return auth_status;
188189
}
189190

191+
// Returns a status indicating whether or not the user has authorized Photos
192+
// access.
193+
std::string PhotosAuthStatus() {
194+
std::string auth_status = "not determined";
195+
196+
if (@available(macOS 10.13, *)) {
197+
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
198+
199+
if (status == PHAuthorizationStatusAuthorized)
200+
auth_status = "authorized";
201+
else if (status == PHAuthorizationStatusDenied)
202+
auth_status = "denied";
203+
else if (status == PHAuthorizationStatusRestricted)
204+
auth_status = "restricted";
205+
} else {
206+
auth_status = "authorized";
207+
}
208+
209+
return auth_status;
210+
}
211+
190212
/***** EXPORTED FUNCTIONS *****/
191213

192-
// Returns the user's access consent status as a string
214+
// Returns the user's access consent status as a string.
193215
Napi::Value GetAuthStatus(const Napi::CallbackInfo &info) {
194216
Napi::Env env = info.Env();
195217
std::string auth_status;
@@ -205,6 +227,8 @@
205227
auth_status = FDAAuthStatus();
206228
} else if (type == "microphone") {
207229
auth_status = MediaAuthStatus("microphone");
230+
} else if (type == "photos") {
231+
auth_status = PhotosAuthStatus();
208232
} else if (type == "camera") {
209233
auth_status = MediaAuthStatus("camera");
210234
} else if (type == "accessibility") {
@@ -218,7 +242,7 @@
218242
return Napi::Value::From(env, auth_status);
219243
}
220244

221-
// Request access to the Contacts store.
245+
// Request Contacts access.
222246
Napi::Promise AskForContactsAccess(const Napi::CallbackInfo &info) {
223247
Napi::Env env = info.Env();
224248
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
@@ -245,7 +269,7 @@
245269
return deferred.Promise();
246270
}
247271

248-
// Request access to Calendar.
272+
// Request Calendar access.
249273
Napi::Promise AskForCalendarAccess(const Napi::CallbackInfo &info) {
250274
Napi::Env env = info.Env();
251275
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
@@ -268,7 +292,7 @@
268292
return deferred.Promise();
269293
}
270294

271-
// Request access to Reminders.
295+
// Request Reminders access.
272296
Napi::Promise AskForRemindersAccess(const Napi::CallbackInfo &info) {
273297
Napi::Env env = info.Env();
274298
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
@@ -300,7 +324,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
300324
[workspace openURL:[NSURL URLWithString:pref_string]];
301325
}
302326

303-
// Request Camera Access.
327+
// Request Camera access.
304328
Napi::Promise AskForCameraAccess(const Napi::CallbackInfo &info) {
305329
Napi::Env env = info.Env();
306330
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
@@ -342,7 +366,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
342366
return deferred.Promise();
343367
}
344368

345-
// Request Microphone Access.
369+
// Request Microphone access.
346370
Napi::Promise AskForMicrophoneAccess(const Napi::CallbackInfo &info) {
347371
Napi::Env env = info.Env();
348372
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);

0 commit comments

Comments
 (0)