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 inner init, enter!");
43 accountDataStorage_ = std::make_shared<OsAccountDataStorage>(
44 Constants::APP_ID, storeID_, Constants::SYNC_OS_ACCOUNT_DATABASE);
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 inner init, create account list.");
52 std::vector<std::string> accountListVec;
53 Json accountList = Json {
54 {Constants::ACCOUNT_LIST, accountListVec},
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_DATABASE);
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 return ERR_OK;
100 }
101
InsertOsAccountIntoDataBase(const OsAccountInfo & osAccountInfo)102 void OsAccountDatabaseOperator::InsertOsAccountIntoDataBase(const OsAccountInfo &osAccountInfo)
103 {
104 if (!InnerInit()) {
105 ACCOUNT_LOGE("InnerInit failed! target localID %{public}d!", osAccountInfo.GetLocalId());
106 return;
107 }
108
109 if (osAccountInfo.GetLocalId() < Constants::START_USER_ID) {
110 ACCOUNT_LOGI("target os account id %{public}d will not be saved in database!", osAccountInfo.GetLocalId());
111 return;
112 }
113
114 ErrCode errCode = accountDataStorage_->AddAccountInfo(osAccountInfo);
115 if (errCode != ERR_OK) {
116 ACCOUNT_LOGE("AddAccountInfo failed, error code %{public}d, target id %{public}d.",
117 errCode, osAccountInfo.GetLocalId());
118 return;
119 }
120 ACCOUNT_LOGI("insert account %{public}d to database succeed.", osAccountInfo.GetLocalId());
121 }
122
DelOsAccountFromDatabase(const int id)123 void OsAccountDatabaseOperator::DelOsAccountFromDatabase(const int id)
124 {
125 if (!InnerInit()) {
126 ACCOUNT_LOGE("InnerInit failed! id %{public}d!", id);
127 return;
128 }
129
130 ErrCode errCode = accountDataStorage_->RemoveValueFromKvStore(std::to_string(id));
131 if (errCode != ERR_OK) {
132 ACCOUNT_LOGE("delete os account %{public}d from database failed! error code %{public}d.", id, errCode);
133 } else {
134 ACCOUNT_LOGI("delete os account %{public}d from database succeed!", id);
135 }
136 }
137
UpdateOsAccountInDatabase(const OsAccountInfo & osAccountInfo)138 void OsAccountDatabaseOperator::UpdateOsAccountInDatabase(const OsAccountInfo &osAccountInfo)
139 {
140 if (!InnerInit()) {
141 ACCOUNT_LOGE("InnerInit failed! local id %{public}d!", osAccountInfo.GetLocalId());
142 return;
143 }
144
145 ErrCode errCode = accountDataStorage_->SaveAccountInfo(osAccountInfo);
146 if (errCode != ERR_OK) {
147 ACCOUNT_LOGE("update os account info in database for account %{public}d failed! errCode = %{public}d.",
148 osAccountInfo.GetLocalId(), errCode);
149 } else {
150 ACCOUNT_LOGI("update os account info in database for account %{public}d succeed!", osAccountInfo.GetLocalId());
151 }
152 }
153
GetOsAccountFromDatabase(const std::string & storeID,const int id,OsAccountInfo & osAccountInfo)154 ErrCode OsAccountDatabaseOperator::GetOsAccountFromDatabase(const std::string& storeID,
155 const int id,
156 OsAccountInfo &osAccountInfo)
157 {
158 if (!InnerInit()) {
159 ACCOUNT_LOGE("InnerInit failed! storeID %{public}s id %{public}d!", storeID.c_str(), id);
160 return ERR_ACCOUNT_COMMON_NOT_INIT_ERROR;
161 }
162
163 if (storeID.empty()) {
164 return accountDataStorage_->GetAccountInfoById(std::to_string(id), osAccountInfo);
165 }
166
167 std::shared_ptr<AccountDataStorage> storagePtr = std::make_shared<OsAccountDataStorage>(
168 Constants::APP_ID, storeID, Constants::SYNC_OS_ACCOUNT_DATABASE);
169 if (storagePtr == nullptr) {
170 ACCOUNT_LOGE("storagePtr is nullptr, for other storeID %{public}s, id %{public}d.", storeID.c_str(), id);
171 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
172 }
173 return storagePtr->GetAccountInfoById(std::to_string(id), osAccountInfo);
174 }
175
GetCreatedOsAccountNumFromDatabase(const std::string & storeID,int & createdOsAccountNum)176 ErrCode OsAccountDatabaseOperator::GetCreatedOsAccountNumFromDatabase(
177 const std::string& storeID, int &createdOsAccountNum)
178 {
179 Json accountListJson;
180 ErrCode ret = GetAccountListFromStoreID(storeID, accountListJson);
181 if (ret != ERR_OK) {
182 ACCOUNT_LOGE("get account list from database failed, storeID %{public}s.", storeID.c_str());
183 return ret;
184 }
185 OHOS::AccountSA::GetDataByType<int>(accountListJson, accountListJson.end(),
186 Constants::COUNT_ACCOUNT_NUM, createdOsAccountNum, OHOS::AccountSA::JsonType::NUMBER);
187 return ERR_OK;
188 }
189
UpdateOsAccountIDListInDatabase(const Json & accountListJson)190 void OsAccountDatabaseOperator::UpdateOsAccountIDListInDatabase(const Json &accountListJson)
191 {
192 if (SaveAccountListToDatabase(accountListJson) != ERR_OK) {
193 ACCOUNT_LOGE("update os account id list to database failed.");
194 return;
195 }
196 ACCOUNT_LOGD("update os account id list to database succeed.");
197 }
198
GetSerialNumberFromDatabase(const std::string & storeID,int64_t & serialNumber)199 ErrCode OsAccountDatabaseOperator::GetSerialNumberFromDatabase(
200 const std::string& storeID, int64_t &serialNumber)
201 {
202 Json accountListJson;
203 ErrCode ret = GetAccountListFromStoreID(storeID, accountListJson);
204 if (ret != ERR_OK) {
205 ACCOUNT_LOGE("get serial number from database failed! err %{public}d, storeID %{public}s.",
206 ret, storeID.c_str());
207 return ret;
208 }
209 OHOS::AccountSA::GetDataByType<int>(accountListJson, accountListJson.end(),
210 Constants::SERIAL_NUMBER_NUM, serialNumber, OHOS::AccountSA::JsonType::NUMBER);
211 return ERR_OK;
212 }
213
GetMaxAllowCreateIdFromDatabase(const std::string & storeID,int & id)214 ErrCode OsAccountDatabaseOperator::GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id)
215 {
216 Json accountListJson;
217 ErrCode ret = GetAccountListFromStoreID(storeID, accountListJson);
218 if (ret != ERR_OK) {
219 ACCOUNT_LOGE("get max allow created id from database failed. err %{public}d, storeID %{public}s.",
220 ret, storeID.c_str());
221 return ret;
222 }
223 OHOS::AccountSA::GetDataByType<int>(accountListJson, accountListJson.end(),
224 Constants::MAX_ALLOW_CREATE_ACCOUNT_ID, id, OHOS::AccountSA::JsonType::NUMBER);
225 return ERR_OK;
226 }
227
GetAccountListFromStoreID(const std::string & storeID,Json & accountListJson)228 ErrCode OsAccountDatabaseOperator::GetAccountListFromStoreID(
229 const std::string& storeID, Json &accountListJson)
230 {
231 accountListJson.clear();
232 if (!InnerInit()) {
233 ACCOUNT_LOGE("InnerInit failed! storeID %{public}s!", storeID.c_str());
234 return ERR_ACCOUNT_COMMON_NOT_INIT_ERROR;
235 }
236
237 std::string accountList;
238 ErrCode errCode = ERR_OK;
239 if (storeID.empty()) {
240 errCode = accountDataStorage_->GetValueFromKvStore(Constants::ACCOUNT_LIST, accountList);
241 } else {
242 std::shared_ptr<AccountDataStorage> storagePtr = std::make_shared<OsAccountDataStorage>(
243 Constants::APP_ID, storeID, Constants::SYNC_OS_ACCOUNT_DATABASE);
244 if (storagePtr == nullptr) {
245 ACCOUNT_LOGE("storagePtr is nullptr, for other storeID %{public}s.", storeID.c_str());
246 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
247 }
248 errCode = storagePtr->GetValueFromKvStore(Constants::ACCOUNT_LIST, accountList);
249 }
250
251 if (errCode != ERR_OK) {
252 ACCOUNT_LOGE("Get account list info from database failed! storeID %{public}s.", storeID.c_str());
253 return ERR_OSACCOUNT_SERVICE_CONTROL_GET_ACCOUNT_LIST_ERROR;
254 }
255 accountListJson = Json::parse(accountList, nullptr, false);
256 return ERR_OK;
257 }
258
SaveAccountListToDatabase(const Json & accountListJson)259 ErrCode OsAccountDatabaseOperator::SaveAccountListToDatabase(const Json &accountListJson)
260 {
261 if (!InnerInit()) {
262 ACCOUNT_LOGE("InnerInit failed!");
263 return ERR_ACCOUNT_COMMON_NOT_INIT_ERROR;
264 }
265 ErrCode errCode = accountDataStorage_->PutValueToKvStore(Constants::ACCOUNT_LIST, accountListJson.dump());
266 if (errCode != ERR_OK) {
267 ACCOUNT_LOGE("Save or Add config info to database failed! errCode %{public}d.", errCode);
268 return ERR_OSACCOUNT_SERVICE_CONTROL_SET_ACCOUNT_LIST_ERROR;
269 }
270 ACCOUNT_LOGD("save or add account list info to database succeed!");
271 return ERR_OK;
272 }
273 } // namespace AccountSA
274 } // namespace OHOS