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 account_osAccount from '@ohos.account.osAccount'; 15 import { AsyncCallback, BusinessError } from '@ohos.base'; 16 ``` 17 183. Obtain an **AccountManager** instance of the system account. 19 20 ```ts 21 let osAccountMgr = account_osAccount.getAccountManager(); 22 ``` 23 24## Checking for a Domain Account 25 26Before 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. 27 28**Procedure** 29 301. Define the domain account to check. 31 32 ```ts 33 let domainAccountInfo: account_osAccount.DomainAccountInfo = { 34 accountName: 'testAccountName', 35 domain: 'testDomain' 36 } 37 ``` 38 392. Use [hasAccount](../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#hasaccount10) to check whether the domain account exists. 40 41 ```ts 42 let isAccountExisted: boolean = await account_osAccount.DomainAccountManager.hasAccount(domainAccountInfo); 43 ``` 44 45## Adding a Domain Account 46 47The 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#createosaccountfordomain) to implement this operation. 48 49**Procedure** 50 511. Define domain account information, including the domain name, account name, and account ID (optional). 52 53 ```ts 54 let domainInfo: account_osAccount.DomainAccountInfo = { 55 domain: 'testDomain', 56 accountName: 'testAccountName' 57 }; 58 ``` 59 602. Use [createOsAccountForDomain](../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#createosaccountfordomain) to create a domain account on the device. 61 62 ```ts 63 try { 64 accountMgr.createOsAccountForDomain(account_osAccount.OsAccountType.NORMAL, domainInfo, 65 (err: BusinessError, osAccountInfo: account_osAccount.OsAccountInfo)=>{ 66 console.log('createOsAccountForDomain err:' + JSON.stringify(err)); 67 console.log('createOsAccountForDomain osAccountInfo:' + JSON.stringify(osAccountInfo)); 68 }); 69 } catch (e) { 70 console.log('createOsAccountForDomain exception: ' + JSON.stringify(e)); 71 } 72 ``` 73 74## Deleting a Domain Account 75 76The user can delete 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. 77 78**Procedure** 79 801. Use [getOsAccountLocalIdForDomain](../reference/apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalidfordomain9) to obtain the system account ID based on the domain account information. 81 82 ```ts 83 let domainInfo: account_osAccount.DomainAccountInfo = { 84 domain: 'testDomain', 85 accountName: 'testAccountName' 86 }; 87 88 try { 89 let localId: number = accountMgr.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 delete the system account. 96 97 ```ts 98 try { 99 accountMgr.removeOsAccount(osAccountInfo.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: account_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 account_osAccount.DomainAccountManager.getAccountInfo(domainAccountInfo, 131 (err: BusinessError, result: account_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