harmony 鸿蒙@ohos.application.uriPermissionManager (URI Permission Management)

  • 2023-06-24
  • 浏览 (349)

@ohos.application.uriPermissionManager (URI Permission Management)

NOTE

The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.

The uriPermissionManager module provides APIs for granting permissions on a file to or revoking the granted permission from an application. The file is identified by a uniform resource identifier (URI).

Modules to Import

import uriPermissionManager from '@ohos.application.uriPermissionManager';

uriPermissionManager.grantUriPermission

grantUriPermission(uri: string, flag: wantConstant.Flags, targetBundleName: string, callback: AsyncCallback<number>): void;

Grants permission on the file of the specified URI to an application. This API uses an asynchronous callback to return the result.

By default, an application can authorize its own URIs to another application. If the application has the ohos.permission.PROXY_AUTHORIZATION_URI permission, there is no such restriction. System API: This is a system API and cannot be called by third-party applications.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Required permissions: ohos.permission.PROXY_AUTHORIZATION_URI

Parameters

Name Type Mandatory Description
uri string Yes URI of the file, for example, fileshare:///com.samples.filesharetest.FileShare/person/10.
flag wantConstant.Flags Yes Read or write permission on the file to grant.
targetBundleName string Yes Bundle name of the application, to which the permission is granted.
callback AsyncCallback<number> Yes Callback invoked to return the result. If the operation is successful, 0 is returned; otherwise, -1 is returned.

Error codes

ID Error Message
16000050 Internal error.
16000058 Invalid URI flag.
16000059 Invalid URI type.
16000060 Sandbox application can not grant URI permission.

For details about the error codes, see Ability Error Codes.

Example

  import uriPermissionManager from '@ohos.application.uriPermissionManager';
  import WantConstant from '@ohos.app.ability.wantConstant';
  import fs from '@ohos.file.fs';
  import fileUri from '@ohos.file.fileuri';

  let targetBundleName = 'com.example.test_case1'
  let path = "file://com.example.test_case1/data/storage/el2/base/haps/entry_test/files/newDir";
  fs.mkdir(path, (err) => {
    if (err) {
      console.log("mkdir error"+err.message)
    } else {
      console.log("mkdir succeed")
    }
  });
  let uri = fileUri.getUriFromPath(path);
  uriPermissionManager.grantUriPermission(uri, WantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, targetBundleName, (result) => {
    console.log("result.code = " + result.code)
  })

uriPermissionManager.grantUriPermission

grantUriPermission(uri: string, flag: wantConstant.Flags, targetBundleName: string): Promise<number>

Grants permission on the file of the specified URI to an application. This API uses a promise to return the result.

By default, an application can authorize its own URIs to another application. If the application has the ohos.permission.PROXY_AUTHORIZATION_URI permission, there is no such restriction. System API: This is a system API and cannot be called by third-party applications.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Required permissions: ohos.permission.PROXY_AUTHORIZATION_URI

Parameters

Name Type Mandatory Description
uri string Yes URI of the file, for example, fileshare:///com.samples.filesharetest.FileShare/person/10.
flag wantConstant.Flags Yes Read or write permission on the file to grant.
targetBundleName string Yes Bundle name of the application, to which the permission is granted.

Return value

Type Description
Promise<number> Promise used to return the result. If the operation is successful, 0 is returned; otherwise, -1 is returned.

Error codes

ID Error Message
16000050 Internal error.
16000058 Invalid URI flag.
16000059 Invalid URI type.
16000060 Sandbox application can not grant URI permission.

For details about the error codes, see Ability Error Codes.

Example

  import uriPermissionManager from '@ohos.application.uriPermissionManager';
  import WantConstant from '@ohos.app.ability.wantConstant';
  import fs from '@ohos.file.fs';
  import fileUri from '@ohos.file.fileuri';
  import { BusinessError } from '@ohos.base';

  let targetBundleName = 'com.example.test_case1'
  let path = "file://com.example.test_case1/data/storage/el2/base/haps/entry_test/files/newDir";

  fs.mkdir(path, (err) => {
    if (err) {
      console.log("mkdir error"+err.message)
    } else {
      console.log("mkdir succeed")
    }
  });
  let uri = fileUri.getUriFromPath(path);
  uriPermissionManager.grantUriPermission(uri, WantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, targetBundleName)
    .then((data) => {
      console.log('Verification succeeded.' + data)
    }).catch((error: BusinessError) => {
    console.log('Verification failed.');
  })

uriPermissionManager.revokeUriPermission

revokeUriPermission(uri: string, targetBundleName: string, callback: AsyncCallback<number>): void;

Revokes the permission on the file of the specified URI from an application. This API uses an asynchronous callback to return the result.

By default, only the URIs obtained by the application itself or the URIs authorized by the application to other applications can be revoked. If the application has the ohos.permission.PROXY_AUTHORIZATION_URI permission, there is no such restriction. System API: This is a system API and cannot be called by third-party applications.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Required permissions: ohos.permission.PROXY_AUTHORIZATION_URI

Parameters

Name Type Mandatory Description
uri string Yes URI of the file, for example, fileshare:///com.samples.filesharetest.FileShare/person/10.
targetBundleName string Yes Bundle name of the application, from which the permission is revoked.
callback AsyncCallback<number> Yes Callback invoked to return the result. If the operation is successful, 0 is returned; otherwise, -1 is returned.

Error codes

ID Error Message
16000050 Internal error.
16000059 Invalid URI type.

For details about the error codes, see Ability Error Codes.

Example

  import uriPermissionManager from '@ohos.application.uriPermissionManager';

  let targetBundleName = 'com.example.test_case2'
  let uri = "file://com.example.test_case1/data/storage/el2/base/haps/entry_test/files/newDir"
  uriPermissionManager.revokeUriPermission(uri, targetBundleName, (result) => {
      console.log("result.code = " + result.code)
  }) 

uriPermissionManager.revokeUriPermission

revokeUriPermission(uri: string, targetBundleName: string): Promise<number>

Revokes the permission on the file of the specified URI from an application. This API uses a promise to return the result.

By default, only the URIs obtained by the application itself or the URIs authorized by the application to other applications can be revoked. If the application has the ohos.permission.PROXY_AUTHORIZATION_URI permission, there is no such restriction. System API: This is a system API and cannot be called by third-party applications.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Required permissions: ohos.permission.PROXY_AUTHORIZATION_URI

Parameters

Name Type Mandatory Description
uri string Yes URI of the file, for example, fileshare:///com.samples.filesharetest.FileShare/person/10.
targetBundleName string Yes Bundle name of the application, from which the permission is revoked.

Return value

Type Description
Promise<number> Promise used to return the result. If the operation is successful, 0 is returned; otherwise, -1 is returned.

Error codes

ID Error Message
16000050 Internal error.
16000059 Invalid URI type.

For details about the error codes, see Ability Error Codes.

Example

  import uriPermissionManager from '@ohos.application.uriPermissionManager';
  import { BusinessError } from '@ohos.base';

  let targetBundleName = 'com.example.test_case2'
  let uri = "file://com.example.test_case1/data/storage/el2/base/haps/entry_test/files/newDir"
  uriPermissionManager.revokeUriPermission(uri, targetBundleName)
  .then((data) => {
      console.log('Verification succeeded.' + data)
  }).catch((error: BusinessError) => {
      console.log('Verification failed.');
  })

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞