harmony 鸿蒙管理域账号(仅对系统应用开放)
管理域账号(仅对系统应用开放)
用户可以在系统中添加域账号,后续可以域账号身份登录、使用系统。
开发准备
申请权限,申请流程请参考:申请应用权限。
- ohos.permission.MANAGE_LOCAL_ACCOUNTS
- ohos.permission.GET_DOMAIN_ACCOUNTS
导入系统账号模块。
import { osAccount, BusinessError } from '@kit.BasicServicesKit';
- 获取系统账号管理对象。
let osAccountMgr = osAccount.getAccountManager();
判断指定域账号是否存在
在添加域账号之前,应该先判断域账号是否存在。开发者可以使用hasAccount接口进行判断。
具体开发实例如下:
- 定义待判断的域账号信息。
let domainAccountInfo: osAccount.DomainAccountInfo = {
accountName: 'testAccountName',
domain: 'testDomain'
}
- 调用hasAccount接口。
osAccount.DomainAccountManager.hasAccount(domainAccountInfo).then((isAccountExisted: boolean)=>{
console.log('execute hasAccount successfully, isAccountExisted:' + JSON.stringify(isAccountExisted));
}).catch((err: BusinessError)=>{
console.error('execute hasAccount err:' + JSON.stringify(err));
});
添加域账号
用户在设置中添加其他域账号,允许其他域账号用户使用同一设备。开发者可以使用createOsAccountForDomain完成此操作。
具体开发实例如下:
- 定义域账号信息,指定域名、账号名、账号标识(可选)。
let domainInfo: osAccount.DomainAccountInfo = {
domain: 'testDomain',
accountName: 'testAccountName'
};
- 指定类型和域账号信息,调用createOsAccountForDomain接口在设备上创建一个域账号。
try {
osAccountMgr.createOsAccountForDomain(osAccount.OsAccountType.NORMAL, domainInfo,
(err: BusinessError, osAccountInfo: osAccount.OsAccountInfo)=>{
if (err) {
console.error('createOsAccountForDomain exception:' + JSON.stringify(err));
} else {
console.log('createOsAccountForDomain osAccountInfo:' + JSON.stringify(osAccountInfo));
}
});
} catch (e) {
console.error('createOsAccountForDomain exception: ' + JSON.stringify(e));
}
删除域账号
用户可以删除不再使用的域账号。由于域账号和系统账号是一一绑定关系,开发者可以使用removeOsAccount接口删除与目标域账号绑定的系统账号,进而实现删除域账号。
具体开发实例如下:
- 调用getOsAccountLocalIdForDomain方法,根据域账号信息获取系统账号ID。
let domainInfo: osAccount.DomainAccountInfo = {
domain: 'testDomain',
accountName: 'testAccountName'
};
let localId: number = 0;
try {
localId = await osAccountMgr.getOsAccountLocalIdForDomain(domainInfo);
} catch (err) {
console.error('getOsAccountLocalIdForDomain exception: ' + JSON.stringify(err));
}
- 调用removeOsAccount方法删除域账号。
try {
osAccountMgr.removeOsAccount(localId, (err: BusinessError)=>{
if (err) {
console.error('removeOsAccount failed, error: ' + JSON.stringify(err));
} else {
console.log('removeOsAccount successfully');
}
});
} catch (err) {
console.error('removeOsAccount exception: ' + JSON.stringify(err));
}
查询域账号信息
用户通过身份认证后,可以查询自己或他人的域账号信息。开发者可以使用getAccountInfo接口完成此操作。
具体开发实例如下:
- 定义查询选项,可以指定需要查询的域名和账号名。选项的类型为GetDomainAccountInfoOptions。
let options: osAccount.GetDomainAccountInfoOptions = {
domain: 'testDomain',
accountName: 'testAccountName'
}
- 调用getAccountInfo接口查询域账号信息。
try {
osAccount.DomainAccountManager.getAccountInfo(options,
(err: BusinessError, result: osAccount.DomainAccountInfo) => {
if (err) {
console.error('call getAccountInfo failed, error: ' + JSON.stringify(err));
} else {
console.log('getAccountInfo result: ' + result);
}
});
} catch (err) {
console.error('getAccountInfo exception = ' + JSON.stringify(err));
}
你可能感兴趣的鸿蒙文章
0
赞
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦