• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.enterprise.accountManager (Account Management)
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 12. 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 that is enabled. For details, see [MDM Kit Development](../../mdm/mdm-kit-guide.md).
12
13## Modules to Import
14
15```ts
16import { accountManager } from '@kit.MDMKit';
17```
18
19## accountManager.disallowOsAccountAddition
20
21disallowOsAccountAddition(admin: Want, disallow: boolean, accountId?: number): void
22
23Users are not allowed to add accounts.
24
25**Required permissions**: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY
26
27**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
28
29
30
31**Parameters**
32
33| Name   | Type                                                   | Mandatory| Description                                                        |
34| --------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
35| admin     | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | EnterpriseAdminExtensionAbility.                                      |
36| 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. |
37| accountId | number                                                  | No  | User ID, which specifies a user. If this parameter is not specified, all users are not allowed to add accounts. If this parameter is specified, specified users are not allowed to add accounts. The value must be greater than or equal to 0.<br>You can call the [getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9) API to obtain the user ID.|
38
39**Error codes**
40
41For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
42
43| ID| Error Message                                                    |
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**Example**
51
52```ts
53import { Want } from '@kit.AbilityKit';
54
55let wantTemp: Want = {
56  bundleName: 'com.example.myapplication',
57  abilityName: 'EntryAbility',
58};
59
60try {
61  accountManager.disallowOsAccountAddition(wantTemp, true, 100);
62  console.info('Succeeded in disallowing os account addition.');
63} catch (err) {
64  console.error(`Failed to disallow os account addition. Code: ${err.code}, message: ${err.message}`);
65}
66```
67
68## accountManager.isOsAccountAdditionDisallowed
69
70isOsAccountAdditionDisallowed(admin: Want, accountId?: number): boolean
71
72Queries whether a user is not allowed to add an account.
73
74**Required permissions**: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY
75
76**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
77
78
79
80**Parameters**
81
82| Name   | Type                                                   | Mandatory| Description                                                        |
83| --------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
84| admin     | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | EnterpriseAdminExtensionAbility.                                      |
85| accountId | number                                                  | No  | User ID, which specifies a user. If this parameter is not specified, the system queries whether all users are not allowed to add accounts. If this parameter is specified, the system queries whether specified users are not allowed to add accounts. The value must be greater than or equal to 0.<br>You can call the [getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9) API to obtain the user ID.|
86
87**Return value**
88
89| Type   | Description                                                      |
90| ------- | ---------------------------------------------------------- |
91| boolean | If **true** is returned, accounts cannot be added.<br>If **false** is returned, the account can be added.|
92
93**Error codes**
94
95For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
96
97| ID| Error Message                                                    |
98| -------- | ------------------------------------------------------------ |
99| 9200001  | The application is not an administrator application of the device. |
100| 9200002  | The administrator application does not have permission to manage the device. |
101| 201      | Permission verification failed. The application does not have the permission required to call the API. |
102| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
103
104**Example**
105
106```ts
107import { Want } from '@kit.AbilityKit';
108
109let wantTemp: Want = {
110  bundleName: 'com.example.myapplication',
111  abilityName: 'EntryAbility',
112};
113
114try {
115  let isDisallowed: boolean = accountManager.isOsAccountAdditionDisallowed(wantTemp, 100);
116  console.info(`Succeeded in querying the os account addition or not: ${isDisallowed}`);
117} catch (err) {
118  console.error(`Failed to query the os account addition or not. Code: ${err.code}, message: ${err.message}`);
119}
120```
121
122## accountManager.addOsAccountAsync
123
124addOsAccountAsync(admin: Want, name: string, type: osAccount.OsAccountType): Promise&lt;osAccount.OsAccountInfo&gt;
125
126Adds an account in the background. This API uses a promise to return the result.
127
128**Required permissions**: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY
129
130**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
131
132
133
134**Parameters**
135
136| Name| Type                                                        | Mandatory| Description                                                        |
137| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
138| admin  | [Want](../apis-ability-kit/js-apis-app-ability-want.md)      | Yes  | EnterpriseAdminExtensionAbility.                                      |
139| name   | string                                                       | Yes  | Account name, which is the name of the account to be added. An account with the same name or an empty name cannot be created.|
140| 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.|
141
142**Return value**
143
144| Type                                                        | Description                |
145| ------------------------------------------------------------ | -------------------- |
146| Promise&lt;[osAccount.OsAccountInfo](../apis-basic-services-kit/js-apis-osAccount.md#osaccountinfo)&gt; | Promise used to return the added account information.|
147
148**Error codes**
149
150For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
151
152| ID| Error Message                                                    |
153| -------- | ------------------------------------------------------------ |
154| 9200001  | The application is not an administrator application of the device. |
155| 9200002  | The administrator application does not have permission to manage the device. |
156| 9201003  | Failed to add an OS account.                                 |
157| 201      | Permission verification failed. The application does not have the permission required to call the API. |
158| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
159
160**Example**
161
162```ts
163import { Want } from '@kit.AbilityKit';
164import { BusinessError, osAccount } from '@kit.BasicServicesKit';
165
166let wantTemp: Want = {
167  bundleName: 'com.example.myapplication',
168  abilityName: 'EntryAbility',
169};
170
171accountManager.addOsAccountAsync(wantTemp, "TestAccountName", osAccount.OsAccountType.NORMAL).then((info) => {
172  console.info(`Succeeded in creating os account: ${JSON.stringify(info)}`);
173}).catch((err: BusinessError) => {
174  console.error(`Failed to creating os account. Code: ${err.code}, message: ${err.message}`);
175});
176```
177## accountManager.setDomainAccountPolicy<sup>19+</sup>
178
179setDomainAccountPolicy(admin: Want, domainAccountInfo: osAccount.DomainAccountInfo, policy: DomainAccountPolicy): void
180
181Sets the domain account policy. This API takes effect only on 2-in-1 devices.
182
183**Required permissions**: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY
184
185**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
186
187**Parameters**
188
189| Name           | Type                                                        | Mandatory| Description                                                        |
190| ----------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
191| admin             | [Want](../apis-ability-kit/js-apis-app-ability-want.md)      | Yes  | EnterpriseAdminExtensionAbility.                                      |
192| domainAccountInfo | [osAccount.DomainAccountInfo](../apis-basic-services-kit/js-apis-osAccount.md#domainaccountinfo8) | Yes  | Domain account information.<br>If the internal attribute of **domainAccountInfo** is empty, a global policy is set for all domain accounts.<br>If the internal attribute of **domainAccountInfo** is not empty, the policy is set for the specified domain account.<br>The priority of the specified domain account policy is higher than that of the global policy. If the specified domain account has a domain account policy, the global policy does not take effect for the domain account.<br>Note: To set a policy for a specified domain account, the **serverConfigId** field in **DomainAccountInfo** is mandatory.|
193| policy            | [DomainAccountPolicy](#domainaccountpolicy19)                | Yes  | Domain account policy.<br>Note: After setting the domain account policy, you must change the domain account password on the device. Otherwise, the **passwordValidityPeriod** and **passwordExpirationNotification** configurations in **DomainAccountPolicy** do not take effect.|
194
195**Error codes**
196
197For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
198
199| ID| Error Message                                                    |
200| -------- | ------------------------------------------------------------ |
201| 9200001  | The application is not an administrator application of the device. |
202| 9200002  | The administrator application does not have permission to manage the device. |
203| 201      | Permission verification failed. The application does not have the permission required to call the API. |
204
205**Example**
206
207```ts
208import { Want } from '@kit.AbilityKit';
209import { BusinessError, osAccount } from '@kit.BasicServicesKit';
210
211async function setDomainAccountPolicy() {
212  let wantTemp: Want = {
213    bundleName: 'com.example.myapplication',
214    abilityName: 'EntryAbility',
215  };
216  let policy: accountManager.DomainAccountPolicy = {
217    authenticationValidityPeriod: 300,
218    passwordValidityPeriod: 420,
219    passwordExpirationNotification: 60,
220  }
221  // Set the global domain account policy.
222  let accountInfo: osAccount.DomainAccountInfo = {
223    domain: '',
224    accountName: '',
225    serverConfigId: '',
226  }
227  try {
228    accountManager.setDomainAccountPolicy(wantTemp, accountInfo, policy);
229    console.info('Succeeded in setting global domainAccount policy.');
230  } catch (err) {
231    console.error(`Failed to set domainAccount policy. Code: ${err.code}, message: ${err.message}`);
232  }
233  // Set the policy for a specified domain account.
234  let accountInfo2: osAccount.DomainAccountInfo = {
235    domain: '',
236    accountName: '',
237    serverConfigId: '',
238  }
239  let userId: number = 100;
240  await osAccount.getAccountManager().getOsAccountDomainInfo(userId)
241    .then((domainAccountInfo: osAccount.DomainAccountInfo) => {
242      accountInfo2 = domainAccountInfo;
243    }).catch((err: BusinessError) => {
244      console.error(`Failed to get account domain info. Code: ${err.code}, message: ${err.message}`);
245    })
246  try {
247    accountManager.setDomainAccountPolicy(wantTemp, accountInfo2, policy);
248    console.info('Succeeded in setting domain account policy.');
249  } catch (err) {
250    console.error(`Failed to set domain account policy. Code: ${err.code}, message: ${err.message}`);
251  }
252}
253```
254
255
256
257## accountManager.getDomainAccountPolicy<sup>19+</sup>
258
259getDomainAccountPolicy(admin: Want, domainAccountInfo: osAccount.DomainAccountInfo): DomainAccountPolicy
260
261Obtains the domain account policy. This API takes effect only on 2-in-1 devices.
262
263**Required permissions**: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY
264
265**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
266
267**Parameters**
268
269| Name           | Type                                                        | Mandatory| Description                                                        |
270| ----------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
271| admin             | [Want](../apis-ability-kit/js-apis-app-ability-want.md)      | Yes  | EnterpriseAdminExtensionAbility.                                      |
272| domainAccountInfo | [osAccount.DomainAccountInfo](../apis-basic-services-kit/js-apis-osAccount.md#domainaccountinfo8) | Yes  | Domain account information.<br>If all the internal attributes of **domainAccountInfo** are empty, the global domain account policy is queried.<br>If the internal attribute of **domainAccountInfo** is not empty, the specified domain account policy is queried.<br>Note: To query a specified domain account policy, the **serverConfigId** field in **DomainAccountInfo** is mandatory.|
273
274**Return value**
275
276| Type                                                        | Description                |
277| ------------------------------------------------------------ | -------------------- |
278| [DomainAccountPolicy](#domainaccountpolicy19) | Domain account policy.|
279
280**Error codes**
281
282For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
283
284| ID| Error Message                                                    |
285| -------- | ------------------------------------------------------------ |
286| 9200001  | The application is not an administrator application of the device. |
287| 9200002  | The administrator application does not have permission to manage the device. |
288| 201      | Permission verification failed. The application does not have the permission required to call the API. |
289
290**Example**
291
292```ts
293import { Want } from '@kit.AbilityKit';
294import { BusinessError, osAccount } from '@kit.BasicServicesKit';
295
296async function getDomainAccountPolicy() {
297  let wantTemp: Want = {
298    bundleName: 'com.example.myapplication',
299    abilityName: 'EntryAbility',
300  };
301  let domainAccountPolicy: accountManager.DomainAccountPolicy = {}
302  // Query the global domain account policy.
303  let accountInfo: osAccount.DomainAccountInfo = {
304    domain: '',
305    accountName: '',
306    serverConfigId: '',
307  }
308  try {
309    domainAccountPolicy = accountManager.getDomainAccountPolicy(wantTemp, accountInfo);
310    console.info('Succeeded in getting global domain account policy.');
311  } catch (err) {
312    console.error(`Failed to get domain account policy. Code: ${err.code}, message: ${err.message}`);
313  }
314  // Query the policy of a specified domain account.
315  let accountInfo2: osAccount.DomainAccountInfo = {
316    domain: '',
317    accountName: '',
318    serverConfigId: '',
319  }
320  let userId: number = 100;
321  await osAccount.getAccountManager()
322    .getOsAccountDomainInfo(userId)
323    .then((domainAccountInfo: osAccount.DomainAccountInfo) => {
324      accountInfo2 = domainAccountInfo;
325    })
326    .catch((err: BusinessError) => {
327      console.error(`Failed to get account domain info. Code: ${err.code}, message: ${err.message}`);
328    })
329  try {
330    domainAccountPolicy = accountManager.getDomainAccountPolicy(wantTemp, accountInfo2);
331    console.info('Succeeded in getting domain account policy.');
332  } catch (err) {
333    console.error(`Failed to get domain account policy. Code: ${err.code}, message: ${err.message}`);
334  }
335}
336```
337
338## DomainAccountPolicy<sup>19+</sup>
339
340Domain account policy.
341
342**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
343
344| Name                          | Type  | Read-Only| Optional| Description                                                        |
345| ------------------------------ | ------ | ---- | ---- |------------------------------------------------------------ |
346| authenticationValidityPeriod   | number | No  | Yes  |Validity period of the domain account authentication token, in seconds. The value range is [-1, 2147483647]. The validity period starts from the time when the domain account is authenticated for the last time, for example, login or unlocking after the screen is locked.<br>The default value is **-1**, indicating that the token is permanently valid. The value **0** indicates that the token becomes invalid immediately. After the token expires or becomes invalid, the domain account and password must be authenticated when a user logs in to the system.|
347| passwordValidityPeriod         | number | No  | Yes  |Validity period of the domain account password, in seconds. The value range is [-1,2147483647]. The validity period starts from the time when the password is last changed on the device.<br>The default value is **-1**, indicating that the domain account password is permanently valid.|
348| passwordExpirationNotification | number | No  | Yes  |Notification period before a domain account password expires, in seconds. The value range is [0, 2147483647].<br>The default value is **0**, indicating that the system does not display a message indicating that the domain account password has expired.<br>Note: **passwordExpirationNotification** must be used together with **passwordValidityPeriod**. When the system time is later than or equal to (the time when the domain account password is last changed on the device + the value of **passwordValidityPeriod** – the value of **passwordExpirationNotification**), a message is displayed, indicating that the password is about to expire.|
349