1# Managing Domain Accounts (for System Applications Only)
2
3The user can add a domain account to a device so that the domain account user can log in to the system and use the device.
4
5## Before You Start
6
71. Request the following permissions. For details, see [Requesting Permissions for system_basic Applications](../../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications).
8   - ohos.permission.MANAGE_LOCAL_ACCOUNTS
9   - ohos.permission.GET_DOMAIN_ACCOUNTS
10
112. Import the **osAccount** module.
12
13   ```ts
14   import { osAccount, BusinessError } from '@kit.BasicServicesKit';
15   ```
16
173. Obtain an **AccountManager** instance for system accounts.
18
19   ```ts
20   let osAccountMgr = osAccount.getAccountManager();
21   ```
22
23## Checking for a Domain Account
24
25Before adding a domain account, the user may need to check whether the domain account exists. You can use [hasAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#hasaccount10) to check whether a domain account exists.
26
27**Procedure**
28
291. Define the domain account to check.
30
31   ```ts
32   let domainAccountInfo: osAccount.DomainAccountInfo = {
33     accountName: 'testAccountName',
34     domain: 'testDomain'
35   }
36   ```
37
382. Use [hasAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#hasaccount10) to check whether the domain account exists.
39
40   ```ts
41   let isAccountExisted: boolean = await osAccount.DomainAccountManager.hasAccount(domainAccountInfo);
42   ```
43
44## Adding a Domain Account
45
46The user can add a domain account in **Settings** to allow the domain account user to log in to and use the device. You can use [createOsAccountForDomain](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#createosaccountfordomain8) to implement this operation.
47
48**Procedure**
49
501. Define domain account information, including the domain name, account name, and account ID (optional).
51
52   ```ts
53   let domainInfo: osAccount.DomainAccountInfo = {
54     domain: 'testDomain',
55     accountName: 'testAccountName'
56   };
57   ```
58
592. Use [createOsAccountForDomain](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#createosaccountfordomain8) to create a domain account on the device.
60
61   ```ts
62   try {
63     osAccountMgr.createOsAccountForDomain(osAccount.OsAccountType.NORMAL, domainInfo,
64     (err: BusinessError, osAccountInfo: osAccount.OsAccountInfo)=>{
65       console.log('createOsAccountForDomain err:' + JSON.stringify(err));
66       console.log('createOsAccountForDomain osAccountInfo:' + JSON.stringify(osAccountInfo));
67   });
68   } catch (e) {
69   console.log('createOsAccountForDomain exception: ' + JSON.stringify(e));
70   }
71   ```
72
73## Removing a Domain Account
74
75The user can remove the domain account that is not required. Since a domain account is in one-to-one relationship with a system account, you can use [removeOsAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#removeosaccount) to delete the system account. The domain account is deleted as well.
76
77**Procedure**
78
791. Use [getOsAccountLocalIdForDomain](../../reference/apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalidfordomain9) to obtain the system account ID based on the domain account information.
80
81   ```ts
82   let domainInfo: osAccount.DomainAccountInfo = {
83       domain: 'testDomain',
84       accountName: 'testAccountName'
85   };
86   let localId: number = 0;
87
88   try {
89     localId = await osAccountMgr.getOsAccountLocalIdForDomain(domainInfo);
90   } catch (err) {
91     console.log('getOsAccountLocalIdForDomain exception: ' + JSON.stringify(err));
92   }
93   ```
94
952. Use [removeOsAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#removeosaccount) to remove the system account.
96
97   ```ts
98   try {
99     osAccountMgr.removeOsAccount(localId, (err: BusinessError)=>{
100       if (err) {
101           console.log('removeOsAccount failed, error: ' + JSON.stringify(err));
102       } else {
103           console.log('removeOsAccount successfully');
104       }
105     });
106   } catch (err) {
107     console.log('removeOsAccount exception: ' + JSON.stringify(err));
108   }
109   ```
110
111## Obtaining Domain Account Information
112
113After passing the authentication, the user can query their own or others' domain account information. You can use [getAccountInfo](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#getaccountinfo10) to implement this operation.
114
115**Procedure**
116
1171. Specify the query options, including the domain name and account name. The option type is [GetDomainAccountInfoOptions](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#getdomainaccountinfooptions10).
118
119   ```ts
120   let options: osAccount.GetDomainAccountInfoOptions = {
121       domain: 'testDomain',
122       accountName: 'testAccountName'
123   }
124   ```
125
1262. Use [getAccountInfo](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#getaccountinfo10) to obtain domain account information.
127
128   ```ts
129   try {
130     osAccount.DomainAccountManager.getAccountInfo(options,
131       (err: BusinessError, result: osAccount.DomainAccountInfo) => {
132       if (err) {
133           console.log('call getAccountInfo failed, error: ' + JSON.stringify(err));
134       } else {
135           console.log('getAccountInfo result: ' + result);
136       }
137     });
138   } catch (err) {
139       console.log('getAccountInfo exception = ' + JSON.stringify(err));
140   }
141   ```
142