harmony 鸿蒙Managing Domain Accounts (for System Applications Only)
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 { osAccount, BusinessError } from '@kit.BasicServicesKit';
- Obtain an AccountManager instance for system accounts.
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.
let isAccountExisted: boolean = await 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: 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)=>{
console.log('createOsAccountForDomain err:' + JSON.stringify(err));
console.log('createOsAccountForDomain osAccountInfo:' + JSON.stringify(osAccountInfo));
});
} catch (e) {
console.log('createOsAccountForDomain exception: ' + JSON.stringify(e));
}
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 (err) {
console.log('getOsAccountLocalIdForDomain exception: ' + JSON.stringify(err));
}
- Use removeOsAccount to remove the system account.
try {
osAccountMgr.removeOsAccount(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: 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.log('call getAccountInfo failed, error: ' + JSON.stringify(err));
} else {
console.log('getAccountInfo result: ' + result);
}
});
} catch (err) {
console.log('getAccountInfo exception = ' + JSON.stringify(err));
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Account Management Overview
harmony 鸿蒙Authenticating a Domain Account (for System Applications Only)
harmony 鸿蒙Applying Constraints for System Accounts
harmony 鸿蒙Managing Application Accounts
harmony 鸿蒙Managing Distributed Accounts (for System Applications Only)
harmony 鸿蒙Managing Domain Account Plugins (for System Applications Only)
harmony 鸿蒙Managing System Account Credentials (for System Application Only)
harmony 鸿蒙Managing System Accounts (for System Applications Only)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦