1/* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import bundleManager from '@ohos.bundle.bundleManager' 17import Logger from '../model/Logger' 18import abilityAccessCtrl from '@ohos.abilityAccessCtrl' 19import type { Permissions } from '@ohos.abilityAccessCtrl' 20import type common from '@ohos.app.ability.common' 21 22const TAG: string = '[requestPermission]' 23const BUNDLE_NAME: string = 'ohos.samples.abilityaccessctrl' 24 25export class requestModel { 26 private permissions: Array<Permissions> = [] 27 28 constructor(permissions: Array<Permissions>) { 29 this.permissions = permissions 30 } 31 32 async requestPermission(bundleName: string, ctx: common.UIAbilityContext): Promise<void> { 33 Logger.info('requestPermission start') 34 let bundleFlag = 0 35 let appInfo = await bundleManager.getApplicationInfo(bundleName, bundleFlag) 36 let tokenID = appInfo.accessTokenId 37 let atManager = abilityAccessCtrl.createAtManager() 38 Logger.info(TAG, `atManager = ${JSON.stringify(atManager)}`) 39 for (let i = 0; i < this.permissions.length; i++) { 40 let result = atManager.verifyAccessTokenSync(tokenID, this.permissions[i]) 41 Logger.info(TAG, `result = ${JSON.stringify(result)}`) 42 if (result === abilityAccessCtrl.GrantStatus.PERMISSION_DENIED) { 43 await ctx.startAbility({ 44 bundleName: BUNDLE_NAME, 45 abilityName: `VerifyAbility`, 46 parameters: { 47 bundleName: BUNDLE_NAME, 48 permissions: this.permissions[i], 49 tokenID 50 } 51 }) 52 } 53 } 54 } 55}