• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Managing Domain Accounts (for System Applications Only)
2
3<!--Kit: Basic Services Kit-->
4<!--Subsystem: Account-->
5<!--Owner: @steven-q-->
6<!--Designer: @JiDong-CS1-->
7<!--Tester: @zhaimengchao-->
8<!--Adviser: @zengyawen-->
9
10The 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.
11
12## Before You Start
13
141. 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).
15   - ohos.permission.MANAGE_LOCAL_ACCOUNTS
16   - ohos.permission.GET_DOMAIN_ACCOUNTS
17
182. Import the **osAccount** module.
19
20   ```ts
21   import { osAccount, BusinessError } from '@kit.BasicServicesKit';
22   ```
23
243. Obtain an **AccountManager** instance for system accounts.
25
26   ```ts
27   let osAccountMgr = osAccount.getAccountManager();
28   ```
29
30## Checking for a Domain Account
31
32Before 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.
33
34**Procedure**
35
361. Define the domain account to check.
37
38   ```ts
39   let domainAccountInfo: osAccount.DomainAccountInfo = {
40     accountName: 'testAccountName',
41     domain: 'testDomain'
42   }
43   ```
44
452. Use [hasAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#hasaccount10) to check whether the domain account exists.
46
47   ```ts
48   osAccount.DomainAccountManager.hasAccount(domainAccountInfo).then((isAccountExisted: boolean)=>{
49     console.log('execute hasAccount successfully, isAccountExisted:' + JSON.stringify(isAccountExisted));
50   }).catch((err: BusinessError)=>{
51     console.error('execute hasAccount err:' + JSON.stringify(err));
52   });
53   ```
54
55## Adding a Domain Account
56
57The 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.
58
59**Procedure**
60
611. Define domain account information, including the domain name, account name, and account ID (optional).
62
63   ```ts
64   let domainInfo: osAccount.DomainAccountInfo = {
65     domain: 'testDomain',
66     accountName: 'testAccountName'
67   };
68   ```
69
702. Use [createOsAccountForDomain](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#createosaccountfordomain8) to create a domain account on the device.
71
72   ```ts
73   try {
74     osAccountMgr.createOsAccountForDomain(osAccount.OsAccountType.NORMAL, domainInfo,
75     (err: BusinessError, osAccountInfo: osAccount.OsAccountInfo)=>{
76       if (err) {
77        console.error('createOsAccountForDomain exception:' + JSON.stringify(err));
78      } else {
79        console.log('createOsAccountForDomain osAccountInfo:' + JSON.stringify(osAccountInfo));
80      }
81   });
82   } catch (e) {
83   console.error('createOsAccountForDomain exception: ' + JSON.stringify(e));
84   }
85   ```
86
87## Removing a Domain Account
88
89The 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.
90
91**Procedure**
92
931. Use [getOsAccountLocalIdForDomain](../../reference/apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalidfordomain9) to obtain the system account ID based on the domain account information.
94
95   ```ts
96   let domainInfo: osAccount.DomainAccountInfo = {
97       domain: 'testDomain',
98       accountName: 'testAccountName'
99   };
100   let localId: number = 0;
101
102   try {
103     localId = await osAccountMgr.getOsAccountLocalIdForDomain(domainInfo);
104   } catch (err) {
105     console.error('getOsAccountLocalIdForDomain exception: ' + JSON.stringify(err));
106   }
107   ```
108
1092. Use [removeOsAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#removeosaccount) to remove the system account.
110
111   ```ts
112   try {
113     osAccountMgr.removeOsAccount(localId, (err: BusinessError)=>{
114       if (err) {
115           console.error('removeOsAccount failed, error: ' + JSON.stringify(err));
116       } else {
117           console.log('removeOsAccount successfully');
118       }
119     });
120   } catch (err) {
121     console.error('removeOsAccount exception: ' + JSON.stringify(err));
122   }
123   ```
124
125## Obtaining Domain Account Information
126
127After 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.
128
129**Procedure**
130
1311. 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).
132
133   ```ts
134   let options: osAccount.GetDomainAccountInfoOptions = {
135       domain: 'testDomain',
136       accountName: 'testAccountName'
137   }
138   ```
139
1402. Use [getAccountInfo](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#getaccountinfo10) to obtain domain account information.
141
142   ```ts
143   try {
144     osAccount.DomainAccountManager.getAccountInfo(options,
145       (err: BusinessError, result: osAccount.DomainAccountInfo) => {
146       if (err) {
147           console.error('call getAccountInfo failed, error: ' + JSON.stringify(err));
148       } else {
149           console.log('getAccountInfo result: ' + result);
150       }
151     });
152   } catch (err) {
153       console.error('getAccountInfo exception = ' + JSON.stringify(err));
154   }
155   ```
156