1/**
2 * @file Describe the file
3 * Copyright (c) 2023 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
18import { Log } from '@ohos/common/src/main/ets/utils/Log';
19
20const TAG = 'CalendarUriHelper'
21
22/**
23 * Verify whether the current calling application has this permission by tokenID
24 *
25 * @param tokenId indicates the user's tokenID ,the tokenID can be a string or number
26 * @return the uri string without BundleName and TokenID in case of changing this data.
27 */
28export async function verifyAccessByTokenId(tokenId: string, permissionName: Permissions): Promise<boolean> {
29  let atManager = abilityAccessCtrl.createAtManager();
30  let result: abilityAccessCtrl.GrantStatus = 2;
31  try {
32    result = await atManager.checkAccessToken(Number.parseInt(tokenId), permissionName);
33  } catch (err) {
34    Log.error(TAG, `err=${err?.message}`);
35  }
36
37  if (result === null || result === undefined) {
38    return false;
39  }
40
41  if (result === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) {
42    // Allow the visitor to call the interface provided by the current application
43    Log.debug(TAG, "This application has been authorized to access");
44    return true;
45  }
46  // Prohibit the visitor to call the interface provided by the current application
47  Log.debug(TAG, "This application is NOT authorized to access");
48  return false;
49}