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