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