• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,{Permissions} from '@ohos.abilityAccessCtrl'
19import common from '@ohos.app.ability.common'
20
21const TAG: string = '[requestPermission]'
22const BUNDLE_NAME: string = 'ohos.samples.abilityaccessctrl'
23
24export class requestModel {
25  private permissions: Array<Permissions> = []
26
27  constructor(permissions: Array<Permissions>) {
28    this.permissions = permissions
29  }
30
31  async requestPermission(bundleName: string, ctx: common.UIAbilityContext) {
32    Logger.info('requestPermission start')
33    let bundleFlag = 0
34    let appInfo = await bundleManager.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 = atManager.verifyAccessTokenSync(tokenID, this.permissions[i])
40      Logger.info(TAG, `result = ${JSON.stringify(result)}`)
41      if (result === abilityAccessCtrl.GrantStatus.PERMISSION_DENIED) {
42        await ctx.startAbility({
43          bundleName: BUNDLE_NAME,
44          abilityName: `VerifyAbility`,
45          parameters: {
46            bundleName: BUNDLE_NAME,
47            permissions: this.permissions[i],
48            tokenID
49          }
50        })
51      }
52    }
53  }
54}