From 8751225e3d358923dde82c6be3456d921727c5bd Mon Sep 17 00:00:00 2001 From: wangweiyuan Date: Sat, 22 Mar 2025 11:37:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81=E6=A0=87?= =?UTF-8?q?=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangweiyuan --- .../AuthenticationDomainAccount.ets | 18 ++++++ .../DomainAccount/ManageDomainAccounts.ets | 36 +++++++++++ .../ManageDomainAccountsPlugin.ets | 16 +++++ .../entry/src/main/ets/pages/Index.ets | 44 ++++++++++++++ .../entry/src/main/ets/pages/Index.ets | 52 ++++++++++++++++ .../entry/src/main/ets/pages/Index.ets | 60 +++++++++++++++++++ .../SystemAccount/ManageSystemAccounts.ets | 44 ++++++++++++++ .../UseConstraintManagementSystemAccount.ets | 18 ++++++ 8 files changed, 288 insertions(+) diff --git a/code/DocsSample/Account/DomainAccount/entry/src/main/ets/pages/DomainAccount/AuthenticationDomainAccount.ets b/code/DocsSample/Account/DomainAccount/entry/src/main/ets/pages/DomainAccount/AuthenticationDomainAccount.ets index 280fd3861e..38417a1cd6 100755 --- a/code/DocsSample/Account/DomainAccount/entry/src/main/ets/pages/DomainAccount/AuthenticationDomainAccount.ets +++ b/code/DocsSample/Account/DomainAccount/entry/src/main/ets/pages/DomainAccount/AuthenticationDomainAccount.ets @@ -13,7 +13,9 @@ * limitations under the License. */ +// [Start import_the_system_account_module] import { osAccount } from '@kit.BasicServicesKit'; +// [End import_the_system_account_module] import { router } from '@kit.ArkUI'; @Entry @@ -22,35 +24,45 @@ struct AuthenticationDomainAccount { @State message: string = 'No Message'; private async passwordAuthenticateDomainAccount(): Promise { + // [Start get_user_input] let domainAccountInfo: osAccount.DomainAccountInfo = { domain: 'CHINA', accountName: 'zhangsan' }; let credential: Uint8Array = new Uint8Array([0]); + // [End get_user_input] + // [Start define_the_callback_for_the_authentication_result] let callback: osAccount.IUserAuthCallback = { onResult: (resultCode: number, authResult: osAccount.AuthResult) => { console.log('auth resultCode = ' + resultCode); console.log('auth authResult = ' + JSON.stringify(authResult)); + // [StartExclude define_the_callback_for_the_authentication_result] if(resultCode == 0) { this.message = 'Successfully authenticated domain account using password'; } else { this.message = 'Failed to authenticate domain account using password: ' + resultCode; } + // [EndExclude define_the_callback_for_the_authentication_result] } }; + // [End define_the_callback_for_the_authentication_result] + // [Start perform_password_authentication] try { osAccount.DomainAccountManager.auth(domainAccountInfo, credential, callback); } catch (err) { console.error('auth exception = ' + JSON.stringify(err)); } + // [End perform_password_authentication] } private async popupAuthenticateDomainAccount(): Promise { + // [Start define_the_callback_object_of_the_authentication_result] let callback: osAccount.IUserAuthCallback = { onResult: (resultCode: number, authResult: osAccount.AuthResult) => { console.log('authWithPopup resultCode = ' + resultCode); console.log('authWithPopup authResult = ' + JSON.stringify(authResult)); + // [StartExclude define_the_callback_object_of_the_authentication_result] if(resultCode == 0) { this.message = 'Successfully authenticated domain account using popup'; } else if(resultCode == 12300003) { @@ -58,14 +70,20 @@ struct AuthenticationDomainAccount { } else { this.message = 'Failed to authenticate domain account using popup: ' + resultCode; } + // [EndExclude define_the_callback_object_of_the_authentication_result] } } + // [End define_the_callback_object_of_the_authentication_result] + // [Start call_operation_to_authenticate_the_current_domain_account] try { osAccount.DomainAccountManager.authWithPopup(callback) } catch (err) { console.error('authWithPopup exception = ' + JSON.stringify(err)); + // [StartExclude call_operation_to_authenticate_the_current_domain_account] this.message = 'Exception occurred during popup authentication: ' + JSON.stringify(err); + // [EndExclude call_operation_to_authenticate_the_current_domain_account] } + // [End call_operation_to_authenticate_the_current_domain_account] } build() { diff --git a/code/DocsSample/Account/DomainAccount/entry/src/main/ets/pages/DomainAccount/ManageDomainAccounts.ets b/code/DocsSample/Account/DomainAccount/entry/src/main/ets/pages/DomainAccount/ManageDomainAccounts.ets index fe45b632df..dc886e8430 100755 --- a/code/DocsSample/Account/DomainAccount/entry/src/main/ets/pages/DomainAccount/ManageDomainAccounts.ets +++ b/code/DocsSample/Account/DomainAccount/entry/src/main/ets/pages/DomainAccount/ManageDomainAccounts.ets @@ -13,10 +13,14 @@ * limitations under the License. */ +// [Start import_the_system_account_module] import { osAccount, BusinessError } from '@kit.BasicServicesKit'; +// [End import_the_system_account_module] import { router } from '@kit.ArkUI'; +// [Start obtain_the_system_account_management_object] let osAccountMgr = osAccount.getAccountManager(); +// [End obtain_the_system_account_management_object] @Entry @Component @@ -24,12 +28,16 @@ struct ManageDomainAccounts { @State message: string = 'Hello World'; private async domainAccountExists(): Promise { + // [Start define_the_domain_account_information_to_be_determined] let domainAccountInfo: osAccount.DomainAccountInfo = { accountName: 'testAccountName', domain: 'testDomain' } + // [End define_the_domain_account_information_to_be_determined] + // [Start call_the_hasaccount_operation] let isAccountExisted: boolean = await osAccount.DomainAccountManager.hasAccount(domainAccountInfo); + // [End call_the_hasaccount_operation] console.log('domainAccount isAccountExisted:' + isAccountExisted); if(isAccountExisted) { this.message = 'Domain account already exists'; @@ -39,25 +47,34 @@ struct ManageDomainAccounts { } private async createDomainAccount(): Promise { + // [Start define_the_domain_account_information] let domainInfo: osAccount.DomainAccountInfo = { domain: 'testDomain', accountName: 'testAccountName' }; + // [End define_the_domain_account_information] + // [Start create_a_domain_account] 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)); + // [StartExclude create_a_domain_account] this.message = 'Account creation information: ' + JSON.stringify(osAccountInfo.localName); + // [EndExclude create_a_domain_account] }); } catch (e) { console.error('createOsAccountForDomain exception: ' + JSON.stringify(e)); + // [StartExclude create_a_domain_account] this.message = 'Account creation failed: ' + JSON.stringify(e); + // [EndExclude create_a_domain_account] } + // [End create_a_domain_account] } private async deleteDomainAccount(): Promise { + // [Start obtain_the_system_account_id_based_on_the_domain_account_information] let domainInfo: osAccount.DomainAccountInfo = { domain: 'testDomain', accountName: 'testAccountName' @@ -67,44 +84,63 @@ struct ManageDomainAccounts { localId = await osAccountMgr.getOsAccountLocalIdForDomain(domainInfo); } catch (err) { console.error('getOsAccountLocalIdForDomain exception: ' + JSON.stringify(err)); + // [StartExclude obtain_the_system_account_id_based_on_the_domain_account_information] this.message = 'Failed to get account deletion information: ' + JSON.stringify(err); + // [EndExclude obtain_the_system_account_id_based_on_the_domain_account_information] } + // [End obtain_the_system_account_id_based_on_the_domain_account_information] + // [Start delete_the_domain_account] try { osAccountMgr.removeOsAccount(localId, (err: BusinessError)=>{ if (err) { console.log('removeOsAccount failed, error: ' + JSON.stringify(err)); + // [StartExclude delete_the_domain_account] this.message = 'Failed to delete account: ' + JSON.stringify(err); + // [EndExclude delete_the_domain_account] } else { console.log('removeOsAccount successfully'); + // [StartExclude delete_the_domain_account] this.message = 'Successfully deleted account'; + // [EndExclude delete_the_domain_account] } }); } catch (err) { console.error('removeOsAccount exception: ' + JSON.stringify(err)); } + // [End delete_the_domain_account] } private async queryDomainAccount(): Promise { + // [Start define_query_options] let options: osAccount.GetDomainAccountInfoOptions = { domain: 'testDomain', accountName: 'testAccountName' } + // [End define_query_options] + // [Start query_the_domain_account_information] try { osAccount.DomainAccountManager.getAccountInfo(options, (err: BusinessError, result: osAccount.DomainAccountInfo) => { if (err) { console.log('call getAccountInfo failed, error: ' + JSON.stringify(err)); + // [StartExclude query_the_domain_account_information] this.message = 'Query failed: ' + JSON.stringify(err); + // [EndExclude query_the_domain_account_information] } else { console.log('getAccountInfo result: ' + result); + // [StartExclude query_the_domain_account_information] this.message = 'Queried account information: ' + JSON.stringify(result.accountName); + // [EndExclude query_the_domain_account_information] } }); } catch (err) { console.error('getAccountInfo exception = ' + JSON.stringify(err)); + // [StartExclude query_the_domain_account_information] this.message = 'Failed to query account information: ' + JSON.stringify(err); + // [EndExclude query_the_domain_account_information] } + // [End query_the_domain_account_information] } build() { diff --git a/code/DocsSample/Account/DomainAccount/entry/src/main/ets/pages/DomainAccount/ManageDomainAccountsPlugin.ets b/code/DocsSample/Account/DomainAccount/entry/src/main/ets/pages/DomainAccount/ManageDomainAccountsPlugin.ets index 4d4ea4e083..324e91b2a1 100755 --- a/code/DocsSample/Account/DomainAccount/entry/src/main/ets/pages/DomainAccount/ManageDomainAccountsPlugin.ets +++ b/code/DocsSample/Account/DomainAccount/entry/src/main/ets/pages/DomainAccount/ManageDomainAccountsPlugin.ets @@ -13,9 +13,12 @@ * limitations under the License. */ +// [Start import_the_system_account_module] import { osAccount, AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; +// [End import_the_system_account_module] import { router } from '@kit.ArkUI'; +// [Start define_the_plug_in] let plugin: osAccount.DomainPlugin = { auth: (domainAccountInfo: osAccount.DomainAccountInfo, credential: Uint8Array, callback: osAccount.IUserAuthCallback) => { @@ -134,6 +137,7 @@ let plugin: osAccount.DomainPlugin = { callback(code, token); } } +// [End define_the_plug_in] @Entry @@ -142,25 +146,37 @@ struct ManageDomainAccountsPlugin { @State message: string = 'Manage domain account plugin'; private async registrationPlugin(): Promise { + // [Start call_the_api_to_register_the_plug_in] try { osAccount.DomainAccountManager.registerPlugin(plugin) console.info('registerPlugin success'); + // [StartExclude call_the_api_to_register_the_plug_in] this.message = 'Successfully registered plugin'; + // [EndExclude call_the_api_to_register_the_plug_in] } catch (err) { console.error('registerPlugin err: ' + JSON.stringify(err)); + // [StartExclude call_the_api_to_register_the_plug_in] this.message = 'Failed to register plugin: ' + JSON.stringify(err); + // [EndExclude call_the_api_to_register_the_plug_in] } + // [End call_the_api_to_register_the_plug_in] } private async logoutPlugin(): Promise { + // [Start call_the_api_to_log_out_the_plug_in] try { osAccount.DomainAccountManager.unregisterPlugin(); console.log('unregisterPlugin success.'); + // [StartExclude call_the_api_to_log_out_the_plug_in] this.message = 'unregisterPlugin success'; + // [EndExclude call_the_api_to_log_out_the_plug_in] } catch(err) { console.error('unregisterPlugin err:' + JSON.stringify(err)); + // [StartExclude call_the_api_to_log_out_the_plug_in] this.message = 'Failed to unregister plugin: ' + JSON.stringify(err); + // [EndExclude call_the_api_to_log_out_the_plug_in] } + // [End call_the_api_to_log_out_the_plug_in] } build() { diff --git a/code/DocsSample/Account/ManageDistributedAccount/entry/src/main/ets/pages/Index.ets b/code/DocsSample/Account/ManageDistributedAccount/entry/src/main/ets/pages/Index.ets index eeb2fa4530..88bd7546fe 100755 --- a/code/DocsSample/Account/ManageDistributedAccount/entry/src/main/ets/pages/Index.ets +++ b/code/DocsSample/Account/ManageDistributedAccount/entry/src/main/ets/pages/Index.ets @@ -13,9 +13,13 @@ * limitations under the License. */ +// [Start import_the_distributed_account_module] import { distributedAccount, BusinessError } from '@kit.BasicServicesKit'; +// [End import_the_distributed_account_module] +// [Start obtain_the_single-instance_object_of_the_distributed_account] const distributedAccountAbility = distributedAccount.getDistributedAccountAbility(); +// [End obtain_the_single-instance_object_of_the_distributed_account] @Entry @Component @@ -23,84 +27,124 @@ struct Index { @State message: string = 'No Name'; private async bindCurrentManageDistributedAccounts(): Promise { + // [Start define_the_distributed_account_information_to_be_logged_in] let distributedInfo: distributedAccount.DistributedInfo = { name: 'ZhangSan', id: '12345', event: 'Ohos.account.event.LOGIN', }; + // [End define_the_distributed_account_information_to_be_logged_in] + // [Start bind_the_current_system_account_to_the_specified_distributed_account] await distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => { console.log('setOsAccountDistributedInfo successfully'); }).catch((err: BusinessError) => { console.error('setOsAccountDistributedInfo exception: ' + JSON.stringify(err)); + // [StartExclude bind_the_current_system_account_to_the_specified_distributed_account] this.message = 'Failed to bind distributed account: ' + JSON.stringify(err); + // [EndExclude bind_the_current_system_account_to_the_specified_distributed_account] }); + // [End bind_the_current_system_account_to_the_specified_distributed_account] + // [Start view_the_login_information_of_distributed_account] distributedAccountAbility.getOsAccountDistributedInfo().then((data: distributedAccount.DistributedInfo) => { console.log('distributed information: ' + JSON.stringify(data)); + // [StartExclude view_the_login_information_of_distributed_account] this.message = 'Bind distributed account: ' + data.name; + // [EndExclude view_the_login_information_of_distributed_account] }).catch((err: BusinessError) => { console.error('getOsAccountDistributedInfo exception: ' + JSON.stringify(err)); + // [StartExclude view_the_login_information_of_distributed_account] this.message = 'Failed to get distributed account: ' + JSON.stringify(err); + // [EndExclude view_the_login_information_of_distributed_account] }); + // [End view_the_login_information_of_distributed_account] } private async unbindCurrentManageDistributedAccounts(): Promise { + // [Start define_the_distributed_account_information_to_be_logged_out] let distributedInfo: distributedAccount.DistributedInfo = { name: 'ZhangSan', id: '12345', event: 'Ohos.account.event.LOGOUT', }; + // [End define_the_distributed_account_information_to_be_logged_out] + // [Start unbind_the_specified_distributed_account_from_the_current_system_account] distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => { console.log('setOsAccountDistributedInfo successfully'); + // [StartExclude unbind_the_specified_distributed_account_from_the_current_system_account] this.message = 'Successfully unbound distributed account' + // [EndExclude unbind_the_specified_distributed_account_from_the_current_system_account] }).catch((err: BusinessError) => { console.error('setOsAccountDistributedInfo exception: ' + JSON.stringify(err)); + // [StartExclude unbind_the_specified_distributed_account_from_the_current_system_account] this.message = 'Failed to unbind distributed account' + // [EndExclude unbind_the_specified_distributed_account_from_the_current_system_account] }); + // [End unbind_the_specified_distributed_account_from_the_current_system_account] } private async bindSpecifyManageDistributedAccounts(): Promise { + // [Start determine_the_target_system_account_and_define_the_distributed_account_information_to_be_logged_in] let localId: number = 100; let distributedInfo: distributedAccount.DistributedInfo = { name: 'ZhangSan', id: '12345', event: 'Ohos.account.event.LOGIN', }; + // [End determine_the_target_system_account_and_define_the_distributed_account_information_to_be_logged_in] + // [Start bind_the_specified_distributed_account_to_the_current_system_account] await distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => { console.log('setOsAccountDistributedInfoByLocalId successfully'); }).catch((err: BusinessError) => { console.error('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); + // [StartExclude bind_the_specified_distributed_account_to_the_current_system_account] this.message = 'Failed to bind distributed account on the specified system account: ' + JSON.stringify(err); + // [EndExclude bind_the_specified_distributed_account_to_the_current_system_account] }); + // [End bind_the_specified_distributed_account_to_the_current_system_account] + // [Start view_the_login_information_of_a_distributed_account] distributedAccountAbility.getOsAccountDistributedInfoByLocalId(localId) .then((data: distributedAccount.DistributedInfo) => { console.log('distributed information: ' + JSON.stringify(data)); + // [StartExclude view_the_login_information_of_a_distributed_account] this.message = 'Bind distributed account on the specified system account: ' + data.name; + // [EndExclude view_the_login_information_of_a_distributed_account] }).catch((err: BusinessError) => { console.error('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); + // [StartExclude view_the_login_information_of_a_distributed_account] this.message = 'Failed to get distributed account on the specified system account: ' + JSON.stringify(err); + // [EndExclude view_the_login_information_of_a_distributed_account] }); + // [End view_the_login_information_of_a_distributed_account] } private async unbindSpecifyManageDistributedAccounts(): Promise { + // [Start determine_the_target_system_account_and_define_the_distributed_account_information_to_be_logged_out] let localId: number = 100; let distributedInfo: distributedAccount.DistributedInfo = { name: 'ZhangSan', id: '12345', event: 'Ohos.account.event.LOGOUT', }; + // [End determine_the_target_system_account_and_define_the_distributed_account_information_to_be_logged_out] + // [Start unbind_the_specified_distributed_account_from_the_target_system_account] distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => { console.log('setOsAccountDistributedInfoByLocalId successfully'); + // [StartExclude unbind_the_specified_distributed_account_from_the_target_system_account] this.message = 'Successfully logged out and unbound distributed account on the specified system account' + // [EndExclude unbind_the_specified_distributed_account_from_the_target_system_account] }).catch((err: BusinessError) => { console.error('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); + // [StartExclude unbind_the_specified_distributed_account_from_the_target_system_account] this.message = 'Failed to log out and unbind distributed account on the specified system account' + // [EndExclude unbind_the_specified_distributed_account_from_the_target_system_account] }); + // [End unbind_the_specified_distributed_account_from_the_target_system_account] } build() { diff --git a/code/DocsSample/Account/ManageSystemAccountCredentials/entry/src/main/ets/pages/Index.ets b/code/DocsSample/Account/ManageSystemAccountCredentials/entry/src/main/ets/pages/Index.ets index 3a6a053fc3..053b9735e6 100755 --- a/code/DocsSample/Account/ManageSystemAccountCredentials/entry/src/main/ets/pages/Index.ets +++ b/code/DocsSample/Account/ManageSystemAccountCredentials/entry/src/main/ets/pages/Index.ets @@ -13,7 +13,9 @@ * limitations under the License. */ +// [Start import_system_account_module] import { osAccount } from '@kit.BasicServicesKit'; +// [End import_system_account_module] import account_osAccount from "@ohos.account.osAccount"; function strToUint8Arr(str: string): Uint8Array { @@ -46,18 +48,24 @@ struct SystemAccount { private async addCred(): Promise { try { this.cid = await account_osAccount.getAccountManager().getForegroundOsAccountLocalId(); + // [Start create_credential_management_object] let userIDM = new account_osAccount.UserIdentityManager(); + // [End create_credential_management_object] let password: Uint8Array = new Uint8Array([0x31, 0x32, 0x33, 0x34, 0x35, 0x36]); if (this.textPassword != '') { password = strToUint8Arr(this.textPassword); } let PINAuth = new account_osAccount.PINAuth(); + // [Start define_pin_credential_information] let credentialInfo :account_osAccount.CredentialInfo = { + // [StartExclude define_pin_credential_information] accountId: this.cid, + // [EndExclude define_pin_credential_information] credType:account_osAccount.AuthType.PIN, credSubType:account_osAccount.AuthSubType.PIN_SIX, token:new Uint8Array([]) } + // [End define_pin_credential_information] PINAuth.unregisterInputer(); PINAuth.registerInputer({ onGetData:(authSubType, callback)=>{ @@ -67,10 +75,12 @@ struct SystemAccount { userIDM.closeSession(this.cid); userIDM.openSession(this.cid).then((data)=>{ console.log('openSession info: ' + JSON.stringify(data)); + // [Start call_the_api_to_add_the_specified_credentials] userIDM.addCredential(credentialInfo,{ onResult: (err, extraInfo)=>{ console.log('addCredential result: ' + JSON.stringify(err)); console.log('edential info: ' + JSON.stringify(extraInfo)); + // [StartExclude call_the_api_to_add_the_specified_credentials] if (err == 0) { this.message = 'PIN code entry successful'; } else { @@ -78,7 +88,9 @@ struct SystemAccount { } PINAuth.unregisterInputer(); userIDM.closeSession(this.cid); + // [EndExclude call_the_api_to_add_the_specified_credentials] }}) + // [End call_the_api_to_add_the_specified_credentials] }).catch((err: Error)=>{ console.error('openSession failed, error: ' + JSON.stringify(err)); this.message = 'Failed to open session:' + JSON.stringify(err); @@ -108,18 +120,22 @@ struct SystemAccount { console.log('openSession info: ' + JSON.stringify(data)); let authType: osAccount.AuthType = osAccount.AuthType.PIN; let authTrustLevel: osAccount.AuthTrustLevel = osAccount.AuthTrustLevel.ATL1; + // [Start call_the_auth_operation_for_authentication] userAuth.auth(data, authType, authTrustLevel, { onResult: (result: number, extraInfo: osAccount.AuthResult) => { console.log('pin auth result = ' + result); console.log('pin auth extraInfo = ' + JSON.stringify(extraInfo)); let authToken = extraInfo.token; + // [StartExclude call_the_auth_operation_for_authentication] if(result == 0) { this.message = 'PIN code entry successful'; } else { this.message = 'PIN code authentication failed: ' + result; } + // [EndExclude call_the_auth_operation_for_authentication] } }); + // [End call_the_auth_operation_for_authentication] }).catch((err: Error)=>{ console.error('openSession failed, error: ' + JSON.stringify(err)); this.message = 'Failed to open session:' + JSON.stringify(err); @@ -130,16 +146,20 @@ struct SystemAccount { } private async addBiologyCred(): Promise { + // [Start define_facial_credential_information] let faceCredInfo: osAccount.CredentialInfo = { credType: osAccount.AuthType.FACE, credSubType: osAccount.AuthSubType.FACE_2D, token: new Uint8Array([1, 2, 3, 4, 5]) } + // [End define_facial_credential_information] + // [Start define_fingerprint_credential_information] let fingerprintCredInfo: osAccount.CredentialInfo = { credType: osAccount.AuthType.FINGERPRINT, credSubType: osAccount.AuthSubType.FINGERPRINT_CAPACITIVE, token: new Uint8Array([1, 2, 3, 4, 5]) } + // [End define_fingerprint_credential_information] try { let userIDM = new account_osAccount.UserIdentityManager(); let userAuth = new account_osAccount.UserAuth(); @@ -162,28 +182,36 @@ struct SystemAccount { console.log('pin auth extraInfo = ' + JSON.stringify(extraInfo)); faceCredInfo.token = extraInfo.token; fingerprintCredInfo.token = extraInfo.token; + // [Start input_facial_credential_information] userIDM.addCredential(faceCredInfo, { onResult: (code: number, result: osAccount.RequestResult) => { console.log('add face credential, resultCode: ' + code); console.log('add face credential, request result: ' + result); + // [StartExclude input_facial_credential_information] if(code == 0) { this.message = 'Successfully entered facial credentials'; } else { this.message = 'Failed to enter facial credentials: ' + code; } + // [EndExclude input_facial_credential_information] } }); + // [End input_facial_credential_information] + // [Start enter_the_fingerprint_credentials] userIDM.addCredential(fingerprintCredInfo, { onResult: (code: number, result: osAccount.RequestResult) => { console.log('add fingerprint credential, resultCode: ' + code); console.log('add fingerprint credential, request result: ' + result); + // [StartExclude enter_the_fingerprint_credentials] if(code == 0) { this.message += 'Successfully entered fingerprint credentials'; } else { this.message = 'Failed to enter fingerprint credentials: ' + code; } + // [EndExclude enter_the_fingerprint_credentials] } }); + // [End enter_the_fingerprint_credentials] } }); }).catch((err: Error)=>{ @@ -212,17 +240,21 @@ struct SystemAccount { console.log('openSession info: ' + JSON.stringify(data)); let authType: osAccount.AuthType = osAccount.AuthType.FACE; let authTrustLevel: osAccount.AuthTrustLevel = osAccount.AuthTrustLevel.ATL1; + // [Start call_the_auth_operation_for_face_authentication] userAuth.auth(data, authType, authTrustLevel, { onResult: (result: number, extraInfo: osAccount.AuthResult) => { console.log('face auth result = ' + result); console.log('face auth extraInfo = ' + JSON.stringify(extraInfo)); + // [StartExclude call_the_auth_operation_for_face_authentication] if(result == 0) { this.message = 'Successfully authenticated biometric credentials'; } else { this.message = 'Failed to authenticate biometric credentials: ' + result; } + // [EndExclude call_the_auth_operation_for_face_authentication] } }); + // [End call_the_auth_operation_for_face_authentication] }).catch((err: Error)=>{ console.error('openSession failed, error: ' + JSON.stringify(err)); this.message = 'Failed to open session:' + JSON.stringify(err); @@ -244,12 +276,16 @@ struct SystemAccount { if (this.textPassword != '') { password = strToUint8Arr(this.textPassword); } + // [Start define_the_credential_information_to_be_updated] let credentialInfo :account_osAccount.CredentialInfo = { + // [StartExclude define_the_credential_information_to_be_updated] accountId:this.cid, + // [EndExclude define_the_credential_information_to_be_updated] credType:account_osAccount.AuthType.PIN, credSubType:account_osAccount.AuthSubType.PIN_SIX, token:new Uint8Array([]) } + // [End define_the_credential_information_to_be_updated] PINAuth.unregisterInputer(); PINAuth.registerInputer({ onGetData:(authSubType, callback)=>{ @@ -271,10 +307,12 @@ struct SystemAccount { callback.onSetData(account_osAccount.AuthSubType.PIN_SIX, password) } }) + // [Start update_your_credentials] userIDM.updateCredential(credentialInfo, { onResult:(result: number, extraInfo: osAccount.RequestResult)=>{ console.log('updateCredential result: ' + JSON.stringify(result)); console.log('updateCredential extraInfo: ' + JSON.stringify(extraInfo)); + // [StartExclude update_your_credentials] if(result == 0) { this.message = 'Successfully updated PIN code credentials'; } else { @@ -282,8 +320,10 @@ struct SystemAccount { } PINAuth.unregisterInputer(); userIDM.closeSession(this.cid); + // [EndExclude update_your_credentials] } }) + // [End update_your_credentials] } }) }).catch((err: Error)=>{ @@ -300,8 +340,10 @@ struct SystemAccount { console.log('getAuthInfo data: ' + JSON.stringify(data)); this.message = 'Query information length: ' + data.length; }) + // [Start obtain_credential_information_of_the_specified_type] let enrolledFingerCredInfoList: osAccount.EnrolledCredInfo[] = await userIDM.getAuthInfo(osAccount.AuthType.PIN); + // [End obtain_credential_information_of_the_specified_type] } catch (err) { console.error('getAuthInfo failed: ' + JSON.stringify(err)); this.message = 'Query information failed: ' + JSON.stringify(err); @@ -312,6 +354,7 @@ struct SystemAccount { let userIDM = new account_osAccount.UserIdentityManager(); let userAuth = new account_osAccount.UserAuth(); let PINAuth = new account_osAccount.PINAuth(); + // [Start obtain_the_credential_information_of_the_fingerprint_type] let credentialId: Uint8Array = new Uint8Array([1, 2, 3, 4, 5]); let token: Uint8Array = new Uint8Array([1, 2, 3, 4, 5]) let credInfoList: osAccount.EnrolledCredInfo[] = @@ -319,6 +362,7 @@ struct SystemAccount { if (credInfoList.length != 0) { credentialId = credInfoList[0].credentialId; } + // [End obtain_the_credential_information_of_the_fingerprint_type] PINAuth.unregisterInputer(); PINAuth.registerInputer({ onGetData:(authSubType, callback)=>{ @@ -336,23 +380,31 @@ struct SystemAccount { console.log('authUser info: ' + JSON.stringify(authResult)); console.log('authUser info.userId: ' + authResult.accountId); try { + // [Start delete_specified_credentials] userIDM.delCred(credentialId, authResult.token, { onResult: (result: number, extraInfo: osAccount.RequestResult) => { console.log('delCred result = ' + result); console.log('delCred extraInfo = ' + JSON.stringify(extraInfo)); + // [StartExclude delete_specified_credentials] if(result == 0) { this.message = 'Successfully deleted credentials'; } else { this.message = 'Failed to delete credentials: ' + result; } + // [EndExclude delete_specified_credentials] } }); + // [End delete_specified_credentials] } catch (e) { console.error('delUser exception = ' + JSON.stringify(e)); this.message = 'Failed to delete credentials: ' + JSON.stringify(e); } + // [Start deactivate_pin_input_device] PINAuth.unregisterInputer(); + // [End deactivate_pin_input_device] + // [Start close_the_session_and_end_credential_management] userIDM.closeSession(this.cid); + // [End close_the_session_and_end_credential_management] } }) }).catch((err: Error)=>{ diff --git a/code/DocsSample/Account/ManagerApplicationAccount/entry/src/main/ets/pages/Index.ets b/code/DocsSample/Account/ManagerApplicationAccount/entry/src/main/ets/pages/Index.ets index bc9d8e88f0..a75e64af22 100755 --- a/code/DocsSample/Account/ManagerApplicationAccount/entry/src/main/ets/pages/Index.ets +++ b/code/DocsSample/Account/ManagerApplicationAccount/entry/src/main/ets/pages/Index.ets @@ -13,9 +13,13 @@ * limitations under the License. */ +// [Start import_the_application_account_module] import { appAccount, BusinessError } from '@kit.BasicServicesKit'; +// [End import_the_application_account_module] +// [Start obtain_the_instance_object_of_the_application_account] const appAccountManager = appAccount.createAppAccountManager(); +// [End obtain_the_instance_object_of_the_application_account] @Entry @Component @@ -23,101 +27,157 @@ struct Index { @State message: string = 'No Name'; private async createAccount(): Promise { + // [Start parameter_preparation] let name: string = 'ZhangSan'; let options: appAccount.CreateAccountOptions = { customData: { age: '10' } }; + // [End parameter_preparation] + // [Start create_an_app_account_based_on_the_name_and_options] try { await appAccountManager.createAccount(name, options); console.log('createAccount successfully'); + // [StartExclude create_an_app_account_based_on_the_name_and_options] this.message = 'Account creation successful'; + // [EndExclude create_an_app_account_based_on_the_name_and_options] } catch (err) { console.error('createAccount failed, error: ' + JSON.stringify(err)); + // [StartExclude create_an_app_account_based_on_the_name_and_options] this.message = 'Account creation failed: ' + JSON.stringify(err); + // [EndExclude create_an_app_account_based_on_the_name_and_options] } + // [End create_an_app_account_based_on_the_name_and_options] } private async getAccounts(): Promise { + // [Start query_the_account_list] appAccountManager.getAllAccounts().then((data: appAccount.AppAccountInfo[]) => { console.debug('getAllAccounts successfully, data: ' + JSON.stringify(data)); + // [StartExclude query_the_account_list] this.message = 'Query account result: ' + data[0].name; + // [EndExclude query_the_account_list] }).catch((err: BusinessError) => { console.error('getAllAccounts failed, error: ' + JSON.stringify(err)); + // [StartExclude query_the_account_list] this.message = 'Query account failed: ' + JSON.stringify(err); + // [EndExclude query_the_account_list] }); + // [End query_the_account_list] } private async setCredential(): Promise { + // [Start prepare_parameters_to_specify_the_account_name_credential_type_and_credential] let name: string = 'ZhangSan'; let credentialType: string = 'PIN_SIX'; let credential: string = 'xxxxxx'; + // [End prepare_parameters_to_specify_the_account_name_credential_type_and_credential] + // [Start set_the_credentials_for_your_account] await appAccountManager.setCredential(name, credentialType, credential).then(() => { console.log('setCredential successfully'); }).catch((err: BusinessError) => { console.error('setCredential failed: ' + JSON.stringify(err)); + // [StartExclude set_the_credentials_for_your_account] this.message = 'Failed to set account credentials: ' + JSON.stringify(err); + // [EndExclude set_the_credentials_for_your_account] }); + // [End set_the_credentials_for_your_account] + // [Start obtain_the_credentials_for_your_account] appAccountManager.getCredential(name, credentialType).then((data: string) => { console.log('getCredential successfully, data: ' + data); + // [StartExclude obtain_the_credentials_for_your_account] this.message = 'Successfully set account credentials: ' + data; + // [EndExclude obtain_the_credentials_for_your_account] }).catch((err: BusinessError) => { console.error('getCredential failed, error: ' + JSON.stringify(err)); + // [StartExclude obtain_the_credentials_for_your_account] this.message = 'Failed to query account credentials: ' + JSON.stringify(err); + // [EndExclude obtain_the_credentials_for_your_account] }); + // [End obtain_the_credentials_for_your_account] } private async setCustomData(): Promise { + // [Start prepare_parameters_specify_the_account_name_and_custom_key_values] let name: string = 'ZhangSan'; let key: string = 'age'; let value: string = '12'; + // [End prepare_parameters_specify_the_account_name_and_custom_key_values] + // [Start set_up_custom_data_for_your_account] await appAccountManager.setCustomData(name, key, value).then(() => { console.log('setCustomData successfully'); }).catch((err: BusinessError) => { console.error('setCustomData failed: ' + JSON.stringify(err)); + // [StartExclude set_up_custom_data_for_your_account] this.message = 'Failed to set custom account credentials: ' + JSON.stringify(err); + // [EndExclude set_up_custom_data_for_your_account] }); + // [End set_up_custom_data_for_your_account] + // [Start obtain_the_custom_data_of_the_account] appAccountManager.getCustomData(name, key).then((data: string) => { console.log('getCustomData successfully, data: ' + data); + // [StartExclude obtain_the_custom_data_of_the_account] this.message = 'Successfully set custom account credentials, ' + key + ' is ' + data; + // [EndExclude obtain_the_custom_data_of_the_account] }).catch((err: BusinessError) => { console.error('getCustomData failed, error: ' + JSON.stringify(err)); + // [StartExclude obtain_the_custom_data_of_the_account] this.message = 'Failed to query custom account credentials: ' + JSON.stringify(err); + // [EndExclude obtain_the_custom_data_of_the_account] }); + // [End obtain_the_custom_data_of_the_account] } private async setAuthToken(): Promise { + // [Start prepare_parameters_to_specify_the_account_name_account_owner_authorization_type_and_authorization_token] let name: string = 'ZhangSan'; let owner: string = 'com.example.s9'; let authType: string = 'getSocialData'; let token: string = 'xxxxxx'; + // [End prepare_parameters_to_specify_the_account_name_account_owner_authorization_type_and_authorization_token] + // [Start set_the_authorization_token_for_the_specified_authorization_type] await appAccountManager.setAuthToken(name, authType, token).then(() => { console.log('setAuthToken successfully'); }).catch((err: BusinessError) => { console.error('setAuthToken failed: ' + JSON.stringify(err)); + // [StartExclude set_the_authorization_token_for_the_specified_authorization_type] this.message = 'Failed to store account authorization token: ' + JSON.stringify(err); + // [EndExclude set_the_authorization_token_for_the_specified_authorization_type] }); + // [End set_the_authorization_token_for_the_specified_authorization_type] + // [Start obtain_an_authorization_token_for_the_specified_authorization_type] await appAccountManager.getAuthToken(name, owner, authType).then((data: string) => { console.log('getAuthToken successfully, data: ' + data); + // [StartExclude obtain_an_authorization_token_for_the_specified_authorization_type] this.message = 'Successfully stored account authorization token: ' + data; + // [EndExclude obtain_an_authorization_token_for_the_specified_authorization_type] }).catch((err: BusinessError) => { console.error('getAuthToken failed, error: ' + JSON.stringify(err)); + // [StartExclude obtain_an_authorization_token_for_the_specified_authorization_type] this.message = 'Failed to get account authorization token: ' + JSON.stringify(err); + // [EndExclude obtain_an_authorization_token_for_the_specified_authorization_type] }); + // [End obtain_an_authorization_token_for_the_specified_authorization_type] } private async removeAccount(): Promise { + // [Start delete_account] let name: string = 'ZhangSan'; appAccountManager.removeAccount(name).then(() => { console.log('removeAccount successfully'); + // [StartExclude delete_account] this.message = 'removeAccount successfully'; + // [EndExclude delete_account] }).catch((err: BusinessError) => { console.error('removeAccount failed, error: ' + JSON.stringify(err)); + // [StartExclude delete_account] this.message = 'Account removal failed: ' + JSON.stringify(err); + // [EndExclude delete_account] }); + // [End delete_account] } diff --git a/code/DocsSample/Account/SystemAccount/entry/src/main/ets/pages/SystemAccount/ManageSystemAccounts.ets b/code/DocsSample/Account/SystemAccount/entry/src/main/ets/pages/SystemAccount/ManageSystemAccounts.ets index 8a74171a47..fd65d482db 100755 --- a/code/DocsSample/Account/SystemAccount/entry/src/main/ets/pages/SystemAccount/ManageSystemAccounts.ets +++ b/code/DocsSample/Account/SystemAccount/entry/src/main/ets/pages/SystemAccount/ManageSystemAccounts.ets @@ -13,9 +13,13 @@ * limitations under the License. */ +// [Start import_system_account_module] import { osAccount, BusinessError } from '@kit.BasicServicesKit'; +// [End import_system_account_module] +// [Start obtain_account_management_single_instance_object] let accountManager = osAccount.getAccountManager(); +// [End obtain_account_management_single_instance_object] @Entry @Component @@ -24,49 +28,64 @@ struct ManageSystemAccounts { @State createLocalId: number = -1; private async createOsAccount(): Promise { + // [Start specify_nickname_and_type_information_to_create_system_account] let name: string = 'Bob'; let type: osAccount.OsAccountType = osAccount.OsAccountType.NORMAL; accountManager.createOsAccount(name, type, (err: BusinessError, osAccountInfo: osAccount.OsAccountInfo)=>{ console.log('createOsAccount err:' + JSON.stringify(err)); console.log('createOsAccount osAccountInfo:' + JSON.stringify(osAccountInfo)); + // [StartExclude specify_nickname_and_type_information_to_create_system_account] if (!err) { this.message = 'Account creation successful. Account name: ' + JSON.stringify(osAccountInfo.localName); this.createLocalId = osAccountInfo.localId; } else { this.message = 'Failed to create account: ' + JSON.stringify(err); } + // [EndExclude specify_nickname_and_type_information_to_create_system_account] }); + // [End specify_nickname_and_type_information_to_create_system_account] } private async findAllOsAccount(): Promise { + // [Start query_the_full_account] accountManager.queryAllCreatedOsAccounts((err: BusinessError, accountArr: osAccount.OsAccountInfo[])=>{ console.log('queryAllCreatedOsAccounts err:' + JSON.stringify(err)); console.log('queryAllCreatedOsAccounts accountArr:' + JSON.stringify(accountArr)); + // [StartExclude query_the_full_account] if (!err) { this.message = 'Query account successful. Account name:' + JSON.stringify(accountArr[0].localName); } else { this.message = 'Query account failed: ' + JSON.stringify(err); } + // [EndExclude query_the_full_account] }); + // [End query_the_full_account] } private async findOsAccount(): Promise { + // [Start query_information_of_the_specified_account] let localId: number = 100; accountManager.queryOsAccountById(localId, (err: BusinessError, accountInfo: osAccount.OsAccountInfo)=>{ console.log('queryOsAccountById err:' + JSON.stringify(err)); console.log('queryOsAccountById accountInfo:' + JSON.stringify(accountInfo)); + // [StartExclude query_information_of_the_specified_account] if (!err) { this.message = 'Query account successful. Account name for account 100: ' + JSON.stringify(accountInfo.localName); } else { this.message = 'Query account failed: ' + JSON.stringify(err); } + // [EndExclude query_information_of_the_specified_account] }); + // [End query_information_of_the_specified_account] } private async changeOsAccountHead(): Promise { + // [Start change_system_account_avatar] let localId: number = 100; + // [StartExclude change_system_account_avatar] localId = this.createLocalId; + // [EndExclude change_system_account_avatar] let newPhoto: string = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAAPCAYAAAA/I0V3AAAAAXNSR0IArs4c6QAAAARnQU1BAA'+ 'Cxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACwSURBVDhPvZLBDYMwDEV/ugsXRjAT0EHCOuFIBwkbdIRewi6unbiAyoGgSn1SFH85+Y'+ 'q/4ljARW62X+LHS8uIzjm4dXUYF+utzBikB52Jo5e5iEPKqpACk7R9NM2RvWm5tIkD2czLCUFNKLD6IjdMHFHDzws285MgGrT0xCtp3WOKHo'+ @@ -74,55 +93,80 @@ struct ManageSystemAccounts { accountManager.setOsAccountProfilePhoto(localId, newPhoto, (err: BusinessError)=>{ console.log('setOsAccountProfilePhoto err:' + JSON.stringify(err)); + // [StartExclude change_system_account_avatar] if(!err) { this.message = 'Successfully updated system account avatar' } else { this.message = 'Failed to update system account avatar: ' + JSON.stringify(err); } + // [EndExclude change_system_account_avatar] }); + // [End change_system_account_avatar] } private async changeOsAccountName(): Promise { + // [Start change_system_account_name] let localId: number = 100; + // [StartExclude change_system_account_name] localId = this.createLocalId; + // [EndExclude change_system_account_name] let newName: string = 'Tom'; accountManager.setOsAccountName(localId, newName, (err: BusinessError) => { if (err) { console.error('setOsAccountName failed, error: ' + JSON.stringify(err)); + // [StartExclude change_system_account_name] this.message = 'Failed to modify system account name: ' + JSON.stringify(err); + // [EndExclude change_system_account_name] } else { console.log('setOsAccountName successfully'); + // [StartExclude change_system_account_name] this.message = 'Successfully modified system account name'; + // [EndExclude change_system_account_name] } }); + // [End change_system_account_name] } private async activateOsAccount(): Promise { + // [Start activate_system_account] let localId: number = 101; + // [StartExclude activate_system_account] localId = this.createLocalId; + // [EndExclude activate_system_account] accountManager.activateOsAccount(localId, (err: BusinessError)=>{ if (err) { console.error(`activateOsAccount failed, code is ${err.code}, message is ${err.message}`); + // [StartExclude activate_system_account] this.message = 'Failed to activate system account: ' + err.code; + // [EndExclude activate_system_account] } else { console.log('activateOsAccount successfully'); + // [StartExclude activate_system_account] this.message = 'Successfully activated system account' + // [EndExclude activate_system_account] } }); + // [End activate_system_account] } private async deleteOsAccount(): Promise { + // [Start delete_the_specified_account] let localId: number = 101; localId = this.createLocalId; accountManager.removeOsAccount(localId, (err: BusinessError)=>{ if (err) { console.error('removeOsAccount failed, error: ' + JSON.stringify(err)); + // [StartExclude delete_the_specified_account] this.message = 'Failed to delete system account: ' + JSON.stringify(err); + // [EndExclude delete_the_specified_account] } else { console.log('removeOsAccount successfully'); + // [StartExclude delete_the_specified_account] this.message = 'Successfully deleted system account' + // [EndExclude delete_the_specified_account] } }); + // [End delete_the_specified_account] } build() { diff --git a/code/DocsSample/Account/SystemAccount/entry/src/main/ets/pages/SystemAccount/UseConstraintManagementSystemAccount.ets b/code/DocsSample/Account/SystemAccount/entry/src/main/ets/pages/SystemAccount/UseConstraintManagementSystemAccount.ets index 3a5acacc09..49edb54335 100755 --- a/code/DocsSample/Account/SystemAccount/entry/src/main/ets/pages/SystemAccount/UseConstraintManagementSystemAccount.ets +++ b/code/DocsSample/Account/SystemAccount/entry/src/main/ets/pages/SystemAccount/UseConstraintManagementSystemAccount.ets @@ -13,10 +13,14 @@ * limitations under the License. */ +// [Start import_system_account_module] import { osAccount } from '@kit.BasicServicesKit'; +// [End import_system_account_module] import { router } from '@kit.ArkUI'; +// [Start obtain_account_single_instance_object] let accountManager = osAccount.getAccountManager(); +// [End obtain_account_single_instance_object] @Entry @Component @@ -24,28 +28,42 @@ struct UseConstraintManagementSystemAccount { @State message: string = 'No work done'; private async setOsAccountConstraints(): Promise { + // [Start constraint_collections] let localId: number = 100; let constraint: string[] = [ 'constraint.wifi.set' ]; + // [End constraint_collections] + // [Start system_account_constraint] try { accountManager.setOsAccountConstraints(localId, constraint, true); console.log('setOsAccountConstraints successfully'); + // [StartExclude system_account_constraint] this.message = 'Successfully set the constraint list for the specified system account'; + // [EndExclude system_account_constraint] } catch (err) { console.error('setOsAccountConstraints failed, error: ' + JSON.stringify(err)); + // [StartExclude system_account_constraint] this.message = 'Failed to set the constraint list for the specified system account'; + // [EndExclude system_account_constraint] } + // [End system_account_constraint] } private async isOsAccountConstraintEnabled(): Promise { + // [Start specify_the_system_account_id_and_constraint_name] let localId: number = 100; let constraint: string = 'constraint.wifi.set'; + // [End specify_the_system_account_id_and_constraint_name] + // [Start check_whether_the_specified_constraint_is_enabled] let isEnabled: boolean = await accountManager.isOsAccountConstraintEnabled(localId, constraint); if (isEnabled) { // your business logic + // [StartExclude check_whether_the_specified_constraint_is_enabled] this.message = 'Set the constraint list for the specified system account'; } else { this.message = 'Can not set the constraint list for the specified system account'; + // [EndExclude check_whether_the_specified_constraint_is_enabled] } + // [End check_whether_the_specified_constraint_is_enabled] } build() { -- Gitee