set-security-label.md 2.2 KB
Newer Older
A
Annie_wang 已提交
1 2
# Setting the Security Level of a Distributed File

A
Annie_wang 已提交
3
The security capabilities vary with devices. For example, small embedded devices provide fewer security capabilities than tablets. The security requirements also vary with data. For example, personal health information and bank card information are not expected to be accessed by devices of lower security levels. OpenHarmony provides a complete set of data and device classification standards and custom data transfer policies for different devices. For details, see [Access Control by Device and Data Level](../database/access-control-by-device-and-data-level.md).
A
Annie_wang 已提交
4 5 6 7 8 9 10

## Available APIs

For details about the APIs, see [ohos.file.securityLabel](../reference/apis/js-apis-file-securityLabel.md).

**Table 1** APIs

A
Annie_wang 已提交
11
| API| Description| Type| Synchronous Programming| Asynchronous Programming|
A
Annie_wang 已提交
12
| -------- | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
13 14
| setSecurityLabel | Sets a security label for a file.| Method| √ | √ |
| getSecurityLabel | Obtains the security label of a file.| Method| √ | √ |
A
Annie_wang 已提交
15

A
Annie_wang 已提交
16
> **NOTICE**
A
Annie_wang 已提交
17
>
A
Annie_wang 已提交
18
> - In distributed networking, a device can view the files that do not match its security level but cannot access them.
A
Annie_wang 已提交
19
>
A
Annie_wang 已提交
20
> - The default security level of the distributed file system data is S3. Applications can set the security level of files.
A
Annie_wang 已提交
21 22 23

## Development Example

A
Annie_wang 已提交
24 25
Obtain the sandbox path of a file and set the data security label. For details about how to obtain the context, see [Obtaining the Context of UIAbility](../application-models/uiability-usage.md#obtaining-the-context-of-uiability).

A
Annie_wang 已提交
26 27 28

```ts
import securityLabel from '@ohos.file.securityLabel';
A
Annie_wang 已提交
29 30
import { BusinessError } from '@ohos.base';
import common from '@ohos.app.ability.common';
A
Annie_wang 已提交
31

A
Annie_wang 已提交
32
// Obtain the sandbox path of the file.
A
Annie_wang 已提交
33
let context = getContext(this) as common.UIAbilityContext; // Obtain UIAbilityContext.
A
Annie_wang 已提交
34 35 36 37 38 39
let pathDir = context.filesDir;
let filePath = pathDir + '/test.txt';

// Set the data level of the file to S0.
securityLabel.setSecurityLabel(filePath, 's0').then(() => {
  console.info('Succeeded in setSecurityLabeling.');
A
Annie_wang 已提交
40
}).catch((err: BusinessError) => {
A
Annie_wang 已提交
41 42 43
  console.error(`Failed to setSecurityLabel. Code: ${err.code}, message: ${err.message}`);
});
```