1 /*
2 * Copyright (c) 2021-2023 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_control_file_manager.h"
16 #include <dirent.h>
17 #include <sstream>
18 #include <sys/types.h>
19 #ifdef WITH_SELINUX
20 #include <policycoreutils.h>
21 #endif // WITH_SELINUX
22 #include "account_log_wrapper.h"
23 #include "os_account_constants.h"
24 #include "os_account_interface.h"
25
26 namespace OHOS {
27 namespace AccountSA {
28 const std::string DEFAULT_ACTIVATED_ACCOUNT_ID = "DefaultActivatedAccountID";
29
GetValidAccountID(const std::string & dirName,std::int32_t & accountID)30 bool GetValidAccountID(const std::string& dirName, std::int32_t& accountID)
31 {
32 // check length first
33 if (dirName.empty() || dirName.size() > Constants::MAX_USER_ID_LENGTH) {
34 return false;
35 }
36
37 auto iter = std::any_of(dirName.begin(), dirName.end(),
38 [dirName](char c) {
39 return (c < '0' || c > '9');
40 });
41 if (iter) {
42 return false;
43 }
44
45 // convert to osaccount id
46 std::stringstream sstream;
47 sstream << dirName;
48 sstream >> accountID;
49 return (accountID >= Constants::ADMIN_LOCAL_ID && accountID <= Constants::MAX_USER_ID);
50 }
51
OsAccountControlFileManager()52 OsAccountControlFileManager::OsAccountControlFileManager()
53 {
54 accountFileOperator_ = std::make_shared<AccountFileOperator>();
55 #ifdef HAS_KV_STORE_PART
56 osAccountDataBaseOperator_ = std::make_shared<OsAccountDatabaseOperator>();
57 #endif
58 osAccountFileOperator_ = std::make_shared<OsAccountFileOperator>();
59 osAccountPhotoOperator_ = std::make_shared<OsAccountPhotoOperator>();
60 }
~OsAccountControlFileManager()61 OsAccountControlFileManager::~OsAccountControlFileManager()
62 {}
Init()63 void OsAccountControlFileManager::Init()
64 {
65 ACCOUNT_LOGI("OsAccountControlFileManager Init start");
66 osAccountFileOperator_->Init();
67 if (!accountFileOperator_->IsJsonFileReady(Constants::ACCOUNT_LIST_FILE_JSON_PATH)) {
68 ACCOUNT_LOGI("OsAccountControlFileManager there is not have valid account list, create!");
69 RecoverAccountListJsonFile();
70 #ifdef WITH_SELINUX
71 Restorecon(Constants::ACCOUNT_LIST_FILE_JSON_PATH.c_str());
72 #endif // WITH_SELINUX
73 }
74 if (!accountFileOperator_->IsJsonFileReady(Constants::BASE_OSACCOUNT_CONSTRAINTS_JSON_PATH)) {
75 ACCOUNT_LOGI("OsAccountControlFileManager there is not have valid account list, create!");
76 BuildAndSaveBaseOAConstraintsJsonFile();
77 #ifdef WITH_SELINUX
78 Restorecon(Constants::BASE_OSACCOUNT_CONSTRAINTS_JSON_PATH.c_str());
79 #endif // WITH_SELINUX
80 }
81 if (!accountFileOperator_->IsJsonFileReady(Constants::GLOBAL_OSACCOUNT_CONSTRAINTS_JSON_PATH)) {
82 ACCOUNT_LOGI("OsAccountControlFileManager there is not have valid account list, create!");
83 BuildAndSaveGlobalOAConstraintsJsonFile();
84 #ifdef WITH_SELINUX
85 Restorecon(Constants::GLOBAL_OSACCOUNT_CONSTRAINTS_JSON_PATH.c_str());
86 #endif // WITH_SELINUX
87 }
88 if (!accountFileOperator_->IsJsonFileReady(Constants::SPECIFIC_OSACCOUNT_CONSTRAINTS_JSON_PATH)) {
89 ACCOUNT_LOGI("OsAccountControlFileManager there is not have valid account list, create!");
90 BuildAndSaveSpecificOAConstraintsJsonFile();
91 #ifdef WITH_SELINUX
92 Restorecon(Constants::SPECIFIC_OSACCOUNT_CONSTRAINTS_JSON_PATH.c_str());
93 #endif // WITH_SELINUX
94 }
95 ACCOUNT_LOGI("OsAccountControlFileManager Init end");
96 }
97
BuildAndSaveAccountListJsonFile(const std::vector<std::string> & accounts)98 void OsAccountControlFileManager::BuildAndSaveAccountListJsonFile(const std::vector<std::string>& accounts)
99 {
100 ACCOUNT_LOGD("enter.");
101 Json accountList = Json {
102 {Constants::ACCOUNT_LIST, accounts},
103 {Constants::COUNT_ACCOUNT_NUM, accounts.size()},
104 {DEFAULT_ACTIVATED_ACCOUNT_ID, Constants::START_USER_ID},
105 {Constants::MAX_ALLOW_CREATE_ACCOUNT_ID, Constants::MAX_USER_ID},
106 {Constants::SERIAL_NUMBER_NUM, Constants::SERIAL_NUMBER_NUM_START},
107 {Constants::IS_SERIAL_NUMBER_FULL, Constants::IS_SERIAL_NUMBER_FULL_INIT_VALUE},
108 };
109 SaveAccountListToFile(accountList);
110 }
111
BuildAndSaveBaseOAConstraintsJsonFile()112 void OsAccountControlFileManager::BuildAndSaveBaseOAConstraintsJsonFile()
113 {
114 ACCOUNT_LOGI("enter.");
115 std::vector<std::string> baseOAConstraints;
116 if (osAccountFileOperator_->GetConstraintsByType(OsAccountType::ADMIN, baseOAConstraints) != ERR_OK) {
117 ACCOUNT_LOGE("get %{public}d base os account constraints failed.", Constants::START_USER_ID);
118 return;
119 }
120 Json baseOsAccountConstraints = Json {
121 {Constants::START_USER_STRING_ID, baseOAConstraints}
122 };
123 SaveBaseOAConstraintsToFile(baseOsAccountConstraints);
124 }
125
BuildAndSaveGlobalOAConstraintsJsonFile()126 void OsAccountControlFileManager::BuildAndSaveGlobalOAConstraintsJsonFile()
127 {
128 ACCOUNT_LOGI("enter.");
129 Json globalOsAccountConstraints = Json {
130 {Constants::DEVICE_OWNER_ID, -1},
131 {Constants::ALL_GLOBAL_CONSTRAINTS, {}}
132 };
133 SaveGlobalOAConstraintsToFile(globalOsAccountConstraints);
134 }
135
BuildAndSaveSpecificOAConstraintsJsonFile()136 void OsAccountControlFileManager::BuildAndSaveSpecificOAConstraintsJsonFile()
137 {
138 Json OsAccountConstraintsList = Json {
139 {Constants::ALL_SPECIFIC_CONSTRAINTS, {}},
140 };
141 Json specificOsAccountConstraints = Json {
142 {Constants::START_USER_STRING_ID, OsAccountConstraintsList},
143 };
144 SaveSpecificOAConstraintsToFile(specificOsAccountConstraints);
145 }
146
RecoverAccountListJsonFile()147 void OsAccountControlFileManager::RecoverAccountListJsonFile()
148 {
149 // get account list
150 std::vector<std::string> accounts;
151 DIR* rootDir = opendir(Constants::USER_INFO_BASE.c_str());
152 if (rootDir == nullptr) {
153 accounts.push_back(std::to_string(Constants::START_USER_ID)); // account 100 always exists
154 BuildAndSaveAccountListJsonFile(accounts);
155 ACCOUNT_LOGE("cannot open dir %{public}s, err %{public}d.", Constants::USER_INFO_BASE.c_str(), errno);
156 return;
157 }
158
159 struct dirent* curDir = nullptr;
160 while ((curDir = readdir(rootDir)) != nullptr) {
161 std::string curDirName(curDir->d_name);
162 if (curDirName == "." || curDirName == ".." || curDir->d_type != DT_DIR) {
163 continue;
164 }
165
166 // get and check os account id
167 std::int32_t accountID = Constants::INVALID_OS_ACCOUNT_ID;
168 if (!GetValidAccountID(curDirName, accountID)) {
169 ACCOUNT_LOGE("invalid account id %{public}s detected in %{public}s.", curDirName.c_str(),
170 Constants::USER_INFO_BASE.c_str());
171 continue;
172 }
173
174 // check repeat
175 bool sameAccountID = false;
176 std::string curAccountIDStr = std::to_string(accountID);
177 for (size_t i = 0; i < accounts.size(); ++i) {
178 if (accounts[i] == curAccountIDStr) {
179 ACCOUNT_LOGE("repeated account id %{public}s detected in %{public}s.", curAccountIDStr.c_str(),
180 Constants::USER_INFO_BASE.c_str());
181 sameAccountID = true;
182 break;
183 }
184 }
185
186 if (!sameAccountID && accountID >= Constants::START_USER_ID) {
187 accounts.push_back(curAccountIDStr);
188 }
189 }
190
191 (void)closedir(rootDir);
192 BuildAndSaveAccountListJsonFile(accounts);
193 }
194
GetOsAccountList(std::vector<OsAccountInfo> & osAccountList)195 ErrCode OsAccountControlFileManager::GetOsAccountList(std::vector<OsAccountInfo> &osAccountList)
196 {
197 osAccountList.clear();
198 Json accountListJson;
199 ErrCode result = GetAccountListFromFile(accountListJson);
200 if (result != ERR_OK) {
201 ACCOUNT_LOGE("GetAccountListFromFile failed!");
202 return result;
203 }
204 const auto &jsonObjectEnd = accountListJson.end();
205 std::vector<std::string> idList;
206 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
207 accountListJson, jsonObjectEnd, Constants::ACCOUNT_LIST, idList, OHOS::AccountSA::JsonType::ARRAY);
208 if (idList.empty()) {
209 return ERR_OK;
210 }
211 for (const auto &it : idList) {
212 OsAccountInfo osAccountInfo;
213 if (GetOsAccountInfoById(std::atoi(it.c_str()), osAccountInfo) == ERR_OK) {
214 if (osAccountInfo.GetPhoto() != "") {
215 std::string photo = osAccountInfo.GetPhoto();
216 GetPhotoById(osAccountInfo.GetLocalId(), photo);
217 osAccountInfo.SetPhoto(photo);
218 }
219 osAccountList.push_back(osAccountInfo);
220 }
221 }
222 return ERR_OK;
223 }
224
GetOsAccountInfoById(const int id,OsAccountInfo & osAccountInfo)225 ErrCode OsAccountControlFileManager::GetOsAccountInfoById(const int id, OsAccountInfo &osAccountInfo)
226 {
227 std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) +
228 Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
229 if (!accountFileOperator_->IsExistFile(path)) {
230 ACCOUNT_LOGE("file %{public}s does not exist err", path.c_str());
231 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
232 }
233 std::string accountInfoStr;
234 if (accountFileOperator_->GetFileContentByPath(path, accountInfoStr) != ERR_OK) {
235 ACCOUNT_LOGE("get content from file %{public}s failed!", path.c_str());
236 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
237 }
238 osAccountInfo.FromJson(Json::parse(accountInfoStr, nullptr, false));
239 return ERR_OK;
240 }
241
GetConstraintsByType(const OsAccountType type,std::vector<std::string> & constraints)242 ErrCode OsAccountControlFileManager::GetConstraintsByType(
243 const OsAccountType type, std::vector<std::string> &constraints)
244 {
245 int typeInit = static_cast<int>(type);
246 return osAccountFileOperator_->GetConstraintsByType(typeInit, constraints);
247 }
248
UpdateBaseOAConstraints(const std::string & idStr,const std::vector<std::string> & ConstraintStr,bool isAdd)249 ErrCode OsAccountControlFileManager::UpdateBaseOAConstraints(const std::string& idStr,
250 const std::vector<std::string>& ConstraintStr, bool isAdd)
251 {
252 Json baseOAConstraintsJson;
253 ErrCode result = GetBaseOAConstraintsFromFile(baseOAConstraintsJson);
254 if (result != ERR_OK) {
255 ACCOUNT_LOGE("get baseOAConstraints from json file failed!");
256 return result;
257 }
258
259 if (baseOAConstraintsJson.find(idStr) == baseOAConstraintsJson.end()) {
260 if (!isAdd) {
261 return ERR_OK;
262 }
263 baseOAConstraintsJson.emplace(idStr, ConstraintStr);
264 } else {
265 std::vector<std::string> baseOAConstraints;
266 auto jsonEnd = baseOAConstraintsJson.end();
267 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
268 baseOAConstraintsJson, jsonEnd, idStr, baseOAConstraints, OHOS::AccountSA::JsonType::ARRAY);
269 for (auto it = ConstraintStr.begin(); it != ConstraintStr.end(); it++) {
270 if (!isAdd) {
271 baseOAConstraints.erase(std::remove(baseOAConstraints.begin(), baseOAConstraints.end(), *it),
272 baseOAConstraints.end());
273 continue;
274 }
275 if (std::find(baseOAConstraints.begin(), baseOAConstraints.end(), *it) == baseOAConstraints.end()) {
276 baseOAConstraints.emplace_back(*it);
277 }
278 }
279 baseOAConstraintsJson[idStr] = baseOAConstraints;
280 }
281 return SaveBaseOAConstraintsToFile(baseOAConstraintsJson);
282 }
283
UpdateGlobalOAConstraints(const std::string & idStr,const std::vector<std::string> & ConstraintStr,bool isAdd)284 ErrCode OsAccountControlFileManager::UpdateGlobalOAConstraints(
285 const std::string& idStr, const std::vector<std::string>& ConstraintStr, bool isAdd)
286 {
287 Json globalOAConstraintsJson;
288 ErrCode result = GetGlobalOAConstraintsFromFile(globalOAConstraintsJson);
289 if (result != ERR_OK) {
290 ACCOUNT_LOGE("get globalOAConstraints from file failed!");
291 return result;
292 }
293 GlobalConstraintsDataOperate(idStr, ConstraintStr, isAdd, globalOAConstraintsJson);
294 return SaveGlobalOAConstraintsToFile(globalOAConstraintsJson);
295 }
296
GlobalConstraintsDataOperate(const std::string & idStr,const std::vector<std::string> & ConstraintStr,bool isAdd,Json & globalOAConstraintsJson)297 void OsAccountControlFileManager::GlobalConstraintsDataOperate(const std::string& idStr,
298 const std::vector<std::string>& ConstraintStr, bool isAdd, Json &globalOAConstraintsJson)
299 {
300 std::vector<std::string> globalOAConstraintsList;
301 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(globalOAConstraintsJson, globalOAConstraintsJson.end(),
302 Constants::ALL_GLOBAL_CONSTRAINTS, globalOAConstraintsList, OHOS::AccountSA::JsonType::ARRAY);
303 std::vector<std::string> waitForErase;
304 for (auto it = ConstraintStr.begin(); it != ConstraintStr.end(); it++) {
305 if (!isAdd) {
306 if (std::find(globalOAConstraintsList.begin(),
307 globalOAConstraintsList.end(), *it) == globalOAConstraintsList.end()) {
308 continue;
309 }
310 std::vector<std::string> constraintSourceList;
311 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(globalOAConstraintsJson,
312 globalOAConstraintsJson.end(), *it, constraintSourceList, OHOS::AccountSA::JsonType::ARRAY);
313 constraintSourceList.erase(std::remove(constraintSourceList.begin(), constraintSourceList.end(), idStr),
314 constraintSourceList.end());
315 if (constraintSourceList.size() == 0) {
316 globalOAConstraintsList.erase(std::remove(globalOAConstraintsList.begin(),
317 globalOAConstraintsList.end(), *it), globalOAConstraintsList.end());
318 globalOAConstraintsJson[Constants::ALL_GLOBAL_CONSTRAINTS] = globalOAConstraintsList;
319 waitForErase.push_back(*it);
320 } else {
321 globalOAConstraintsJson[*it] = constraintSourceList;
322 }
323 continue;
324 }
325 if (std::find(globalOAConstraintsList.begin(),
326 globalOAConstraintsList.end(), *it) != globalOAConstraintsList.end()) {
327 std::vector<std::string> constraintSourceList;
328 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(globalOAConstraintsJson,
329 globalOAConstraintsJson.end(), *it, constraintSourceList, OHOS::AccountSA::JsonType::ARRAY);
330 if (std::find(constraintSourceList.begin(),
331 constraintSourceList.end(), idStr) == constraintSourceList.end()) {
332 constraintSourceList.emplace_back(idStr);
333 globalOAConstraintsJson[*it] = constraintSourceList;
334 }
335 continue;
336 }
337 std::vector<std::string> constraintSourceList;
338 constraintSourceList.emplace_back(idStr);
339 globalOAConstraintsList.emplace_back(*it);
340 globalOAConstraintsJson.emplace(*it, constraintSourceList);
341 globalOAConstraintsJson[Constants::ALL_GLOBAL_CONSTRAINTS] = globalOAConstraintsList;
342 }
343 for (auto keyStr : waitForErase) {
344 globalOAConstraintsJson.erase(keyStr);
345 }
346 }
347
UpdateSpecificOAConstraints(const std::string & idStr,const std::string & targetIdStr,const std::vector<std::string> & ConstraintStr,bool isAdd)348 ErrCode OsAccountControlFileManager::UpdateSpecificOAConstraints(
349 const std::string& idStr, const std::string& targetIdStr, const std::vector<std::string>& ConstraintStr, bool isAdd)
350 {
351 Json specificOAConstraintsJson;
352 ErrCode result = GetSpecificOAConstraintsFromFile(specificOAConstraintsJson);
353 if (result != ERR_OK) {
354 ACCOUNT_LOGE("get specificOAConstraints from file failed!");
355 return result;
356 }
357 if (specificOAConstraintsJson.find(targetIdStr) == specificOAConstraintsJson.end()) {
358 if (!isAdd) {
359 return ERR_OK;
360 }
361 Json osAccountConstraintsList = Json {
362 {Constants::ALL_SPECIFIC_CONSTRAINTS, {}},
363 };
364 specificOAConstraintsJson.emplace(targetIdStr, osAccountConstraintsList);
365 }
366 Json userPrivateConstraintsDataJson = specificOAConstraintsJson[targetIdStr];
367 SpecificConstraintsDataOperate(idStr, targetIdStr, ConstraintStr, isAdd, userPrivateConstraintsDataJson);
368 specificOAConstraintsJson[targetIdStr] = userPrivateConstraintsDataJson;
369 return SaveSpecificOAConstraintsToFile(specificOAConstraintsJson);
370 }
371
SpecificConstraintsDataOperate(const std::string & idStr,const std::string & targetIdStr,const std::vector<std::string> & ConstraintStr,bool isAdd,Json & userPrivateConstraintsDataJson)372 void OsAccountControlFileManager::SpecificConstraintsDataOperate(
373 const std::string& idStr, const std::string& targetIdStr, const std::vector<std::string>& ConstraintStr,
374 bool isAdd, Json& userPrivateConstraintsDataJson)
375 {
376 std::vector<std::string> specificOAConstraintsList;
377 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(userPrivateConstraintsDataJson,
378 userPrivateConstraintsDataJson.end(), Constants::ALL_SPECIFIC_CONSTRAINTS,
379 specificOAConstraintsList, OHOS::AccountSA::JsonType::ARRAY);
380 std::vector<std::string> waitForErase;
381 for (auto it = ConstraintStr.begin(); it != ConstraintStr.end(); it++) {
382 if (!isAdd) {
383 if (userPrivateConstraintsDataJson.find(*it) == userPrivateConstraintsDataJson.end()) {
384 continue;
385 }
386 std::vector<std::string> constraintSourceList;
387 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(userPrivateConstraintsDataJson,
388 userPrivateConstraintsDataJson.end(), *it, constraintSourceList, OHOS::AccountSA::JsonType::ARRAY);
389 constraintSourceList.erase(std::remove(constraintSourceList.begin(), constraintSourceList.end(), idStr),
390 constraintSourceList.end());
391 if (constraintSourceList.size() == 0) {
392 specificOAConstraintsList.erase(std::remove(specificOAConstraintsList.begin(),
393 specificOAConstraintsList.end(), *it), specificOAConstraintsList.end());
394 userPrivateConstraintsDataJson[Constants::ALL_SPECIFIC_CONSTRAINTS] = specificOAConstraintsList;
395 waitForErase.push_back(*it);
396 } else {
397 userPrivateConstraintsDataJson[*it] = constraintSourceList;
398 }
399 continue;
400 }
401 if (std::find(specificOAConstraintsList.begin(),
402 specificOAConstraintsList.end(), *it) != specificOAConstraintsList.end()) {
403 std::vector<std::string> constraintSourceList;
404 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(userPrivateConstraintsDataJson,
405 userPrivateConstraintsDataJson.end(), *it, constraintSourceList, OHOS::AccountSA::JsonType::ARRAY);
406 if (std::find(constraintSourceList.begin(),
407 constraintSourceList.end(), idStr) == constraintSourceList.end()) {
408 constraintSourceList.emplace_back(idStr);
409 userPrivateConstraintsDataJson[*it] = constraintSourceList;
410 }
411 continue;
412 }
413 std::vector<std::string> constraintSourceList;
414 constraintSourceList.emplace_back(idStr);
415 specificOAConstraintsList.emplace_back(*it);
416 userPrivateConstraintsDataJson.emplace(*it, constraintSourceList);
417 userPrivateConstraintsDataJson[Constants::ALL_SPECIFIC_CONSTRAINTS] = specificOAConstraintsList;
418 }
419 for (auto keyStr : waitForErase) {
420 userPrivateConstraintsDataJson.erase(keyStr);
421 }
422 }
423
RemoveOAConstraintsInfo(const int32_t id)424 ErrCode OsAccountControlFileManager::RemoveOAConstraintsInfo(const int32_t id)
425 {
426 ErrCode errCode = RemoveOABaseConstraintsInfo(id);
427 if (errCode != ERR_OK) {
428 ACCOUNT_LOGE("remove os account %{public}d base constraints info failed!", id);
429 return errCode;
430 }
431 errCode = RemoveOAGlobalConstraintsInfo(id);
432 if (errCode != ERR_OK) {
433 ACCOUNT_LOGE("remove os account %{public}d global constraints info failed!", id);
434 return errCode;
435 }
436 errCode = RemoveOASpecificConstraintsInfo(id);
437 if (errCode != ERR_OK) {
438 ACCOUNT_LOGE("remove os account %{public}d specific constraints info failed!", id);
439 return errCode;
440 }
441 return ERR_OK;
442 }
443
RemoveOABaseConstraintsInfo(const int32_t id)444 ErrCode OsAccountControlFileManager::RemoveOABaseConstraintsInfo(const int32_t id)
445 {
446 Json baseOAConstraintsJson;
447 ErrCode result = GetBaseOAConstraintsFromFile(baseOAConstraintsJson);
448 if (result != ERR_OK) {
449 ACCOUNT_LOGE("get baseOAConstraints from file failed!");
450 return result;
451 }
452 baseOAConstraintsJson.erase(std::to_string(id));
453 result = SaveBaseOAConstraintsToFile(baseOAConstraintsJson);
454 if (result != ERR_OK) {
455 ACCOUNT_LOGE("SaveBaseOAConstraintsToFile failed!");
456 return result;
457 }
458 return ERR_OK;
459 }
460
RemoveOAGlobalConstraintsInfo(const int32_t id)461 ErrCode OsAccountControlFileManager::RemoveOAGlobalConstraintsInfo(const int32_t id)
462 {
463 Json globalOAConstraintsJson;
464 ErrCode result = GetGlobalOAConstraintsFromFile(globalOAConstraintsJson);
465 if (result != ERR_OK) {
466 ACCOUNT_LOGE("get globalOAConstraints from file failed!");
467 return result;
468 }
469 std::vector<std::string> waitForErase;
470 for (auto it = globalOAConstraintsJson.begin(); it != globalOAConstraintsJson.end(); it++) {
471 if (it.key() != Constants::ALL_GLOBAL_CONSTRAINTS && it.key() != Constants::DEVICE_OWNER_ID) {
472 std::vector<std::string> sourceList;
473 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(globalOAConstraintsJson,
474 globalOAConstraintsJson.end(),
475 it.key(),
476 sourceList,
477 OHOS::AccountSA::JsonType::ARRAY);
478 sourceList.erase(std::remove(sourceList.begin(), sourceList.end(), std::to_string(id)), sourceList.end());
479 if (sourceList.size() == 0) {
480 std::vector<std::string> allGlobalConstraints;
481 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(globalOAConstraintsJson,
482 globalOAConstraintsJson.end(),
483 Constants::ALL_GLOBAL_CONSTRAINTS,
484 allGlobalConstraints,
485 OHOS::AccountSA::JsonType::ARRAY);
486 allGlobalConstraints.erase(std::remove(allGlobalConstraints.begin(),
487 allGlobalConstraints.end(), it.key()), allGlobalConstraints.end());
488 globalOAConstraintsJson[Constants::ALL_GLOBAL_CONSTRAINTS] = allGlobalConstraints;
489 waitForErase.push_back(it.key());
490 } else {
491 globalOAConstraintsJson[it.key()] = sourceList;
492 }
493 }
494 }
495 for (auto keyStr : waitForErase) {
496 globalOAConstraintsJson.erase(keyStr);
497 }
498 return SaveGlobalOAConstraintsToFile(globalOAConstraintsJson);
499 }
500
RemoveOASpecificConstraintsInfo(const int32_t id)501 ErrCode OsAccountControlFileManager::RemoveOASpecificConstraintsInfo(const int32_t id)
502 {
503 Json specificOAConstraintsJson;
504 ErrCode result = GetSpecificOAConstraintsFromFile(specificOAConstraintsJson);
505 if (result != ERR_OK) {
506 ACCOUNT_LOGE("get specificOAConstraints from file failed!");
507 return result;
508 }
509 if (specificOAConstraintsJson.find(std::to_string(id)) != specificOAConstraintsJson.end()) {
510 specificOAConstraintsJson.erase(std::to_string(id));
511 }
512 for (auto it = specificOAConstraintsJson.begin(); it != specificOAConstraintsJson.end(); it++) {
513 std::vector<std::string> waitForErase;
514 Json userPrivateConstraintsJson;
515 OHOS::AccountSA::GetDataByType<Json>(specificOAConstraintsJson, specificOAConstraintsJson.end(),
516 it.key(), userPrivateConstraintsJson, OHOS::AccountSA::JsonType::OBJECT);
517 std::vector<std::string> allSpecificConstraints;
518 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(userPrivateConstraintsJson,
519 userPrivateConstraintsJson.end(), Constants::ALL_SPECIFIC_CONSTRAINTS,
520 allSpecificConstraints, OHOS::AccountSA::JsonType::ARRAY);
521 if (allSpecificConstraints.size() == 0) {
522 continue;
523 }
524 for (auto item = userPrivateConstraintsJson.begin(); item != userPrivateConstraintsJson.end(); item++) {
525 if (item.key() == Constants::ALL_SPECIFIC_CONSTRAINTS) {
526 continue;
527 }
528 std::vector<std::string> sourceList;
529 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(userPrivateConstraintsJson,
530 userPrivateConstraintsJson.end(), item.key(), sourceList, OHOS::AccountSA::JsonType::ARRAY);
531 sourceList.erase(std::remove(sourceList.begin(),
532 sourceList.end(), std::to_string(id)), sourceList.end());
533 if (sourceList.size() == 0) {
534 allSpecificConstraints.erase(std::remove(allSpecificConstraints.begin(),
535 allSpecificConstraints.end(), item.key()), allSpecificConstraints.end());
536 userPrivateConstraintsJson[Constants::ALL_SPECIFIC_CONSTRAINTS] = allSpecificConstraints;
537 waitForErase.push_back(item.key());
538 } else {
539 userPrivateConstraintsJson[item.key()] = sourceList;
540 }
541 }
542 for (auto keyStr : waitForErase) {
543 userPrivateConstraintsJson.erase(keyStr);
544 }
545 specificOAConstraintsJson[it.key()] = userPrivateConstraintsJson;
546 }
547 return SaveSpecificOAConstraintsToFile(specificOAConstraintsJson);
548 }
549
UpdateAccountList(const std::string & idStr,bool isAdd)550 ErrCode OsAccountControlFileManager::UpdateAccountList(const std::string& idStr, bool isAdd)
551 {
552 Json accountListJson;
553 ErrCode result = GetAccountListFromFile(accountListJson);
554 if (result != ERR_OK) {
555 ACCOUNT_LOGE("get account list failed!");
556 return result;
557 }
558
559 std::vector<std::string> accountIdList;
560 auto jsonEnd = accountListJson.end();
561 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
562 accountListJson, jsonEnd, Constants::ACCOUNT_LIST, accountIdList, OHOS::AccountSA::JsonType::ARRAY);
563
564 if (isAdd) {
565 // check repeat
566 if (std::find(accountIdList.begin(), accountIdList.end(), idStr) != accountIdList.end()) {
567 return ERR_OK; // already exist, no need to add.
568 }
569 accountIdList.emplace_back(idStr);
570 } else {
571 accountIdList.erase(std::remove(accountIdList.begin(), accountIdList.end(), idStr), accountIdList.end());
572 }
573 accountListJson[Constants::ACCOUNT_LIST] = accountIdList;
574 accountListJson[Constants::COUNT_ACCOUNT_NUM] = accountIdList.size();
575 return SaveAccountListToFileAndDataBase(accountListJson);
576 }
577
InsertOsAccount(OsAccountInfo & osAccountInfo)578 ErrCode OsAccountControlFileManager::InsertOsAccount(OsAccountInfo &osAccountInfo)
579 {
580 ACCOUNT_LOGD("enter");
581 if (osAccountInfo.GetLocalId() < Constants::ADMIN_LOCAL_ID ||
582 osAccountInfo.GetLocalId() > Constants::MAX_USER_ID) {
583 ACCOUNT_LOGE("error id %{public}d cannot insert", osAccountInfo.GetLocalId());
584 return ERR_OSACCOUNT_SERVICE_CONTROL_ID_CANNOT_CREATE_ERROR;
585 }
586
587 std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + osAccountInfo.GetPrimeKey() +
588 Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
589 if (accountFileOperator_->IsExistFile(path) && accountFileOperator_->IsJsonFormat(path)) {
590 ACCOUNT_LOGE("OsAccountControlFileManagerInsertOsAccountControlFileManagerCreateAccountDir ERR");
591 return ERR_OSACCOUNT_SERVICE_CONTROL_INSERT_FILE_EXISTS_ERROR;
592 }
593
594 std::string accountInfoStr = osAccountInfo.ToString();
595 if (accountInfoStr.empty()) {
596 ACCOUNT_LOGE("os account info is empty! maybe some illegal characters caused exception!");
597 return ERR_OSACCOUNT_SERVICE_ACCOUNT_INFO_EMPTY_ERROR;
598 }
599 ErrCode result = accountFileOperator_->InputFileByPathAndContent(path, accountInfoStr);
600 if (result != ERR_OK) {
601 ACCOUNT_LOGE("InputFileByPathAndContent failed! path %{public}s.", path.c_str());
602 return result;
603 }
604 #ifdef HAS_KV_STORE_PART
605 osAccountDataBaseOperator_->InsertOsAccountIntoDataBase(osAccountInfo);
606 #endif
607
608 if (osAccountInfo.GetLocalId() >= Constants::START_USER_ID) {
609 return UpdateAccountList(osAccountInfo.GetPrimeKey(), true);
610 }
611 ACCOUNT_LOGD("end");
612 return ERR_OK;
613 }
614
DelOsAccount(const int id)615 ErrCode OsAccountControlFileManager::DelOsAccount(const int id)
616 {
617 ACCOUNT_LOGD("enter");
618 if (id <= Constants::START_USER_ID || id > Constants::MAX_USER_ID) {
619 ACCOUNT_LOGE("invalid input id %{public}d to delete!", id);
620 return ERR_OSACCOUNT_SERVICE_CONTROL_CANNOT_DELETE_ID_ERROR;
621 }
622
623 std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id);
624 ErrCode result = accountFileOperator_->DeleteDirOrFile(path);
625 if (result != ERR_OK) {
626 ACCOUNT_LOGE("DeleteDirOrFile failed! path %{public}s.", path.c_str());
627 return result;
628 }
629 #ifdef HAS_KV_STORE_PART
630 osAccountDataBaseOperator_->DelOsAccountFromDatabase(id);
631 #endif
632 return UpdateAccountList(std::to_string(id), false);
633 }
634
UpdateOsAccount(OsAccountInfo & osAccountInfo)635 ErrCode OsAccountControlFileManager::UpdateOsAccount(OsAccountInfo &osAccountInfo)
636 {
637 ACCOUNT_LOGD("start");
638 std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + osAccountInfo.GetPrimeKey() +
639 Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
640 if (!accountFileOperator_->IsExistFile(path)) {
641 ACCOUNT_LOGE("path %{public}s does not exist!", path.c_str());
642 return ERR_OSACCOUNT_SERVICE_CONTROL_UPDATE_FILE_NOT_EXISTS_ERROR;
643 }
644
645 std::string accountInfoStr = osAccountInfo.ToString();
646 if (accountInfoStr.empty()) {
647 ACCOUNT_LOGE("account info str is empty!");
648 return ERR_OSACCOUNT_SERVICE_ACCOUNT_INFO_EMPTY_ERROR;
649 }
650 ErrCode result = accountFileOperator_->InputFileByPathAndContent(path, accountInfoStr);
651 if (result != ERR_OK) {
652 return result;
653 }
654
655 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
656 // update in database
657 if (osAccountInfo.GetLocalId() >= Constants::START_USER_ID) {
658 osAccountDataBaseOperator_->UpdateOsAccountInDatabase(osAccountInfo);
659 }
660 #else // DISTRIBUTED_FEATURE_ENABLED
661 ACCOUNT_LOGI("No distributed feature!");
662 #endif // DISTRIBUTED_FEATURE_ENABLED
663
664 ACCOUNT_LOGD("end");
665 return ERR_OK;
666 }
667
GetMaxCreatedOsAccountNum(int & maxCreatedOsAccountNum)668 ErrCode OsAccountControlFileManager::GetMaxCreatedOsAccountNum(int &maxCreatedOsAccountNum)
669 {
670 ACCOUNT_LOGD("start");
671 Json accountListJson;
672 ErrCode result = GetAccountListFromFile(accountListJson);
673 if (result != ERR_OK) {
674 return result;
675 }
676 OHOS::AccountSA::GetDataByType<int>(accountListJson,
677 accountListJson.end(),
678 Constants::MAX_ALLOW_CREATE_ACCOUNT_ID,
679 maxCreatedOsAccountNum,
680 OHOS::AccountSA::JsonType::NUMBER);
681 maxCreatedOsAccountNum -= Constants::START_USER_ID;
682 ACCOUNT_LOGD("end");
683 return ERR_OK;
684 }
685
AccountExistsWithSerialNumber(const std::vector<OsAccountInfo> & osAccountInfos,int serialNumber)686 bool AccountExistsWithSerialNumber(const std::vector<OsAccountInfo>& osAccountInfos, int serialNumber)
687 {
688 for (const auto& accountInfo : osAccountInfos) {
689 if (accountInfo.GetSerialNumber() ==
690 Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN * Constants::CARRY_NUM + serialNumber) {
691 return true;
692 }
693 }
694 return false;
695 }
696
GetSerialNumber(int64_t & serialNumber)697 ErrCode OsAccountControlFileManager::GetSerialNumber(int64_t &serialNumber)
698 {
699 Json accountListJson;
700 ErrCode result = GetAccountListFromFile(accountListJson);
701 if (result != ERR_OK) {
702 ACCOUNT_LOGE("GetSerialNumber get accountList error");
703 return result;
704 }
705 OHOS::AccountSA::GetDataByType<int64_t>(accountListJson, accountListJson.end(), Constants::SERIAL_NUMBER_NUM,
706 serialNumber, OHOS::AccountSA::JsonType::NUMBER);
707 if (serialNumber == Constants::CARRY_NUM) {
708 accountListJson[Constants::IS_SERIAL_NUMBER_FULL] = true;
709 serialNumber = Constants::SERIAL_NUMBER_NUM_START;
710 }
711 bool isSerialNumberFull = false;
712 OHOS::AccountSA::GetDataByType<bool>(accountListJson, accountListJson.end(), Constants::IS_SERIAL_NUMBER_FULL,
713 isSerialNumberFull, OHOS::AccountSA::JsonType::BOOLEAN);
714 if (isSerialNumberFull) {
715 std::vector<OsAccountInfo> osAccountInfos;
716 result = GetOsAccountList(osAccountInfos);
717 if (result != ERR_OK) {
718 ACCOUNT_LOGE("GetSerialNumber get accountList error");
719 return result;
720 }
721 while (serialNumber < Constants::CARRY_NUM) {
722 bool exists = false;
723 exists = AccountExistsWithSerialNumber(osAccountInfos, serialNumber);
724 if (!exists) {
725 break;
726 }
727 serialNumber++;
728 serialNumber = (serialNumber == Constants::CARRY_NUM) ? Constants::SERIAL_NUMBER_NUM_START : serialNumber;
729 }
730 }
731 accountListJson[Constants::SERIAL_NUMBER_NUM] = serialNumber + 1;
732 result = SaveAccountListToFileAndDataBase(accountListJson);
733 if (result != ERR_OK) {
734 return result;
735 }
736 serialNumber = serialNumber + Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN * Constants::CARRY_NUM;
737 return ERR_OK;
738 }
739
GetAllowCreateId(int & id)740 ErrCode OsAccountControlFileManager::GetAllowCreateId(int &id)
741 {
742 Json accountListJson;
743 ErrCode result = GetAccountListFromFile(accountListJson);
744 if (result != ERR_OK) {
745 ACCOUNT_LOGE("GetAllowCreateId get accountList error");
746 return result;
747 }
748 int countCreatedNum = 0;
749 auto jsonEnd = accountListJson.end();
750 OHOS::AccountSA::GetDataByType<int>(
751 accountListJson, jsonEnd, Constants::COUNT_ACCOUNT_NUM, countCreatedNum, OHOS::AccountSA::JsonType::NUMBER);
752 if (countCreatedNum >= Constants::MAX_USER_ID - Constants::START_USER_ID) {
753 ACCOUNT_LOGE("GetAllowCreateId cannot create more account error");
754 return ERR_OSACCOUNT_SERVICE_CONTROL_MAX_CAN_CREATE_ERROR;
755 }
756 std::vector<std::string> accountIdList;
757 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
758 accountListJson, jsonEnd, Constants::ACCOUNT_LIST, accountIdList, OHOS::AccountSA::JsonType::ARRAY);
759 id = Constants::START_USER_ID + 1;
760 while (std::find(accountIdList.begin(), accountIdList.end(), std::to_string(id)) != accountIdList.end() &&
761 id != Constants::MAX_USER_ID + 1) {
762 id++;
763 }
764 if (id == Constants::MAX_USER_ID + 1) {
765 id = -1;
766 return ERR_OSACCOUNT_SERVICE_CONTROL_SELECT_CAN_USE_ID_ERROR;
767 }
768 return ERR_OK;
769 }
770
GetAccountListFromFile(Json & accountListJson)771 ErrCode OsAccountControlFileManager::GetAccountListFromFile(Json &accountListJson)
772 {
773 ACCOUNT_LOGD("enter");
774 accountListJson.clear();
775 std::string accountList;
776 std::lock_guard<std::mutex> lock(accountListFileLock_);
777 ErrCode errCode = accountFileOperator_->GetFileContentByPath(Constants::ACCOUNT_LIST_FILE_JSON_PATH,
778 accountList);
779 if (errCode != ERR_OK) {
780 ACCOUNT_LOGE("GetFileContentByPath failed! error code %{public}d.", errCode);
781 return errCode;
782 }
783 accountListJson = Json::parse(accountList, nullptr, false);
784 ACCOUNT_LOGD("end");
785 return ERR_OK;
786 }
787
GetBaseOAConstraintsFromFile(Json & baseOAConstraintsJson)788 ErrCode OsAccountControlFileManager::GetBaseOAConstraintsFromFile(Json &baseOAConstraintsJson)
789 {
790 baseOAConstraintsJson.clear();
791 std::string baseOAConstraints;
792 std::lock_guard<std::mutex> lock(baseOAConstraintsFileLock_);
793 ErrCode errCode = accountFileOperator_->GetFileContentByPath(
794 Constants::BASE_OSACCOUNT_CONSTRAINTS_JSON_PATH, baseOAConstraints);
795 if (errCode != ERR_OK) {
796 ACCOUNT_LOGE("GetFileContentByPath failed! error code %{public}d.", errCode);
797 return errCode;
798 }
799 baseOAConstraintsJson = Json::parse(baseOAConstraints, nullptr, false);
800 if (!baseOAConstraintsJson.is_object()) {
801 ACCOUNT_LOGE("base constraints json data parse failed code.");
802 return errCode;
803 }
804
805 return ERR_OK;
806 }
807
GetGlobalOAConstraintsFromFile(Json & globalOAConstraintsJson)808 ErrCode OsAccountControlFileManager::GetGlobalOAConstraintsFromFile(Json &globalOAConstraintsJson)
809 {
810 globalOAConstraintsJson.clear();
811 std::string globalOAConstraints;
812 std::lock_guard<std::mutex> lock(globalOAConstraintsFileLock_);
813 ErrCode errCode = accountFileOperator_->GetFileContentByPath(
814 Constants::GLOBAL_OSACCOUNT_CONSTRAINTS_JSON_PATH, globalOAConstraints);
815 if (errCode != ERR_OK) {
816 ACCOUNT_LOGE("GetFileContentByPath failed! error code %{public}d.", errCode);
817 return errCode;
818 }
819 globalOAConstraintsJson = Json::parse(globalOAConstraints, nullptr, false);
820 if (!globalOAConstraintsJson.is_object()) {
821 ACCOUNT_LOGE("global constraints json data parse failed code.");
822 return errCode;
823 }
824
825 return ERR_OK;
826 }
827
GetSpecificOAConstraintsFromFile(Json & specificOAConstraintsJson)828 ErrCode OsAccountControlFileManager::GetSpecificOAConstraintsFromFile(Json &specificOAConstraintsJson)
829 {
830 specificOAConstraintsJson.clear();
831 std::string specificOAConstraints;
832 std::lock_guard<std::mutex> lock(specificOAConstraintsFileLock_);
833 ErrCode errCode = accountFileOperator_->GetFileContentByPath(
834 Constants::SPECIFIC_OSACCOUNT_CONSTRAINTS_JSON_PATH, specificOAConstraints);
835 if (errCode != ERR_OK) {
836 ACCOUNT_LOGE("GetFileContentByPath failed! error code %{public}d.", errCode);
837 return errCode;
838 }
839 specificOAConstraintsJson = Json::parse(specificOAConstraints, nullptr, false);
840 if (!specificOAConstraintsJson.is_object()) {
841 ACCOUNT_LOGE("specific constraints json data parse failed code.");
842 return errCode;
843 }
844
845 return ERR_OK;
846 }
847
IsFromBaseOAConstraintsList(const int32_t id,const std::string constraint,bool & isExist)848 ErrCode OsAccountControlFileManager::IsFromBaseOAConstraintsList(
849 const int32_t id, const std::string constraint, bool &isExist)
850 {
851 isExist = false;
852 std::vector<std::string> constraintsList;
853 ErrCode errCode = osAccountFileOperator_->GetBaseOAConstraintsList(id, constraintsList);
854 if (errCode != ERR_OK) {
855 ACCOUNT_LOGE("GetBaseOAConstraintsList failed! error code %{public}d.", errCode);
856 return errCode;
857 }
858
859 if (std::find(constraintsList.begin(), constraintsList.end(), constraint) != constraintsList.end()) {
860 isExist = true;
861 }
862
863 return ERR_OK;
864 }
865
IsFromGlobalOAConstraintsList(const int32_t id,const int32_t deviceOwnerId,const std::string constraint,std::vector<ConstraintSourceTypeInfo> & globalSourceList)866 ErrCode OsAccountControlFileManager::IsFromGlobalOAConstraintsList(const int32_t id, const int32_t deviceOwnerId,
867 const std::string constraint, std::vector<ConstraintSourceTypeInfo> &globalSourceList)
868 {
869 globalSourceList.clear();
870 std::vector<std::string> constraintsList;
871 ErrCode errCode = osAccountFileOperator_->GetGlobalOAConstraintsList(constraintsList);
872 if (errCode != ERR_OK) {
873 ACCOUNT_LOGE("GetGlobalOAConstraintsList failed! error code %{public}d.", errCode);
874 return errCode;
875 }
876 if (constraintsList.size() == 0) {
877 return ERR_OK;
878 }
879 if (std::find(constraintsList.begin(), constraintsList.end(), constraint) != constraintsList.end()) {
880 Json globalOAConstraintsJson;
881 errCode = GetGlobalOAConstraintsFromFile(globalOAConstraintsJson);
882 if (errCode != ERR_OK) {
883 ACCOUNT_LOGE("get globalOAConstraints from file failed!");
884 return errCode;
885 }
886 std::vector<std::string> globalOAConstraintsList;
887 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
888 globalOAConstraintsJson,
889 globalOAConstraintsJson.end(),
890 constraint,
891 globalOAConstraintsList,
892 OHOS::AccountSA::JsonType::ARRAY);
893 ConstraintSourceTypeInfo constraintSourceTypeInfo;
894 for (auto it = globalOAConstraintsList.begin(); it != globalOAConstraintsList.end(); it++) {
895 if (stoi(*it) == deviceOwnerId) {
896 constraintSourceTypeInfo.localId = stoi(*it);
897 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_TYPE_DEVICE_OWNER;
898 globalSourceList.push_back(constraintSourceTypeInfo);
899 } else {
900 constraintSourceTypeInfo.localId = stoi(*it);
901 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_TYPE_PROFILE_OWNER;
902 globalSourceList.push_back(constraintSourceTypeInfo);
903 }
904 }
905 }
906 return ERR_OK;
907 }
908
IsFromSpecificOAConstraintsList(const int32_t id,const int32_t deviceOwnerId,const std::string constraint,std::vector<ConstraintSourceTypeInfo> & specificSourceList)909 ErrCode OsAccountControlFileManager::IsFromSpecificOAConstraintsList(const int32_t id, const int32_t deviceOwnerId,
910 const std::string constraint, std::vector<ConstraintSourceTypeInfo> &specificSourceList)
911 {
912 specificSourceList.clear();
913 std::vector<std::string> constraintsList;
914 ErrCode errCode = osAccountFileOperator_->GetSpecificOAConstraintsList(id, constraintsList);
915 if (errCode != ERR_OK) {
916 ACCOUNT_LOGE("GetSpecificOAConstraintsList failed! error code %{public}d.", errCode);
917 return errCode;
918 }
919
920 if (std::find(constraintsList.begin(), constraintsList.end(), constraint) != constraintsList.end()) {
921 Json specificOAConstraintsJson;
922 errCode = GetSpecificOAConstraintsFromFile(specificOAConstraintsJson);
923 if (errCode != ERR_OK) {
924 ACCOUNT_LOGE("get specificOAConstraints from file failed!");
925 return errCode;
926 }
927 Json specificOAConstraintsInfo;
928 OHOS::AccountSA::GetDataByType<Json>(specificOAConstraintsJson, specificOAConstraintsJson.end(),
929 std::to_string(id), specificOAConstraintsInfo, OHOS::AccountSA::JsonType::OBJECT);
930 std::vector<std::string> specificConstraintSource;
931 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(specificOAConstraintsInfo,
932 specificOAConstraintsInfo.end(), constraint,
933 specificConstraintSource, OHOS::AccountSA::JsonType::ARRAY);
934 ConstraintSourceTypeInfo constraintSourceTypeInfo;
935 for (auto it = specificConstraintSource.begin(); it != specificConstraintSource.end(); it++) {
936 if (stoi(*it) == deviceOwnerId) {
937 constraintSourceTypeInfo.localId =stoi(*it);
938 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_TYPE_DEVICE_OWNER;
939 specificSourceList.push_back(constraintSourceTypeInfo);
940 } else {
941 constraintSourceTypeInfo.localId =stoi(*it);
942 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_TYPE_PROFILE_OWNER;
943 specificSourceList.push_back(constraintSourceTypeInfo);
944 }
945 }
946 }
947 return ERR_OK;
948 }
949
SaveAccountListToFile(const Json & accountListJson)950 ErrCode OsAccountControlFileManager::SaveAccountListToFile(const Json &accountListJson)
951 {
952 std::lock_guard<std::mutex> lock(accountListFileLock_);
953 ErrCode result =
954 accountFileOperator_->InputFileByPathAndContent(Constants::ACCOUNT_LIST_FILE_JSON_PATH, accountListJson.dump());
955 if (result != ERR_OK) {
956 ACCOUNT_LOGE("cannot save save account list file content!");
957 return result;
958 }
959 ACCOUNT_LOGD("save account list file succeed!");
960 return ERR_OK;
961 }
962
SaveBaseOAConstraintsToFile(const Json & baseOAConstraints)963 ErrCode OsAccountControlFileManager::SaveBaseOAConstraintsToFile(const Json &baseOAConstraints)
964 {
965 std::lock_guard<std::mutex> lock(baseOAConstraintsFileLock_);
966 ErrCode result = accountFileOperator_->InputFileByPathAndContent(
967 Constants::BASE_OSACCOUNT_CONSTRAINTS_JSON_PATH, baseOAConstraints.dump());
968 if (result != ERR_OK) {
969 ACCOUNT_LOGE("cannot save base osaccount constraints file content!");
970 return result;
971 }
972
973 return ERR_OK;
974 }
975
SaveGlobalOAConstraintsToFile(const Json & globalOAConstraints)976 ErrCode OsAccountControlFileManager::SaveGlobalOAConstraintsToFile(const Json &globalOAConstraints)
977 {
978 std::lock_guard<std::mutex> lock(globalOAConstraintsFileLock_);
979 ErrCode result = accountFileOperator_->InputFileByPathAndContent(
980 Constants::GLOBAL_OSACCOUNT_CONSTRAINTS_JSON_PATH, globalOAConstraints.dump());
981 if (result != ERR_OK) {
982 ACCOUNT_LOGE("cannot save global osAccount constraints file content!");
983 return result;
984 }
985
986 return ERR_OK;
987 }
988
SaveSpecificOAConstraintsToFile(const Json & specificOAConstraints)989 ErrCode OsAccountControlFileManager::SaveSpecificOAConstraintsToFile(const Json &specificOAConstraints)
990 {
991 std::lock_guard<std::mutex> lock(specificOAConstraintsFileLock_);
992 ErrCode result = accountFileOperator_->InputFileByPathAndContent(
993 Constants::SPECIFIC_OSACCOUNT_CONSTRAINTS_JSON_PATH, specificOAConstraints.dump());
994 if (result != ERR_OK) {
995 ACCOUNT_LOGE("cannot save specific osAccount constraints file content!");
996 return result;
997 }
998
999 return ERR_OK;
1000 }
1001
GetDeviceOwnerId(int & deviceOwnerId)1002 ErrCode OsAccountControlFileManager::GetDeviceOwnerId(int &deviceOwnerId)
1003 {
1004 Json globalOAConstraintsJson;
1005 ErrCode result = GetGlobalOAConstraintsFromFile(globalOAConstraintsJson);
1006 if (result != ERR_OK) {
1007 ACCOUNT_LOGE("get global json data from file failed!");
1008 return result;
1009 }
1010 OHOS::AccountSA::GetDataByType<int>(
1011 globalOAConstraintsJson,
1012 globalOAConstraintsJson.end(),
1013 Constants::DEVICE_OWNER_ID,
1014 deviceOwnerId,
1015 OHOS::AccountSA::JsonType::NUMBER);
1016 return ERR_OK;
1017 }
1018
UpdateDeviceOwnerId(const int deviceOwnerId)1019 ErrCode OsAccountControlFileManager::UpdateDeviceOwnerId(const int deviceOwnerId)
1020 {
1021 Json globalOAConstraintsJson;
1022 ErrCode result = GetGlobalOAConstraintsFromFile(globalOAConstraintsJson);
1023 if (result != ERR_OK) {
1024 ACCOUNT_LOGE("get global json data from file failed!");
1025 return result;
1026 }
1027 globalOAConstraintsJson[Constants::DEVICE_OWNER_ID] = deviceOwnerId;
1028 return SaveGlobalOAConstraintsToFile(globalOAConstraintsJson);
1029 }
1030
SetDefaultActivatedOsAccount(const int32_t id)1031 ErrCode OsAccountControlFileManager::SetDefaultActivatedOsAccount(const int32_t id)
1032 {
1033 Json accountListJson;
1034 ErrCode result = GetAccountListFromFile(accountListJson);
1035 if (result != ERR_OK) {
1036 ACCOUNT_LOGE("get account list failed!");
1037 return result;
1038 }
1039
1040 accountListJson[DEFAULT_ACTIVATED_ACCOUNT_ID] = id;
1041 return SaveAccountListToFileAndDataBase(accountListJson);
1042 }
1043
GetDefaultActivatedOsAccount(int32_t & id)1044 ErrCode OsAccountControlFileManager::GetDefaultActivatedOsAccount(int32_t &id)
1045 {
1046 Json accountListJsonData;
1047 ErrCode result = GetAccountListFromFile(accountListJsonData);
1048 if (result != ERR_OK) {
1049 return result;
1050 }
1051 OHOS::AccountSA::GetDataByType<int>(accountListJsonData,
1052 accountListJsonData.end(),
1053 DEFAULT_ACTIVATED_ACCOUNT_ID,
1054 id,
1055 OHOS::AccountSA::JsonType::NUMBER);
1056 return ERR_OK;
1057 }
1058
SaveAccountListToFileAndDataBase(const Json & accountListJson)1059 ErrCode OsAccountControlFileManager::SaveAccountListToFileAndDataBase(const Json &accountListJson)
1060 {
1061 #ifdef HAS_KV_STORE_PART
1062 osAccountDataBaseOperator_->UpdateOsAccountIDListInDatabase(accountListJson);
1063 #endif
1064 return SaveAccountListToFile(accountListJson);
1065 }
1066
IsOsAccountExists(const int id,bool & isExists)1067 ErrCode OsAccountControlFileManager::IsOsAccountExists(const int id, bool &isExists)
1068 {
1069 isExists = false;
1070 std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) +
1071 Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
1072 // check exist
1073 if (!accountFileOperator_->IsExistFile(path)) {
1074 ACCOUNT_LOGI("IsOsAccountExists path %{public}s does not exist!", path.c_str());
1075 return ERR_OK;
1076 }
1077
1078 // check format
1079 if (!accountFileOperator_->IsJsonFormat(path)) {
1080 ACCOUNT_LOGI("IsOsAccountExists path %{public}s wrong format!", path.c_str());
1081 return ERR_OK;
1082 }
1083
1084 isExists = true;
1085 return ERR_OK;
1086 }
1087
GetPhotoById(const int id,std::string & photo)1088 ErrCode OsAccountControlFileManager::GetPhotoById(const int id, std::string &photo)
1089 {
1090 std::string path =
1091 Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) + Constants::PATH_SEPARATOR + photo;
1092 std::string byteStr = "";
1093 ErrCode errCode = accountFileOperator_->GetFileContentByPath(path, byteStr);
1094 if (errCode != ERR_OK) {
1095 ACCOUNT_LOGE("GetPhotoById cannot find photo file error");
1096 return errCode;
1097 }
1098 if (photo == Constants::USER_PHOTO_FILE_JPG_NAME) {
1099 photo =
1100 Constants::USER_PHOTO_BASE_JPG_HEAD + osAccountPhotoOperator_->EnCode(byteStr.c_str(), byteStr.length());
1101 } else {
1102 photo =
1103 Constants::USER_PHOTO_BASE_PNG_HEAD + osAccountPhotoOperator_->EnCode(byteStr.c_str(), byteStr.length());
1104 }
1105 std::string substr = "\r\n";
1106 while (photo.find(substr) != std::string::npos) {
1107 photo.erase(photo.find(substr), substr.length());
1108 }
1109 return ERR_OK;
1110 }
1111
SetPhotoById(const int id,const std::string & photo)1112 ErrCode OsAccountControlFileManager::SetPhotoById(const int id, const std::string &photo)
1113 {
1114 std::string path = "";
1115 std::string subPhoto = "";
1116 if (photo.find(Constants::USER_PHOTO_BASE_JPG_HEAD) != std::string::npos) {
1117 path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) + Constants::PATH_SEPARATOR +
1118 Constants::USER_PHOTO_FILE_JPG_NAME;
1119 subPhoto = photo.substr(Constants::USER_PHOTO_BASE_JPG_HEAD.size());
1120 } else if (photo.find(Constants::USER_PHOTO_BASE_PNG_HEAD) != std::string::npos) {
1121 path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) + Constants::PATH_SEPARATOR +
1122 Constants::USER_PHOTO_FILE_PNG_NAME;
1123 subPhoto = photo.substr(Constants::USER_PHOTO_BASE_PNG_HEAD.size());
1124 } else {
1125 ACCOUNT_LOGE("SetPhotoById photo str error");
1126 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
1127 }
1128 std::string bytePhoto = osAccountPhotoOperator_->DeCode(subPhoto);
1129 ErrCode errCode = accountFileOperator_->InputFileByPathAndContent(path, bytePhoto);
1130 if (errCode != ERR_OK) {
1131 return errCode;
1132 }
1133 return ERR_OK;
1134 }
1135
GetGlobalOAConstraintsList(std::vector<std::string> & constraintsList)1136 ErrCode OsAccountControlFileManager::GetGlobalOAConstraintsList(std::vector<std::string> &constraintsList)
1137 {
1138 return osAccountFileOperator_->GetGlobalOAConstraintsList(constraintsList);
1139 }
1140
GetSpecificOAConstraintsList(const int32_t id,std::vector<std::string> & constraintsList)1141 ErrCode OsAccountControlFileManager::GetSpecificOAConstraintsList(
1142 const int32_t id, std::vector<std::string> &constraintsList)
1143 {
1144 return osAccountFileOperator_->GetSpecificOAConstraintsList(id, constraintsList);
1145 }
1146
GetIsMultiOsAccountEnable(bool & isMultiOsAccountEnable)1147 ErrCode OsAccountControlFileManager::GetIsMultiOsAccountEnable(bool &isMultiOsAccountEnable)
1148 {
1149 return osAccountFileOperator_->GetIsMultiOsAccountEnable(isMultiOsAccountEnable);
1150 }
CheckConstraintsList(const std::vector<std::string> & constraints,bool & isExists,bool & isOverSize)1151 ErrCode OsAccountControlFileManager::CheckConstraintsList(
1152 const std::vector<std::string> &constraints, bool &isExists, bool &isOverSize)
1153 {
1154 return osAccountFileOperator_->CheckConstraintsList(constraints, isExists, isOverSize);
1155 }
1156
IsAllowedCreateAdmin(bool & isAllowedCreateAdmin)1157 ErrCode OsAccountControlFileManager::IsAllowedCreateAdmin(bool &isAllowedCreateAdmin)
1158 {
1159 return osAccountFileOperator_->IsAllowedCreateAdmin(isAllowedCreateAdmin);
1160 }
1161
GetCreatedOsAccountNumFromDatabase(const std::string & storeID,int & createdOsAccountNum)1162 ErrCode OsAccountControlFileManager::GetCreatedOsAccountNumFromDatabase(const std::string& storeID,
1163 int &createdOsAccountNum)
1164 {
1165 #ifdef HAS_KV_STORE_PART
1166 return osAccountDataBaseOperator_->GetCreatedOsAccountNumFromDatabase(storeID, createdOsAccountNum);
1167 #else
1168 return ERR_ACCOUNT_COMMON_INTERFACE_NOT_SUPPORT_ERROR;
1169 #endif
1170 }
1171
GetSerialNumberFromDatabase(const std::string & storeID,int64_t & serialNumber)1172 ErrCode OsAccountControlFileManager::GetSerialNumberFromDatabase(const std::string& storeID,
1173 int64_t &serialNumber)
1174 {
1175 #ifdef HAS_KV_STORE_PART
1176 return osAccountDataBaseOperator_->GetSerialNumberFromDatabase(storeID, serialNumber);
1177 #else
1178 return ERR_ACCOUNT_COMMON_INTERFACE_NOT_SUPPORT_ERROR;
1179 #endif
1180 }
1181
GetMaxAllowCreateIdFromDatabase(const std::string & storeID,int & id)1182 ErrCode OsAccountControlFileManager::GetMaxAllowCreateIdFromDatabase(const std::string& storeID,
1183 int &id)
1184 {
1185 #ifdef HAS_KV_STORE_PART
1186 return osAccountDataBaseOperator_->GetMaxAllowCreateIdFromDatabase(storeID, id);
1187 #else
1188 return ERR_ACCOUNT_COMMON_INTERFACE_NOT_SUPPORT_ERROR;
1189 #endif
1190 }
1191
GetOsAccountFromDatabase(const std::string & storeID,const int id,OsAccountInfo & osAccountInfo)1192 ErrCode OsAccountControlFileManager::GetOsAccountFromDatabase(const std::string& storeID,
1193 const int id, OsAccountInfo &osAccountInfo)
1194 {
1195 #ifdef HAS_KV_STORE_PART
1196 return osAccountDataBaseOperator_->GetOsAccountFromDatabase(storeID, id, osAccountInfo);
1197 #else
1198 return ERR_ACCOUNT_COMMON_INTERFACE_NOT_SUPPORT_ERROR;
1199 #endif
1200 }
1201
GetOsAccountListFromDatabase(const std::string & storeID,std::vector<OsAccountInfo> & osAccountList)1202 ErrCode OsAccountControlFileManager::GetOsAccountListFromDatabase(const std::string& storeID,
1203 std::vector<OsAccountInfo> &osAccountList)
1204 {
1205 #ifdef HAS_KV_STORE_PART
1206 return osAccountDataBaseOperator_->GetOsAccountListFromDatabase(storeID, osAccountList);
1207 #else
1208 return ERR_ACCOUNT_COMMON_INTERFACE_NOT_SUPPORT_ERROR;
1209 #endif
1210 }
1211 } // namespace AccountSA
1212 } // namespace OHOS