1e41f4b71Sopenharmony_ci# @ohos.enterprise.accountManager(账户管理)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci本模块提供设备账户管理能力,包括禁止创建本地用户等。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> 本模块接口仅可在Stage模型下使用。
10e41f4b71Sopenharmony_ci>
11e41f4b71Sopenharmony_ci> 本模块接口仅对[设备管理应用](../../mdm/mdm-kit-guide.md#功能介绍)开放,需将设备管理应用激活后调用,实现相应功能。
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci## 导入模块
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci```ts
16e41f4b71Sopenharmony_ciimport { accountManager } from '@kit.MDMKit';
17e41f4b71Sopenharmony_ci```
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci## accountManager.disallowOsAccountAddition
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_cidisallowOsAccountAddition(admin: Want, disallow: boolean, accountId?: number): void
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci指定设备管理应用禁止用户添加账号。
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci**参数:**
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci| 参数名    | 类型                                                    | 必填 | 说明                                                        |
34e41f4b71Sopenharmony_ci| --------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- |
35e41f4b71Sopenharmony_ci| admin     | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 设备管理应用。                                              |
36e41f4b71Sopenharmony_ci| disallow  | boolean                                                 | 是   | 是否禁止创建本地用户,true表示禁止创建,false表示允许创建。 |
37e41f4b71Sopenharmony_ci| accountId | number                                                  | 否   | 用户ID,指定具体用户,取值范围:大于等于0。                 |
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci**错误码**:
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                                     |
44e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
45e41f4b71Sopenharmony_ci| 9200001  | The application is not an administrator application of the device. |
46e41f4b71Sopenharmony_ci| 9200002  | The administrator application does not have permission to manage the device. |
47e41f4b71Sopenharmony_ci| 201      | Permission verification failed. The application does not have the permission required to call the API. |
48e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci**示例:**
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci```ts
53e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit';
54e41f4b71Sopenharmony_cilet wantTemp: Want = {
55e41f4b71Sopenharmony_ci  bundleName: 'com.example.myapplication',
56e41f4b71Sopenharmony_ci  abilityName: 'EntryAbility',
57e41f4b71Sopenharmony_ci};
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_citry {
60e41f4b71Sopenharmony_ci  accountManager.disallowOsAccountAddition(wantTemp, true, 100);
61e41f4b71Sopenharmony_ci  console.info('Succeeded in disallowing os account addition.');
62e41f4b71Sopenharmony_ci} catch (err) {
63e41f4b71Sopenharmony_ci  console.error(`Failed to disallow os account addition. Code: ${err.code}, message: ${err.message}`);
64e41f4b71Sopenharmony_ci}
65e41f4b71Sopenharmony_ci```
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci## accountManager.isOsAccountAdditionDisallowed
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ciisOsAccountAdditionDisallowed(admin: Want, accountId?: number): boolean
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci指定设备管理应用查询是否禁止某用户添加账号。
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci**参数:**
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci| 参数名    | 类型                                                    | 必填 | 说明                                        |
82e41f4b71Sopenharmony_ci| --------- | ------------------------------------------------------- | ---- | ------------------------------------------- |
83e41f4b71Sopenharmony_ci| admin     | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 设备管理应用。                              |
84e41f4b71Sopenharmony_ci| accountId | number                                                  | 否   | 用户ID,指定具体用户,取值范围:大于等于0。 |
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci**返回值:**
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci| 类型    | 说明                                                         |
89e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------------ |
90e41f4b71Sopenharmony_ci| boolean | 返回true表示该用户禁止添加账号,<br/>返回false表示该用户允许添加账号。 |
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci**错误码**:
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                                     |
97e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
98e41f4b71Sopenharmony_ci| 9200001  | The application is not an administrator application of the device. |
99e41f4b71Sopenharmony_ci| 9200002  | The administrator application does not have permission to manage the device. |
100e41f4b71Sopenharmony_ci| 201      | Permission verification failed. The application does not have the permission required to call the API. |
101e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci**示例:**
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci```ts
106e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit';
107e41f4b71Sopenharmony_cilet wantTemp: Want = {
108e41f4b71Sopenharmony_ci  bundleName: 'com.example.myapplication',
109e41f4b71Sopenharmony_ci  abilityName: 'EntryAbility',
110e41f4b71Sopenharmony_ci};
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_citry {
113e41f4b71Sopenharmony_ci  let isDisallowed: boolean = accountManager.isOsAccountAdditionDisallowed(wantTemp, 100);
114e41f4b71Sopenharmony_ci  console.info(`Succeeded in querying the os account addition or not: ${isDisallowed}`);
115e41f4b71Sopenharmony_ci} catch (err) {
116e41f4b71Sopenharmony_ci  console.error(`Failed to query the os account addition or not. Code: ${err.code}, message: ${err.message}`);
117e41f4b71Sopenharmony_ci}
118e41f4b71Sopenharmony_ci```
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci## accountManager.addOsAccountAsync
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ciaddOsAccountAsync(admin: Want, name: string, type: osAccount.OsAccountType): Promise&lt;osAccount.OsAccountInfo&gt;
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci指定设备管理应用后台添加账号。使用promise异步回调。
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci**参数:**
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci| 参数名 | 类型                                                         | 必填 | 说明                                                         |
135e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
136e41f4b71Sopenharmony_ci| admin  | [Want](../apis-ability-kit/js-apis-app-ability-want.md)      | 是   | 设备管理应用。                                               |
137e41f4b71Sopenharmony_ci| name   | string                                                       | 是   | 用户ID,指定具体用户,取值范围:大于等于0。                  |
138e41f4b71Sopenharmony_ci| type   | [osAccount.OsAccountType](../apis-basic-services-kit/js-apis-osAccount.md#osaccounttype) | 是   | 要添加的账号的类型。<br/>取值范围:ADMIN、NORMAL、GUEST。<br/>· ADMIN:管理员账号。<br/>· NORMAL:普通账号。<br/>· GUEST:访客账号。 |
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci**返回值:**
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci| 类型                                                         | 说明                 |
143e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | -------------------- |
144e41f4b71Sopenharmony_ci| [osAccount.OsAccountInfo](../apis-basic-services-kit/js-apis-osAccount.md#osaccounttype) | 返回添加的账号信息。 |
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci**错误码**:
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                                     |
151e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
152e41f4b71Sopenharmony_ci| 9200001  | The application is not an administrator application of the device. |
153e41f4b71Sopenharmony_ci| 9200002  | The administrator application does not have permission to manage the device. |
154e41f4b71Sopenharmony_ci| 9201003  | Failed to add an OS account.                                 |
155e41f4b71Sopenharmony_ci| 201      | Permission verification failed. The application does not have the permission required to call the API. |
156e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci**示例**:
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci```ts
161e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit';
162e41f4b71Sopenharmony_ciimport { BusinessError, osAccount } from '@kit.BasicServicesKit';
163e41f4b71Sopenharmony_cilet wantTemp: Want = {
164e41f4b71Sopenharmony_ci  bundleName: 'com.example.myapplication',
165e41f4b71Sopenharmony_ci  abilityName: 'EntryAbility',
166e41f4b71Sopenharmony_ci};
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ciaccountManager.addOsAccountAsync(wantTemp, "TestAccountName", osAccount.OsAccountType.NORMAL).then((info) => {
169e41f4b71Sopenharmony_ci  console.info(`Succeeded in creating os account: ${JSON.stringify(info)}`);
170e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
171e41f4b71Sopenharmony_ci  console.error(`Failed to creating os account. Code: ${err.code}, message: ${err.message}`);
172e41f4b71Sopenharmony_ci});
173e41f4b71Sopenharmony_ci```
174