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