• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_OHOS_ACCOUNT_MANAGER_H
17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OHOS_ACCOUNT_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <string>
22 #include "account_info.h"
23 #include "account_state_machine.h"
24 #include "ohos_account_data_deal.h"
25 
26 namespace OHOS {
27 namespace AccountSA {
28 const std::string ACCOUNT_CFG_DIR_ROOT_PATH = "/data/service/el1/public/account/";
29 
30 class OhosAccountManager;
31 using OhosAccountEventFunc = bool (OhosAccountManager::*)(const OhosAccountInfo &ohosAccountInfo,
32     const std::string &eventStr);
33 /**
34  * Ohos account manager
35  */
36 class OhosAccountManager {
37 public:
38     /**
39      * Get current ohos account information.
40      *
41      * @return current account information.
42      */
43     AccountInfo GetCurrentOhosAccountInfo();
44 
45     /**
46      * Get ohos account information by local userId.
47      *
48      * @param userId target local account id
49      * @return ohos account info which is bound to the local userId.
50      */
51     ErrCode GetAccountInfoByUserId(std::int32_t userId, AccountInfo &info);
52 
53     /**
54      * Get current account state.
55      *
56      * @return current account state id.
57      */
58     int GetCurrentOhosAccountState();
59 
60     /**
61      * Init ohos account manager.
62      *
63      */
64     bool OnInitialize();
65 
66     /**
67      * Process an account event.
68      *
69      * @param curOhosAccount current ohos account info
70      * @param eventStr ohos account state change event
71      * @return true if the processing was completed, otherwise false
72      */
73     bool HandleEvent(AccountInfo &curOhosAccount, const std::string &eventStr);
74 
75     /**
76      * login ohos (for distributed network) account.
77      *
78      * @param name ohos account name
79      * @param uid ohos account uid
80      * @param eventStr ohos account state change event
81      * @return true if the processing was completed, otherwise false
82      */
83     bool LoginOhosAccount(const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr);
84 
85     /**
86      * logout ohos (for distributed network) account.
87      *
88      * @param name ohos account name
89      * @param uid ohos account uid
90      * @param eventStr ohos account state change event
91      * @return true if the processing was completed, otherwise false
92      */
93     bool LogoutOhosAccount(const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr);
94 
95     /**
96      * logoff ohos (for distributed network) account.
97      *
98      * @param name ohos account name
99      * @param uid ohos account uid
100      * @param eventStr ohos account state change event
101      * @return true if the processing was completed, otherwise false
102      */
103     bool LogoffOhosAccount(const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr);
104 
105     /**
106      * Handle token_invalid event of ohos (for distributed network) account .
107      *
108      * @param name ohos account name
109      * @param uid ohos account uid
110      * @param eventStr ohos account state change event
111      * @return true if the processing was completed, otherwise false
112      */
113     bool HandleOhosAccountTokenInvalidEvent(const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr);
114 
115     /**
116      * Handle device account switch event.
117      *
118      * @param None
119      * @return None
120      */
121     void HandleDevAccountSwitchEvent();
122 
123     /**
124      * Ohos account state change.
125      *
126      * @param name ohos account name
127      * @param uid ohos account uid
128      * @param eventStr ohos account state change event
129      * @return true if the processing was completed, otherwise false
130      */
131     bool OhosAccountStateChange(const std::string &name, const std::string &uid, const std::string &eventStr);
132 
133     /**
134      * Ohos account state change.
135      *
136      * @param ohosAccountInfo ohos account information
137      * @param eventStr ohos account state change event
138      * @return true if the processing was completed, otherwise false
139      */
140     bool OhosAccountStateChange(const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr);
141 
142 private:
143     /**
144      * Account state machine.
145      */
146     std::unique_ptr<AccountStateMachine> accountState_{};
147 
148     /**
149      * Deal with file storage.
150      */
151     std::unique_ptr<OhosAccountDataDeal> dataDealer_{};
152 
153     /**
154      * event mapper.
155      */
156     std::map<std::string, ACCOUNT_INNER_EVENT_TYPE> eventMap_;
157 
158     /**
159      * mutex lock for synchronization.
160      */
161     std::mutex mgrMutex_;
162 
163     /**
164      * build event mapper.
165      */
166     void BuildEventsMapper();
167 
168     /**
169      * Config current account config.
170      *
171      * @param ohosAccountInfo target ohos account information.
172      * @return true if success.
173      */
174     bool SaveOhosAccountInfo(AccountInfo &ohosAccountInfo) const;
175 
176     /**
177      * Clear current account config.
178      * @param curOhosAccountInfo current ohos account info.
179      * @param clrStatus account status.
180      */
181     bool ClearOhosAccount(AccountInfo &curOhosAccountInfo, std::int32_t clrStatus = ACCOUNT_STATE_UNBOUND) const;
182 
183     /**
184      * Check whether the ohos account can be bound to the current user or not
185      * @return true if can.
186      */
187     bool CheckOhosAccountCanBind(const AccountInfo &currAccountInfo,
188         const OhosAccountInfo &newOhosAccountInfo, const std::string &newOhosUid) const;
189 
190     /**
191      * Get current ohos account info and check whether input information match or not
192      * @return true if matches.
193      */
194     bool GetCurOhosAccountAndCheckMatch(AccountInfo &curOhosAccountInfo,
195                                         const std::string &inputName,
196                                         const std::string &inputUid,
197                                         const std::int32_t callingUserId) const;
198 
199     /**
200      * event function map
201      */
202     std::map<std::string, OhosAccountEventFunc> eventFuncMap_;
203 };
204 }  // namespace AccountSA
205 }  // namespace OHOS
206 
207 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OHOS_ACCOUNT_MANAGER_H
208