提交 92787162 编写于 作者: A Annie_wang

update docs

Signed-off-by: NAnnie_wang <annie.wangli@huawei.com>
上级 25a57275
# Access Control Development # Access Control Development
## Scenario ## When to Use
In this example, the app requires the **ohos.permission.PERMISSION1** and **ohos.permission.PERMISSION2** permissions to implement core functions. In this example, the app requires the **ohos.permission.PERMISSION1** and **ohos.permission.PERMISSION2** permissions to implement core functions.
...@@ -26,7 +26,7 @@ The table below lists only the API used in this guide. For more information, see ...@@ -26,7 +26,7 @@ The table below lists only the API used in this guide. For more information, see
### config.json ### config.json
Declare the permissions required by the app one by one in the **config.json** file. The app cannot obtain a permission if it is not declared in the **config.json** file. Declare the permissions required by the app one by one in the **config.json** file. The app can obtain permissions that have been declared in the **config.json** file.
**Description of config.json** **Description of config.json**
...@@ -107,7 +107,7 @@ In addition to declaring all the permissions in the **config.json** file, you mu ...@@ -107,7 +107,7 @@ In addition to declaring all the permissions in the **config.json** file, you mu
After the permissions are declared, the system grants the system_grant permission during the installation of the app. The user_grant permission must be authorized by the user. After the permissions are declared, the system grants the system_grant permission during the installation of the app. The user_grant permission must be authorized by the user.
Therefore, before invoking the API protected by the **ohos.permission.PERMISSION2 permission**, the app needs to verify whether it has the permission. Therefore, before allowing the app to call the API protected by the **ohos.permission.PERMISSION2** permission, the system needs to verify whether the app has the permission to do so.
If the verification result indicates that the app has the permission, the app can access the target API. Otherwise, the app needs to request user authorization and then proceeds based on the authorization result. For details, see [Access Control Overview](accesstoken-overview.md). If the verification result indicates that the app has the permission, the app can access the target API. Otherwise, the app needs to request user authorization and then proceeds based on the authorization result. For details, see [Access Control Overview](accesstoken-overview.md).
...@@ -119,34 +119,26 @@ If the verification result indicates that the app has the permission, the app ca ...@@ -119,34 +119,26 @@ If the verification result indicates that the app has the permission, the app ca
The procedure is as follows: The procedure is as follows:
1. Obtain the caller's identity tokenId. 1. Obtain the ability context.
2. Determine the permission to be verified. In this example, the permission is **permissionNameUser**. 2. Call **requestPermissionsFromUser** to verify whether the app has required permissions.
3. Call **verifyAccessToken** to verify the permissions of the caller. 3. Proceed based on the permission verification result.
4. Proceed based on the permission verification result.
```js ```js
import {describe, beforeEach, afterEach, it, expect} from 'deccjsunit/index' // OnWindowStageCreate lifecycle of the ability
import abilityAccessCtrl from '@ohos.abilityAccessCtrl' onWindowStageCreate() {
import bundle from '@ohos.bundle' var context = this.context
let array:Array<string> = ["ohos.permission.PERMISSION2"];
async requestPermission() { // requestPermissionsFromUser determines whether to invoke a pop-up window based on the permission authorization status.
var permissionNameUser = "ohos.permission.PERMISSION2"; context.requestPermissionsFromUser(array).then(function(data) {
var bundleFlag = 0; console.log("data type:" + typeof(data));
var tokenID = undefined; console.log("data:" + data);
var userID = 100; console.log("data permissions:" + data.permissions);
var appInfo = await bundle.getApplicationInfo('ohos.acts.security.access_token.normal', bundleFlag, userID); console.log("data result:" + data.authResults);
tokenID = appInfo.accessTokenId; }, (err) => {
console.log("AccessTokenTest accessTokenId:" + appInfo.accessTokenId + ", name:" + appInfo.name console.error('Failed to start ability', err.code);
+ ", bundleName:" + appInfo.bundleName) });
var atManager = abilityAccessCtrl.createAtManager();
var result = await atManager.verifyAccessToken(tokenID, permissionNameUser);
if (result == abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) {
// Execute the operation.
} else {
// Apply for dynamic user authorization using requestPermissionsFromUser.
}
} }
``` ```
> ![icon-note.gif](../public_sys-resources/icon-note.gif)**NOTE**<br/> > **NOTE**<br>
> For details about how to use **requestPermissionsFromUser**, see [API Reference] (../reference/apis/js-apis-ability-context.md#abilitycontextrequestpermissionsfromuser). > For details about how to use **requestPermissionsFromUser**, see [API Reference](../reference/apis/js-apis-ability-context.md#abilitycontextrequestpermissionsfromuser).
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册