• 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 #include "os_account_database_operator.h"
16 #include "account_log_wrapper.h"
17 #include "os_account_constants.h"
18 #include "os_account_data_storage.h"
19 #include "parameter.h"
20 
21 namespace OHOS {
22 namespace AccountSA {
OsAccountDatabaseOperator()23 OsAccountDatabaseOperator::OsAccountDatabaseOperator()
24     : accountDataStorage_(nullptr), storeID_("")
25 {}
~OsAccountDatabaseOperator()26 OsAccountDatabaseOperator::~OsAccountDatabaseOperator()
27 {}
Init()28 void OsAccountDatabaseOperator::Init()
29 {
30     char udid[Constants::DEVICE_UUID_LENGTH] = {0};
31     int ret = GetDevUdid(udid, Constants::DEVICE_UUID_LENGTH);
32     if (ret != 0) {
33         ACCOUNT_LOGE("GetDevUdid failed! ret = %{public}d.", ret);
34         return;
35     }
36     storeID_ = std::string(udid);
37 }
38 
InnerInit()39 bool OsAccountDatabaseOperator::InnerInit()
40 {
41     if (accountDataStorage_ == nullptr) {
42         ACCOUNT_LOGI("database operator innerinit, enter!");
43         accountDataStorage_ = std::make_shared<OsAccountDataStorage>(
44             Constants::APP_ID, storeID_, Constants::SYNC_OS_ACCOUNT_DATABSE);
45         if (accountDataStorage_ == nullptr) {
46             ACCOUNT_LOGE("accountDataStorage_ is still nullptr.");
47             return false;
48         }
49 
50         if (!accountDataStorage_->IsKeyExists(Constants::ACCOUNT_LIST)) {
51             ACCOUNT_LOGI("database operator innerinit, create account list.");
52             std::vector<std::string> accountListt;
53             Json accountList = Json {
54                 {Constants::ACCOUNT_LIST, accountListt},
55                 {Constants::COUNT_ACCOUNT_NUM, 0},
56                 {Constants::MAX_ALLOW_CREATE_ACCOUNT_ID, Constants::MAX_USER_ID},
57                 {Constants::SERIAL_NUMBER_NUM, Constants::SERIAL_NUMBER_NUM_START},
58             };
59             SaveAccountListToDatabase(accountList);
60         }
61     }
62     return true;
63 }
64 
GetOsAccountListFromDatabase(const std::string & storeID,std::vector<OsAccountInfo> & osAccountList)65 ErrCode OsAccountDatabaseOperator::GetOsAccountListFromDatabase(const std::string& storeID,
66                                                                 std::vector<OsAccountInfo> &osAccountList)
67 {
68     osAccountList.clear();
69     if (!InnerInit()) {
70         ACCOUNT_LOGE("InnerInit failed!");
71         return ERR_ACCOUNT_COMMON_NOT_INIT_ERROR;
72     }
73 
74     std::map<std::string, std::shared_ptr<IAccountInfo>> osAccountMapInfos;
75     ErrCode errCode = ERR_OK;
76     if (storeID.empty()) {
77         errCode = accountDataStorage_->LoadAllData(osAccountMapInfos);
78     } else {
79         std::shared_ptr<AccountDataStorage> storagePtr = std::make_shared<OsAccountDataStorage>(
80             Constants::APP_ID, storeID, Constants::SYNC_OS_ACCOUNT_DATABSE);
81         if (storagePtr == nullptr) {
82             ACCOUNT_LOGE("storagePtr is still nullptr, storeID %{public}s.", storeID.c_str());
83             return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
84         }
85         errCode = storagePtr->LoadAllData(osAccountMapInfos);
86     }
87 
88     if (errCode != ERR_OK) {
89         ACCOUNT_LOGE("LoadAllData failed! storeID %{public}s.", storeID.c_str());
90         return errCode;
91     }
92 
93     for (auto it = osAccountMapInfos.begin(); it != osAccountMapInfos.end(); it++) {
94         OsAccountInfo curOsInfo = *(std::static_pointer_cast<OsAccountInfo>(it->second));
95         if (curOsInfo.GetLocalId() >= Constants::START_USER_ID) {
96             osAccountList.push_back(curOsInfo);
97         }
98     }
99     ACCOUNT_LOGI("Totally get %{public}zu os accounts from database. storeID %{public}s.",
100         osAccountList.size(), storeID.c_str());
101     return ERR_OK;
102 }
103 
InsertOsAccountIntoDataBase(const OsAccountInfo & osAccountInfo)104 void OsAccountDatabaseOperator::InsertOsAccountIntoDataBase(const OsAccountInfo &osAccountInfo)
105 {
106     if (!InnerInit()) {
107         ACCOUNT_LOGE("InnerInit failed! target localID %{public}d!", osAccountInfo.GetLocalId());
108         return;
109     }
110 
111     if (osAccountInfo.GetLocalId() < Constants::START_USER_ID) {
112         ACCOUNT_LOGI("target os account id %{public}d will not be saved in database!", osAccountInfo.GetLocalId());
113         return;
114     }
115 
116     ErrCode errCode = accountDataStorage_->AddAccountInfo(osAccountInfo);
117     if (errCode != ERR_OK) {
118         ACCOUNT_LOGE("AddAccountInfo failed, error code %{public}d, target id %{public}d.",
119             errCode, osAccountInfo.GetLocalId());
120         return;
121     }
122     ACCOUNT_LOGI("insert account %{public}d to database succeed.", osAccountInfo.GetLocalId());
123 }
124 
DelOsAccountFromDatabase(const int id)125 void OsAccountDatabaseOperator::DelOsAccountFromDatabase(const int id)
126 {
127     if (!InnerInit()) {
128         ACCOUNT_LOGE("InnerInit failed! id %{public}d!", id);
129         return;
130     }
131 
132     ErrCode errCode = accountDataStorage_->RemoveValueFromKvStore(std::to_string(id));
133     if (errCode != ERR_OK) {
134         ACCOUNT_LOGE("delete os account %{public}d from database failed! error code %{public}d.", id, errCode);
135     } else {
136         ACCOUNT_LOGI("delete os account %{public}d from database succeed!", id);
137     }
138 }
139 
UpdateOsAccountInDatabase(const OsAccountInfo & osAccountInfo)140 void OsAccountDatabaseOperator::UpdateOsAccountInDatabase(const OsAccountInfo &osAccountInfo)
141 {
142     if (!InnerInit()) {
143         ACCOUNT_LOGE("InnerInit failed! localid %{public}d!", osAccountInfo.GetLocalId());
144         return;
145     }
146 
147     ErrCode errCode = accountDataStorage_->SaveAccountInfo(osAccountInfo);
148     if (errCode != ERR_OK) {
149         ACCOUNT_LOGE("updata os account info in database for account %{public}d failed! errCode = %{public}d.",
150             osAccountInfo.GetLocalId(), errCode);
151     } else {
152         ACCOUNT_LOGI("updata os account info in database for account %{public}d succeed!", osAccountInfo.GetLocalId());
153     }
154 }
155 
GetOsAccountFromDatabase(const std::string & storeID,const int id,OsAccountInfo & osAccountInfo)156 ErrCode OsAccountDatabaseOperator::GetOsAccountFromDatabase(const std::string& storeID,
157                                                             const int id,
158                                                             OsAccountInfo &osAccountInfo)
159 {
160     if (!InnerInit()) {
161         ACCOUNT_LOGE("InnerInit failed! storeID %{public}s id %{public}d!", storeID.c_str(), id);
162         return ERR_ACCOUNT_COMMON_NOT_INIT_ERROR;
163     }
164 
165     if (storeID.empty()) {
166         return accountDataStorage_->GetAccountInfoById(std::to_string(id), osAccountInfo);
167     }
168 
169     std::shared_ptr<AccountDataStorage> storagePtr = std::make_shared<OsAccountDataStorage>(
170         Constants::APP_ID, storeID, Constants::SYNC_OS_ACCOUNT_DATABSE);
171     if (storagePtr == nullptr) {
172         ACCOUNT_LOGE("storagePtr is nullptr, for other storeID %{public}s, id %{public}d.", storeID.c_str(), id);
173         return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
174     }
175     return storagePtr->GetAccountInfoById(std::to_string(id), osAccountInfo);
176 }
177 
GetCreatedOsAccountNumFromDatabase(const std::string & storeID,int & createdOsAccountNum)178 ErrCode OsAccountDatabaseOperator::GetCreatedOsAccountNumFromDatabase(
179     const std::string& storeID, int &createdOsAccountNum)
180 {
181     Json accountListJson;
182     ErrCode ret = GetAccountListFromStoreID(storeID, accountListJson);
183     if (ret != ERR_OK) {
184         ACCOUNT_LOGE("get account list from database failed, storeID %{public}s.", storeID.c_str());
185         return ret;
186     }
187     OHOS::AccountSA::GetDataByType<int>(accountListJson, accountListJson.end(),
188         Constants::COUNT_ACCOUNT_NUM, createdOsAccountNum, OHOS::AccountSA::JsonType::NUMBER);
189     return ERR_OK;
190 }
191 
UpdateOsAccountIDListInDatabase(const Json & accountListJson)192 void OsAccountDatabaseOperator::UpdateOsAccountIDListInDatabase(const Json &accountListJson)
193 {
194     if (SaveAccountListToDatabase(accountListJson) != ERR_OK) {
195         ACCOUNT_LOGE("update os account id list to database failed.");
196         return;
197     }
198     ACCOUNT_LOGI("update os account id list to database succeed.");
199 }
200 
GetSerialNumberFromDatabase(const std::string & storeID,int64_t & serialNumber)201 ErrCode OsAccountDatabaseOperator::GetSerialNumberFromDatabase(
202     const std::string& storeID, int64_t &serialNumber)
203 {
204     Json accountListJson;
205     ErrCode ret = GetAccountListFromStoreID(storeID, accountListJson);
206     if (ret != ERR_OK) {
207         ACCOUNT_LOGE("get serial number from database failed! err %{public}d, storeID %{public}s.",
208             ret, storeID.c_str());
209         return ret;
210     }
211     OHOS::AccountSA::GetDataByType<int>(accountListJson, accountListJson.end(),
212         Constants::SERIAL_NUMBER_NUM, serialNumber, OHOS::AccountSA::JsonType::NUMBER);
213     return ERR_OK;
214 }
215 
GetMaxAllowCreateIdFromDatabase(const std::string & storeID,int & id)216 ErrCode OsAccountDatabaseOperator::GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id)
217 {
218     Json accountListJson;
219     ErrCode ret = GetAccountListFromStoreID(storeID, accountListJson);
220     if (ret != ERR_OK) {
221         ACCOUNT_LOGE("get max allow created id from database failed. err %{public}d, storeID %{public}s.",
222             ret, storeID.c_str());
223         return ret;
224     }
225     OHOS::AccountSA::GetDataByType<int>(accountListJson, accountListJson.end(),
226         Constants::MAX_ALLOW_CREATE_ACCOUNT_ID, id, OHOS::AccountSA::JsonType::NUMBER);
227     return ERR_OK;
228 }
229 
GetAccountListFromStoreID(const std::string & storeID,Json & accountListJson)230 ErrCode OsAccountDatabaseOperator::GetAccountListFromStoreID(
231     const std::string& storeID, Json &accountListJson)
232 {
233     accountListJson.clear();
234     if (!InnerInit()) {
235         ACCOUNT_LOGE("InnerInit failed! storeID %{public}s!", storeID.c_str());
236         return ERR_ACCOUNT_COMMON_NOT_INIT_ERROR;
237     }
238 
239     std::string accountList;
240     ErrCode errCode = ERR_OK;
241     if (storeID.empty()) {
242         errCode = accountDataStorage_->GetValueFromKvStore(Constants::ACCOUNT_LIST, accountList);
243     } else {
244         std::shared_ptr<AccountDataStorage> storagePtr = std::make_shared<OsAccountDataStorage>(
245             Constants::APP_ID, storeID, Constants::SYNC_OS_ACCOUNT_DATABSE);
246         if (storagePtr == nullptr) {
247             ACCOUNT_LOGE("storagePtr is nullptr, for other storeID %{public}s.", storeID.c_str());
248             return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
249         }
250         errCode = storagePtr->GetValueFromKvStore(Constants::ACCOUNT_LIST, accountList);
251     }
252 
253     if (errCode != ERR_OK) {
254         ACCOUNT_LOGE("Get account list info from database failed! storeID %{public}s.", storeID.c_str());
255         return ERR_OSACCOUNT_SERVICE_CONTROL_GET_ACCOUNT_LIST_ERROR;
256     }
257     accountListJson = Json::parse(accountList, nullptr, false);
258     return ERR_OK;
259 }
260 
SaveAccountListToDatabase(const Json & accountListJson)261 ErrCode OsAccountDatabaseOperator::SaveAccountListToDatabase(const Json &accountListJson)
262 {
263     if (!InnerInit()) {
264         ACCOUNT_LOGE("InnerInit failed!");
265         return ERR_ACCOUNT_COMMON_NOT_INIT_ERROR;
266     }
267     ErrCode errCode = accountDataStorage_->PutValueToKvStore(Constants::ACCOUNT_LIST, accountListJson.dump());
268     if (errCode != ERR_OK) {
269         ACCOUNT_LOGE("Save or Add config info to database failed! errCode %{public}d.", errCode);
270         return ERR_OSACCOUNT_SERVICE_CONTROL_SET_ACCOUNT_LIST_ERROR;
271     }
272     ACCOUNT_LOGI("save or add account list info to database succeed!");
273     return ERR_OK;
274 }
275 }  // namespace AccountSA
276 }  // namespace OHOS