js-apis-abilityAccessCtrl.md 8.9 KB
Newer Older
1
# Ability Access Control
W
wusongqing 已提交
2

3
> **NOTE**<br>
W
wusongqing 已提交
4 5 6 7
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

## Modules to Import

W
wusongqing 已提交
8
```js
W
wusongqing 已提交
9 10 11 12 13 14 15 16 17
import abilityAccessCtrl from '@ohos.abilityAccessCtrl'
```

## abilityAccessCtrl.createAtManager

createAtManager(): AtManager

Creates an **AtManager** instance, which is used for ability access control.

W
wusongqing 已提交
18 19 20
**System capability**: SystemCapability.Security.AccessToken


W
wusongqing 已提交
21 22
**Return value**

23 24 25
 | Type | Description |
 | -------- | -------- |
 | [AtManager](#atmanager) | **AtManager** instance obtained. |
W
wusongqing 已提交
26 27 28

**Example**

W
wusongqing 已提交
29
```js
W
wusongqing 已提交
30 31 32 33 34 35 36 37 38 39 40
var AtManager = abilityAccessCtrl.createAtManager();
```

## AtManager

Implements ability access control.

### verifyAccessToken

verifyAccessToken(tokenID: number, permissionName: string): Promise&lt;GrantStatus&gt;

W
wusongqing 已提交
41 42 43
Checks whether an application has been granted the specified permission. This API uses a promise to return the result.

**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
44 45 46

**Parameters**

47 48 49 50
 | Name | Type | Mandatory | Description |
 | -------- | ------------------- | ---- | ------------------------------------------ |
 | tokenID |  number | Yes | ID of the application. |
 | permissionName | string | Yes | Name of the permission to verify. |
W
wusongqing 已提交
51 52 53

**Return value**

54 55
 | Type | Description |
 | :------------ | :---------------------------------- |
56
 | Promise&lt;GrantStatus&gt; | Promise used to return the result. |
W
wusongqing 已提交
57 58 59

**Example**

W
wusongqing 已提交
60
```js
W
wusongqing 已提交
61
var AtManager = abilityAccessCtrl.createAtManager();
W
wusongqing 已提交
62 63 64 65 66 67 68 69 70 71 72
let tokenID = 0;
let promise = AtManager.verifyAccessToken(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS");
promise.then(data => {
    console.log(`promise: data->${JSON.stringify(data)}`);
});
```

### grantUserGrantedPermission

grantUserGrantedPermission(tokenID: number, permissionName: string, permissionFlag: number): Promise&lt;number&gt;

W
wusongqing 已提交
73 74 75
Grants a user granted permission to an application. This API uses a promise to return the result.

**Required permissions**: ohos.permission.GRANT_SENSITIVE_PERMISSIONS
W
wusongqing 已提交
76

W
wusongqing 已提交
77
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
78 79 80

**Parameters**

81 82 83 84 85
 | Name | Type | Mandatory | Description |
 | --------- | ------------------- | ---- | ------------------------------------------------------------ |
 | tokenID | number | Yes | ID of the application. |
 | permissionName | string | Yes | Name of the permission to grant. |
 | permissionFlag | number | Yes | Permission flag. The value **1** means that a dialog box will still be displayed after the user grants or denies the permission. The value **2** means that no dialog box will be displayed after the user grants or denies the permission. The value **3** means a system permission that cannot be changed. |
W
wusongqing 已提交
86 87 88

**Return value**

89 90
 | Type | Description |
 | :------------ | :---------------------------------- |
91
 | Promise&lt;number&gt; | Promise used to return the result. |
W
wusongqing 已提交
92 93 94

**Example**

W
wusongqing 已提交
95
```js
W
wusongqing 已提交
96
var AtManager = abilityAccessCtrl.createAtManager();
W
wusongqing 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109
let tokenID = 0;
let promise = AtManager.grantUserGrantedPermission(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS");
promise.then(data => {
    console.log(`promise: data->${JSON.stringify(data)}`);
});
```



### grantUserGrantedPermission

grantUserGrantedPermission(tokenID: number, permissionName: string, permissionFlag: number, callback: AsyncCallback&lt;number&gt;): void

W
wusongqing 已提交
110 111 112
Grants a user granted permission to an application. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.GRANT_SENSITIVE_PERMISSIONS
W
wusongqing 已提交
113

W
wusongqing 已提交
114
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
115 116 117

**Parameters**

118 119 120 121 122 123
 | Name | Type | Mandatory | Description |
 | --------- | ------------------- | ---- | ------------------------------------------------------------ |
 | tokenID | number | Yes | ID of the application. |
 | permissionName | string | Yes | Name of the permission to grant. |
 | permissionFlag | number | Yes | Permission flag. The value **1** means that a dialog box will still be displayed after the user grants or denies the permission. The value **2** means that no dialog box will be displayed after the user grants or denies the permission. The value **3** means a system permission that cannot be changed. |
 | callback | AsyncCallback&lt;number&gt; | Yes | Callback used to return the result. |
W
wusongqing 已提交
124 125 126

**Example**

W
wusongqing 已提交
127
```js
W
wusongqing 已提交
128
var AtManager = abilityAccessCtrl.createAtManager();
W
wusongqing 已提交
129 130 131 132 133 134 135 136 137 138 139
let tokenID = 0;
let permissionFlag = 1;
AtManager.grantUserGrantedPermission(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS",permissionFlag, data => {
    console.log(`callback: data->${JSON.stringify(data)}`);
});
```

### revokeUserGrantedPermission

revokeUserGrantedPermission(tokenID: number, permissionName: string, permissionFlag: number): Promise&lt;number&gt;

W
wusongqing 已提交
140 141 142
Revokes a user granted permission given to an application. This API uses a promise to return the result.

**Required permissions**: ohos.permission.REVOKE_SENSITIVE_PERMISSIONS
W
wusongqing 已提交
143

W
wusongqing 已提交
144
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
145 146 147

**Parameters**

148 149 150 151 152
 | Name | Type | Mandatory | Description |
 | --------- | ------------------- | ---- | ------------------------------------------------------------ |
 | tokenID | number | Yes | ID of the application. |
 | permissionName | string | Yes | Name of the permission to revoke. |
 | permissionFlag | number | Yes | Permission flag. The value **1** means that a dialog box will still be displayed after the user grants or denies the permission. The value **2** means that no dialog box will be displayed after the user grants or denies the permission. The value **3** means a system permission that cannot be changed. |
W
wusongqing 已提交
153 154 155

**Return value**

156 157
 | Type | Description |
 | :------------ | :---------------------------------- |
158
 | Promise&lt;number&gt; | Promise used to return the result. |
W
wusongqing 已提交
159 160 161

**Example**

W
wusongqing 已提交
162
```js
W
wusongqing 已提交
163
var AtManager = abilityAccessCtrl.createAtManager();
W
wusongqing 已提交
164 165 166 167 168 169 170 171 172 173 174 175
let tokenID = 0;
let permissionFlag = 1;
let promise = AtManager.revokeUserGrantedPermission(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS", permissionFlag);
promise.then(data => {
    console.log(`promise: data->${JSON.stringify(data)}`);
});
```

### revokeUserGrantedPermission

revokeUserGrantedPermission(tokenID: number, permissionName: string, permissionFlag: number, callback: AsyncCallback&lt;number&gt;): void

W
wusongqing 已提交
176 177 178
Revokes a user granted permission given to an application. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.REVOKE_SENSITIVE_PERMISSIONS
W
wusongqing 已提交
179

W
wusongqing 已提交
180
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
181 182 183

**Parameters**

184 185 186 187 188 189
 | Name | Type | Mandatory | Description |
 | --------- | ------------------- | ---- | ------------------------------------------------------------ |
 | tokenID | number | Yes | ID of the application. |
 | permissionName | string | Yes | Name of the permission to revoke. |
 | permissionFlag | number | Yes | Permission flag. The value **1** means that a dialog box will still be displayed after the user grants or denies the permission. The value **2** means that no dialog box will be displayed after the user grants or denies the permission. The value **3** means a system permission that cannot be changed. |
 | callback | AsyncCallback&lt;number&gt; | Yes | Callback used to return the result. |
W
wusongqing 已提交
190 191 192

**Example**

W
wusongqing 已提交
193
```js
W
wusongqing 已提交
194
var AtManager = abilityAccessCtrl.createAtManager();
W
wusongqing 已提交
195 196 197 198 199 200 201 202 203 204
let tokenID = 0;
AtManager.revokeUserGrantedPermission(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS",permissionFlag, data => {
    console.log(`callback: data->${JSON.stringify(data)}`);
});
```

### getPermissionFlags

getPermissionFlags(tokenID: number, permissionName: string): Promise&lt;number&gt;

W
wusongqing 已提交
205 206
Obtains the flags of the specified permission of a given application. This API uses a promise to return the result.

W
wusongqing 已提交
207 208
**Required permissions**: ohos.permission.GET_SENSITIVE_PERMISSIONS, GRANT_SENSITIVE_PERMISSIONS, or REVOKE_SENSITIVE_PERMISSIONS

W
wusongqing 已提交
209
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
210 211 212

**Parameters**

213 214 215 216
 | Name | Type | Mandatory | Description |
 | --------- | ------------------- | ---- | ------------------------------------------------------------ |
 | tokenID | number | Yes | ID of the application. |
 | permissionName | string | Yes | Name of the permission to query. |
W
wusongqing 已提交
217 218 219

**Return value**

220 221
 | Type | Description |
 | :------------ | :---------------------------------- |
222
 | Promise&lt;number&gt; | Promise used to return the result. |
W
wusongqing 已提交
223 224 225

**Example**

W
wusongqing 已提交
226
```js
W
wusongqing 已提交
227
var AtManager = abilityAccessCtrl.createAtManager();
W
wusongqing 已提交
228 229 230 231 232 233
let tokenID = 0;
let promise = AtManager.getPermissionFlags(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS");
promise.then(data => {
    console.log(`promise: data->${JSON.stringify(data)}`);
});
```
W
wusongqing 已提交
234 235 236 237 238

### GrantStatus

Enumerates the permission grant states.

W
wusongqing 已提交
239
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
240

241 242 243 244
 | Name | Default Value | Description |
 | ----------------------------- | ---------------------- | ----------------------- |
 | PERMISSION_DENIED | -1 | Permission denied. |
 | PERMISSION_GRANTED | 0 | Permission granted. |