Managing Domain Accounts (for System Applications Only)
The 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.
Before You Start
-
Request the following permissions. For details, see Requesting Permissions for system_basic Applications.
- ohos.permission.MANAGE_LOCAL_ACCOUNTS
- ohos.permission.GET_DOMAIN_ACCOUNTS
-
Import the osAccount module.
import account_osAccount from '@ohos.account.osAccount'; import { AsyncCallback, BusinessError } from '@ohos.base';
-
Obtain an AccountManager instance of the system account.
let osAccountMgr = account_osAccount.getAccountManager();
Checking for a Domain Account
Before adding a domain account, the user may need to check whether the domain account exists. You can use hasAccount to check whether a domain account exists.
Procedure
-
Define the domain account to check.
let domainAccountInfo: account_osAccount.DomainAccountInfo = { accountName: 'testAccountName', domain: 'testDomain' }
-
Use hasAccount to check whether the domain account exists.
let isAccountExisted: boolean = await account_osAccount.DomainAccountManager.hasAccount(domainAccountInfo);
Adding a Domain Account
The 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 to implement this operation.
Procedure
-
Define domain account information, including the domain name, account name, and account ID (optional).
let domainInfo: account_osAccount.DomainAccountInfo = { domain: 'testDomain', accountName: 'testAccountName' };
-
Use createOsAccountForDomain to create a domain account on the device.
try { accountMgr.createOsAccountForDomain(account_osAccount.OsAccountType.NORMAL, domainInfo, (err: BusinessError, osAccountInfo: account_osAccount.OsAccountInfo)=>{ console.log('createOsAccountForDomain err:' + JSON.stringify(err)); console.log('createOsAccountForDomain osAccountInfo:' + JSON.stringify(osAccountInfo)); }); } catch (e) { console.log('createOsAccountForDomain exception: ' + JSON.stringify(e)); }
Deleting a Domain Account
The 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 to delete the system account. The domain account is deleted as well.
Procedure
-
Use getOsAccountLocalIdForDomain to obtain the system account ID based on the domain account information.
let domainInfo: account_osAccount.DomainAccountInfo = { domain: 'testDomain', accountName: 'testAccountName' }; try { let localId: number = accountMgr.getOsAccountLocalIdForDomain(domainInfo); } catch (err) { console.log('getOsAccountLocalIdForDomain exception: ' + JSON.stringify(err)); }
-
Use removeOsAccount to delete the system account.
try { accountMgr.removeOsAccount(osAccountInfo.localId, (err: BusinessError)=>{ if (err) { console.log('removeOsAccount failed, error: ' + JSON.stringify(err)); } else { console.log('removeOsAccount successfully'); } }); } catch (err) { console.log('removeOsAccount exception: ' + JSON.stringify(err)); }
Obtaining Domain Account Information
After passing the authentication, the user can query their own or others' domain account information. You can use getAccountInfo to implement this operation.
Procedure
-
Specify the query options, including the domain name and account name. The option type is GetDomainAccountInfoOptions.
let options: account_osAccount.GetDomainAccountInfoOptions = { domain: 'testDomain', accountName: 'testAccountName' }
-
Use getAccountInfo to obtain domain account information.
try { account_osAccount.DomainAccountManager.getAccountInfo(domainAccountInfo, (err: BusinessError, result: account_osAccount.DomainAccountInfo) => { if (err) { console.log('call getAccountInfo failed, error: ' + JSON.stringify(err)); } else { console.log('getAccountInfo result: ' + result); } }); } catch (err) { console.log('getAccountInfo exception = ' + JSON.stringify(err)); }