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

W
wusongqing 已提交
3
The **AbilityAccessCtrl** module provides APIs for application permission management, including authentication, authorization, and revocation.
W
wusongqing 已提交
4 5 6

> **NOTE**
>
W
wusongqing 已提交
7 8 9 10
> 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 已提交
11
```js
W
wusongqing 已提交
12 13 14 15 16 17 18 19 20
import abilityAccessCtrl from '@ohos.abilityAccessCtrl'
```

## abilityAccessCtrl.createAtManager

createAtManager(): AtManager

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

W
wusongqing 已提交
21 22 23
**System capability**: SystemCapability.Security.AccessToken


W
wusongqing 已提交
24 25
**Return value**

W
wusongqing 已提交
26 27 28
| Type| Description|
| -------- | -------- |
| [AtManager](#atmanager) | **AtManager** instance obtained.|
W
wusongqing 已提交
29 30 31

**Example**

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

## AtManager

Implements ability access control.

### verifyAccessToken

verifyAccessToken(tokenID: number, permissionName: string): Promise<GrantStatus>

W
wusongqing 已提交
44 45 46
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 已提交
47 48 49

**Parameters**

W
wusongqing 已提交
50 51
| Name  | Type                | Mandatory| Description                                      |
| -------- | -------------------  | ---- | ------------------------------------------ |
W
wusongqing 已提交
52
| tokenID   |  number   | Yes  | ID of the application. The value can be obtained from [ApplicationInfo](js-apis-bundle-ApplicationInfo.md).             |
W
wusongqing 已提交
53
| permissionName | string | Yes  | Name of the permission to verify.|
W
wusongqing 已提交
54 55 56

**Return value**

W
wusongqing 已提交
57 58 59
| Type         | Description                               |
| :------------ | :---------------------------------- |
| Promise<GrantStatus> | Promise instance used to return the result.|
W
wusongqing 已提交
60 61 62

**Example**

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

W
wusongqing 已提交
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
### verifyAccessTokenSync<sup>9+</sup>

verifyAccessTokenSync(tokenID: number, permissionName: string): GrantStatus

Checks whether an application has been granted the specified permission. This API synchronously returns the result.

**System capability**: SystemCapability.Security.AccessToken

**Parameters**

| Name  | Type                | Mandatory| Description                                      |
| -------- | -------------------  | ---- | ------------------------------------------ |
| tokenID   |  number   | Yes  | ID of the application.             |
| permissionName | string | Yes  | Name of the permission to verify.|

**Return value**

| Type         | Description                               |
| :------------ | :---------------------------------- |
| [GrantStatus](#grantstatus) | Permission grant state.|

**Example**

```js
var AtManager = abilityAccessCtrl.createAtManager();
let tokenID = 0;
let data = verifyAccessTokenSync(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS");
console.log(`data->${JSON.stringify(data)}`);
```

W
wusongqing 已提交
102 103 104 105
### grantUserGrantedPermission

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

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

W
wusongqing 已提交
108
This is a system API.
W
wusongqing 已提交
109

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

W
wusongqing 已提交
112
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
113 114 115

**Parameters**

W
wusongqing 已提交
116 117 118 119 120
| 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 已提交
121 122 123

**Return value**

W
wusongqing 已提交
124 125 126
| Type         | Description                               |
| :------------ | :---------------------------------- |
| Promise&lt;number&gt; | Promise instance used to return the result.|
W
wusongqing 已提交
127 128 129

**Example**

W
wusongqing 已提交
130
```js
W
wusongqing 已提交
131
var AtManager = abilityAccessCtrl.createAtManager();
W
wusongqing 已提交
132
let tokenID = 0;
W
wusongqing 已提交
133
let permissionFlag = 1;
W
wusongqing 已提交
134
let promise = AtManager.grantUserGrantedPermission(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS", permissionFlag);
W
wusongqing 已提交
135 136 137 138 139 140 141 142 143
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 已提交
144 145
Grants a user granted permission to an application. This API uses an asynchronous callback to return the result.

W
wusongqing 已提交
146
This is a system API.
W
wusongqing 已提交
147

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

W
wusongqing 已提交
150
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
151 152 153

**Parameters**

W
wusongqing 已提交
154 155 156 157 158 159
| 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 已提交
160 161 162

**Example**

W
wusongqing 已提交
163
```js
W
wusongqing 已提交
164
var AtManager = abilityAccessCtrl.createAtManager();
W
wusongqing 已提交
165 166
let tokenID = 0;
let permissionFlag = 1;
W
wusongqing 已提交
167 168 169 170 171 172
AtManager.grantUserGrantedPermission(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS", permissionFlag, (err, data) => {
    if (err) {
        console.log(`callback: err->${JSON.stringify(err)}`);
    } else {
        console.log(`callback: data->${JSON.stringify(data)}`);
    }
W
wusongqing 已提交
173 174 175 176 177 178 179
});
```

### revokeUserGrantedPermission

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

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

W
wusongqing 已提交
182
This is a system API.
W
wusongqing 已提交
183

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

W
wusongqing 已提交
186
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
187 188 189

**Parameters**

W
wusongqing 已提交
190 191 192 193 194
| 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 已提交
195 196 197

**Return value**

W
wusongqing 已提交
198 199 200
| Type         | Description                               |
| :------------ | :---------------------------------- |
| Promise&lt;number&gt; | Promise instance used to return the result.|
W
wusongqing 已提交
201 202 203

**Example**

W
wusongqing 已提交
204
```js
W
wusongqing 已提交
205
var AtManager = abilityAccessCtrl.createAtManager();
W
wusongqing 已提交
206 207 208 209 210 211 212 213 214 215 216 217
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 已提交
218 219
Revokes a user granted permission given to an application. This API uses an asynchronous callback to return the result.

W
wusongqing 已提交
220
This is a system API.
W
wusongqing 已提交
221

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

W
wusongqing 已提交
224
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
225 226 227

**Parameters**

W
wusongqing 已提交
228 229 230 231 232 233
| 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 已提交
234 235 236

**Example**

W
wusongqing 已提交
237
```js
W
wusongqing 已提交
238
var AtManager = abilityAccessCtrl.createAtManager();
W
wusongqing 已提交
239
let tokenID = 0;
W
wusongqing 已提交
240
let permissionFlag = 1;
W
wusongqing 已提交
241 242 243 244 245 246
AtManager.revokeUserGrantedPermission(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS", permissionFlag, (err, data) => {
    if (err) {
        console.log(`callback: err->${JSON.stringify(err)}`);
    } else {
        console.log(`callback: data->${JSON.stringify(data)}`);
    }
W
wusongqing 已提交
247 248 249 250 251 252 253
});
```

### getPermissionFlags

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

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

W
wusongqing 已提交
256
This is a system API.
W
wusongqing 已提交
257 258

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

W
wusongqing 已提交
260
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
261 262 263

**Parameters**

W
wusongqing 已提交
264 265 266 267
| Name   | Type               | Mandatory| Description                         |
| --------- | ------------------- | ---- | ------------------------------------------------------------ |
| tokenID      | number              | Yes  | ID of the application.           |
| permissionName | string              | Yes  | Name of the permission to query.|
W
wusongqing 已提交
268 269 270

**Return value**

W
wusongqing 已提交
271 272 273
| Type         | Description                               |
| :------------ | :---------------------------------- |
| Promise&lt;number&gt; | Promise instance used to return the result.|
W
wusongqing 已提交
274 275 276

**Example**

W
wusongqing 已提交
277
```js
W
wusongqing 已提交
278
var AtManager = abilityAccessCtrl.createAtManager();
W
wusongqing 已提交
279 280 281 282 283 284
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 已提交
285 286 287 288 289

### GrantStatus

Enumerates the permission grant states.

W
wusongqing 已提交
290
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
291

W
wusongqing 已提交
292 293 294 295
| Name                         | Default Value                 | Description                   |
| ----------------------------- | ---------------------- | -----------------------  |
| PERMISSION_DENIED             | -1                     | Permission denied.            |
| PERMISSION_GRANTED            | 0                      | Permission granted.            |