• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup OsAccount
18  * @{
19  *
20  * @brief Provides os account management.
21  *
22  * Provides abilities for you to manage and perform operations on your OS accounts.
23  *
24  * @since 7.0
25  * @version 7.0
26  */
27 
28 /**
29  * @file os_account_manager.h
30  *
31  * @brief Declares os account manager interfaces.
32  *
33  * @since 7.0
34  * @version 7.0
35  */
36 
37 #ifndef OS_ACCOUNT_INTERFACES_INNERKITS_OS_ACCOUNT_MANAGER_H
38 #define OS_ACCOUNT_INTERFACES_INNERKITS_OS_ACCOUNT_MANAGER_H
39 
40 #include <string>
41 #include <vector>
42 #include "domain_account_callback.h"
43 #include "os_account_info.h"
44 #include "os_account_subscriber.h"
45 #include "account_error_no.h"
46 namespace OHOS {
47 namespace AccountSA {
48 class OsAccountManager {
49 public:
50     /**
51      * @brief Creates an OS account using the local name and account type.
52      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
53      * @param localName - Indicates the local name of the OS account to create.
54      * @param type - Indicates the type of the OS account to create.
55      * @param osAccountInfo - Indicates the information about the created OS account.
56      * @return error code, see account_error_no.h
57      */
58     static ErrCode CreateOsAccount(const std::string &name, const OsAccountType &type, OsAccountInfo &osAccountInfo);
59 
60     /**
61      * @brief Creates an OS account using the local name, short name and account type.
62      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
63      * @param localName - Indicates the local name of the OS account to create.
64      * @param shortName - Indicates the short name of the OS account to create.
65      * @param type - Indicates the type of the OS account to create.
66      * @param osAccountInfo - Indicates the information about the created OS account.
67      * @return error code, see account_error_no.h
68      */
69     static ErrCode CreateOsAccount(const std::string& localName, const std::string& shortName,
70         const OsAccountType& type, OsAccountInfo& osAccountInfo);
71 
72     /**
73      * @brief Creates an OS account using the local name, short name, account type and other options.
74      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
75      * @param localName - Indicates the local name of the OS account to create.
76      * @param shortName - Indicates the short name of the OS account to create.
77      * @param type - Indicates the type of the OS account to create.
78      * @param options - Indicates the options of the OS account to create.
79      *                - Include disallowedHapList - Indicates the disallowed install hap list.
80      * @param osAccountInfo - Indicates the information about the created OS account.
81      * @return error code, see account_error_no.h
82      */
83     static ErrCode CreateOsAccount(const std::string& localName, const std::string& shortName,
84         const OsAccountType& type, const CreateOsAccountOptions& options, OsAccountInfo& osAccountInfo);
85 
86     /**
87      * @brief Creates an OS account using full user info
88      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
89      * @param osAccountInfo - Indicates the information about the created OS account.
90      * @return error code, see account_error_no.h
91      */
92     static ErrCode CreateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo);
93 
94     /**
95      * @brief Updates an OS account using full user info
96      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
97      * @param osAccountInfo - Indicates the information about the created OS account.
98      * @return error code, see account_error_no.h
99      */
100     static ErrCode UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo);
101 
102     /**
103      * @brief Creates an OS account using the account type and domain account info.
104      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
105      * @param type - Indicates the type of the OS account to create.
106      * @param domainInfo - Indicates the domain account info.
107      * @param callback - Indicates the callback for getting the information of the created OS account.
108      * @return error code, see account_error_no.h
109      */
110     static ErrCode CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo,
111         const std::shared_ptr<DomainAccountCallback> &callback);
112 
113     /**
114      * @brief Removes an OS account based on its local ID.
115      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
116      * @param id - Indicates the local ID of the OS account.
117      * @return error code, see account_error_no.h
118      */
119     static ErrCode RemoveOsAccount(const int id);
120 
121     /**
122      * @brief Checks whether the specified OS account exists.
123      * @param id - Indicates the local ID of the OS account.
124      * @param isOsAccountExists - Indicates whether the specified OS account exists.
125      * @return error code, see account_error_no.h
126      */
127     static ErrCode IsOsAccountExists(const int id, bool &isOsAccountExists);
128 
129     /**
130      * @brief Checks whether an OS account is activated based on its local ID.
131      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS or ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
132      * @param id - Indicates the local ID of the OS account.
133      * @param isOsAccountActived - Indicates whether the OS account is activated.
134      * @return error code, see account_error_no.h
135      */
136     static ErrCode IsOsAccountActived(const int id, bool &isOsAccountActived);
137 
138     /**
139      * @brief Checks whether the specified constraint is enabled for the specified OS account.
140      * @param id - Indicates the local ID of the OS account.
141      * @param constriaint - Indicates the constraint.
142      * @param isConstraintEnable - Indicates whether the specified constraint is enabled.
143      * @return error code, see account_error_no.h
144      */
145     static ErrCode IsOsAccountConstraintEnable(const int id, const std::string &constraint, bool &isConstraintEnable);
146 
147     /**
148      * @brief Checks whether the specified constraint is enabled for the specified OS account.
149      * @param id - Indicates the local ID of the OS account.
150      * @param constriaint - Indicates the constraint.
151      * @param isEnabled - Indicates whether the specified constraint is enabled.
152      * @return error code, see account_error_no.h
153      */
154     static ErrCode CheckOsAccountConstraintEnabled(
155         const int id, const std::string &constraint, bool &isEnabled);
156 
157     /**
158      * @brief Checks whether the specified OS account is verified.
159      * @param id - Indicates the local ID of the OS account.
160      * @param isVerified - Indicates whether the current OS account is verified.
161      * @return error code, see account_error_no.h
162      */
163     static ErrCode IsOsAccountVerified(const int id, bool &isVerified);
164 
165     /**
166      * @brief Gets the number of all OS accounts created on a device.
167      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
168      * @param osAccountsCount - Returns the number of created OS accounts.
169      * @return error code, see account_error_no.h
170      */
171     static ErrCode GetCreatedOsAccountsCount(unsigned int &osAccountsCount);
172 
173     /**
174      * @brief Gets the local ID of the current OS account.
175      * @param id - Indicates the local ID of the current OS account.
176      * @return error code, see account_error_no.h
177      */
178     static ErrCode GetOsAccountLocalIdFromProcess(int &id);
179 
180     /**
181      * @brief Checks whether current process belongs to the main account.
182      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
183      * @param isMainOsAccount - Indicates whether the current process belongs to the main OS account.
184      * @return error code, see account_error_no.h
185      */
186     static ErrCode IsMainOsAccount(bool &isMainOsAccount);
187 
188     /**
189      * @brief Gets the local ID of an OS account from the process UID
190      * @param uid - Indicates the process UID.
191      * @param id - Indicates the local ID of the OS account associated with the specified UID.
192      * @return error code, see account_error_no.h
193      */
194     static ErrCode GetOsAccountLocalIdFromUid(const int uid, int &id);
195 
196     /**
197      * @brief Gets the bundle ID associated with the specified UID.
198      * @param uid - Indicates the target uid.
199      * @param bundleId - Indicates the bundle ID associated with the specified UID.
200      * @return error code, see account_error_no.h
201      */
202     static ErrCode GetBundleIdFromUid(const int uid, int &bundleId);
203 
204     /**
205      * @brief Gets the local ID of the OS account associated with the specified domain account.
206      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
207      * @param domainInfo - Indicates the domain account info.
208      * @param id - Indicates the local ID of the OS account associated with the specified domain account.
209      * @return error code, see account_error_no.h
210      */
211     static ErrCode GetOsAccountLocalIdFromDomain(const DomainAccountInfo &domainInfo, int &id);
212 
213     /**
214      * @brief Queries the maximum number of OS accounts that can be created on a device.
215      * @param maxOsAccountNumber - Returns the maximum number of OS accounts that can be created.
216      * @return error code, see account_error_no.h
217      */
218     static ErrCode QueryMaxOsAccountNumber(int &maxOsAccountNumber);
219 
220     /**
221      * @brief Gets all constraints of an account based on its ID.
222      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
223      * @param localId - Indicates the local ID of the OS account.
224      * @param constraints - Indicates a list of constraints.
225      * @return error code, see account_error_no.h
226      */
227     static ErrCode GetOsAccountAllConstraints(const int id, std::vector<std::string> &constraints);
228 
229     /**
230      * @brief Queries the list of all the OS accounts that have been created in the system.
231      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
232      * @param osAccountInfos - Indicates a list of OS accounts.
233      * @return error code, see account_error_no.h
234      */
235     static ErrCode QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &osAccountInfos);
236 
237     /**
238      * @brief Gets information about the current OS account.
239      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
240      * @param osAccountInfo - Indicates the information about the current OS account.
241      * @return error code, see account_error_no.h
242      */
243     static ErrCode QueryCurrentOsAccount(OsAccountInfo &osAccountInfo);
244 
245     /**
246      * @brief Queries OS account information based on the local ID.
247      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS or ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION
248      * @param localId - Indicates the local ID of the OS account.
249      * @param osAccountInfo - Indicates the OS account information.
250      * @return error code, see account_error_no.h
251      */
252     static ErrCode QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo);
253 
254     /**
255      * @brief Gets the type of this OS account from the current process.
256      * @param type - Indicates the OS account type.
257      * @return error code, see account_error_no.h
258      */
259     static ErrCode GetOsAccountTypeFromProcess(OsAccountType &type);
260 
261     /**
262      * @brief Gets the profile photo of an OS account based on its local ID.
263      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
264      * @param id - Indicates the local ID of the OS account.
265      * @param photo - Indicates the profile photo.
266      * @return error code, see account_error_no.h
267      */
268     static ErrCode GetOsAccountProfilePhoto(const int id, std::string &photo);
269 
270     /**
271      * @brief Checks whether the function of supporting multiple OS accounts is enabled.
272      * @param isMultiOsAccountEnable - Indicates whether multiple OS account feature is enabled.
273      * @return error code, see account_error_no.h
274      */
275     static ErrCode IsMultiOsAccountEnable(bool &isMultiOsAccountEnable);
276 
277     /**
278      * @brief Sets the local name for an OS account based on its local ID.
279      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
280      * @param localId - Indicates the local ID of the OS account.
281      * @param localName - Indicates the local name to set for the OS account.
282      * @return error code, see account_error_no.h
283      */
284     static ErrCode SetOsAccountName(const int id, const std::string &localName);
285 
286     /**
287      * @brief Sets constraints for an OS account based on its local ID.
288      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
289      * @param localId - Indicates the local ID of the OS account.
290      * @param constraints - Indicates the constraints to set for the OS account. The value can be:
291      *        <ul>
292      *        <li>{@code constraint.wifi.set} - Indicates the constraint on configuring the Wi-Fi access point.
293      *        </li>
294      *        <li>{@code constraint.sms.use} - Indicates the constraint on sending and receiving short messages.
295      *        </li>
296      *        <li>{@code constraint.calls.outgoing} - Indicates the constraint on making calls.</li>
297      *        <li>{@code constraint.unknown.sources.install} - Indicates the constraint on installing applications
298      *        from unknown sources.</li>
299      *        </ul>
300      * @param enable - Specifies whether to enable the constraint.
301      * @return error code, see account_error_no.h
302      */
303     static ErrCode SetOsAccountConstraints(
304         const int id, const std::vector<std::string> &constraints, const bool enable);
305 
306     /**
307      * @brief Sets the profile photo for an OS account based on its local ID.
308      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
309      * @param localId - Indicates the local ID of the OS account.
310      * @param photo - Indicates the profile photo to set for the OS account.
311      * @return error code, see account_error_no.h
312      */
313     static ErrCode SetOsAccountProfilePhoto(const int id, const std::string &photo);
314 
315     /**
316      * @brief Gets the distributed virtual device ID (DVID).
317      * <p>
318      * If the same OHOS account has logged in to multiple devices, these devices constitute a super device
319      * through the distributed networking. On the connected devices, you can call this method to obtain the DVIDs.
320      * The same application running on different devices obtains the same DVID, whereas different applications
321      * obtain different DVIDs.
322      * <p>
323      *
324      * @permission ohos.permission.DISTRIBUTED_DATASYNC or ohos.permission.MANAGE_LOCAL_ACCOUNTS
325      * @param deviceId - Indicates the DVID if obtained; returns an empty string if no OHOS account has logged in.
326      * @return error code, see account_error_no.h
327      */
328     static ErrCode GetDistributedVirtualDeviceId(std::string &deviceId);
329 
330     /**
331      * @brief Activates a specified OS account.
332      * <p>
333      * If multiple OS accounts are available, you can call this method to enable a specific OS account
334      * to run in the foreground. Then, the OS account originally running in the foreground will be
335      * switched to the background.
336      * </p>
337      *
338      * @permission ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION
339      * @param id - Indicates the local ID of the OS account.
340      * @return error code, see account_error_no.h
341      */
342     static ErrCode ActivateOsAccount(const int id);
343 
344     /**
345      * @brief Deactivates a specified OS account.
346      * <p>
347      * You can call this method to disable a specific OS account.
348      * </p>
349      *
350      * @permission ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION
351      * @param id - Indicates the local ID of the OS account.
352      * @return error code, see account_error_no.h
353      */
354     static ErrCode DeactivateOsAccount(const int id);
355 
356     /**
357      * @brief Starts the specified OS account.
358      * @param id - Indicates the local ID of the OS account.
359      * @return error code, see account_error_no.h
360      */
361     static ErrCode StartOsAccount(const int id);
362 
363     /**
364      * @brief Stops the specified OS account.
365      * @param id - Indicates the local ID of the OS account.
366      * @return error code, see account_error_no.h
367      */
368     static ErrCode StopOsAccount(const int id);
369 
370     /**
371      * @brief Gets localId according to serial number.
372      * @param serialNumber - Indicates serial number.
373      * @param id - Indicates the local ID of the OS account.
374      * @return error code, see account_error_no.h
375      */
376     static ErrCode GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber, int &id);
377 
378     /**
379      * @brief Gets serial number according to localId.
380      * @param localId - Indicates the local ID of the OS account.
381      * @param serialNumber - Indicates the serial number.
382      * @return error code, see account_error_no.h
383      */
384     static ErrCode GetSerialNumberByOsAccountLocalId(const int &id, int64_t &serialNumber);
385 
386     /**
387      * @brief Subscribes the event of an OS account by the subscriber.
388      * @param subscriber subscriber information
389      * @return error code, see account_error_no.h
390      */
391     static ErrCode SubscribeOsAccount(const std::shared_ptr<OsAccountSubscriber> &subscriber);
392 
393     /**
394      * @brief Unsubscribes the event of an OS account by the subscriber.
395      * @param subscriber subscriber information
396      * @return error code, see account_error_no.h
397      */
398     static ErrCode UnsubscribeOsAccount(const std::shared_ptr<OsAccountSubscriber> &subscriber);
399 
400     /**
401      * @brief Gets the OS account switch mode.
402      * @return switch mode
403      */
404     static OS_ACCOUNT_SWITCH_MOD GetOsAccountSwitchMod();
405 
406     /**
407      * @brief Checks whether the current OS account is verified.
408      * @param isVerified - Indicates whether the current OS account is verified.
409      * @return error code, see account_error_no.h
410      */
411     static ErrCode IsCurrentOsAccountVerified(bool &isVerified);
412 
413     /**
414      * @brief Checks whether the specified OS account is created completely.
415      * @param id - Indicates the local ID of the specified OS account.
416      * @param isOsAccountCompleted - Indicates whether the current OS account is created completely.
417      * @return error code, see account_error_no.h
418      */
419     static ErrCode IsOsAccountCompleted(const int id, bool &isOsAccountCompleted);
420 
421     /**
422      * @brief Sets the current OS account to be verified.
423      * @param isVerified - Indicates whether the current OS account is verified.
424      * @return error code, see account_error_no.h
425      */
426     static ErrCode SetCurrentOsAccountIsVerified(const bool isVerified);
427 
428     /**
429      * @brief Sets the specified OS account to be verified.
430      * @param id - Indicates the local ID of the specified OS account.
431      * @param isVerified - Indicates whether the current OS account is verified.
432      * @return error code, see account_error_no.h
433      */
434     static ErrCode SetOsAccountIsVerified(const int id, const bool isVerified);
435 
436     /**
437      * @brief Gets the number of the created OS account from database.
438      * @param storeID - Indicates the store ID.
439      * @param id - Indicates the number of the created OS account.
440      * @return error code, see account_error_no.h
441      */
442     static ErrCode GetCreatedOsAccountNumFromDatabase(const std::string& storeID, int &createdOsAccountNum);
443 
444     /**
445      * @brief Get serial number from database.
446      * @param storeID - Indicates the store ID.
447      * @param serialNumber - Indicates the serial number.
448      * @return error code, see account_error_no.h
449      */
450     static ErrCode GetSerialNumberFromDatabase(const std::string& storeID, int64_t &serialNumber);
451 
452     /**
453      * @brief Gets the max ID of the OS account to be created.
454      * @param storeID - Indicates the store ID.
455      * @param id - Indicates the max ID of the OS account to be created.
456      * @return error code, see account_error_no.h
457      */
458     static ErrCode GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id);
459 
460     /**
461      * @brief Sets the specified OS account from database.
462      * @param storeID - Indicates the store ID.
463      * @param id - Indicates the local ID of the specified OS account.
464      * @param osAccountInfo - Indicates the OS account information.
465      * @return error code, see account_error_no.h
466      */
467     static ErrCode GetOsAccountFromDatabase(const std::string& storeID, const int id, OsAccountInfo &osAccountInfo);
468 
469     /**
470      * @brief Get a list of OS accounts from database.
471      * @param storeID - Indicates the store ID.
472      * @param osAccountList - Indicates a list of OS accounts.
473      * @return error code, see account_error_no.h
474      */
475     static ErrCode GetOsAccountListFromDatabase(const std::string& storeID, std::vector<OsAccountInfo> &osAccountList);
476 
477     /**
478      * @brief Gets the local IDs of all activated OS accounts.
479      * @param ids - Indicates the local IDs of all activated OS accounts.
480      * @return error code, see account_error_no.h
481      */
482     static ErrCode QueryActiveOsAccountIds(std::vector<int32_t>& ids);
483 
484     /**
485      * @brief Gets a list of constraint source types for the specified os account.
486      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
487      * @param localId - Indicates the local ID of the OS account.
488      * @param constraint - Indicates the constraint to query the source type.
489      * @param constraintSourceTypeInfos - Indicates the list of constraint source types for the specified os account.
490      * @return error code, see account_error_no.h
491      */
492     static ErrCode QueryOsAccountConstraintSourceTypes(const int32_t id, const std::string constraint,
493         std::vector<ConstraintSourceTypeInfo> &constraintSourceTypeInfos);
494 
495     /**
496      * @brief Sets the global constraints for all OS accounts.
497      * @param constraints - Indicates the local IDs of all activated OS accounts.
498      * @param isEnabled - Indicates whether the constraints are enabled.
499      * @param enforcerId - Indicates the local ID of the OS account who enforce the operation.
500      * @param isDeviceOwner - Indicates whether the enforcer is device owner.
501      * @return error code, see account_error_no.h
502      */
503     static ErrCode SetGlobalOsAccountConstraints(const std::vector<std::string> &constraints,
504         const bool isEnabled, const int32_t enforcerId = 0, const bool isDeviceOwner = false);
505 
506     /**
507      * @brief Sets the constraints for the specified OS accounts.
508      * @param constraints - Indicates the local IDs of all activated OS accounts.
509      * @param enable - Indicates whether the constraints are enabled.
510      * @param targetId - Indicates the local ID of the target OS account.
511      * @param enforcerId - Indicates the local ID of the OS account who enforce the operation.
512      * @param isDeviceOwner - Indicates whether the enforcer is device owner.
513      * @return error code, see account_error_no.h
514      */
515     static ErrCode SetSpecificOsAccountConstraints(const std::vector<std::string> &constraints,
516         const bool enable, const int32_t targetId, const int32_t enforcerId, const bool isDeviceOwner);
517 
518     /**
519      * @brief Sets the default activated OS account.
520      * @param id - Indicates the local IDs of the default activated OS accounts.
521      * @return error code, see account_error_no.h
522      */
523     static ErrCode SetDefaultActivatedOsAccount(const int32_t id);
524 
525     /**
526      * @brief Gets the default activated OS account.
527      * @param id - Indicates the local IDs of the default activated OS accounts.
528      * @return error code, see account_error_no.h
529      */
530     static ErrCode GetDefaultActivatedOsAccount(int32_t &id);
531 
532     /**
533      * @brief Gets the currend user short name.
534      * @param id - Indicates the current user short name of the OS account.
535      * @return error code, see account_error_no.h
536      */
537     static ErrCode GetOsAccountShortName(std::string &shortName);
538 };
539 }  // namespace AccountSA
540 }  // namespace OHOS
541 #endif  // OS_ACCOUNT_INTERFACES_INNERKITS_OS_ACCOUNT_MANAGER_H
542