提交 f3e6d5a3 编写于 作者: Z zhouyan

访问控制指南ArkTS告警整改

Signed-off-by: Nzhouyan <zhouyan160@huawei.com>
Change-Id: I3ff426f9b9d481bea499b69c08b4919515de0b53
上级 db741516
...@@ -173,6 +173,7 @@ ...@@ -173,6 +173,7 @@
```ts ```ts
import bundleManager from '@ohos.bundle.bundleManager'; import bundleManager from '@ohos.bundle.bundleManager';
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl'; import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
import { BusinessError } from '@ohos.base';
async function checkAccessToken(permission: Permissions): Promise<abilityAccessCtrl.GrantStatus> { async function checkAccessToken(permission: Permissions): Promise<abilityAccessCtrl.GrantStatus> {
let atManager = abilityAccessCtrl.createAtManager(); let atManager = abilityAccessCtrl.createAtManager();
...@@ -184,14 +185,16 @@ ...@@ -184,14 +185,16 @@
let bundleInfo: bundleManager.BundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION); let bundleInfo: bundleManager.BundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
let appInfo: bundleManager.ApplicationInfo = bundleInfo.appInfo; let appInfo: bundleManager.ApplicationInfo = bundleInfo.appInfo;
tokenId = appInfo.accessTokenId; tokenId = appInfo.accessTokenId;
} catch (err) { } catch (error) {
let err: BusinessError = error as BusinessError;
console.error(`Failed to get bundle info for self. Code is ${err.code}, message is ${err.message}`); console.error(`Failed to get bundle info for self. Code is ${err.code}, message is ${err.message}`);
} }
// 校验应用是否被授予权限 // 校验应用是否被授予权限
try { try {
grantStatus = await atManager.checkAccessToken(tokenId, permission); grantStatus = await atManager.checkAccessToken(tokenId, permission);
} catch (err) { } catch (error) {
let err: BusinessError = error as BusinessError;
console.error(`Failed to check access token. Code is ${err.code}, message is ${err.message}`); console.error(`Failed to check access token. Code is ${err.code}, message is ${err.message}`);
} }
...@@ -222,12 +225,11 @@ ...@@ -222,12 +225,11 @@
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window'; import window from '@ohos.window';
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl'; import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
import { BusinessError } from '@ohos.base';
const permissions: Array<Permissions> = ['ohos.permission.READ_CALENDAR']; const permissions: Array<Permissions> = ['ohos.permission.READ_CALENDAR'];
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
onWindowStageCreate(windowStage: window.WindowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
// Main window is created, set main page for this ability // Main window is created, set main page for this ability
let context = this.context; let context = this.context;
...@@ -246,7 +248,7 @@ ...@@ -246,7 +248,7 @@
} }
} }
// 授权成功 // 授权成功
}).catch((err) => { }).catch((err: BusinessError) => {
console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`); console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`);
}) })
// ... // ...
...@@ -259,6 +261,7 @@ ...@@ -259,6 +261,7 @@
```typescript ```typescript
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl'; import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
import common from '@ohos.app.ability.common'; import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';
const permissions: Array<Permissions> = ['ohos.permission.READ_CALENDAR']; const permissions: Array<Permissions> = ['ohos.permission.READ_CALENDAR'];
...@@ -281,7 +284,7 @@ ...@@ -281,7 +284,7 @@
} }
} }
// 授权成功 // 授权成功
}).catch((err) => { }).catch((err: BusinessError) => {
console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`); console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`);
}) })
} }
...@@ -297,10 +300,59 @@ ...@@ -297,10 +300,59 @@
调用[requestPermissionsFromUser()](../reference/apis/js-apis-abilityAccessCtrl.md#requestpermissionsfromuser9)方法后,应用程序将等待用户授权的结果。如果用户授权,则可以继续访问目标操作。如果用户拒绝授权,则需要提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限。 调用[requestPermissionsFromUser()](../reference/apis/js-apis-abilityAccessCtrl.md#requestpermissionsfromuser9)方法后,应用程序将等待用户授权的结果。如果用户授权,则可以继续访问目标操作。如果用户拒绝授权,则需要提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限。
ArkTS语法不支持直接使用globalThis,需要通过一个单例的map来做中转。开发者需要:
a. 在EntryAbility.ets中导入构建的单例对象GlobalThis。
```ts
import { GlobalThis } from '../utils/globalThis'; // 需要根据globalThis.ets的路径自行适配
```
b. 在onCreate中添加:
```ts
GlobalThis.getInstance().setContext('context', this.context);
```
> **说明:**
>
> 由于在ts中引入ets文件会有告警提示,需要将EntryAbility.ts的文件后缀修改为EntryAbility.ets,并在module.json5中同步修改。
**globalThis.ets示例代码如下:**
```ts
import common from '@ohos.app.ability.common';
// 构造单例对象
export class GlobalThis {
private constructor() {}
private static instance: GlobalThis;
private _uiContexts = new Map<string, common.UIAbilityContext>();
public static getInstance(): GlobalThis {
if (!GlobalThis.instance) {
GlobalThis.instance = new GlobalThis();
}
return GlobalThis.instance;
}
getContext(key: string): common.UIAbilityContext | undefined {
return this._uiContexts.get(key);
}
setContext(key: string, value: common.UIAbilityContext): void {
this._uiContexts.set(key, value);
}
// 其他需要传递的内容依此扩展
}
```
```ts ```ts
import { BusinessError } from '@ohos.base';
import Want from '@ohos.app.ability.Want';
import { GlobalThis } from '../utils/globalThis';
import common from '@ohos.app.ability.common';
function openPermissionsInSystemSettings(): void { function openPermissionsInSystemSettings(): void {
let context = getContext(this) as common.UIAbilityContext; let context: common.UIAbilityContext = GlobalThis.getInstance().getContext('context');
let wantInfo = { let wantInfo: Want = {
action: 'action.settings.app.info', action: 'action.settings.app.info',
parameters: { parameters: {
settingsParamBundleName: 'com.example.myapplication' // 打开指定应用的详情页面 settingsParamBundleName: 'com.example.myapplication' // 打开指定应用的详情页面
...@@ -308,7 +360,7 @@ ...@@ -308,7 +360,7 @@
} }
context.startAbility(wantInfo).then(() => { context.startAbility(wantInfo).then(() => {
// ... // ...
}).catch((err) => { }).catch((err: BusinessError) => {
// ... // ...
}) })
} }
...@@ -319,6 +371,7 @@ ...@@ -319,6 +371,7 @@
通过调用[requestPermissionsFromUser()](../reference/apis/js-apis-inner-app-context.md#contextrequestpermissionsfromuser7)接口向用户动态申请授权。 通过调用[requestPermissionsFromUser()](../reference/apis/js-apis-inner-app-context.md#contextrequestpermissionsfromuser7)接口向用户动态申请授权。
```js ```js
import { BusinessError } from '@ohos.base';
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
reqPermissions() { reqPermissions() {
...@@ -329,7 +382,7 @@ reqPermissions() { ...@@ -329,7 +382,7 @@ reqPermissions() {
console.log("data:" + JSON.stringify(data)); console.log("data:" + JSON.stringify(data));
console.log("data permissions:" + JSON.stringify(data.permissions)); console.log("data permissions:" + JSON.stringify(data.permissions));
console.log("data result:" + JSON.stringify(data.authResults)); console.log("data result:" + JSON.stringify(data.authResults));
}, (err) => { }, (err: BusinessError) => {
console.error('Failed to start ability', err.code); console.error('Failed to start ability', err.code);
}); });
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册