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_IINNER_OS_ACCOUNT_MANAGER_H
17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OSACCOUNT_IINNER_OS_ACCOUNT_MANAGER_H
18 
19 #include <memory>
20 #include "iinner_os_account.h"
21 #include "inner_domain_account_manager.h"
22 #include "ios_account_control.h"
23 #include "ios_account_subscribe.h"
24 #include "ohos_account_manager.h"
25 #include "os_account_interface.h"
26 #include "os_account_plugin_manager.h"
27 #include "safe_map.h"
28 #include "singleton.h"
29 
30 namespace OHOS {
31 namespace AccountSA {
32 class IInnerOsAccountManager : public IInnerOsAccount {
33 public:
34     static IInnerOsAccountManager &GetInstance();
35     void Init() override;
36     ErrCode CreateOsAccount(
37         const std::string &name, const OsAccountType &type, OsAccountInfo &osAccountInfo) override;
38     ErrCode CreateOsAccount(const std::string &localName, const std::string &shortName,
39         const OsAccountType &type, OsAccountInfo &osAccountInfo, const CreateOsAccountOptions &options = {}) override;
40     ErrCode CreateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo,
41         const CreateOsAccountOptions &options = {}) override;
42     ErrCode UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) override;
43     ErrCode CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo,
44         const sptr<IDomainAccountCallback> &callback, const CreateOsAccountForDomainOptions &options = {}) override;
45     ErrCode RemoveOsAccount(const int id) override;
46     ErrCode IsOsAccountExists(const int id, bool &isOsAccountExits) override;
47     ErrCode IsOsAccountActived(const int id, bool &isOsAccountActived) override;
48     ErrCode IsOsAccountConstraintEnable(
49         const int id, const std::string &constraint, bool &isOsAccountConstraintEnable) override;
50     ErrCode IsOsAccountVerified(const int id, bool &isVerified) override;
51     ErrCode IsOsAccountDeactivating(const int id, bool &isDeactivating) override;
52     ErrCode GetCreatedOsAccountsCount(unsigned int &createdOsAccountCount) override;
53     ErrCode QueryMaxOsAccountNumber(uint32_t &maxOsAccountNumber) override;
54     ErrCode QueryMaxLoggedInOsAccountNumber(uint32_t &maxNum) override;
55     ErrCode GetOsAccountAllConstraints(const int id, std::vector<std::string> &constraints) override;
56     ErrCode QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &osAccountInfos) override;
57     ErrCode QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo) override;
58     ErrCode GetOsAccountShortName(const int id, std::string &shortName) override;
59     ErrCode GetOsAccountName(const int id, std::string &name) override;
60     ErrCode GetOsAccountType(const int id, OsAccountType &type) override;
61     ErrCode GetOsAccountProfilePhoto(const int id, std::string &photo) override;
62     ErrCode IsMultiOsAccountEnable(bool &isMultiOsAccountEnable) override;
63     ErrCode SetOsAccountName(const int id, const std::string &name) override;
64     ErrCode SetOsAccountConstraints(
65         const int id, const std::vector<std::string> &constraints, const bool enable) override;
66     ErrCode SetOsAccountProfilePhoto(const int id, const std::string &photo) override;
67     ErrCode ActivateOsAccount(const int id, const bool startStorage = true, const uint64_t displayId = 0) override;
68     ErrCode DeactivateOsAccount(const int id, bool isStopStorage = true) override;
69     ErrCode StartOsAccount(const int id) override;
70     ErrCode GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber, int &id) override;
71     ErrCode GetSerialNumberByOsAccountLocalId(const int &id, int64_t &serialNumber) override;
72     ErrCode SubscribeOsAccount(
73         const OsAccountSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &eventListener) override;
74     ErrCode UnsubscribeOsAccount(const sptr<IRemoteObject> &eventListener) override;
75     const std::shared_ptr<OsAccountSubscribeInfo> GetSubscribeRecordInfo(
76         const sptr<IRemoteObject> &eventListener) override;
77     OS_ACCOUNT_SWITCH_MOD GetOsAccountSwitchMod() override;
78     ErrCode IsOsAccountCompleted(const int id, bool &isOsAccountCompleted) override;
79     ErrCode SetOsAccountIsVerified(const int id, const bool isVerified) override;
80     ErrCode SetOsAccountIsLoggedIn(const int32_t id, const bool isLoggedIn) override;
81     ErrCode GetOsAccountCredentialId(const int id, uint64_t &credentialId) override;
82     ErrCode SetOsAccountCredentialId(const int id, uint64_t credentialId) override;
83     ErrCode IsAllowedCreateAdmin(bool &isAllowedCreateAdmin) override;
84     ErrCode GetOsAccountLocalIdFromDomain(const DomainAccountInfo &domainInfo, int &id) override;
85     ErrCode GetCreatedOsAccountNumFromDatabase(const std::string& storeID,
86         int &createdOsAccountNum) override;
87     ErrCode GetSerialNumberFromDatabase(const std::string& storeID, int64_t &serialNumber) override;
88     ErrCode GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id) override;
89     ErrCode GetOsAccountFromDatabase(const std::string& storeID, const int id,
90         OsAccountInfo &osAccountInfo) override;
91     ErrCode GetOsAccountListFromDatabase(const std::string& storeID,
92         std::vector<OsAccountInfo> &osAccountList) override;
93     ErrCode QueryActiveOsAccountIds(std::vector<int32_t>& ids) override;
94     ErrCode QueryOsAccountConstraintSourceTypes(const int32_t id,
95         const std::string &constraint, std::vector<ConstraintSourceTypeInfo> &constraintSourceTypeInfos) override;
96     ErrCode SetBaseOsAccountConstraints(const int32_t id,
97         const std::vector<std::string> &constraints, const bool enable) override;
98     ErrCode SetGlobalOsAccountConstraints(const std::vector<std::string> &constraints,
99         const bool enable, const int32_t enforcerId, const bool isDeviceOwner) override;
100     ErrCode SetSpecificOsAccountConstraints(const std::vector<std::string> &constraints,
101         const bool enable, const int32_t targetId, const int32_t enforcerId, const bool isDeviceOwner) override;
102 
103     ErrCode SetDefaultActivatedOsAccount(const int32_t id) override;
104     ErrCode GetDefaultActivatedOsAccount(int32_t &id) override;
105     ErrCode ValidateShortName(const std::string &shortName) override;
106     ErrCode IsOsAccountForeground(const int32_t localId, const uint64_t displayId, bool &isForeground) override;
107     ErrCode GetForegroundOsAccountLocalId(const uint64_t displayId, int32_t &localId) override;
108     ErrCode GetForegroundOsAccounts(std::vector<ForegroundOsAccount> &accounts) override;
109     ErrCode GetBackgroundOsAccountLocalIds(std::vector<int32_t> &localIds) override;
110     ErrCode SetOsAccountToBeRemoved(int32_t localId, bool toBeRemoved) override;
111     ErrCode BindDomainAccount(const OsAccountType &type, const DomainAccountInfo &domainAccountInfo,
112         OsAccountInfo &osAccountInfo, const CreateOsAccountForDomainOptions &options = {});
113     ErrCode SendMsgForAccountCreate(OsAccountInfo &osAccountInfo, const CreateOsAccountOptions &options = {});
114     ErrCode GetOsAccountInfoById(const int id, OsAccountInfo &osAccountInfo);
115     ErrCode UpdateAccountStatusForDomain(const int id, DomainAccountStatus status);
116     ErrCode GetTypeNumber(const OsAccountType& type, int32_t& typeNumber) override;
117     ErrCode CheckTypeNumber(const OsAccountType& type) override;
118     ErrCode UpdateAccountInfoByDomainAccountInfo(int32_t userId, const DomainAccountInfo &newDomainAccountInfo);
119     bool IsSameAccount(const DomainAccountInfo &domainInfoSrc, const DomainAccountInfo &domainInfoTar);
120     ErrCode ActivateDefaultOsAccount() override;
121     int32_t CleanGarbageOsAccounts(int32_t excludeId = -1) override;
122     void ResetAccountStatus() override;
123     bool CheckAndCleanOsAccounts();
124 
125 private:
126     IInnerOsAccountManager();
127     ~IInnerOsAccountManager() = default;
128     DISALLOW_COPY_AND_MOVE(IInnerOsAccountManager);
129     void SetOsAccountControl(std::shared_ptr<IOsAccountControl> ptr);
130     void RestartActiveAccount();
131     void CreateBaseAdminAccount();
132     void CreateBaseStandardAccount();
133     ErrCode PrepareRemoveOsAccount(OsAccountInfo &osAccountInfo, bool isCleanGarbage = false);
134     ErrCode RemoveOsAccountOperate(const int id, OsAccountInfo &osAccountInfo, bool isCleanGarbage = false);
135     ErrCode DeactivateOsAccountById(const int id);
136     ErrCode DeactivateOsAccountByInfo(OsAccountInfo &osAccountInfo);
137     ErrCode PrepareOsAccountInfo(const std::string &name, const OsAccountType &type,
138         const DomainAccountInfo &domainAccount, OsAccountInfo &osAccountInfo);
139     ErrCode PrepareOsAccountInfo(const std::string &localName, const std::string &shortName, const OsAccountType &type,
140         const DomainAccountInfo &domainAccount, OsAccountInfo &osAccountInfo);
141     ErrCode FillOsAccountInfo(const std::string &localName, const std::string &shortName, const OsAccountType &type,
142         const DomainAccountInfo &domainAccount, OsAccountInfo &osAccountInfo);
143     ErrCode PrepareOsAccountInfoWithFullInfo(OsAccountInfo &osAccountInfo);
144     ErrCode SendMsgForAccountActivate(OsAccountInfo &osAccountInfo, const bool startStorage = true,
145                                       const uint64_t dispalyId = 0);
146     ErrCode SendToStorageAccountStart(OsAccountInfo &osAccountInfo);
147     ErrCode SendToAMSAccountStart(OsAccountInfo &osAccountInfo, const uint64_t dispalyId);
148     ErrCode SendMsgForAccountDeactivate(OsAccountInfo &osAccountInfo, bool isStopStorage = true);
149     ErrCode SendMsgForAccountStop(OsAccountInfo &osAccountInfo);
150     ErrCode SendMsgForAccountRemove(OsAccountInfo &osAccountInfo);
151     ErrCode ValidateOsAccount(const OsAccountInfo &osAccountInfo);
152     void RemoveLocalIdToOperating(int32_t localId);
153     bool CheckAndAddLocalIdOperating(int32_t localId);
154     ErrCode DealWithDeviceOwnerId(const bool isDeviceOwner, const int32_t localId);
155     void CheckAndRefreshLocalIdRecord(const int id);
156     void RollBackToEarlierAccount(int32_t fromId, int32_t toId);
157     bool IsToBeRemoved(const int32_t localId);
158 
159     // operations for active list
160     void PushIdIntoActiveList(int32_t id);
161     void EraseIdFromActiveList(int32_t id);
162     bool IsOsAccountIDInActiveList(int32_t id);
163     void CopyFromActiveList(std::vector<int32_t>& idList);
164     bool CheckDomainAccountBound(const std::vector<OsAccountInfo> &osAccountInfos, const DomainAccountInfo &info);
165     void RetryToGetAccount(OsAccountInfo &osAccountInfo);
166     bool JudgeOsAccountUpdate(Json &accountIndexJson);
167     std::shared_ptr<std::mutex> GetOrInsertUpdateLock(int32_t id);
168     ErrCode UpdateAccountToForeground(const uint64_t displayId, OsAccountInfo &osAccountInfo);
169     ErrCode UpdateAccountToBackground(int32_t oldId);
170     ErrCode IsValidOsAccount(const OsAccountInfo &osAccountInfo);
171     ErrCode GetRealOsAccountInfoById(const int id, OsAccountInfo &osAccountInfo);
172 
173 private:
174     std::shared_ptr<IOsAccountControl> osAccountControl_;
175     std::vector<int32_t> activeAccountId_;
176     std::vector<int32_t> operatingId_;
177     IOsAccountSubscribe &subscribeManager_;
178     std::int32_t deviceOwnerId_ = -1;
179     std::int32_t defaultActivatedId_ = -1;
180     OsAccountConfig config_;
181     mutable std::mutex ativeMutex_;
182     mutable std::mutex operatingMutex_;
183     mutable std::mutex updateLockMutex_;
184     SafeMap<uint64_t, int32_t> foregroundAccountMap_;
185     OsAccountPluginManager &pluginManager_;
186     SafeMap<int32_t, bool> loggedInAccounts_;
187     SafeMap<int32_t, bool> verifiedAccounts_;
188     SafeMap<int32_t, bool> deactivatingAccounts_;
189     std::map<int32_t, std::shared_ptr<std::mutex>> updateLocks_;
190 };
191 }  // namespace AccountSA
192 }  // namespace OHOS
193 
194 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OSACCOUNT_IINNER_OS_ACCOUNT_MANAGER_H
195