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
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:设备指纹认证能力。<br/>- usb:设备USB能力。禁用后外接的USB设备无法使用。<br/>- wifi:设备WIFI能力。<br/>- tethering<sup>14+</sup>:网络共享能力。<br/>- inactiveUserFreeze<sup>14+</sup>:非活跃用户运行能力。企业空间场景下,系统切换到企业空间用户,个人空间用户属于非活跃用户。<br/>- camera<sup>14+</sup>:设备相机能力。<!--RP1--><!--RP1End--> |
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: 'bundleName',
54  abilityName: 'abilityName',
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
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设备无法使用。<br/>- wifi:设备WIFI能力。<br/>- tethering<sup>14+</sup>:网络共享能力。 <br/>- inactiveUserFreeze<sup>14+</sup>:非活跃用户运行能力,当前仅支持2in1使用。企业空间场景下,系统切换到企业空间用户,个人空间用户属于非活跃用户。<br/>- camera<sup>14+</sup>:设备相机能力。 <!--RP2--><!--RP2End--> |
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: 'bundleName',
105  abilityName: 'abilityName',
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设置禁用/启用某用户的某特性,当前仅支持2in1使用。
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:设备指纹认证能力。|
132| disallow | boolean                                                 | 是   | true表示禁用,false表示启用。                        |
133| accountId | number                                                 | 是   | 用户ID,取值范围:大于等于0。                     |
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: 'bundleName',
153  abilityName: 'abilityName',
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获取某用户的某特性状态,当前仅支持2in1使用。
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:设备指纹认证能力。|
180| accountId | number                                                 | 是   | 用户ID,取值范围:大于等于0。                     |
181
182**返回值:**
183
184| 类型    | 说明                                                         |
185| ------- | ------------------------------------------------------------ |
186| boolean | 返回true表示入参对应的用户的特性被禁用,false表示入参对应的用户的特性未被禁用。 |
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: 'bundleName',
205  abilityName: 'abilityName',
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为用户添加禁用某特征的名单,当前仅支持2in1使用。
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                                                  | 是   | 特征名称。<br/>- snapshotSkip:屏幕快照跳过。|
232| list | Array\<string>                                                 | 是   | 包名等内容的名单集合。                      |
233| accountId | number                                                 | 是   | 用户ID,取值范围:大于等于0。                     |
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: 'bundleName',
252  abilityName: 'abilityName',
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为用户移除禁用的某特征的名单,当前仅支持2in1使用。
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                                                  | 是   | 特征名称。<br/>- snapshotSkip:屏幕快照跳过。|
279| list | Array\<string>                                                 | 是   | 包名等内容的名单集合。                       |
280| accountId | number                                                 | 是   | 用户ID,取值范围:大于等于0。                     |
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: 'bundleName',
299  abilityName: 'abilityName',
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获取用户禁用某特征的名单,当前仅支持2in1使用。
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                                                  | 是   | 特征名称。<br/>- snapshotSkip:屏幕快照跳过。|
326| accountId | number                                                 | 是   | 用户ID,取值范围:大于等于0。                     |
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: 'bundleName',
351  abilityName: 'abilityName',
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