• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.enterprise.accountManager (Account Management) (System API)
2
3The **accountManager** module provides APIs for account management of enterprise devices.
4
5> **NOTE**
6>
7> - The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9> - The APIs of this module can be used only in the stage model.
10>
11> - The APIs of this module can be called only by a [device administrator application](../../mdm/mdm-kit-guide.md#introduction) that is [enabled](js-apis-enterprise-adminManager-sys.md#adminmanagerenableadmin-2).
12>
13> - This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.enterprise.accountManager](js-apis-enterprise-accountManager.md).
14
15## Modules to Import
16
17```ts
18import { accountManager } from '@kit.MDMKit';
19```
20
21## accountManager.disallowAddLocalAccount
22
23disallowAddLocalAccount(admin: Want, disallow: boolean, callback: AsyncCallback<void>): void
24
25Disallows a device to create local user accounts. This API uses an asynchronous callback to return the result.
26
27**Required permissions**: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY
28
29**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
30
31
32
33**Parameters**
34
35| Name     | Type                                      | Mandatory  | Description                      |
36| -------- | ---------------------------------------- | ---- | ------------------------------- |
37| admin    | [Want](../apis-ability-kit/js-apis-app-ability-want.md)     | Yes   | EnterpriseAdminExtensionAbility.     |
38| disallow    | boolean     | Yes   | Whether to forbid the creation of local user accounts. The value **true** means the creation of local user accounts is forbidden, and the value **false** means the opposite.                 |
39| callback | AsyncCallback<void>            | Yes   | Callback invoked to return the result. If the operation is successful, **err** is **null**. Otherwise, **err** is an error object.      |
40
41**Error codes**
42
43For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
44
45| ID| Error Message                                                                      |
46| ------- | ---------------------------------------------------------------------------- |
47| 9200001 | The application is not an administrator application of the device. |
48| 9200002 | The administrator application does not have permission to manage the device. |
49| 201 | Permission verification failed. The application does not have the permission required to call the API. |
50| 202 | Permission verification failed. A non-system application calls a system API. |
51| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
52
53**Example**
54
55```ts
56import { Want } from '@kit.AbilityKit';
57
58let wantTemp: Want = {
59  bundleName: 'com.example.myapplication',
60  abilityName: 'EntryAbility',
61};
62
63accountManager.disallowAddLocalAccount(wantTemp, true, (err) => {
64  if (err) {
65    console.error(`Failed to disallow add local account. Code: ${err.code}, message: ${err.message}`);
66    return;
67  }
68  console.info('Succeeded in disallowing add local account');
69});
70```
71
72## accountManager.disallowAddLocalAccount
73
74disallowAddLocalAccount(admin: Want, disallow: boolean): Promise<void>
75
76Disallows a device to create local user accounts. This API uses a promise to return the result.
77
78**Required permissions**: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY
79
80**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
81
82
83
84**Parameters**
85
86| Name  | Type                                 | Mandatory  | Description     |
87| ----- | ----------------------------------- | ---- | ------- |
88| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes   | EnterpriseAdminExtensionAbility.|
89| disallow    | boolean     | Yes   | Whether to forbid the creation of local user accounts. The value **true** means the creation of local user accounts is forbidden, and the value **false** means the opposite.                 |
90
91**Return value**
92
93| Type                  | Description                     |
94| --------------------- | ------------------------- |
95| Promise<void> | Promise that returns no value. An error object will be thrown if the operation fails.|
96
97**Error codes**
98
99For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
100
101| ID| Error Message                                                                    |
102| ------- | ---------------------------------------------------------------------------- |
103| 9200001 | The application is not an administrator application of the device.            |
104| 9200002 | The administrator application does not have permission to manage the device. |
105| 201 | Permission verification failed. The application does not have the permission required to call the API. |
106| 202 | Permission verification failed. A non-system application calls a system API. |
107| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
108
109**Example**
110
111```ts
112import { Want } from '@kit.AbilityKit';
113import { BusinessError } from '@kit.BasicServicesKit';
114
115let wantTemp: Want = {
116  bundleName: 'com.example.myapplication',
117  abilityName: 'EntryAbility',
118};
119
120accountManager.disallowAddLocalAccount(wantTemp, true).then(() => {
121  console.info('Succeeded in disallowing add local account');
122}).catch((err: BusinessError) => {
123  console.error(`Failed to disallow add local account. Code: ${err.code}, message: ${err.message}`);
124});
125```
126
127## accountManager.disallowAddOsAccountByUser<sup>11+</sup>
128
129disallowAddOsAccountByUser(admin: Want, userId: number, disallow: boolean): void
130
131Disallows a user to add accounts.
132
133**Required permissions**: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY
134
135**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
136
137
138
139**Parameters**
140
141| Name  | Type                                                   | Mandatory| Description                                                       |
142| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- |
143| admin    | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | EnterpriseAdminExtensionAbility.                     |
144| userId   | number                                                  | Yes  | User ID, which must be greater than or equal to 0.                |
145| disallow | boolean                                                 | Yes  | Whether to disallow the user to add system accounts. The value **true** means to disallow the user to add system accounts; the value **false** means the opposite.|
146
147**Error codes**
148
149For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
150
151| ID| Error Message                                                    |
152| -------- | ------------------------------------------------------------ |
153| 9200001  | The application is not an administrator application of the device. |
154| 9200002  | The administrator application does not have permission to manage the device. |
155| 201      | Permission verification failed. The application does not have the permission required to call the API. |
156| 202      | Permission verification failed. A non-system application calls a system API. |
157| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
158
159**Example**
160
161```ts
162import { Want } from '@kit.AbilityKit';
163
164let wantTemp: Want = {
165  bundleName: 'com.example.myapplication',
166  abilityName: 'EntryAbility',
167};
168
169try {
170  accountManager.disallowAddOsAccountByUser(wantTemp, 100, true);
171  console.info(`Succeeded in disallowing user add os account`);
172} catch (err) {
173  console.error(`Failed to disallow user add os account. Code: ${err.code}, message: ${err.message}`);
174}
175```
176
177## accountManager.isAddOsAccountByUserDisallowed<sup>11+</sup>
178
179isAddOsAccountByUserDisallowed(admin: Want, userId: number): boolean
180
181Queries whether to disallow a user to add accounts.
182
183**Required permissions**: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY
184
185**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
186
187
188
189**Parameters**
190
191| Name| Type                                                   | Mandatory| Description                                       |
192| ------ | ------------------------------------------------------- | ---- | ------------------------------------------- |
193| admin  | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | EnterpriseAdminExtensionAbility.     |
194| userId | number                                                  | Yes  | User ID, which must be greater than or equal to 0.|
195
196**Return value**
197
198| Type   | Description                                                        |
199| ------- | ------------------------------------------------------------ |
200| boolean | Returns **true** if the user is not allowed to add system accounts; returns **false** otherwise.|
201
202**Error codes**
203
204For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
205
206| ID| Error Message                                                    |
207| -------- | ------------------------------------------------------------ |
208| 9200001  | The application is not an administrator application of the device. |
209| 9200002  | The administrator application does not have permission to manage the device. |
210| 201      | Permission verification failed. The application does not have the permission required to call the API. |
211| 202      | Permission verification failed. A non-system application calls a system API. |
212| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
213
214**Example**
215
216```ts
217import { Want } from '@kit.AbilityKit';
218
219let wantTemp: Want = {
220  bundleName: 'com.example.myapplication',
221  abilityName: 'EntryAbility',
222};
223
224try {
225  let isDisallowed: boolean = accountManager.isAddOsAccountByUserDisallowed(wantTemp, 100);
226  console.info(`Succeeded in querying the user can add os account or not: ${isDisallowed}`);
227} catch (err) {
228  console.error(`Failed to query the user can add os account or not. Code: ${err.code}, message: ${err.message}`);
229}
230```
231
232## accountManager.addOsAccount<sup>11+</sup>
233
234addOsAccount(admin: Want, name: string, type: osAccount.OsAccountType): osAccount.OsAccountInfo
235
236Adds an account in the background.
237
238**Required permissions**: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY
239
240**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
241
242
243
244**Parameters**
245
246| Name| Type                                                        | Mandatory| Description                                                        |
247| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
248| admin  | [Want](../apis-ability-kit/js-apis-app-ability-want.md)      | Yes  | EnterpriseAdminExtensionAbility.                      |
249| name   | string                                                       | Yes  | User ID, which must be greater than or equal to 0.                 |
250| type   | [osAccount.OsAccountType](../apis-basic-services-kit/js-apis-osAccount.md#osaccounttype) | Yes  | Type of the account to add.<br>The value can be any of the following:<br>· **ADMIN**: administrator account.<br>· **NORMAL**: normal account.<br>· **GUEST**: guest account.|
251
252**Return value**
253
254| Type                                                        | Description                |
255| ------------------------------------------------------------ | -------------------- |
256| [osAccount.OsAccountInfo](../apis-basic-services-kit/js-apis-osAccount.md#osaccounttype) | Information about the account added.|
257
258**Error codes**
259
260For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
261
262| ID| Error Message                                                    |
263| -------- | ------------------------------------------------------------ |
264| 9200001  | The application is not an administrator application of the device. |
265| 9200002  | The administrator application does not have permission to manage the device. |
266| 9201003  | Failed to add an OS account.                                 |
267| 201      | Permission verification failed. The application does not have the permission required to call the API. |
268| 202      | Permission verification failed. A non-system application calls a system API. |
269| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
270
271**Example**
272
273```ts
274import { Want } from '@kit.AbilityKit';
275import { osAccount } from '@kit.BasicServicesKit';
276
277let wantTemp: Want = {
278  bundleName: 'com.example.myapplication',
279  abilityName: 'EntryAbility',
280};
281
282try {
283  let info: osAccount.OsAccountInfo = accountManager.addOsAccount(wantTemp, "TestAccountName", osAccount.OsAccountType.NORMAL);
284  console.info(`Succeeded in creating os account: ${JSON.stringify(info)}`);
285} catch (err) {
286  console.error(`Failed to creating os account. Code: ${err.code}, message: ${err.message}`);
287}
288```
289