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