Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c5e000e

Browse files
committedDec 5, 2019
feat: add ability to query for location status
1 parent 2e28718 commit c5e000e

File tree

5 files changed

+60
-19
lines changed

5 files changed

+60
-19
lines changed
 

‎README.md

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,52 @@ This native Node.js module allows you to manage an app's access to:
1717
* Camera
1818
* Microphone
1919
* Accessibility
20+
* Location
2021

2122
## API
2223

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

25-
* `type` String - The type of system component to which you are requesting access. Can be one of 'contacts', 'full-disk-access', 'photos', 'reminders', 'camera', 'accessibility', 'microphone', or 'calendar'.
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`, `photos`, or `reminders`.
2627

27-
Returns `String` - Can be one of 'not determined', 'denied', 'authorized', or 'restricted'.
28+
Returns `String` - Can be one of `not determined`, `denied`, `authorized`, or `restricted`.
2829

2930
Checks the authorization status of the application to access `type` on macOS.
3031

3132
Return Value Descriptions:
32-
* 'not determined' - The user has not yet made a choice regarding whether the application may access `type` data.
33-
* 'restricted' - The application is not authorized to access `type` data. The user cannot change this application’s status, possibly due to active restrictions such as parental controls being in place.
34-
* 'denied' - The user explicitly denied access to `type` data for the application.
35-
* 'authorized' - The application is authorized to access `type` data.
33+
* `not determined` - The user has not yet made a choice regarding whether the application may access `type` data.
34+
* `restricted` - The application is not authorized to access `type` data. The user cannot change this application’s status, possibly due to active restrictions such as parental controls being in place.
35+
* `denied` - The user explicitly denied access to `type` data for the application.
36+
* `authorized` - The application is authorized to access `type` data.
3637

3738
**Notes:**
38-
* 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.
39-
* 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.
39+
* 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.
40+
* 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.
41+
42+
Example:
43+
```js
44+
const types = [
45+
'contacts',
46+
'calendar',
47+
'reminders',
48+
'full-disk-access',
49+
'camera',
50+
'microphone',
51+
'accessibility',
52+
'location'
53+
]
54+
55+
const statuses = ['not determined', 'denied', 'authorized', 'restricted']
56+
for (const type of types) {
57+
const status = getAuthStatus(type)
58+
console.log(`Access to ${type} is ${status}`)
59+
}
60+
```
4061

4162
## `permissions.askForContactsAccess(callback)`
4263

4364
* `callback` Function
44-
* `status` String - Whether or not the request succeeded or failed; can be 'authorized' or 'denied'.
65+
* `status` String - Whether or not the request succeeded or failed; can be `authorized` or `denied`.
4566

4667
Your app’s `Info.plist` file must provide a value for the `NSContactsUsageDescription` key that explains to the user why your app is requesting Contacts access.
4768

@@ -50,7 +71,7 @@ Your app’s `Info.plist` file must provide a value for the `NSContactsUsageDesc
5071
<string>Your reason for wanting to access the Contact store</string>
5172
```
5273

53-
**Note:** `status` will be called back as 'authorized' prior to macOS 10.11, as access to contacts was unilaterally allowed until that version.
74+
**Note:** `status` will be called back as `authorized` prior to macOS 10.11, as access to contacts was unilaterally allowed until that version.
5475

5576
Example:
5677
```js
@@ -64,7 +85,7 @@ askForContactsAccess((status) => {
6485
## `permissions.askForCalendarAccess(callback)`
6586

6687
* `callback` Function
67-
* `status` String - Whether or not the request succeeded or failed; can be 'authorized' or 'denied'.
88+
* `status` String - Whether or not the request succeeded or failed; can be `authorized` or `denied`.
6889

6990
Example:
7091
```js
@@ -78,7 +99,7 @@ askForCalendarAccess((status) => {
7899
## `permissions.askForRemindersAccess(callback)`
79100

80101
* `callback` Function
81-
* `status` String - Whether or not the request succeeded or failed; can be 'authorized' or 'denied'.
102+
* `status` String - Whether or not the request succeeded or failed; can be `authorized` or `denied`.
82103

83104
Example:
84105
```js
@@ -102,10 +123,10 @@ askForFullDiskAccess()
102123

103124
## `permissions.askForMediaAccess(type, callback)`
104125

105-
* `type` String - The type of media to which you are requesting access. Can be 'microphone' or 'camera'.
126+
* `type` String - The type of media to which you are requesting access. Can be `microphone` or `camera`.
106127

107128
* `callback` Function
108-
* `status` String - Whether or not the request succeeded or failed; can be 'authorized' or 'denied'.
129+
* `status` String - Whether or not the request succeeded or failed; can be `authorized` or `denied`.
109130

110131
Your app must provide an explanation for its use of capture devices using the `NSCameraUsageDescription` or `NSMicrophoneUsageDescription` `Info.plist` keys; Calling this method or attempting to start a capture session without a usage description raises an exception.
111132

@@ -116,7 +137,7 @@ Your app must provide an explanation for its use of capture devices using the `N
116137
<string>Your reason for wanting to access the Microphone</string>
117138
```
118139

119-
**Note:** `status` will be called back as 'authorized' prior to macOS 10.14 High Sierra, as access to the camera and microphone was unilaterally allowed until that version.
140+
**Note:** `status` will be called back as `authorized` prior to macOS 10.14 High Sierra, as access to the camera and microphone was unilaterally allowed until that version.
120141

121142
Example:
122143
```js

‎binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
2020
"xcode_settings": {
2121
"OTHER_CPLUSPLUSFLAGS": ["-std=c++14", "-stdlib=libc++", "-mmacosx-version-min=10.10"],
22-
"OTHER_LDFLAGS": ["-framework CoreFoundation -framework AppKit -framework Contacts -framework EventKit -framework Photos -framework AVFoundation"]
22+
"OTHER_LDFLAGS": ["-framework CoreFoundation -framework CoreLocation -framework AppKit -framework Contacts -framework EventKit -framework Photos -framework AVFoundation"]
2323
}
2424
}]
2525
}

‎index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ function getAuthStatus(type) {
88
'full-disk-access',
99
'camera',
1010
'microphone',
11-
'accessibility'
11+
'accessibility',
12+
'location'
1213
]
1314

1415
if (!validTypes.includes(type)) {

‎permissions.mm

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
// Apple APIs
44
#import <AppKit/AppKit.h>
5-
#import <AVFoundation/AVFoundation.h>
5+
#import <AVFoundation/AVFoundation.h>
6+
#import <CoreLocation/CoreLocation.h>
67
#import <Contacts/Contacts.h>
78
#import <EventKit/EventKit.h>
89
#import <Foundation/Foundation.h>
@@ -105,6 +106,21 @@
105106
return auth_status;
106107
}
107108

109+
std::string LocationAuthStatus() {
110+
std::string auth_status = "not determined";
111+
112+
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
113+
114+
if (status == kCLAuthorizationStatusAuthorizedAlways)
115+
auth_status = "authorized";
116+
else if (status == kCLAuthorizationStatusDenied)
117+
auth_status = "denied";
118+
else if (status == kCLAuthorizationStatusNotDetermined)
119+
auth_status = "restricted";
120+
121+
return auth_status;
122+
}
123+
108124
/***** EXPORTED FUNCTIONS *****/
109125

110126
// Returns the user's access consent status as a string
@@ -127,6 +143,8 @@
127143
auth_status = MediaAuthStatus("camera");
128144
} else if (type == "accessibility") {
129145
auth_status = AXIsProcessTrusted() ? "authorized" : "denied";
146+
} else if (type == "location") {
147+
auth_status = LocationAuthStatus();
130148
}
131149

132150
return Napi::Value::From(env, auth_status);

‎test/module.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ describe('node-mac-permissions', () => {
2020
'full-disk-access',
2121
'camera',
2222
'microphone',
23-
'accessibility'
23+
'accessibility',
24+
'location'
2425
]
2526

2627
const statuses = ['not determined', 'denied', 'authorized', 'restricted']

0 commit comments

Comments
 (0)
Please sign in to comment.