1# Managing System Accounts (for System Applications Only) 2 3The system provides APIs for managing system accounts. 4After applying for required permissions for your system application, you can use the APIs to create, activate, modify, and delete system accounts. For third-party applications, you can use the APIs to query basic information about system accounts to develop service logic related to system accounts. 5 6## Basic Concepts 7 8### System Account Type 9 10Currently, only the following types of system accounts can be created: 11| Name | Value| Description | 12| ------ | ------ | ----------- | 13| ADMIN | 0 | Administrator account.| 14| NORMAL | 1 | Normal account. | 15| GUEST | 2 | Guest account. | 16| PRIVATE<sup>12+</sup> | 1024 | Private account. | 17 18### Account Information 19 20For details about complete system account information, see [OsAccountInfo](../../reference/apis-basic-services-kit/js-apis-osAccount.md#osaccountinfo). 21 22## Before You Start 23 241. Request the ohos.permission.MANAGE_LOCAL_ACCOUNTS permission. For details, see [Requesting Permissions for system_basic Applications](../../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications). 25 262. Import the **osAccount** module. 27 28 ```ts 29 import { osAccount, BusinessError } from '@kit.BasicServicesKit'; 30 ``` 31 323. Obtain an **AccountManager** instance. 33 34 ```ts 35 let accountManager = osAccount.getAccountManager(); 36 ``` 37 38## Creating a System Account 39 40The default system account is created during the system initialization. The user cal also create multiple system accounts as required. 41 42**Procedure** 43 44Use [createOsAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#createosaccount) to create a system account with the specified name and type. 45 46```ts 47let name: string = 'Bob'; 48let type: osAccount.OsAccountType = osAccount.OsAccountType.NORMAL; 49 50accountManager.createOsAccount(name, type, (err: BusinessError, osAccountInfo: osAccount.OsAccountInfo)=>{ 51 console.log('createOsAccount err:' + JSON.stringify(err)); 52 console.log('createOsAccount osAccountInfo:' + JSON.stringify(osAccountInfo)); 53}); 54``` 55 56## Obtaining All System Accounts 57 58The account management page may need to display information about all the system accounts. 59 60**Procedure** 61 62Use [queryAllCreatedOsAccounts](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#queryallcreatedosaccounts) to obtain informatory about all system accounts. 63 64```ts 65accountManager.queryAllCreatedOsAccounts((err: BusinessError, accountArr: osAccount.OsAccountInfo[])=>{ 66 console.log('queryAllCreatedOsAccounts err:' + JSON.stringify(err)); 67 console.log('queryAllCreatedOsAccounts accountArr:' + JSON.stringify(accountArr)); 68}); 69``` 70 71## Obtaining Information of a System Account 72 73Detailed information about a system account can be obtained based on the account ID. 74 75**Procedure** 76 77Use [queryOsAccountById](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#queryosaccountbyid) to obtain detailed information about a system account. 78 79```ts 80let localId: number = 100; 81accountManager.queryOsAccountById(localId, (err: BusinessError, accountInfo: osAccount.OsAccountInfo)=>{ 82 console.log('queryOsAccountById err:' + JSON.stringify(err)); 83 console.log('queryOsAccountById accountInfo:' + JSON.stringify(accountInfo)); 84}); 85``` 86 87## Changing the Profile Photo and Nickname of a System Account 88 89Change the profile photo and nickname of a system account as required. 90 91**Procedure** 92 931. Use [setOsAccountProfilePhoto](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#setosaccountprofilephoto) to change the profile picture of a system account. 94 95 ```ts 96 let localId: number = 100; 97 let newPhoto: string = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAAPCAYAAAA/I0V3AAAAAXNSR0IArs4c6QAAAARnQU1BAA'+ 98 'Cxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACwSURBVDhPvZLBDYMwDEV/ugsXRjAT0EHCOuFIBwkbdIRewi6unbiAyoGgSn1SFH85+Y'+ 99 'q/4ljARW62X+LHS8uIzjm4dXUYF+utzBikB52Jo5e5iEPKqpACk7R9NM2RvWm5tIkD2czLCUFNKLD6IjdMHFHDzws285MgGrT0xCtp3WOKHo'+ 100 '+7q0mP0DZW9pNmoEFUzrQjp5cCnaen2kSJXLFD8ghbXyZCMQf/8e8Ns1XVAG/XAgqKzVnJFAAAAABJRU5ErkJggg==' 101 102 accountManager.setOsAccountProfilePhoto(localId, newPhoto, (err: BusinessError)=>{ 103 console.log('setOsAccountProfilePhoto err:' + JSON.stringify(err)); 104 }); 105 ``` 106 1072. Use [setOsAccountName](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#setosaccountname) to change the system account name. 108 109 ```ts 110 let localId: number = 100; 111 let newName: string = 'Tom'; 112 accountManager.setOsAccountName(localId, newName, (err: BusinessError) => { 113 if (err) { 114 console.log('setOsAccountName failed, error: ' + JSON.stringify(err)); 115 } else { 116 console.log('setOsAccountName successfully'); 117 } 118 }); 119 ``` 120 121## Activating a System Account 122 123System accounts are not activated by default. A system account can be used only after being activated. You can use [activateOsAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#activateosaccount) to activate a system account. 124 125**Procedure** 126 127Use [activateOsAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#activateosaccount) to activate a system account. 128 129```ts 130let localId: number = 101; 131accountManager.activateOsAccount(localId, (err: BusinessError)=>{ 132 if (err) { 133 console.error(`activateOsAccount failed, code is ${err.code}, message is ${err.message}`); 134 } else { 135 console.log('activateOsAccount successfully'); 136 } 137}); 138``` 139 140## Removing a System Account 141 142Remove the system account that is no longer used. 143 144**Procedure** 145 146Use [removeOsAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#removeosaccount) to remove a system account. 147 148```ts 149let localId: number = 101; 150accountManager.removeOsAccount(localId, (err: BusinessError)=>{ 151 if (err) { 152 console.log('removeOsAccount failed, error: ' + JSON.stringify(err)); 153 } else { 154 console.log('removeOsAccount successfully'); 155 } 156}); 157``` 158