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