• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.enterprise.restrictions (限制类策略)
2
3本模块提供设置通用限制类策略能力。可以全局禁用和解除禁用蓝牙、HDC、USB、Wi-Fi等特性。
4
5> **说明**:
6>
7> 本模块首批接口从API version 12 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9> 本模块接口仅可在Stage模型下使用。
10>
11> 本模块接口仅对[设备管理应用](../../mdm/mdm-kit-guide.md#功能介绍)开放,需将设备管理应用激活后调用,实现相应功能。
12
13## 导入模块
14
15```ts
16import { restrictions } from '@kit.MDMKit';
17```
18
19## restrictions.setDisallowedPolicy
20
21setDisallowedPolicy(admin: Want, feature: string, disallow: boolean): void
22
23设置禁用/启用某特性。
24
25**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS 或者 ohos.permission.PERSONAL_MANAGE_RESTRICTIONS<sup>15+</sup>
26
27**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
28
29**参数:**
30
31| 参数名   | 类型                                                    | 必填 | 说明                                                         |
32| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
33| admin    | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 企业设备管理扩展组件。                                       |
34| feature  | string                                                  | 是   | feature名称。<br/>- bluetooth:设备蓝牙能力。<br/>- modifyDateTime:设备修改系统时间能力,当前仅支持2in1设备使用。<br/>- printer:设备打印能力,当前仅支持2in1设备使用。<br/>- hdc:设备HDC能力。<br/>- microphone:设备麦克风能力。<br/>- fingerprint:设备指纹认证能力。当已经通过[setDisallowedPolicyForAccount](#restrictionssetdisallowedpolicyforaccount14)设置了某用户禁用设备指纹认证能力时,再通过本接口启用设备指纹认证能力,会报策略冲突。<br/>- usb:设备USB能力。禁用后外接的USB设备无法使用,即在当前设备为HOST模式时,无法外接其他DEVICE设备。<br/>  以下三种情况再通过本接口禁用设备USB能力,会报策略冲突。<br/>  1)通过[addAllowedUsbDevices](js-apis-enterprise-usbManager.md#usbmanageraddallowedusbdevices)接口添加了USB设备可用白名单。<br/>  2)通过[setUsbStorageDeviceAccessPolicy](js-apis-enterprise-usbManager.md#usbmanagersetusbstoragedeviceaccesspolicy)接口设置了USB存储设备访问策略为只读/禁用。<br/>  3)通过[addDisallowedUsbDevices](js-apis-enterprise-usbManager.md#usbmanageradddisallowedusbdevices14)接口添加了禁止使用的USB设备类型。<br/>- wifi:设备WIFI能力。<br/>- tethering<sup>14+</sup>:网络共享能力,当前仅支持2in1设备使用。<br/>- inactiveUserFreeze<sup>14+</sup>:非活跃用户运行能力,当前仅支持2in1设备使用。企业空间场景下,系统切换到企业空间用户,个人空间用户属于非活跃用户。<br/>- camera<sup>14+</sup>:设备相机能力。<br/>- mtpClient<sup>18+</sup>:MTP客户端能力,当前仅支持2in1设备使用。MTP(MediaTransferProtocol,媒体传输协议),该协议允许用户在移动设备上线性访问媒体文件。<br/>- mtpServer<sup>18+</sup>:MTP服务端能力,当前仅支持手机、平板、2in1设备使用。<!--RP1--><!--RP1End--> <br/> **说明:** 从API version 15开始,应用申请权限ohos.permission.PERSONAL_MANAGE_RESTRICTIONS并[激活为自带设备管理应用](./js-apis-enterprise-adminManager.md#adminmanagerstartadminprovision15),可以使用此接口设置以下特性:bluetooth、hdc、microphone、usb、wifi、tethering、camera。 |
35| disallow | boolean                                                 | 是   | true表示禁止使用,false表示允许使用。                        |
36
37**错误码**:
38
39以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
40
41| 错误码ID | 错误信息                                                     |
42| -------- | ------------------------------------------------------------ |
43| 9200001  | The application is not an administrator application of the device. |
44| 9200002  | The administrator application does not have permission to manage the device. |
45| 201      | Permission verification failed. The application does not have the permission required to call the API. |
46| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
47
48**示例:**
49
50```ts
51import { Want } from '@kit.AbilityKit';
52let wantTemp: Want = {
53  bundleName: 'com.example.myapplication',
54  abilityName: 'EntryAbility',
55};
56
57try {
58  restrictions.setDisallowedPolicy(wantTemp, 'printer', true);
59  console.info('Succeeded in setting printer disabled');
60} catch (err) {
61  console.error(`Failed to set printer disabled. Code is ${err.code}, message is ${err.message}`);
62}
63```
64
65## restrictions.getDisallowedPolicy
66
67getDisallowedPolicy(admin: Want, feature: string): boolean
68
69获取某特性的禁用状态。
70
71**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS 或者 ohos.permission.PERSONAL_MANAGE_RESTRICTIONS<sup>15+</sup>
72
73**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
74
75**参数:**
76
77| 参数名  | 类型                                                    | 必填 | 说明                                                         |
78| ------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
79| admin   | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 企业设备管理扩展组件。                                       |
80| feature | string                                                  | 是   | feature名称。<br/>- bluetooth:设备蓝牙能力。<br/>- modifyDateTime:设备修改系统时间能力,当前仅支持2in1设备使用。<br/>- printer:设备打印能力,当前仅支持2in1设备使用。<br/>- hdc:设备HDC能力。<br/>- microphone:设备麦克风能力。<br/>- fingerprint:设备指纹认证能力。<br/>- usb:设备USB能力。禁用后外接的USB设备无法使用,即在当前设备为HOST模式时,无法外接其他DEVICE设备。<br/>- wifi:设备WIFI能力。<br/>- tethering<sup>14+</sup>:网络共享能力,当前仅支持2in1设备使用。 <br/>- inactiveUserFreeze<sup>14+</sup>:非活跃用户运行能力,当前仅支持2in1设备使用。企业空间场景下,系统切换到企业空间用户,个人空间用户属于非活跃用户。<br/>- camera<sup>14+</sup>:设备相机能力。<br/>- mtpClient<sup>18+</sup>:MTP客户端能力,当前仅支持2in1设备使用。MTP(MediaTransferProtocol,媒体传输协议),该协议允许用户在移动设备上线性访问媒体文件。<br/>- mtpServer<sup>18+</sup>:MTP服务端能力,当前仅支持手机、平板、2in1设备使用。<!--RP2--><!--RP2End--> <br/> **说明:** 从API version 15开始,应用申请权限ohos.permission.PERSONAL_MANAGE_RESTRICTIONS并[激活为自带设备管理应用](./js-apis-enterprise-adminManager.md#adminmanagerstartadminprovision15),可以使用此接口获取以下特性状态:bluetooth、hdc、microphone、usb、wifi、tethering、camera。 |
81
82**返回值:**
83
84| 类型    | 说明                                                         |
85| ------- | ------------------------------------------------------------ |
86| boolean | 返回true表示feature对应的某种特性被禁用,false表示feature对应的某种特性未被禁用。 |
87
88**错误码**:
89
90以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
91
92| 错误码ID | 错误信息                                                     |
93| -------- | ------------------------------------------------------------ |
94| 9200001  | The application is not an administrator application of the device. |
95| 9200002  | The administrator application does not have permission to manage the device. |
96| 201      | Permission verification failed. The application does not have the permission required to call the API. |
97| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
98
99**示例:**
100
101```ts
102import { Want } from '@kit.AbilityKit';
103let wantTemp: Want = {
104  bundleName: 'com.example.myapplication',
105  abilityName: 'EntryAbility',
106};
107
108try {
109  let result: boolean = restrictions.getDisallowedPolicy(wantTemp, 'printer');
110  console.info(`Succeeded in querying is the printing function disabled : ${result}`);
111} catch (err) {
112  console.error(`Failed to set printer disabled. Code is ${err.code}, message is ${err.message}`);
113}
114```
115
116## restrictions.setDisallowedPolicyForAccount<sup>14+</sup>
117
118setDisallowedPolicyForAccount(admin: Want, feature: string, disallow: boolean, accountId: number): void
119
120设置禁用/启用指定用户的某特性。
121
122**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS
123
124**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
125
126**参数:**
127
128| 参数名   | 类型                                                    | 必填 | 说明                                                         |
129| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
130| admin    | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 企业设备管理扩展组件。                                   |
131| feature  | string                                                  | 是   | feature名称。<br/>- fingerprint:设备指纹认证能力,当前仅支持2in1设备使用。使用此参数时有以下规则:<br/>1. 通过[setDisallowedPolicy](#restrictionssetdisallowedpolicy)接口禁用了设备指纹认证能力,再使用本接口传入此参数,会报策略冲突。<br/>2. 通过本接口设置禁用/启用指定用户的设备指纹认证能力后,再通过[setDisallowedPolicy](#restrictionssetdisallowedpolicy)接口禁用设备指纹认证能力时,后者会覆盖前者的策略。此后再通过[setDisallowedPolicy](#restrictionssetdisallowedpolicy)接口启用设备指纹认证能力,则所有用户都允许使用设备指纹认证能力。 |
132| disallow | boolean                                                 | 是   | true表示禁用,false表示启用。                        |
133| accountId | number                                                 | 是   | 用户ID,取值范围:大于等于0。<br/>accountId可以通过[getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9)等接口来获取。 |
134
135**错误码**:
136
137以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
138
139| 错误码ID | 错误信息                                                     |
140| -------- | ------------------------------------------------------------ |
141| 9200001  | The application is not an administrator application of the device. |
142| 9200002  | the administrator application does not have permission to manage the device. |
143| 9200010  | A conflict policy has been configured.                       |
144| 201      | Permission verification failed. The application does not have the permission required to call the API. |
145| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
146
147**示例:**
148
149```ts
150import { Want } from '@kit.AbilityKit';
151let wantTemp: Want = {
152  bundleName: 'com.example.myapplication',
153  abilityName: 'EntryAbility',
154};
155
156try {
157  restrictions.setDisallowedPolicyForAccount(wantTemp, 'fingerprint', true, 100);
158  console.info('Succeeded in setting fingerprint disabled');
159} catch (err) {
160  console.error(`Failed to set fingerprint disabled. Code is ${err.code}, message is ${err.message}`);
161}
162```
163
164## restrictions.getDisallowedPolicyForAccount<sup>14+</sup>
165
166getDisallowedPolicyForAccount(admin: Want, feature: string, accountId: number): boolean
167
168获取指定用户的某特性状态。
169
170**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS
171
172**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
173
174**参数:**
175
176| 参数名  | 类型                                                    | 必填 | 说明                                                         |
177| ------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
178| admin   | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 企业设备管理扩展组件。                                   |
179| feature | string                                                  | 是   | feature名称。<br/>- fingerprint:设备指纹认证能力,当前仅支持2in1设备使用。使用此参数时有以下规则:当已经通过[setDisallowedPolicyForAccount](#restrictionssetdisallowedpolicyforaccount14)接口设置禁用/启用指定用户的设备指纹认证能力后,再通过[setDisallowedPolicy](#restrictionssetdisallowedpolicy)接口禁用设备指纹认证能力时,后者会覆盖前者的策略。即此时调用本接口结果为false。 |
180| accountId | number                                                 | 是   | 用户ID,取值范围:大于等于0。<br/>accountId可以通过[getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9)等接口来获取。 |
181
182**返回值:**
183
184| 类型    | 说明                                                         |
185| ------- | ------------------------------------------------------------ |
186| boolean | 返回true表示参数feature对应的特性被禁用,false表示参数feature对应的特性未被禁用。 |
187
188**错误码**:
189
190以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
191
192| 错误码ID | 错误信息                                                     |
193| -------- | ------------------------------------------------------------ |
194| 9200001  | The application is not an administrator application of the device. |
195| 9200002  | the administrator application does not have permission to manage the device. |
196| 201      | Permission verification failed. The application does not have the permission required to call the API. |
197| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
198
199**示例:**
200
201```ts
202import { Want } from '@kit.AbilityKit';
203let wantTemp: Want = {
204  bundleName: 'com.example.myapplication',
205  abilityName: 'EntryAbility',
206};
207
208try {
209  let result: boolean = restrictions.getDisallowedPolicyForAccount(wantTemp, 'fingerprint', 100);
210  console.info(`Succeeded in querying is the fingerprint function disabled : ${result}`);
211} catch (err) {
212  console.error(`Failed to set fingerprint disabled. Code is ${err.code}, message is ${err.message}`);
213}
214```
215
216## restrictions.addDisallowedListForAccount<sup>14+</sup>
217
218addDisallowedListForAccount(admin: Want, feature: string, list: Array\<string>, accountId: number): void
219
220为指定用户添加禁止使用某特性的应用名单。
221
222**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS
223
224**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
225
226**参数:**
227
228| 参数名   | 类型                                                    | 必填 | 说明                                                         |
229| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
230| admin    | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 企业设备管理扩展组件。                                   |
231| feature  | string                                                  | 是   | feature名称。<br/>- snapshotSkip:屏幕快照能力。 |
232| list | Array\<string>                                                 | 是   | 包名等内容的名单集合。                      |
233| accountId | number                                                 | 是   | 用户ID,取值范围:大于等于0。<br/>accountId可以通过[getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9)等接口来获取。 |
234
235**错误码**:
236
237以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
238
239| 错误码ID | 错误信息                                                     |
240| -------- | ------------------------------------------------------------ |
241| 9200001  | The application is not an administrator application of the device. |
242| 9200002  | The administrator application does not have permission to manage the device. |                   |
243| 201      | Permission verification failed. The application does not have the permission required to call the API. |
244| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
245
246**示例:**
247
248```ts
249import { Want } from '@kit.AbilityKit';
250let wantTemp: Want = {
251  bundleName: 'com.example.myapplication',
252  abilityName: 'EntryAbility',
253};
254let valueList:Array<string> = ["com.xx.aa.", "com.xx.bb"];
255try {
256  restrictions.addDisallowedListForAccount(wantTemp, 'snapshotSkip', valueList, 100);
257  console.info('Succeeded in adding disallowed snapshotSkip feature');
258} catch (err) {
259  console.error(`Failed to add disallowed snapshotSkip feature. Code is ${err.code}, message is ${err.message}`);
260}
261```
262
263## restrictions.removeDisallowedListForAccount<sup>14+</sup>
264
265removeDisallowedListForAccount(admin: Want, feature: string, list: Array\<string>, accountId: number): void
266
267为指定用户移除禁止使用某特性的应用名单。
268
269**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS
270
271**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
272
273**参数:**
274
275| 参数名   | 类型                                                    | 必填 | 说明                                                         |
276| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
277| admin    | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 企业设备管理扩展组件。                                   |
278| feature  | string                                                  | 是   | feature名称。<br/>- snapshotSkip:屏幕快照能力。 |
279| list | Array\<string>                                                 | 是   | 包名等内容的名单集合。                       |
280| accountId | number                                                 | 是   | 用户ID,取值范围:大于等于0。<br/>accountId可以通过[getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9)等接口来获取。 |
281
282**错误码**:
283
284以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
285
286| 错误码ID | 错误信息                                                     |
287| -------- | ------------------------------------------------------------ |
288| 9200001  | The application is not an administrator application of the device. |
289| 9200002  | The administrator application does not have permission to manage the device. |                    |
290| 201      | Permission verification failed. The application does not have the permission required to call the API. |
291| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
292
293**示例:**
294
295```ts
296import { Want } from '@kit.AbilityKit';
297let wantTemp: Want = {
298  bundleName: 'com.example.myapplication',
299  abilityName: 'EntryAbility',
300};
301let valueList:Array<string> = ["com.xx.aa.", "com.xx.bb"];
302try {
303  restrictions.removeDisallowedListForAccount(wantTemp, 'snapshotSkip', valueList, 100);
304  console.info('Succeeded in removing disallowed snapshotSkip feature');
305} catch (err) {
306  console.error(`Failed to remove disallowed snapshotSkip feature. Code is ${err.code}, message is ${err.message}`);
307}
308```
309
310## restrictions.getDisallowedListForAccount<sup>14+</sup>
311
312getDisallowedListForAccount(admin: Want, feature: string, accountId: number): Array\<string>
313
314获取指定用户禁止使用某特性的应用名单。
315
316**需要权限:** ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS
317
318**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
319
320**参数:**
321
322| 参数名  | 类型                                                    | 必填 | 说明                                                         |
323| ------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
324| admin   | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 企业设备管理扩展组件。                                   |
325| feature | string                                                  | 是   | feature名称。<br/>- snapshotSkip:屏幕快照能力。 |
326| accountId | number                                                 | 是   | 用户ID,取值范围:大于等于0。<br/>accountId可以通过[getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9)等接口来获取。 |
327
328**返回值:**
329
330| 类型    | 说明                                                         |
331| ------- | ------------------------------------------------------------ |
332| Array\<string> | 用户已添加的禁用某特征的应用名单。 |
333
334**错误码**:
335
336以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
337
338| 错误码ID | 错误信息                                                     |
339| -------- | ------------------------------------------------------------ |
340| 9200001  | The application is not an administrator application of the device. |
341| 9200002  | The administrator application does not have permission to manage the device. |
342| 201      | Permission verification failed. The application does not have the permission required to call the API. |
343| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
344
345**示例:**
346
347```ts
348import { Want } from '@kit.AbilityKit';
349let wantTemp: Want = {
350  bundleName: 'com.example.myapplication',
351  abilityName: 'EntryAbility',
352};
353
354try {
355  let result: Array<string> = restrictions.getDisallowedListForAccount(wantTemp, 'snapshotSkip', 100);
356  console.info('Succeeded in querying disallowed list for account');
357} catch (err) {
358  console.error(`Failed to query disallowed list for account. Code is ${err.code}, message is ${err.message}`);
359}
360```
361