1 /*
2  * Copyright (c) 2021-2024 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 #ifndef OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OSACCOUNT_OS_ACCOUNT_MANAGER_SERVICE_H
17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OSACCOUNT_OS_ACCOUNT_MANAGER_SERVICE_H
18 
19 #include <stdint.h>
20 #include <sys/types.h>
21 #include <memory>
22 #include "account_permission_manager.h"
23 #include "os_account_stub.h"
24 #include "idomain_account_callback.h"
25 #include "iinner_os_account_manager.h"
26 
27 namespace OHOS {
28 namespace AccountSA {
29 class OsAccountManagerService : public OsAccountStub {
30 public:
31     OsAccountManagerService();
32     ~OsAccountManagerService() override;
33 
34     ErrCode CreateOsAccount(
35         const std::string &name, const OsAccountType &type, OsAccountInfo &osAccountInfo) override;
36     ErrCode CreateOsAccount(const std::string &localName, const std::string &shortName,
37         const OsAccountType &type, OsAccountInfo &osAccountInfo, const CreateOsAccountOptions &options = {}) override;
38     ErrCode CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo,
39         const sptr<IDomainAccountCallback> &callback, const CreateOsAccountForDomainOptions &options = {}) override;
40 
41     ErrCode CreateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo,
42         const CreateOsAccountOptions &options = {}) override;
43     ErrCode UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) override;
44 
45     ErrCode RemoveOsAccount(const int id) override;
46 
47     ErrCode IsOsAccountExists(const int id, bool &isOsAccountExists) override;
48     ErrCode IsOsAccountActived(const int id, bool &isOsAccountActived) override;
49 
50     ErrCode IsOsAccountConstraintEnable(
51         const int id, const std::string &constraint, bool &isConstraintEnable) override;
52     ErrCode CheckOsAccountConstraintEnabled(
53         const int id, const std::string &constraint, bool &isEnabled) override;
54     ErrCode IsOsAccountVerified(const int id, bool &isVerified) override;
55 
56     ErrCode GetCreatedOsAccountsCount(unsigned int &osAccountsCount) override;
57     ErrCode GetOsAccountLocalIdFromProcess(int &id) override;
58     ErrCode IsMainOsAccount(bool &isMainOsAccount) override;
59 
60     ErrCode GetOsAccountLocalIdFromDomain(const DomainAccountInfo &domainInfo, int &id) override;
61     ErrCode QueryMaxOsAccountNumber(uint32_t &maxOsAccountNumber) override;
62     ErrCode QueryMaxLoggedInOsAccountNumber(uint32_t &maxNum) override;
63 
64     ErrCode GetOsAccountAllConstraints(const int id, std::vector<std::string> &constraints) override;
65     ErrCode QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &osAccountInfos) override;
66 
67     ErrCode QueryCurrentOsAccount(OsAccountInfo &osAccountInfo) override;
68     ErrCode QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo) override;
69 
70     ErrCode GetOsAccountTypeFromProcess(OsAccountType& type) override;
71     ErrCode GetOsAccountType(const int id, OsAccountType& type) override;
72     ErrCode GetOsAccountProfilePhoto(const int id, std::string &photo) override;
73 
74     ErrCode IsMultiOsAccountEnable(bool &isMultiOsAccountEnable) override;
75     ErrCode SetOsAccountName(const int id, const std::string &name) override;
76 
77     ErrCode SetOsAccountConstraints(
78         const int id, const std::vector<std::string> &constraints, const bool enable) override;
79     ErrCode SetOsAccountProfilePhoto(const int id, const std::string &photo) override;
80 
81     ErrCode ActivateOsAccount(const int id) override;
82     ErrCode DeactivateOsAccount(const int id) override;
83     ErrCode DeactivateAllOsAccounts() override;
84 
85     ErrCode StartOsAccount(const int id) override;
86 
87     ErrCode GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber, int &id) override;
88     ErrCode GetSerialNumberByOsAccountLocalId(const int &id, int64_t &serialNumber) override;
89 
90     ErrCode SubscribeOsAccount(
91         const OsAccountSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &eventListener) override;
92     ErrCode UnsubscribeOsAccount(const sptr<IRemoteObject> &eventListener) override;
93 
94     OS_ACCOUNT_SWITCH_MOD GetOsAccountSwitchMod() override;
95     ErrCode IsCurrentOsAccountVerified(bool &isVerified) override;
96 
97     ErrCode IsOsAccountCompleted(const int id, bool &isOsAccountCompleted) override;
98     ErrCode SetCurrentOsAccountIsVerified(const bool isVerified) override;
99     ErrCode SetOsAccountIsVerified(const int id, const bool isVerified) override;
100     ErrCode DumpState(const int &id, std::vector<std::string> &state) override;
101     ErrCode DumpOsAccountInfo(std::vector<std::string> &state);
102 
103     ErrCode GetCreatedOsAccountNumFromDatabase(const std::string& storeID,
104         int &createdOsAccountNum) override;
105     ErrCode GetSerialNumberFromDatabase(const std::string& storeID,
106         int64_t &serialNumber) override;
107     ErrCode GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id) override;
108     ErrCode GetOsAccountFromDatabase(const std::string& storeID,
109         const int id, OsAccountInfo &osAccountInfo) override;
110     ErrCode GetOsAccountListFromDatabase(const std::string& storeID,
111         std::vector<OsAccountInfo> &osAccountList) override;
112     ErrCode QueryActiveOsAccountIds(std::vector<int32_t>& ids) override;
113 
114     ErrCode QueryOsAccountConstraintSourceTypes(const int32_t id,
115         const std::string &constraint, std::vector<ConstraintSourceTypeInfo> &constraintSourceTypeInfos) override;
116     ErrCode SetGlobalOsAccountConstraints(const std::vector<std::string> &constraints,
117         const bool enable, const int32_t enforcerId, const bool isDeviceOwner) override;
118     ErrCode SetSpecificOsAccountConstraints(const std::vector<std::string> &constraints,
119         const bool enable, const int32_t targetId, const int32_t enforcerId, const bool isDeviceOwner) override;
120 
121     ErrCode SetDefaultActivatedOsAccount(const int32_t id) override;
122     ErrCode GetDefaultActivatedOsAccount(int32_t &id) override;
123     ErrCode GetOsAccountShortName(std::string &shortName) override;
124     ErrCode GetOsAccountName(std::string &name) override;
125     ErrCode GetOsAccountShortNameById(const int32_t id, std::string &shortName) override;
126 
127     ErrCode IsOsAccountForeground(const int32_t localId, const uint64_t displayId, bool &isForeground) override;
128     ErrCode GetForegroundOsAccountLocalId(const uint64_t displayId, int32_t &localId) override;
129     ErrCode GetForegroundOsAccounts(std::vector<ForegroundOsAccount> &accounts) override;
130     ErrCode GetBackgroundOsAccountLocalIds(std::vector<int32_t> &localIds) override;
131     ErrCode SetOsAccountToBeRemoved(int32_t localId, bool toBeRemoved) override;
132 
133 private:
134     virtual ErrCode DumpStateByAccounts(
135         const std::vector<OsAccountInfo> &osAccountInfos, std::vector<std::string> &state);
136     bool PermissionCheck(const std::string& permissionName, const std::string& constraintName);
137     bool CheckCreateOsAccountWhiteList();
138     ErrCode ValidateShortName(const std::string &shortName);
139     void GetCurrentLocalId(int32_t &localId);
140     ErrCode GetOsAccountShortNameCommon(const int32_t id, std::string &shortName);
141     ErrCode ValidateAccountCreateParamAndPermission(const std::string &localName, const OsAccountType &type);
142 
143 private:
144     IInnerOsAccountManager &innerManager_;
145     DISALLOW_COPY_AND_MOVE(OsAccountManagerService);
146 };
147 }  // namespace AccountSA
148 }  // namespace OHOS
149 
150 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OSACCOUNT_OS_ACCOUNT_MANAGER_SERVICE_H
151