# @ohos.enterprise.restrictions (限制类策略)
本模块提供设置通用限制类策略能力。可以全局禁用和解除禁用蓝牙、HDC、USB、Wi-Fi等特性。
> **说明**:
>
> 本模块首批接口从API version 12 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
>
> 本模块接口仅可在Stage模型下使用。
>
> 本模块接口仅对[设备管理应用](../../mdm/mdm-kit-guide.md#功能介绍)开放,需将设备管理应用激活后调用,实现相应功能。
## 导入模块
```ts
import { restrictions } from '@kit.MDMKit';
```
## restrictions.setDisallowedPolicy
setDisallowedPolicy(admin: Want, feature: string, disallow: boolean): void
设置禁用/启用某特性。
**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS 或者 ohos.permission.PERSONAL_MANAGE_RESTRICTIONS15+
**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 |
| feature | string | 是 | feature名称。
- bluetooth:设备蓝牙能力。
- modifyDateTime:设备修改系统时间能力,当前仅支持2in1设备使用。
- printer:设备打印能力,当前仅支持2in1设备使用。
- hdc:设备HDC能力。
- microphone:设备麦克风能力。
- fingerprint:设备指纹认证能力。当已经通过[setDisallowedPolicyForAccount](#restrictionssetdisallowedpolicyforaccount14)设置了某用户禁用设备指纹认证能力时,再通过本接口启用设备指纹认证能力,会报策略冲突。
- usb:设备USB能力。禁用后外接的USB设备无法使用,即在当前设备为HOST模式时,无法外接其他DEVICE设备。
以下三种情况再通过本接口禁用设备USB能力,会报策略冲突。
1)通过[addAllowedUsbDevices](js-apis-enterprise-usbManager.md#usbmanageraddallowedusbdevices)接口添加了USB设备可用白名单。
2)通过[setUsbStorageDeviceAccessPolicy](js-apis-enterprise-usbManager.md#usbmanagersetusbstoragedeviceaccesspolicy)接口设置了USB存储设备访问策略为只读/禁用。
3)通过[addDisallowedUsbDevices](js-apis-enterprise-usbManager.md#usbmanageradddisallowedusbdevices14)接口添加了禁止使用的USB设备类型。
- wifi:设备WIFI能力。
- tethering14+:网络共享能力,当前仅支持2in1设备使用。
- inactiveUserFreeze14+:非活跃用户运行能力,当前仅支持2in1设备使用。企业空间场景下,系统切换到企业空间用户,个人空间用户属于非活跃用户。
- camera14+:设备相机能力。
- mtpClient18+:MTP客户端能力,当前仅支持2in1设备使用。MTP(MediaTransferProtocol,媒体传输协议),该协议允许用户在移动设备上线性访问媒体文件。
- mtpServer18+:MTP服务端能力,当前仅支持手机、平板、2in1设备使用。
**说明:** 从API version 15开始,应用申请权限ohos.permission.PERSONAL_MANAGE_RESTRICTIONS并[激活为自带设备管理应用](./js-apis-enterprise-adminManager.md#adminmanagerstartadminprovision15),可以使用此接口设置以下特性:bluetooth、hdc、microphone、usb、wifi、tethering、camera。 |
| disallow | boolean | 是 | true表示禁止使用,false表示允许使用。 |
**错误码**:
以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------------ |
| 9200001 | The application is not an administrator application of the device. |
| 9200002 | The administrator application does not have permission to manage the device. |
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
**示例:**
```ts
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
try {
restrictions.setDisallowedPolicy(wantTemp, 'printer', true);
console.info('Succeeded in setting printer disabled');
} catch (err) {
console.error(`Failed to set printer disabled. Code is ${err.code}, message is ${err.message}`);
}
```
## restrictions.getDisallowedPolicy
getDisallowedPolicy(admin: Want, feature: string): boolean
获取某特性的禁用状态。
**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS 或者 ohos.permission.PERSONAL_MANAGE_RESTRICTIONS15+
**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 |
| feature | string | 是 | feature名称。
- bluetooth:设备蓝牙能力。
- modifyDateTime:设备修改系统时间能力,当前仅支持2in1设备使用。
- printer:设备打印能力,当前仅支持2in1设备使用。
- hdc:设备HDC能力。
- microphone:设备麦克风能力。
- fingerprint:设备指纹认证能力。
- usb:设备USB能力。禁用后外接的USB设备无法使用,即在当前设备为HOST模式时,无法外接其他DEVICE设备。
- wifi:设备WIFI能力。
- tethering14+:网络共享能力,当前仅支持2in1设备使用。
- inactiveUserFreeze14+:非活跃用户运行能力,当前仅支持2in1设备使用。企业空间场景下,系统切换到企业空间用户,个人空间用户属于非活跃用户。
- camera14+:设备相机能力。
- mtpClient18+:MTP客户端能力,当前仅支持2in1设备使用。MTP(MediaTransferProtocol,媒体传输协议),该协议允许用户在移动设备上线性访问媒体文件。
- mtpServer18+:MTP服务端能力,当前仅支持手机、平板、2in1设备使用。
**说明:** 从API version 15开始,应用申请权限ohos.permission.PERSONAL_MANAGE_RESTRICTIONS并[激活为自带设备管理应用](./js-apis-enterprise-adminManager.md#adminmanagerstartadminprovision15),可以使用此接口获取以下特性状态:bluetooth、hdc、microphone、usb、wifi、tethering、camera。 |
**返回值:**
| 类型 | 说明 |
| ------- | ------------------------------------------------------------ |
| boolean | 返回true表示feature对应的某种特性被禁用,false表示feature对应的某种特性未被禁用。 |
**错误码**:
以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------------ |
| 9200001 | The application is not an administrator application of the device. |
| 9200002 | The administrator application does not have permission to manage the device. |
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
**示例:**
```ts
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
try {
let result: boolean = restrictions.getDisallowedPolicy(wantTemp, 'printer');
console.info(`Succeeded in querying is the printing function disabled : ${result}`);
} catch (err) {
console.error(`Failed to set printer disabled. Code is ${err.code}, message is ${err.message}`);
}
```
## restrictions.setDisallowedPolicyForAccount14+
setDisallowedPolicyForAccount(admin: Want, feature: string, disallow: boolean, accountId: number): void
设置禁用/启用指定用户的某特性。
**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS
**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 |
| feature | string | 是 | feature名称。
- fingerprint:设备指纹认证能力,当前仅支持2in1设备使用。使用此参数时有以下规则:
1. 通过[setDisallowedPolicy](#restrictionssetdisallowedpolicy)接口禁用了设备指纹认证能力,再使用本接口传入此参数,会报策略冲突。
2. 通过本接口设置禁用/启用指定用户的设备指纹认证能力后,再通过[setDisallowedPolicy](#restrictionssetdisallowedpolicy)接口禁用设备指纹认证能力时,后者会覆盖前者的策略。此后再通过[setDisallowedPolicy](#restrictionssetdisallowedpolicy)接口启用设备指纹认证能力,则所有用户都允许使用设备指纹认证能力。 |
| disallow | boolean | 是 | true表示禁用,false表示启用。 |
| accountId | number | 是 | 用户ID,取值范围:大于等于0。
accountId可以通过[getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9)等接口来获取。 |
**错误码**:
以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------------ |
| 9200001 | The application is not an administrator application of the device. |
| 9200002 | the administrator application does not have permission to manage the device. |
| 9200010 | A conflict policy has been configured. |
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
**示例:**
```ts
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
try {
restrictions.setDisallowedPolicyForAccount(wantTemp, 'fingerprint', true, 100);
console.info('Succeeded in setting fingerprint disabled');
} catch (err) {
console.error(`Failed to set fingerprint disabled. Code is ${err.code}, message is ${err.message}`);
}
```
## restrictions.getDisallowedPolicyForAccount14+
getDisallowedPolicyForAccount(admin: Want, feature: string, accountId: number): boolean
获取指定用户的某特性状态。
**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS
**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 |
| feature | string | 是 | feature名称。
- fingerprint:设备指纹认证能力,当前仅支持2in1设备使用。使用此参数时有以下规则:当已经通过[setDisallowedPolicyForAccount](#restrictionssetdisallowedpolicyforaccount14)接口设置禁用/启用指定用户的设备指纹认证能力后,再通过[setDisallowedPolicy](#restrictionssetdisallowedpolicy)接口禁用设备指纹认证能力时,后者会覆盖前者的策略。即此时调用本接口结果为false。 |
| accountId | number | 是 | 用户ID,取值范围:大于等于0。
accountId可以通过[getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9)等接口来获取。 |
**返回值:**
| 类型 | 说明 |
| ------- | ------------------------------------------------------------ |
| boolean | 返回true表示参数feature对应的特性被禁用,false表示参数feature对应的特性未被禁用。 |
**错误码**:
以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------------ |
| 9200001 | The application is not an administrator application of the device. |
| 9200002 | the administrator application does not have permission to manage the device. |
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
**示例:**
```ts
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
try {
let result: boolean = restrictions.getDisallowedPolicyForAccount(wantTemp, 'fingerprint', 100);
console.info(`Succeeded in querying is the fingerprint function disabled : ${result}`);
} catch (err) {
console.error(`Failed to set fingerprint disabled. Code is ${err.code}, message is ${err.message}`);
}
```
## restrictions.addDisallowedListForAccount14+
addDisallowedListForAccount(admin: Want, feature: string, list: Array\, accountId: number): void
为指定用户添加禁止使用某特性的应用名单。
**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS
**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 |
| feature | string | 是 | feature名称。
- snapshotSkip:屏幕快照能力。 |
| list | Array\ | 是 | 包名等内容的名单集合。 |
| accountId | number | 是 | 用户ID,取值范围:大于等于0。
accountId可以通过[getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9)等接口来获取。 |
**错误码**:
以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------------ |
| 9200001 | The application is not an administrator application of the device. |
| 9200002 | The administrator application does not have permission to manage the device. | |
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
**示例:**
```ts
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
let valueList:Array = ["com.xx.aa.", "com.xx.bb"];
try {
restrictions.addDisallowedListForAccount(wantTemp, 'snapshotSkip', valueList, 100);
console.info('Succeeded in adding disallowed snapshotSkip feature');
} catch (err) {
console.error(`Failed to add disallowed snapshotSkip feature. Code is ${err.code}, message is ${err.message}`);
}
```
## restrictions.removeDisallowedListForAccount14+
removeDisallowedListForAccount(admin: Want, feature: string, list: Array\, accountId: number): void
为指定用户移除禁止使用某特性的应用名单。
**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS
**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 |
| feature | string | 是 | feature名称。
- snapshotSkip:屏幕快照能力。 |
| list | Array\ | 是 | 包名等内容的名单集合。 |
| accountId | number | 是 | 用户ID,取值范围:大于等于0。
accountId可以通过[getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9)等接口来获取。 |
**错误码**:
以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------------ |
| 9200001 | The application is not an administrator application of the device. |
| 9200002 | The administrator application does not have permission to manage the device. | |
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
**示例:**
```ts
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
let valueList:Array = ["com.xx.aa.", "com.xx.bb"];
try {
restrictions.removeDisallowedListForAccount(wantTemp, 'snapshotSkip', valueList, 100);
console.info('Succeeded in removing disallowed snapshotSkip feature');
} catch (err) {
console.error(`Failed to remove disallowed snapshotSkip feature. Code is ${err.code}, message is ${err.message}`);
}
```
## restrictions.getDisallowedListForAccount14+
getDisallowedListForAccount(admin: Want, feature: string, accountId: number): Array\
获取指定用户禁止使用某特性的应用名单。
**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS
**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 |
| feature | string | 是 | feature名称。
- snapshotSkip:屏幕快照能力。 |
| accountId | number | 是 | 用户ID,取值范围:大于等于0。
accountId可以通过[getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9)等接口来获取。 |
**返回值:**
| 类型 | 说明 |
| ------- | ------------------------------------------------------------ |
| Array\ | 用户已添加的禁用某特征的应用名单。 |
**错误码**:
以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------------ |
| 9200001 | The application is not an administrator application of the device. |
| 9200002 | The administrator application does not have permission to manage the device. |
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
**示例:**
```ts
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
try {
let result: Array = restrictions.getDisallowedListForAccount(wantTemp, 'snapshotSkip', 100);
console.info('Succeeded in querying disallowed list for account');
} catch (err) {
console.error(`Failed to query disallowed list for account. Code is ${err.code}, message is ${err.message}`);
}
```