1/* 2 * Copyright (c) 2022 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 16 17import bundle from '@ohos.bundle' 18import Logger from '../model/Logger' 19import abilityAccessCtrl from '@ohos.abilityAccessCtrl' 20import featureAbility from '@ohos.ability.featureAbility' 21 22const TAG: string = '[requestPermission]' 23const BUNDLE_NAME: string = 'ohos.samples.abilityaccessctrl' 24 25export class requestModel { 26 private permissions: Array<string> = [] 27 28 constructor(permissions: Array<string>) { 29 this.permissions = permissions 30 } 31 32 async requestPermission(bundleName: string) { 33 let bundleFlag = 0 34 let appInfo = await bundle.getApplicationInfo(bundleName, bundleFlag) 35 let tokenID = appInfo.accessTokenId 36 let atManager = abilityAccessCtrl.createAtManager() 37 Logger.info(TAG, `atManager = ${JSON.stringify(atManager)}`) 38 for (let i = 0;i < this.permissions.length; i++) { 39 let result = await atManager.verifyAccessToken(tokenID, this.permissions[i]) 40 Logger.info(TAG, `result = ${JSON.stringify(result)}`) 41 if (result === abilityAccessCtrl.GrantStatus.PERMISSION_DENIED) { 42 Logger.info(TAG, `result = ${JSON.stringify(result)}`) 43 featureAbility.startAbility({ 44 want: { 45 bundleName: BUNDLE_NAME, 46 abilityName: `${BUNDLE_NAME}.VerifyAbility`, 47 parameters: { 48 bundleName: BUNDLE_NAME, 49 permissions: this.permissions[i] 50 } 51 } 52 }) 53 } 54 } 55 } 56}