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.
Getting Started
-
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 { osAccount, BusinessError } from '@kit.BasicServicesKit';
-
Obtain an AccountManager instance.
let osAccountMgr = 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: osAccount.DomainAccountInfo = {
accountName: 'testAccountName',
domain: 'testDomain'
}
-
Use hasAccount to check whether the domain account exists.
osAccount.DomainAccountManager.hasAccount(domainAccountInfo).then((isAccountExisted: boolean)=>{
console.info('execute hasAccount successfully, isAccountExisted:' + JSON.stringify(isAccountExisted));
// ···
}).catch((err: BusinessError)=>{
console.error(`execute hasAccount code is ${err.code}, message is ${err.message}`);
});
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: osAccount.DomainAccountInfo = {
domain: 'testDomain',
accountName: 'testAccountName'
};
-
Use createOsAccountForDomain to create a domain account on the device.
try {
osAccountMgr.createOsAccountForDomain(osAccount.OsAccountType.NORMAL, domainInfo,
(err: BusinessError, osAccountInfo: osAccount.OsAccountInfo)=>{
if (err) {
console.error(`createOsAccountForDomain exception: code is ${err.code}, message is ${err.message}`);
} else {
console.info('createOsAccountForDomain osAccountInfo:' + JSON.stringify(osAccountInfo));
}
});
} catch (e) {
const err = e as BusinessError;
console.error(`createOsAccountForDomain exception: code is ${err.code}, message is ${err.message}`);
}
Removing a Domain Account
The 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 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: osAccount.DomainAccountInfo = {
domain: 'testDomain',
accountName: 'testAccountName'
};
let localId: number = 0;
try {
localId = await osAccountMgr.getOsAccountLocalIdForDomain(domainInfo);
} catch (e) {
const err = e as BusinessError;
console.error(`getOsAccountLocalIdForDomain exception: code is ${err.code}, message is ${err.message}`);
// ···
}
-
Use removeOsAccount to remove the system account.
try {
osAccountMgr.removeOsAccount(localId, (err: BusinessError)=>{
if (err) {
console.error(`removeOsAccount failed, code is ${err.code}, message is ${err.message}`);
// ···
} else {
console.info('removeOsAccount successfully');
// ···
}
});
} catch (e) {
const err = e as BusinessError;
console.error(`removeOsAccount exception: code is ${err.code}, message is ${err.message}`);
}
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: osAccount.GetDomainAccountInfoOptions = {
domain: 'testDomain',
accountName: 'testAccountName'
}
-
Use getAccountInfo to obtain domain account information.
try {
osAccount.DomainAccountManager.getAccountInfo(options,
(err: BusinessError, result: osAccount.DomainAccountInfo) => {
if (err) {
console.error(`call getAccountInfo failed, code is ${err.code}, message is ${err.message}`);
// ···
} else {
console.info('getAccountInfo result: ' + result);
// ···
}
});
} catch (e) {
const err = e as BusinessError;
console.error(`getAccountInfo exception = code is ${err.code}, message is ${err.message}`);
// ···
}
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 manage-distributed-account-sys
openharmony 鸿蒙 account-overview-sys
openharmony 鸿蒙 auth-domain-account-sys
openharmony 鸿蒙 manage-os-account-credential-sys
openharmony 鸿蒙 manage-os-account-sys
openharmony 鸿蒙 manage-application-account