• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 <string>
18 #include <pthread.h>
19 #include <securec.h>
20 #include <sstream>
21 #include <sys/types.h>
22 #include <thread>
23 #include <unistd.h>
24 
25 #include "account_log_wrapper.h"
26 #include "account_hisysevent_adapter.h"
27 #ifdef HAS_CONFIG_POLICY_PART
28 #include "config_policy_utils.h"
29 #endif
30 #include "string_ex.h"
31 #include "os_account_constants.h"
32 #include "os_account_interface.h"
33 #include "parameters.h"
34 
35 namespace OHOS {
36 namespace AccountSA {
37 namespace {
38 const char DEFAULT_ACTIVATED_ACCOUNT_ID[] = "DefaultActivatedAccountID";
39 const std::string OS_ACCOUNT_STORE_ID = "os_account_info";
40 #ifdef ENABLE_FILE_WATCHER
41 constexpr uint32_t ALG_COMMON_SIZE = 32;
42 const char DISTRIBUTED_ACCOUNT_FILE_NAME[] = "/account.json";
43 #endif // ENABLE_FILE_WATCHER
44 #ifndef ACCOUNT_TEST
45 const std::string ACCOUNT_CFG_DIR_ROOT_PATH = "/data/service/el1/public/account/";
46 const std::string DEFAULT_OS_ACCOUNT_CONFIG_FILE = "/system/etc/account/os_account_config.json";
47 #else
48 const std::string ACCOUNT_CFG_DIR_ROOT_PATH = "/data/service/el1/public/account/test/";
49 const std::string DEFAULT_OS_ACCOUNT_CONFIG_FILE = ACCOUNT_CFG_DIR_ROOT_PATH + "os_account_config.json";
50 #endif // ACCOUNT_TEST
51 #ifdef HAS_CONFIG_POLICY_PART
52 const char OS_ACCOUNT_CONFIG_FILE[] = "etc/os_account/os_account_config.json";
53 #endif // HAS_CONFIG_POLICY_PART
54 const char MAX_OS_ACCOUNT_NUM[] = "maxOsAccountNum";
55 const char MAX_LOGGED_IN_OS_ACCOUNT_NUM[] = "maxLoggedInOsAccountNum";
56 const char DEVELOPER_MODE_STATE[] = "const.security.developermode.state";
57 const char DEVELOPER_MODE[] = "developerMode";
58 const char USER_PHOTO_FILE_PNG_NAME[] = "fase.png";
59 const char USER_PHOTO_FILE_JPG_NAME[] = "fase.jpg";
60 const char USER_PHOTO_BASE_JPG_HEAD[] = "data:image/jpeg;base64,";
61 const char USER_PHOTO_BASE_PNG_HEAD[] = "data:image/png;base64,";
62 const char START_USER_STRING_ID[] = "100";
63 const char DEVICE_OWNER_ID[] = "deviceOwnerId";
64 const char NEXT_LOCAL_ID[] = "NextLocalId";
65 const char IS_SERIAL_NUMBER_FULL[] = "isSerialNumberFull";
66 }
67 
GetValidAccountID(const std::string & dirName,std::int32_t & accountID)68 bool GetValidAccountID(const std::string& dirName, std::int32_t& accountID)
69 {
70     // check length first
71     if (dirName.empty() || dirName.size() > Constants::MAX_USER_ID_LENGTH) {
72         return false;
73     }
74 
75     auto iter = std::any_of(dirName.begin(), dirName.end(),
76         [dirName](char c) {
77             return (c < '0' || c > '9');
78         });
79     if (iter) {
80         return false;
81     }
82 
83     // convert to osaccount id
84     std::stringstream sstream;
85     sstream << dirName;
86     sstream >> accountID;
87     return (accountID >= Constants::ADMIN_LOCAL_ID && accountID <= Constants::MAX_USER_ID);
88 }
89 
GetOsAccountConfig(OsAccountConfig & config)90 ErrCode OsAccountControlFileManager::GetOsAccountConfig(OsAccountConfig &config)
91 {
92     std::string cfgPath = DEFAULT_OS_ACCOUNT_CONFIG_FILE;
93 #ifdef HAS_CONFIG_POLICY_PART
94     CfgFiles *cfgFiles = GetCfgFiles(OS_ACCOUNT_CONFIG_FILE);
95     if (cfgFiles != nullptr) {
96         if (cfgFiles->paths[0] != nullptr) {
97             cfgPath = cfgFiles->paths[0];
98         }
99         FreeCfgFiles(cfgFiles);
100     }
101 #endif
102     std::string configStr;
103     ErrCode errCode = accountFileOperator_->GetFileContentByPath(cfgPath, configStr);
104     if (errCode != ERR_OK) {
105         ACCOUNT_LOGE("Get content from file %{public}s failed!", cfgPath.c_str());
106         return errCode;
107     }
108     Json configJson = Json::parse(configStr, nullptr, false);
109     if (configJson.is_discarded()) {
110         ACCOUNT_LOGE("Parse os account info json data failed");
111         return ERR_ACCOUNT_COMMON_BAD_JSON_FORMAT_ERROR;
112     }
113     auto jsonEnd = configJson.end();
114     int32_t maxOsAccountNum = -1;
115     OHOS::AccountSA::GetDataByType<int32_t>(configJson, jsonEnd, MAX_OS_ACCOUNT_NUM,
116         maxOsAccountNum, OHOS::AccountSA::JsonType::NUMBER);
117     if (maxOsAccountNum > 0) {
118         config.maxOsAccountNum = static_cast<uint32_t>(maxOsAccountNum);
119     }
120     OHOS::AccountSA::GetDataByType<int32_t>(configJson, jsonEnd, MAX_LOGGED_IN_OS_ACCOUNT_NUM,
121         config.maxLoggedInOsAccountNum, OHOS::AccountSA::JsonType::NUMBER);
122 
123     bool isDeveloperMode = OHOS::system::GetBoolParameter(DEVELOPER_MODE_STATE, false);
124     if (isDeveloperMode && configJson.find(DEVELOPER_MODE) != jsonEnd) {
125         Json modeJson = configJson.at(DEVELOPER_MODE);
126         OHOS::AccountSA::GetDataByType<int32_t>(modeJson, modeJson.end(), MAX_LOGGED_IN_OS_ACCOUNT_NUM,
127             config.maxLoggedInOsAccountNum, OHOS::AccountSA::JsonType::NUMBER);
128     }
129     if ((config.maxLoggedInOsAccountNum > config.maxOsAccountNum) ||
130         (config.maxLoggedInOsAccountNum <= 0)) {
131         config.maxLoggedInOsAccountNum = config.maxOsAccountNum;
132     }
133     return ERR_OK;
134 }
135 
136 #ifdef ENABLE_FILE_WATCHER
RecoverAccountData(const std::string & fileName,const int32_t id)137 bool OsAccountControlFileManager::RecoverAccountData(const std::string &fileName, const int32_t id)
138 {
139 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
140     std::string recoverDataStr;
141     if (fileName == Constants::ACCOUNT_LIST_FILE_JSON_PATH) {
142         Json accountListJson;
143         osAccountDataBaseOperator_->GetAccountListFromStoreID(OS_ACCOUNT_STORE_ID, accountListJson);
144         recoverDataStr = accountListJson.dump();
145     } else if (id >= Constants::START_USER_ID) {
146         OsAccountInfo osAccountInfo;
147         if (GetOsAccountFromDatabase(OS_ACCOUNT_STORE_ID, id, osAccountInfo) != ERR_OK) {
148             ACCOUNT_LOGW("Failed to get osaccount from database");
149             return false;
150         }
151         recoverDataStr = osAccountInfo.ToString();
152     } else {
153         ACCOUNT_LOGW("Failed to parse parameters");
154         return false;
155     }
156     if (recoverDataStr.empty()) {
157         ACCOUNT_LOGW("Get empty recover file data");
158         return false;
159     }
160     // recover data
161     ErrCode result = accountFileOperator_->InputFileByPathAndContent(fileName, recoverDataStr);
162     if (result != ERR_OK) {
163         ACCOUNT_LOGE("Recover local file data failed, err = %{public}d", result);
164         return false;
165     }
166     // update local digest
167     if (accountFileWatcherMgr_.AddAccountInfoDigest(recoverDataStr, fileName) != ERR_OK) {
168         ACCOUNT_LOGE("Failed to update local digest");
169         return false;
170     }
171 #endif // defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
172     return true;
173 }
174 
DealWithFileModifyEvent(const std::string & fileName,const int32_t id)175 bool OsAccountControlFileManager::DealWithFileModifyEvent(const std::string &fileName, const int32_t id)
176 {
177     ACCOUNT_LOGI("Enter");
178     {
179         std::unique_lock<std::shared_timed_mutex> lock(accountFileOperator_->fileLock_);
180         if (accountFileOperator_->GetValidModifyFileOperationFlag(fileName)) {
181             ACCOUNT_LOGD("This is valid service operate, no need to deal with it.");
182             accountFileOperator_->SetValidModifyFileOperationFlag(fileName, false);
183             return true;
184         }
185     }
186     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
187     std::string fileInfoStr;
188     if (accountFileOperator_->GetFileContentByPath(fileName, fileInfoStr) != ERR_OK) {
189         ACCOUNT_LOGE("Get content from file %{public}s failed!", fileName.c_str());
190         return false;
191     }
192     uint8_t localDigestData[ALG_COMMON_SIZE] = {0};
193     accountFileWatcherMgr_.GetAccountInfoDigestFromFile(fileName, localDigestData, ALG_COMMON_SIZE);
194 #ifdef HAS_HUKS_PART
195     uint8_t newDigestData[ALG_COMMON_SIZE] = {0};
196     GenerateAccountInfoDigest(fileInfoStr, newDigestData, ALG_COMMON_SIZE);
197 
198     if (memcmp(localDigestData, newDigestData, ALG_COMMON_SIZE) == EOK) {
199         ACCOUNT_LOGD("No need to recover local file data.");
200         return true;
201     }
202 #endif // HAS_HUKS_PART
203     ReportOsAccountDataTampered(id, fileName, "OS_ACCOUNT_INFO");
204     ACCOUNT_LOGW("Local file data has been changed");
205     return RecoverAccountData(fileName, id);
206 }
207 
DealWithFileDeleteEvent(const std::string & fileName,const int32_t id)208 bool OsAccountControlFileManager::DealWithFileDeleteEvent(const std::string &fileName, const int32_t id)
209 {
210     ACCOUNT_LOGI("Enter");
211     {
212         std::unique_lock<std::shared_timed_mutex> lock(accountFileOperator_->fileLock_);
213         if (accountFileOperator_->GetValidDeleteFileOperationFlag(fileName)) {
214             ACCOUNT_LOGD("This is valid service operate, no need to deal with it.");
215             accountFileOperator_->SetValidDeleteFileOperationFlag(fileName, false);
216             accountFileWatcherMgr_.RemoveFileWatcher(id, fileName);
217             return true;
218         }
219     }
220     ReportOsAccountDataTampered(id, fileName, "OS_ACCOUNT_INFO");
221     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
222     if (!RecoverAccountData(fileName, id)) {
223         ACCOUNT_LOGE("Recover account data failed.");
224     }
225     accountFileWatcherMgr_.AddFileWatcher(id, eventCallbackFunc_, fileName);
226     return true;
227 }
228 
DealWithFileMoveEvent(const std::string & fileName,const int32_t id)229 bool OsAccountControlFileManager::DealWithFileMoveEvent(const std::string &fileName, const int32_t id)
230 {
231     ACCOUNT_LOGI("Enter");
232     // delete old file watcher
233     accountFileWatcherMgr_.RemoveFileWatcher(id, fileName);
234     ReportOsAccountDataTampered(id, fileName, "OS_ACCOUNT_INFO");
235     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
236     if (!RecoverAccountData(fileName, id)) {
237         ACCOUNT_LOGE("Recover account data failed.");
238     }
239     accountFileWatcherMgr_.AddFileWatcher(id, eventCallbackFunc_, fileName);
240     return true;
241 }
242 #endif // ENABLE_FILE_WATCHER
243 
244 #ifdef ENABLE_FILE_WATCHER
OsAccountControlFileManager()245 OsAccountControlFileManager::OsAccountControlFileManager()
246     : accountFileWatcherMgr_(AccountFileWatcherMgr::GetInstance())
247 {
248     accountFileOperator_ = accountFileWatcherMgr_.accountFileOperator_;
249 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
250     osAccountDataBaseOperator_ = std::make_shared<OsAccountDatabaseOperator>();
251 #endif // defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
252     osAccountFileOperator_ = std::make_shared<OsAccountFileOperator>();
253     osAccountPhotoOperator_ = std::make_shared<OsAccountPhotoOperator>();
254     eventCallbackFunc_ = [this](const std::string &fileName, int32_t id, uint32_t event) {
255         ACCOUNT_LOGI("Inotify event = %{public}d, fileName = %{public}s", event, fileName.c_str());
256         switch (event) {
257             case IN_MODIFY: {
258                 return DealWithFileModifyEvent(fileName, id);
259             }
260             case IN_MOVE_SELF: {
261                 return DealWithFileMoveEvent(fileName, id);
262             }
263             case IN_DELETE_SELF: {
264                 return DealWithFileDeleteEvent(fileName, id);
265             }
266             default: {
267                 ACCOUNT_LOGW("Get event invalid!");
268                 return false;
269             }
270         }
271     };
272 }
273 #else
OsAccountControlFileManager()274 OsAccountControlFileManager::OsAccountControlFileManager()
275 {
276     accountFileOperator_ = std::make_shared<AccountFileOperator>();
277 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
278     osAccountDataBaseOperator_ = std::make_shared<OsAccountDatabaseOperator>();
279 #endif // HAS_KV_STORE_PART && defined(DISTRIBUTED_FEATURE_ENABLED)
280     osAccountFileOperator_ = std::make_shared<OsAccountFileOperator>();
281     osAccountPhotoOperator_ = std::make_shared<OsAccountPhotoOperator>();
282 }
283 #endif // ENABLE_FILE_WATCHER
~OsAccountControlFileManager()284 OsAccountControlFileManager::~OsAccountControlFileManager()
285 {}
286 
Init()287 void OsAccountControlFileManager::Init()
288 {
289     osAccountFileOperator_->Init();
290     FileInit();
291     Json accountListJson;
292     ErrCode result = GetAccountListFromFile(accountListJson);
293     if (result != ERR_OK) {
294         return;
295     }
296     auto jsonEnd = accountListJson.end();
297     std::vector<std::string> accountIdList;
298     OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
299         accountListJson, jsonEnd, Constants::ACCOUNT_LIST, accountIdList, OHOS::AccountSA::JsonType::ARRAY);
300 #ifdef ENABLE_FILE_WATCHER
301     if (!accountIdList.empty()) {
302         InitFileWatcherInfo(accountIdList);
303     }
304 #endif // ENABLE_FILE_WATCHER
305     ACCOUNT_LOGI("OsAccountControlFileManager Init end");
306 }
307 
FileInit()308 void OsAccountControlFileManager::FileInit()
309 {
310     if (!accountFileOperator_->IsJsonFileReady(Constants::ACCOUNT_LIST_FILE_JSON_PATH)) {
311         ACCOUNT_LOGI("OsAccountControlFileManager there is not have valid account list, create!");
312         RecoverAccountListJsonFile();
313     }
314 #ifdef ENABLE_FILE_WATCHER
315     if (!accountFileOperator_->IsJsonFileReady(Constants::ACCOUNT_INFO_DIGEST_FILE_PATH)) {
316         ACCOUNT_LOGI("OsAccountControlFileManager there is not have valid account info digest file, create!");
317         RecoverAccountInfoDigestJsonFile();
318     }
319 #endif // ENABLE_FILE_WATCHER
320     // -1 is special refers to conmon account data file.
321 #ifdef ENABLE_FILE_WATCHER
322     accountFileWatcherMgr_.AddFileWatcher(-1, eventCallbackFunc_, Constants::ACCOUNT_LIST_FILE_JSON_PATH);
323 #endif // ENABLE_FILE_WATCHER
324     if (!accountFileOperator_->IsJsonFileReady(Constants::ACCOUNT_INDEX_JSON_PATH)) {
325         ACCOUNT_LOGI("OsAccountControlFileManager there is not have valid account index file, create!");
326         BuildAndSaveOsAccountIndexJsonFile();
327     }
328 #ifdef ENABLE_FILE_WATCHER
329     accountFileWatcherMgr_.AddFileWatcher(-1, eventCallbackFunc_, Constants::ACCOUNT_INDEX_JSON_PATH);
330 #endif // ENABLE_FILE_WATCHER
331     if (!accountFileOperator_->IsJsonFileReady(Constants::BASE_OSACCOUNT_CONSTRAINTS_JSON_PATH)) {
332         ACCOUNT_LOGI("OsAccountControlFileManager there is not have valid account list, create!");
333         BuildAndSaveBaseOAConstraintsJsonFile();
334     }
335 #ifdef ENABLE_FILE_WATCHER
336     accountFileWatcherMgr_.AddFileWatcher(-1, eventCallbackFunc_, Constants::BASE_OSACCOUNT_CONSTRAINTS_JSON_PATH);
337 #endif // ENABLE_FILE_WATCHER
338     if (!accountFileOperator_->IsJsonFileReady(Constants::GLOBAL_OSACCOUNT_CONSTRAINTS_JSON_PATH)) {
339         ACCOUNT_LOGI("OsAccountControlFileManager there is not have valid account list, create!");
340         BuildAndSaveGlobalOAConstraintsJsonFile();
341     }
342 #ifdef ENABLE_FILE_WATCHER
343     accountFileWatcherMgr_.AddFileWatcher(-1, eventCallbackFunc_, Constants::GLOBAL_OSACCOUNT_CONSTRAINTS_JSON_PATH);
344 #endif // ENABLE_FILE_WATCHER
345     if (!accountFileOperator_->IsJsonFileReady(Constants::SPECIFIC_OSACCOUNT_CONSTRAINTS_JSON_PATH)) {
346         ACCOUNT_LOGI("OsAccountControlFileManager there is not have valid account list, create!");
347         BuildAndSaveSpecificOAConstraintsJsonFile();
348     }
349 #ifdef ENABLE_FILE_WATCHER
350     accountFileWatcherMgr_.AddFileWatcher(-1, eventCallbackFunc_, Constants::SPECIFIC_OSACCOUNT_CONSTRAINTS_JSON_PATH);
351 #endif // ENABLE_FILE_WATCHER
352 }
353 
354 #ifdef ENABLE_FILE_WATCHER
InitFileWatcherInfo(std::vector<std::string> & accountIdList)355 void OsAccountControlFileManager::InitFileWatcherInfo(std::vector<std::string> &accountIdList)
356 {
357     for (std::string i : accountIdList) {
358         int32_t id = 0;
359         if (!StrToInt(i, id)) {
360             ACCOUNT_LOGE("Convert localId failed");
361             continue;
362         }
363         accountFileWatcherMgr_.AddFileWatcher(id, eventCallbackFunc_);
364     }
365 }
366 #endif // ENABLE_FILE_WATCHER
367 
BuildAndSaveAccountListJsonFile(const std::vector<std::string> & accounts)368 void OsAccountControlFileManager::BuildAndSaveAccountListJsonFile(const std::vector<std::string>& accounts)
369 {
370     ACCOUNT_LOGD("Enter.");
371     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
372     Json accountList = Json {
373         {Constants::ACCOUNT_LIST, accounts},
374         {Constants::COUNT_ACCOUNT_NUM, accounts.size()},
375         {DEFAULT_ACTIVATED_ACCOUNT_ID, Constants::START_USER_ID},
376         {Constants::MAX_ALLOW_CREATE_ACCOUNT_ID, Constants::MAX_USER_ID},
377         {Constants::SERIAL_NUMBER_NUM, Constants::SERIAL_NUMBER_NUM_START},
378         {IS_SERIAL_NUMBER_FULL, Constants::IS_SERIAL_NUMBER_FULL_INIT_VALUE},
379         {NEXT_LOCAL_ID, Constants::START_USER_ID + 1},
380     };
381     SaveAccountListToFile(accountList);
382 }
383 
BuildAndSaveBaseOAConstraintsJsonFile()384 void OsAccountControlFileManager::BuildAndSaveBaseOAConstraintsJsonFile()
385 {
386     ACCOUNT_LOGI("Enter.");
387     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
388     std::vector<std::string> baseOAConstraints;
389     if (osAccountFileOperator_->GetConstraintsByType(OsAccountType::ADMIN, baseOAConstraints) != ERR_OK) {
390         ACCOUNT_LOGE("Get %{public}d base os account constraints failed.", Constants::START_USER_ID);
391         return;
392     }
393     Json baseOsAccountConstraints = Json {
394         {START_USER_STRING_ID, baseOAConstraints}
395     };
396     SaveBaseOAConstraintsToFile(baseOsAccountConstraints);
397 }
398 
BuildAndSaveGlobalOAConstraintsJsonFile()399 void OsAccountControlFileManager::BuildAndSaveGlobalOAConstraintsJsonFile()
400 {
401     ACCOUNT_LOGI("Enter.");
402     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
403     Json globalOsAccountConstraints = Json {
404         {DEVICE_OWNER_ID, -1},
405         {Constants::ALL_GLOBAL_CONSTRAINTS, {}}
406     };
407     SaveGlobalOAConstraintsToFile(globalOsAccountConstraints);
408 }
409 
BuildAndSaveSpecificOAConstraintsJsonFile()410 void OsAccountControlFileManager::BuildAndSaveSpecificOAConstraintsJsonFile()
411 {
412     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
413     Json OsAccountConstraintsList = Json {
414         {Constants::ALL_SPECIFIC_CONSTRAINTS, {}},
415     };
416     Json specificOsAccountConstraints = Json {
417         {START_USER_STRING_ID, OsAccountConstraintsList},
418     };
419     SaveSpecificOAConstraintsToFile(specificOsAccountConstraints);
420 }
421 
BuildAndSaveOsAccountIndexJsonFile()422 void OsAccountControlFileManager::BuildAndSaveOsAccountIndexJsonFile()
423 {
424     std::string accountIndex;
425     ErrCode result = GetAccountIndexInfo(accountIndex);
426     if (result != ERR_OK) {
427         ACCOUNT_LOGE("Get account index info error code %{public}d.", result);
428         return;
429     }
430     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
431     result = accountFileOperator_->InputFileByPathAndContent(Constants::ACCOUNT_INDEX_JSON_PATH, accountIndex);
432     if (result != ERR_OK) {
433         ACCOUNT_LOGE("Failed to input account index info to file!");
434     }
435     return;
436 }
437 
RecoverAccountInfoDigestJsonFile()438 void OsAccountControlFileManager::RecoverAccountInfoDigestJsonFile()
439 {
440     std::string listInfoStr;
441     accountFileOperator_->GetFileContentByPath(Constants::ACCOUNT_LIST_FILE_JSON_PATH, listInfoStr);
442 #ifdef HAS_HUKS_PART
443     uint8_t digestOutData[ALG_COMMON_SIZE] = {0};
444     GenerateAccountInfoDigest(listInfoStr, digestOutData, ALG_COMMON_SIZE);
445     Json digestJsonData = Json {
446         {Constants::ACCOUNT_LIST_FILE_JSON_PATH, digestOutData},
447     };
448     accountFileOperator_->InputFileByPathAndContent(Constants::ACCOUNT_INFO_DIGEST_FILE_PATH, digestJsonData.dump());
449 #endif // HAS_HUKS_PART
450     return;
451 }
452 
RecoverAccountListJsonFile()453 void OsAccountControlFileManager::RecoverAccountListJsonFile()
454 {
455     // get account list
456     std::vector<std::string> accounts;
457     DIR* rootDir = opendir(Constants::USER_INFO_BASE.c_str());
458     if (rootDir == nullptr) {
459         accounts.push_back(std::to_string(Constants::START_USER_ID));  // account 100 always exists
460         BuildAndSaveAccountListJsonFile(accounts);
461         ACCOUNT_LOGE("Cannot open dir %{public}s, err %{public}d.", Constants::USER_INFO_BASE.c_str(), errno);
462         return;
463     }
464 
465     struct dirent* curDir = nullptr;
466     while ((curDir = readdir(rootDir)) != nullptr) {
467         std::string curDirName(curDir->d_name);
468         if (curDirName == "." || curDirName == ".." || curDir->d_type != DT_DIR) {
469             continue;
470         }
471 
472         // get and check os account id
473         std::int32_t accountID = Constants::INVALID_OS_ACCOUNT_ID;
474         if (!GetValidAccountID(curDirName, accountID)) {
475             ACCOUNT_LOGE("Invalid account id %{public}s detected in %{public}s.", curDirName.c_str(),
476                 Constants::USER_INFO_BASE.c_str());
477             continue;
478         }
479 
480         // check repeat
481         bool sameAccountID = false;
482         std::string curAccountIDStr = std::to_string(accountID);
483         for (size_t i = 0; i < accounts.size(); ++i) {
484             if (accounts[i] == curAccountIDStr) {
485                 ACCOUNT_LOGE("Repeated account id %{public}s detected in %{public}s.", curAccountIDStr.c_str(),
486                     Constants::USER_INFO_BASE.c_str());
487                 sameAccountID = true;
488                 break;
489             }
490         }
491 
492         if (!sameAccountID && accountID >= Constants::START_USER_ID) {
493             accounts.push_back(curAccountIDStr);
494         }
495     }
496 
497     if (closedir(rootDir) != 0) {
498         ACCOUNT_LOGE("Cannot closedir dir %{public}s, err %{public}d.", Constants::USER_INFO_BASE.c_str(), errno);
499     }
500     BuildAndSaveAccountListJsonFile(accounts);
501 }
502 
GetOsAccountIdList(std::vector<int32_t> & idList)503 ErrCode OsAccountControlFileManager::GetOsAccountIdList(std::vector<int32_t> &idList)
504 {
505     idList.clear();
506     Json accountListJson;
507     ErrCode errCode = GetAccountListFromFile(accountListJson);
508     if (errCode != ERR_OK) {
509         return errCode;
510     }
511     std::vector<std::string> idStrList;
512     OHOS::AccountSA::GetDataByType<std::vector<std::string>>(accountListJson, accountListJson.end(),
513         Constants::ACCOUNT_LIST, idStrList, OHOS::AccountSA::JsonType::ARRAY);
514     for (const auto &idStr : idStrList) {
515         int32_t id = 0;
516         if (!StrToInt(idStr, id)) {
517             ACCOUNT_LOGE("Convert localId failed");
518             continue;
519         }
520         idList.emplace_back(id);
521     }
522     return errCode;
523 }
524 
GetOsAccountList(std::vector<OsAccountInfo> & osAccountList)525 ErrCode OsAccountControlFileManager::GetOsAccountList(std::vector<OsAccountInfo> &osAccountList)
526 {
527     osAccountList.clear();
528     Json accountListJson;
529     ErrCode result = GetAccountListFromFile(accountListJson);
530     if (result != ERR_OK) {
531         ACCOUNT_LOGE("GetAccountListFromFile failed!");
532 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
533         if (osAccountDataBaseOperator_->GetAccountListFromStoreID("", accountListJson) == ERR_OK) {
534             SaveAccountListToFile(accountListJson);
535         } else {
536             return result;
537         }
538 #else
539         return result;
540 #endif // defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
541     }
542     const auto &jsonObjectEnd = accountListJson.end();
543     std::vector<std::string> idList;
544     OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
545         accountListJson, jsonObjectEnd, Constants::ACCOUNT_LIST, idList, OHOS::AccountSA::JsonType::ARRAY);
546 
547     for (const auto& it : idList) {
548         OsAccountInfo osAccountInfo;
549         int32_t id = 0;
550         if (!StrToInt(it, id)) {
551             ACCOUNT_LOGE("Convert localId failed");
552             continue;
553         }
554         if (GetOsAccountInfoById(id, osAccountInfo) == ERR_OK) {
555             if (osAccountInfo.GetPhoto() != "") {
556                 std::string photo = osAccountInfo.GetPhoto();
557                 GetPhotoById(osAccountInfo.GetLocalId(), photo);
558                 osAccountInfo.SetPhoto(photo);
559             }
560             osAccountList.push_back(osAccountInfo);
561         }
562     }
563     return ERR_OK;
564 }
565 
GetOsAccountInfoById(const int id,OsAccountInfo & osAccountInfo)566 ErrCode OsAccountControlFileManager::GetOsAccountInfoById(const int id, OsAccountInfo &osAccountInfo)
567 {
568     std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) +
569                        Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
570     ErrCode err = accountFileOperator_->CheckFileExistence(path);
571     if (err != ERR_OK) {
572         ACCOUNT_LOGE("File %{public}s does not exist err, errcode=%{public}d", path.c_str(), err);
573         if (GetOsAccountFromDatabase("", id, osAccountInfo) == ERR_OK) {
574             InsertOsAccount(osAccountInfo);
575             return ERR_OK;
576         }
577         return err == ERR_ACCOUNT_COMMON_FILE_NOT_EXIST ?
578             ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR : ERR_ACCOUNT_COMMON_FILE_READ_FAILED;
579     }
580     std::string accountInfoStr;
581     if (accountFileOperator_->GetFileContentByPath(path, accountInfoStr) != ERR_OK) {
582         ACCOUNT_LOGE("Get content from file %{public}s failed!", path.c_str());
583         if (GetOsAccountFromDatabase("", id, osAccountInfo) == ERR_OK) {
584             return ERR_OK;
585         }
586         return ERR_ACCOUNT_COMMON_FILE_READ_FAILED;
587     }
588     Json osAccountInfoJson = Json::parse(accountInfoStr, nullptr, false);
589     if (osAccountInfoJson.is_discarded() || !osAccountInfo.FromJson(osAccountInfoJson)) {
590         ACCOUNT_LOGE("Parse os account info json for %{public}d failed", id);
591         if (GetOsAccountFromDatabase("", id, osAccountInfo) != ERR_OK) {
592             ACCOUNT_LOGE("GetOsAccountFromDatabase failed id=%{public}d", id);
593             return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
594         }
595     }
596     return ERR_OK;
597 }
598 
GetConstraintsByType(const OsAccountType type,std::vector<std::string> & constraints)599 ErrCode OsAccountControlFileManager::GetConstraintsByType(
600     const OsAccountType type, std::vector<std::string> &constraints)
601 {
602     int typeInit = static_cast<int>(type);
603     return osAccountFileOperator_->GetConstraintsByType(typeInit, constraints);
604 }
605 
UpdateBaseOAConstraints(const std::string & idStr,const std::vector<std::string> & ConstraintStr,bool isAdd)606 ErrCode OsAccountControlFileManager::UpdateBaseOAConstraints(const std::string& idStr,
607     const std::vector<std::string>& ConstraintStr, bool isAdd)
608 {
609     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
610     Json baseOAConstraintsJson;
611     ErrCode result = GetBaseOAConstraintsFromFile(baseOAConstraintsJson);
612     if (result != ERR_OK) {
613         ACCOUNT_LOGE("Get baseOAConstraints from json file failed!");
614         return result;
615     }
616 
617     if (baseOAConstraintsJson.find(idStr) == baseOAConstraintsJson.end()) {
618         if (!isAdd) {
619             return ERR_OK;
620         }
621         baseOAConstraintsJson.emplace(idStr, ConstraintStr);
622     } else {
623         std::vector<std::string> baseOAConstraints;
624         auto jsonEnd = baseOAConstraintsJson.end();
625         OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
626             baseOAConstraintsJson, jsonEnd, idStr, baseOAConstraints, OHOS::AccountSA::JsonType::ARRAY);
627         for (auto it = ConstraintStr.begin(); it != ConstraintStr.end(); it++) {
628             if (!isAdd) {
629                 baseOAConstraints.erase(std::remove(baseOAConstraints.begin(), baseOAConstraints.end(), *it),
630                     baseOAConstraints.end());
631                 continue;
632             }
633             if (std::find(baseOAConstraints.begin(), baseOAConstraints.end(), *it) == baseOAConstraints.end()) {
634                 baseOAConstraints.emplace_back(*it);
635             }
636         }
637         baseOAConstraintsJson[idStr] = baseOAConstraints;
638     }
639     return SaveBaseOAConstraintsToFile(baseOAConstraintsJson);
640 }
641 
UpdateGlobalOAConstraints(const std::string & idStr,const std::vector<std::string> & ConstraintStr,bool isAdd)642 ErrCode OsAccountControlFileManager::UpdateGlobalOAConstraints(
643     const std::string& idStr, const std::vector<std::string>& ConstraintStr, bool isAdd)
644 {
645     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
646     Json globalOAConstraintsJson;
647     ErrCode result = GetGlobalOAConstraintsFromFile(globalOAConstraintsJson);
648     if (result != ERR_OK) {
649         ACCOUNT_LOGE("Get globalOAConstraints from file failed!");
650         return result;
651     }
652     GlobalConstraintsDataOperate(idStr, ConstraintStr, isAdd, globalOAConstraintsJson);
653     return SaveGlobalOAConstraintsToFile(globalOAConstraintsJson);
654 }
655 
GlobalConstraintsDataOperate(const std::string & idStr,const std::vector<std::string> & ConstraintStr,bool isAdd,Json & globalOAConstraintsJson)656 void OsAccountControlFileManager::GlobalConstraintsDataOperate(const std::string& idStr,
657     const std::vector<std::string>& ConstraintStr, bool isAdd, Json &globalOAConstraintsJson)
658 {
659     std::vector<std::string> globalOAConstraintsList;
660     OHOS::AccountSA::GetDataByType<std::vector<std::string>>(globalOAConstraintsJson, globalOAConstraintsJson.end(),
661         Constants::ALL_GLOBAL_CONSTRAINTS, globalOAConstraintsList, OHOS::AccountSA::JsonType::ARRAY);
662     std::vector<std::string> waitForErase;
663     for (auto it = ConstraintStr.begin(); it != ConstraintStr.end(); it++) {
664         if (!isAdd) {
665             if (std::find(globalOAConstraintsList.begin(),
666             globalOAConstraintsList.end(), *it) == globalOAConstraintsList.end()) {
667                 continue;
668             }
669             std::vector<std::string> constraintSourceList;
670             OHOS::AccountSA::GetDataByType<std::vector<std::string>>(globalOAConstraintsJson,
671                 globalOAConstraintsJson.end(), *it, constraintSourceList, OHOS::AccountSA::JsonType::ARRAY);
672             constraintSourceList.erase(std::remove(constraintSourceList.begin(), constraintSourceList.end(), idStr),
673                 constraintSourceList.end());
674             if (constraintSourceList.size() == 0) {
675                 globalOAConstraintsList.erase(std::remove(globalOAConstraintsList.begin(),
676                     globalOAConstraintsList.end(), *it), globalOAConstraintsList.end());
677                 globalOAConstraintsJson[Constants::ALL_GLOBAL_CONSTRAINTS] = globalOAConstraintsList;
678                 waitForErase.push_back(*it);
679             } else {
680                 globalOAConstraintsJson[*it] = constraintSourceList;
681             }
682             continue;
683         }
684         if (std::find(globalOAConstraintsList.begin(),
685             globalOAConstraintsList.end(), *it) != globalOAConstraintsList.end()) {
686             std::vector<std::string> constraintSourceList;
687             OHOS::AccountSA::GetDataByType<std::vector<std::string>>(globalOAConstraintsJson,
688                 globalOAConstraintsJson.end(), *it, constraintSourceList, OHOS::AccountSA::JsonType::ARRAY);
689             if (std::find(constraintSourceList.begin(),
690                 constraintSourceList.end(), idStr) == constraintSourceList.end()) {
691                 constraintSourceList.emplace_back(idStr);
692                 globalOAConstraintsJson[*it] = constraintSourceList;
693             }
694             continue;
695         }
696         std::vector<std::string> constraintSourceList;
697         constraintSourceList.emplace_back(idStr);
698         globalOAConstraintsList.emplace_back(*it);
699         globalOAConstraintsJson.emplace(*it, constraintSourceList);
700         globalOAConstraintsJson[Constants::ALL_GLOBAL_CONSTRAINTS] = globalOAConstraintsList;
701     }
702     for (auto keyStr : waitForErase) {
703         globalOAConstraintsJson.erase(keyStr);
704     }
705 }
706 
UpdateSpecificOAConstraints(const std::string & idStr,const std::string & targetIdStr,const std::vector<std::string> & ConstraintStr,bool isAdd)707 ErrCode OsAccountControlFileManager::UpdateSpecificOAConstraints(
708     const std::string& idStr, const std::string& targetIdStr, const std::vector<std::string>& ConstraintStr, bool isAdd)
709 {
710     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
711     Json specificOAConstraintsJson;
712     ErrCode result = GetSpecificOAConstraintsFromFile(specificOAConstraintsJson);
713     if (result != ERR_OK) {
714         ACCOUNT_LOGE("Get specificOAConstraints from file failed!");
715         return result;
716     }
717     if (specificOAConstraintsJson.find(targetIdStr) == specificOAConstraintsJson.end()) {
718         if (!isAdd) {
719             return ERR_OK;
720         }
721         Json osAccountConstraintsList = Json {
722             {Constants::ALL_SPECIFIC_CONSTRAINTS, {}},
723         };
724         specificOAConstraintsJson.emplace(targetIdStr, osAccountConstraintsList);
725     }
726     Json userPrivateConstraintsDataJson = specificOAConstraintsJson[targetIdStr];
727     SpecificConstraintsDataOperate(idStr, targetIdStr, ConstraintStr, isAdd, userPrivateConstraintsDataJson);
728     specificOAConstraintsJson[targetIdStr] = userPrivateConstraintsDataJson;
729     return SaveSpecificOAConstraintsToFile(specificOAConstraintsJson);
730 }
731 
SpecificConstraintsDataOperate(const std::string & idStr,const std::string & targetIdStr,const std::vector<std::string> & ConstraintStr,bool isAdd,Json & userPrivateConstraintsDataJson)732 void OsAccountControlFileManager::SpecificConstraintsDataOperate(
733     const std::string& idStr, const std::string& targetIdStr, const std::vector<std::string>& ConstraintStr,
734     bool isAdd, Json& userPrivateConstraintsDataJson)
735 {
736     std::vector<std::string> specificOAConstraintsList;
737     OHOS::AccountSA::GetDataByType<std::vector<std::string>>(userPrivateConstraintsDataJson,
738         userPrivateConstraintsDataJson.end(), Constants::ALL_SPECIFIC_CONSTRAINTS,
739         specificOAConstraintsList, OHOS::AccountSA::JsonType::ARRAY);
740     std::vector<std::string> waitForErase;
741     for (auto it = ConstraintStr.begin(); it != ConstraintStr.end(); it++) {
742         if (!isAdd) {
743             if (userPrivateConstraintsDataJson.find(*it) == userPrivateConstraintsDataJson.end()) {
744                 continue;
745             }
746             std::vector<std::string> constraintSourceList;
747             OHOS::AccountSA::GetDataByType<std::vector<std::string>>(userPrivateConstraintsDataJson,
748                 userPrivateConstraintsDataJson.end(), *it, constraintSourceList, OHOS::AccountSA::JsonType::ARRAY);
749             constraintSourceList.erase(std::remove(constraintSourceList.begin(), constraintSourceList.end(), idStr),
750                 constraintSourceList.end());
751             if (constraintSourceList.size() == 0) {
752                 specificOAConstraintsList.erase(std::remove(specificOAConstraintsList.begin(),
753                     specificOAConstraintsList.end(), *it), specificOAConstraintsList.end());
754                 userPrivateConstraintsDataJson[Constants::ALL_SPECIFIC_CONSTRAINTS] = specificOAConstraintsList;
755                 waitForErase.push_back(*it);
756             } else {
757                 userPrivateConstraintsDataJson[*it] = constraintSourceList;
758             }
759             continue;
760         }
761         if (std::find(specificOAConstraintsList.begin(),
762             specificOAConstraintsList.end(), *it) != specificOAConstraintsList.end()) {
763             std::vector<std::string> constraintSourceList;
764             OHOS::AccountSA::GetDataByType<std::vector<std::string>>(userPrivateConstraintsDataJson,
765             userPrivateConstraintsDataJson.end(), *it, constraintSourceList, OHOS::AccountSA::JsonType::ARRAY);
766             if (std::find(constraintSourceList.begin(),
767                 constraintSourceList.end(), idStr) == constraintSourceList.end()) {
768                 constraintSourceList.emplace_back(idStr);
769                 userPrivateConstraintsDataJson[*it] = constraintSourceList;
770             }
771             continue;
772         }
773         std::vector<std::string> constraintSourceList;
774         constraintSourceList.emplace_back(idStr);
775         specificOAConstraintsList.emplace_back(*it);
776         userPrivateConstraintsDataJson.emplace(*it, constraintSourceList);
777         userPrivateConstraintsDataJson[Constants::ALL_SPECIFIC_CONSTRAINTS] = specificOAConstraintsList;
778     }
779     for (auto keyStr : waitForErase) {
780         userPrivateConstraintsDataJson.erase(keyStr);
781     }
782 }
783 
RemoveOAConstraintsInfo(const int32_t id)784 ErrCode OsAccountControlFileManager::RemoveOAConstraintsInfo(const int32_t id)
785 {
786     ErrCode errCode = RemoveOABaseConstraintsInfo(id);
787     if (errCode != ERR_OK) {
788         ACCOUNT_LOGE("Remove os account %{public}d base constraints info failed!", id);
789         return errCode;
790     }
791     errCode = RemoveOAGlobalConstraintsInfo(id);
792     if (errCode != ERR_OK) {
793         ACCOUNT_LOGE("Remove os account %{public}d global constraints info failed!", id);
794         return errCode;
795     }
796     errCode = RemoveOASpecificConstraintsInfo(id);
797     if (errCode != ERR_OK) {
798         ACCOUNT_LOGE("Remove os account %{public}d specific constraints info failed!", id);
799         return errCode;
800     }
801     return ERR_OK;
802 }
803 
RemoveOABaseConstraintsInfo(const int32_t id)804 ErrCode OsAccountControlFileManager::RemoveOABaseConstraintsInfo(const int32_t id)
805 {
806     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
807     Json baseOAConstraintsJson;
808     ErrCode result = GetBaseOAConstraintsFromFile(baseOAConstraintsJson);
809     if (result != ERR_OK) {
810         ACCOUNT_LOGE("Get baseOAConstraints from file failed!");
811         return result;
812     }
813     baseOAConstraintsJson.erase(std::to_string(id));
814     result = SaveBaseOAConstraintsToFile(baseOAConstraintsJson);
815     if (result != ERR_OK) {
816         ACCOUNT_LOGE("SaveBaseOAConstraintsToFile failed!");
817         return result;
818     }
819     return ERR_OK;
820 }
821 
RemoveOAGlobalConstraintsInfo(const int32_t id)822 ErrCode OsAccountControlFileManager::RemoveOAGlobalConstraintsInfo(const int32_t id)
823 {
824     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
825     Json globalOAConstraintsJson;
826     ErrCode result = GetGlobalOAConstraintsFromFile(globalOAConstraintsJson);
827     if (result != ERR_OK) {
828         ACCOUNT_LOGE("Get globalOAConstraints from file failed!");
829         return result;
830     }
831     std::vector<std::string> waitForErase;
832     for (auto it = globalOAConstraintsJson.begin(); it != globalOAConstraintsJson.end(); it++) {
833         if (it.key() != Constants::ALL_GLOBAL_CONSTRAINTS && it.key() != DEVICE_OWNER_ID) {
834             std::vector<std::string> sourceList;
835             OHOS::AccountSA::GetDataByType<std::vector<std::string>>(globalOAConstraintsJson,
836                 globalOAConstraintsJson.end(),
837                 it.key(),
838                 sourceList,
839                 OHOS::AccountSA::JsonType::ARRAY);
840             sourceList.erase(std::remove(sourceList.begin(), sourceList.end(), std::to_string(id)), sourceList.end());
841             if (sourceList.size() == 0) {
842                 std::vector<std::string> allGlobalConstraints;
843                 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(globalOAConstraintsJson,
844                     globalOAConstraintsJson.end(),
845                     Constants::ALL_GLOBAL_CONSTRAINTS,
846                     allGlobalConstraints,
847                     OHOS::AccountSA::JsonType::ARRAY);
848                 allGlobalConstraints.erase(std::remove(allGlobalConstraints.begin(),
849                     allGlobalConstraints.end(), it.key()), allGlobalConstraints.end());
850                 globalOAConstraintsJson[Constants::ALL_GLOBAL_CONSTRAINTS] = allGlobalConstraints;
851                 waitForErase.push_back(it.key());
852             } else {
853                 globalOAConstraintsJson[it.key()] = sourceList;
854             }
855         }
856     }
857     for (auto keyStr : waitForErase) {
858         globalOAConstraintsJson.erase(keyStr);
859     }
860     return SaveGlobalOAConstraintsToFile(globalOAConstraintsJson);
861 }
862 
RemoveOASpecificConstraintsInfo(const int32_t id)863 ErrCode OsAccountControlFileManager::RemoveOASpecificConstraintsInfo(const int32_t id)
864 {
865     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
866     Json specificOAConstraintsJson;
867     ErrCode result = GetSpecificOAConstraintsFromFile(specificOAConstraintsJson);
868     if (result != ERR_OK) {
869         ACCOUNT_LOGE("Get specificOAConstraints from file failed!");
870         return result;
871     }
872     if (specificOAConstraintsJson.find(std::to_string(id)) != specificOAConstraintsJson.end()) {
873         specificOAConstraintsJson.erase(std::to_string(id));
874     }
875     for (auto it = specificOAConstraintsJson.begin(); it != specificOAConstraintsJson.end(); it++) {
876         std::vector<std::string> waitForErase;
877         Json userPrivateConstraintsJson;
878         OHOS::AccountSA::GetDataByType<Json>(specificOAConstraintsJson, specificOAConstraintsJson.end(),
879             it.key(), userPrivateConstraintsJson, OHOS::AccountSA::JsonType::OBJECT);
880         std::vector<std::string> allSpecificConstraints;
881         OHOS::AccountSA::GetDataByType<std::vector<std::string>>(userPrivateConstraintsJson,
882             userPrivateConstraintsJson.end(), Constants::ALL_SPECIFIC_CONSTRAINTS,
883             allSpecificConstraints, OHOS::AccountSA::JsonType::ARRAY);
884         if (allSpecificConstraints.size() == 0) {
885             continue;
886         }
887         for (auto item = userPrivateConstraintsJson.begin(); item != userPrivateConstraintsJson.end(); item++) {
888             if (item.key() == Constants::ALL_SPECIFIC_CONSTRAINTS) {
889                 continue;
890             }
891             std::vector<std::string> sourceList;
892             OHOS::AccountSA::GetDataByType<std::vector<std::string>>(userPrivateConstraintsJson,
893                 userPrivateConstraintsJson.end(), item.key(), sourceList, OHOS::AccountSA::JsonType::ARRAY);
894             sourceList.erase(std::remove(sourceList.begin(),
895                 sourceList.end(), std::to_string(id)), sourceList.end());
896             if (sourceList.size() == 0) {
897                 allSpecificConstraints.erase(std::remove(allSpecificConstraints.begin(),
898                     allSpecificConstraints.end(), item.key()), allSpecificConstraints.end());
899                 userPrivateConstraintsJson[Constants::ALL_SPECIFIC_CONSTRAINTS] = allSpecificConstraints;
900                 waitForErase.push_back(item.key());
901             } else {
902                 userPrivateConstraintsJson[item.key()] = sourceList;
903             }
904         }
905         for (auto keyStr : waitForErase) {
906             userPrivateConstraintsJson.erase(keyStr);
907         }
908         specificOAConstraintsJson[it.key()] = userPrivateConstraintsJson;
909     }
910     return SaveSpecificOAConstraintsToFile(specificOAConstraintsJson);
911 }
912 
UpdateAccountList(const std::string & idStr,bool isAdd)913 ErrCode OsAccountControlFileManager::UpdateAccountList(const std::string& idStr, bool isAdd)
914 {
915     Json accountListJson;
916     ErrCode result = GetAccountListFromFile(accountListJson);
917     if (result != ERR_OK) {
918         ACCOUNT_LOGE("Get account list failed!");
919         return result;
920     }
921 
922     std::vector<std::string> accountIdList;
923     auto jsonEnd = accountListJson.end();
924     OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
925         accountListJson, jsonEnd, Constants::ACCOUNT_LIST, accountIdList, OHOS::AccountSA::JsonType::ARRAY);
926 
927     if (isAdd) {
928         // check repeat
929         if (std::find(accountIdList.begin(), accountIdList.end(), idStr) != accountIdList.end()) {
930             return ERR_OK;  // already exist, no need to add.
931         }
932         accountIdList.emplace_back(idStr);
933     } else {
934         accountIdList.erase(std::remove(accountIdList.begin(), accountIdList.end(), idStr), accountIdList.end());
935     }
936     accountListJson[Constants::ACCOUNT_LIST] = accountIdList;
937     accountListJson[Constants::COUNT_ACCOUNT_NUM] = accountIdList.size();
938     return SaveAccountListToFileAndDataBase(accountListJson);
939 }
940 
UpdateAccountIndex(const OsAccountInfo & osAccountInfo,const bool isDelete)941 ErrCode OsAccountControlFileManager::UpdateAccountIndex(const OsAccountInfo &osAccountInfo, const bool isDelete)
942 {
943     // private type account not write index to index file, don't check name in ValidateOsAccount
944     if (osAccountInfo.GetType() == OsAccountType::PRIVATE) {
945         return ERR_OK;
946     }
947     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
948     Json accountIndexJson;
949     ErrCode result = GetAccountIndexFromFile(accountIndexJson);
950     if (result != ERR_OK) {
951         ACCOUNT_LOGE("Get account index failed!");
952         return result;
953     }
954     std::string localIdStr = std::to_string(osAccountInfo.GetLocalId());
955     if (isDelete) {
956         accountIndexJson.erase(localIdStr);
957     } else {
958         Json accountBaseInfo;
959         accountBaseInfo[Constants::LOCAL_NAME] = osAccountInfo.GetLocalName();
960         accountBaseInfo[Constants::SHORT_NAME] = osAccountInfo.GetShortName();
961         accountIndexJson[localIdStr] = accountBaseInfo;
962     }
963     std::string lastAccountIndexStr = accountIndexJson.dump();
964     result = accountFileOperator_->InputFileByPathAndContent(Constants::ACCOUNT_INDEX_JSON_PATH, lastAccountIndexStr);
965     if (result != ERR_OK) {
966         ACCOUNT_LOGE("Failed to input account index info to file!");
967         return result;
968     }
969 #ifdef ENABLE_FILE_WATCHER
970     accountFileWatcherMgr_.AddAccountInfoDigest(lastAccountIndexStr, Constants::ACCOUNT_INDEX_JSON_PATH);
971 #endif // ENABLE_FILE_WATCHER
972     return ERR_OK;
973 }
974 
SetNextLocalId(const int32_t & nextLocalId)975 ErrCode OsAccountControlFileManager::SetNextLocalId(const int32_t &nextLocalId)
976 {
977     std::lock_guard<std::mutex> lock(operatingIdMutex_);
978     Json accountListJson;
979     ErrCode result = GetAccountListFromFile(accountListJson);
980     if (result != ERR_OK) {
981         ACCOUNT_LOGE("SetNextLocalId get accountList error.");
982         return result;
983     }
984     int32_t nextLocalIdJson = -1;
985     auto jsonEnd = accountListJson.end();
986     if (!GetDataByType<std::int32_t>(accountListJson, jsonEnd,
987         NEXT_LOCAL_ID, nextLocalIdJson, JsonType::NUMBER)) {
988         ACCOUNT_LOGW("SetNextLocalId get next localId failed");
989         nextLocalIdJson = Constants::START_USER_ID + 1;
990     }
991     accountListJson[NEXT_LOCAL_ID] = std::max(nextLocalId, nextLocalIdJson);
992     result = SaveAccountListToFileAndDataBase(accountListJson);
993     if (result != ERR_OK) {
994         ACCOUNT_LOGE("SetNextLocalId save accountListJson error.");
995     }
996     return result;
997 }
998 
RemoveAccountIndex(const int32_t id)999 ErrCode OsAccountControlFileManager::RemoveAccountIndex(const int32_t id)
1000 {
1001     Json accountIndexJson;
1002     ErrCode result = GetAccountIndexFromFile(accountIndexJson);
1003     if (result != ERR_OK) {
1004         ACCOUNT_LOGE("Get account index failed!");
1005         return result;
1006     }
1007     std::string localIdStr = std::to_string(id);
1008     accountIndexJson.erase(localIdStr);
1009     std::string lastAccountIndexStr = accountIndexJson.dump();
1010     result = accountFileOperator_->InputFileByPathAndContent(Constants::ACCOUNT_INDEX_JSON_PATH, lastAccountIndexStr);
1011     if (result != ERR_OK) {
1012         ACCOUNT_LOGE("Failed to input account index info to file!");
1013         return result;
1014     }
1015 #ifdef ENABLE_FILE_WATCHER
1016     accountFileWatcherMgr_.AddAccountInfoDigest(lastAccountIndexStr, Constants::ACCOUNT_INDEX_JSON_PATH);
1017 #endif // ENABLE_FILE_WATCHER
1018     return ERR_OK;
1019 }
1020 
InsertOsAccount(OsAccountInfo & osAccountInfo)1021 ErrCode OsAccountControlFileManager::InsertOsAccount(OsAccountInfo &osAccountInfo)
1022 {
1023     ACCOUNT_LOGI("Enter");
1024     if (osAccountInfo.GetLocalId() < Constants::ADMIN_LOCAL_ID) {
1025         ACCOUNT_LOGE("Error id %{public}d cannot insert", osAccountInfo.GetLocalId());
1026         return ERR_OSACCOUNT_SERVICE_CONTROL_ID_CANNOT_CREATE_ERROR;
1027     }
1028 
1029     std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + osAccountInfo.GetPrimeKey() +
1030                        Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
1031     if (accountFileOperator_->IsExistFile(path) && accountFileOperator_->IsJsonFormat(path)) {
1032         ACCOUNT_LOGE("Account %{public}d already exists", osAccountInfo.GetLocalId());
1033         return ERR_OSACCOUNT_SERVICE_CONTROL_INSERT_FILE_EXISTS_ERROR;
1034     }
1035 
1036     std::string accountInfoStr = osAccountInfo.ToString();
1037     if (accountInfoStr.empty()) {
1038         ACCOUNT_LOGE("Os account info is empty! maybe some illegal characters caused exception!");
1039         return ERR_OSACCOUNT_SERVICE_ACCOUNT_INFO_EMPTY_ERROR;
1040     }
1041     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
1042     if (osAccountInfo.GetLocalId() >= Constants::START_USER_ID) {
1043         ErrCode updateRet = UpdateAccountList(osAccountInfo.GetPrimeKey(), true);
1044         if (updateRet != ERR_OK) {
1045             ACCOUNT_LOGE("Update account list failed, errCode: %{public}d", updateRet);
1046             return updateRet;
1047         }
1048     }
1049     ErrCode result = accountFileOperator_->InputFileByPathAndContent(path, accountInfoStr);
1050     if (result != ERR_OK) {
1051         ACCOUNT_LOGE("InputFileByPathAndContent failed! path %{public}s.", path.c_str());
1052         return result;
1053     }
1054 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1055     osAccountDataBaseOperator_->InsertOsAccountIntoDataBase(osAccountInfo);
1056 #endif // defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1057 
1058 #ifdef ENABLE_FILE_WATCHER
1059     if (osAccountInfo.GetLocalId() >= Constants::START_USER_ID) {
1060         accountFileWatcherMgr_.AddAccountInfoDigest(accountInfoStr, path);
1061         accountFileWatcherMgr_.AddFileWatcher(osAccountInfo.GetLocalId(), eventCallbackFunc_);
1062     }
1063 #endif // ENABLE_FILE_WATCHER
1064     ACCOUNT_LOGI("End");
1065     return ERR_OK;
1066 }
1067 
DelOsAccount(const int id)1068 ErrCode OsAccountControlFileManager::DelOsAccount(const int id)
1069 {
1070     ACCOUNT_LOGD("Enter");
1071     if (id <= Constants::START_USER_ID || id > Constants::MAX_USER_ID) {
1072         ACCOUNT_LOGE("Ivalid input id %{public}d to delete!", id);
1073         return ERR_OSACCOUNT_SERVICE_CONTROL_CANNOT_DELETE_ID_ERROR;
1074     }
1075     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
1076     std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id);
1077     ErrCode result = accountFileOperator_->DeleteDirOrFile(path);
1078     if (result != ERR_OK) {
1079         ACCOUNT_LOGE("DeleteDirOrFile failed! path %{public}s.", path.c_str());
1080         return result;
1081     }
1082 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1083     osAccountDataBaseOperator_->DelOsAccountFromDatabase(id);
1084 #endif // defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1085 #ifdef ENABLE_FILE_WATCHER
1086     path += Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
1087     accountFileWatcherMgr_.DeleteAccountInfoDigest(path);
1088     std::string distributedDataPath =
1089         Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) + DISTRIBUTED_ACCOUNT_FILE_NAME;
1090     accountFileWatcherMgr_.DeleteAccountInfoDigest(distributedDataPath);
1091 #endif // ENABLE_FILE_WATCHER
1092     RemoveAccountIndex(id);
1093     return UpdateAccountList(std::to_string(id), false);
1094 }
1095 
1096 
UpdateOsAccount(OsAccountInfo & osAccountInfo)1097 ErrCode OsAccountControlFileManager::UpdateOsAccount(OsAccountInfo &osAccountInfo)
1098 {
1099     ACCOUNT_LOGD("Start");
1100     std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + osAccountInfo.GetPrimeKey() +
1101                        Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
1102     if (!accountFileOperator_->IsExistFile(path)) {
1103         ACCOUNT_LOGE("Path %{public}s does not exist!", path.c_str());
1104         return ERR_OSACCOUNT_SERVICE_CONTROL_UPDATE_FILE_NOT_EXISTS_ERROR;
1105     }
1106 
1107     std::string accountInfoStr = osAccountInfo.ToString();
1108     if (accountInfoStr.empty()) {
1109         ACCOUNT_LOGE("Account info str is empty!");
1110         return ERR_OSACCOUNT_SERVICE_ACCOUNT_INFO_EMPTY_ERROR;
1111     }
1112     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
1113     ErrCode result = accountFileOperator_->InputFileByPathAndContent(path, accountInfoStr);
1114     if (result != ERR_OK) {
1115         return result;
1116     }
1117 
1118 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1119     // update in database
1120     if (osAccountInfo.GetLocalId() >= Constants::START_USER_ID) {
1121         osAccountDataBaseOperator_->UpdateOsAccountInDatabase(osAccountInfo);
1122     }
1123 #else  // DISTRIBUTED_FEATURE_ENABLED
1124     ACCOUNT_LOGI("No distributed feature!");
1125 #endif // defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1126 #ifdef ENABLE_FILE_WATCHER
1127     accountFileWatcherMgr_.AddAccountInfoDigest(accountInfoStr, path);
1128 #endif // ENABLE_FILE_WATCHER
1129     ACCOUNT_LOGD("End");
1130     return ERR_OK;
1131 }
1132 
AccountExistsWithSerialNumber(const std::vector<OsAccountInfo> & osAccountInfos,int serialNumber)1133 bool AccountExistsWithSerialNumber(const std::vector<OsAccountInfo>& osAccountInfos, int serialNumber)
1134 {
1135     const auto targetSerialNumber = Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN * Constants::CARRY_NUM + serialNumber;
1136     return std::any_of(osAccountInfos.begin(), osAccountInfos.end(),
1137         [&targetSerialNumber](const OsAccountInfo& accountInfo) {
1138         return accountInfo.GetSerialNumber() == targetSerialNumber;
1139     });
1140 }
1141 
GetSerialNumber(int64_t & serialNumber)1142 ErrCode OsAccountControlFileManager::GetSerialNumber(int64_t &serialNumber)
1143 {
1144     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
1145     Json accountListJson;
1146     ErrCode result = GetAccountListFromFile(accountListJson);
1147     if (result != ERR_OK) {
1148         ACCOUNT_LOGE("GetSerialNumber get accountList error");
1149         return result;
1150     }
1151     OHOS::AccountSA::GetDataByType<int64_t>(accountListJson, accountListJson.end(), Constants::SERIAL_NUMBER_NUM,
1152         serialNumber, OHOS::AccountSA::JsonType::NUMBER);
1153     if (serialNumber == Constants::CARRY_NUM) {
1154         accountListJson[IS_SERIAL_NUMBER_FULL] = true;
1155         serialNumber = Constants::SERIAL_NUMBER_NUM_START;
1156     }
1157     bool isSerialNumberFull = false;
1158     OHOS::AccountSA::GetDataByType<bool>(accountListJson, accountListJson.end(), IS_SERIAL_NUMBER_FULL,
1159         isSerialNumberFull, OHOS::AccountSA::JsonType::BOOLEAN);
1160     if (isSerialNumberFull) {
1161         std::vector<OsAccountInfo> osAccountInfos;
1162         result = GetOsAccountList(osAccountInfos);
1163         if (result != ERR_OK) {
1164             ACCOUNT_LOGE("GetSerialNumber get accountList error");
1165             return result;
1166         }
1167         while (serialNumber < Constants::CARRY_NUM) {
1168             bool exists = false;
1169             exists = AccountExistsWithSerialNumber(osAccountInfos, serialNumber);
1170             if (!exists) {
1171                 break;
1172             }
1173             serialNumber++;
1174             serialNumber = (serialNumber == Constants::CARRY_NUM) ? Constants::SERIAL_NUMBER_NUM_START : serialNumber;
1175         }
1176     }
1177     accountListJson[Constants::SERIAL_NUMBER_NUM] = serialNumber + 1;
1178     result = SaveAccountListToFileAndDataBase(accountListJson);
1179     if (result != ERR_OK) {
1180         return result;
1181     }
1182     serialNumber = serialNumber + Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN * Constants::CARRY_NUM;
1183     return ERR_OK;
1184 }
1185 
GetNextLocalId(const std::vector<std::string> & accountIdList,int32_t startId)1186 int32_t OsAccountControlFileManager::GetNextLocalId(const std::vector<std::string> &accountIdList, int32_t startId)
1187 {
1188     do {
1189         if ((startId <= Constants::START_USER_ID) || (startId > Constants::MAX_USER_ID)) {
1190             startId = Constants::START_USER_ID + 1;
1191         }
1192         if (std::find(accountIdList.begin(), accountIdList.end(), std::to_string(startId)) ==
1193             accountIdList.end()) {
1194             break;
1195         }
1196         ++startId;
1197     } while (true);
1198     return startId;
1199 }
1200 
GetAllowCreateId(int & id)1201 ErrCode OsAccountControlFileManager::GetAllowCreateId(int &id)
1202 {
1203     std::lock_guard<std::mutex> lock(operatingIdMutex_);
1204     Json accountListJson;
1205     ErrCode result = GetAccountListFromFile(accountListJson);
1206     if (result != ERR_OK) {
1207         ACCOUNT_LOGE("GetAllowCreateId get accountList error.");
1208         return result;
1209     }
1210     auto jsonEnd = accountListJson.end();
1211     std::vector<std::string> accountIdList;
1212     int32_t nextLocalId = -1;
1213     if (!GetDataByType<std::vector<std::string>>(accountListJson, jsonEnd,
1214         Constants::ACCOUNT_LIST, accountIdList, JsonType::ARRAY)) {
1215         ACCOUNT_LOGE("GetAllowCreateId get accountIdList error");
1216         return ERR_ACCOUNT_COMMON_BAD_JSON_FORMAT_ERROR;
1217     }
1218     if (!GetDataByType<std::int32_t>(accountListJson, jsonEnd,
1219         NEXT_LOCAL_ID, nextLocalId, JsonType::NUMBER)) {
1220         ACCOUNT_LOGW("Get next localId failed");
1221         int32_t lastLocalId = -1;
1222         if (!accountIdList.empty() && StrToInt(accountIdList[accountIdList.size() - 1], lastLocalId)) {
1223             nextLocalId = lastLocalId + 1;
1224         } else {
1225             nextLocalId = Constants::START_USER_ID + 1;
1226             ACCOUNT_LOGW("Convert last item in accountIdList to string failed.");
1227         }
1228     }
1229 
1230     id = GetNextLocalId(accountIdList, nextLocalId);
1231     accountListJson[NEXT_LOCAL_ID] = id + 1;
1232     result = SaveAccountListToFileAndDataBase(accountListJson);
1233     if (result != ERR_OK) {
1234         ACCOUNT_LOGE("GetAllowCreateId save accountListJson error, errCode %{public}d.", result);
1235     }
1236     return result;
1237 }
1238 
GetAccountListFromFile(Json & accountListJson)1239 ErrCode OsAccountControlFileManager::GetAccountListFromFile(Json &accountListJson)
1240 {
1241     ACCOUNT_LOGD("Enter");
1242     accountListJson.clear();
1243     std::string accountList;
1244     std::lock_guard<std::mutex> lock(accountListFileLock_);
1245     ErrCode errCode = accountFileOperator_->GetFileContentByPath(Constants::ACCOUNT_LIST_FILE_JSON_PATH,
1246         accountList);
1247     if (errCode != ERR_OK) {
1248         ACCOUNT_LOGE("GetFileContentByPath failed! error code %{public}d.", errCode);
1249         return errCode;
1250     }
1251     accountListJson = Json::parse(accountList, nullptr, false);
1252     if (accountListJson.is_discarded()) {
1253         ACCOUNT_LOGE("AccountListFile does not comply with the json format.");
1254 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1255         return osAccountDataBaseOperator_->GetAccountListFromStoreID(OS_ACCOUNT_STORE_ID, accountListJson);
1256 #else
1257         return ERR_ACCOUNT_COMMON_BAD_JSON_FORMAT_ERROR;
1258 #endif // defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1259     }
1260     ACCOUNT_LOGD("End");
1261     return ERR_OK;
1262 }
1263 
GetAccountIndexFromFile(Json & accountIndexJson)1264 ErrCode OsAccountControlFileManager::GetAccountIndexFromFile(Json &accountIndexJson)
1265 {
1266     accountIndexJson.clear();
1267     std::string accountIndex;
1268     if (!accountFileOperator_->IsJsonFileReady(Constants::ACCOUNT_INDEX_JSON_PATH)) {
1269         ErrCode result = GetAccountIndexInfo(accountIndex);
1270         if (result != ERR_OK) {
1271             ACCOUNT_LOGE("GetAccountIndexInfo error code %{public}d.", result);
1272             return result;
1273         }
1274     } else {
1275         ErrCode errCode = accountFileOperator_->GetFileContentByPath(Constants::ACCOUNT_INDEX_JSON_PATH, accountIndex);
1276         if (errCode != ERR_OK) {
1277             ACCOUNT_LOGE("GetFileContentByPath failed! error code %{public}d.", errCode);
1278             return errCode;
1279         }
1280     }
1281     accountIndexJson = Json::parse(accountIndex, nullptr, false);
1282     if (accountIndexJson.is_discarded()) {
1283         ACCOUNT_LOGE("parse os account info json data failed");
1284         return ERR_ACCOUNT_COMMON_BAD_JSON_FORMAT_ERROR;
1285     }
1286     return ERR_OK;
1287 }
1288 
GetAccountIndexInfo(std::string & accountIndexInfo)1289 ErrCode OsAccountControlFileManager::GetAccountIndexInfo(std::string &accountIndexInfo)
1290 {
1291     std::vector<OsAccountInfo> osAccountInfos;
1292     ErrCode result = GetOsAccountList(osAccountInfos);
1293     if (result != ERR_OK) {
1294         return result;
1295     }
1296     Json accountIndexJson;
1297     for (auto account = osAccountInfos.begin(); account != osAccountInfos.end(); account++) {
1298         // private account don't check name
1299         if (account->GetType() == OsAccountType::PRIVATE) {
1300             continue;
1301         }
1302         std::string localIdStr = std::to_string(account->GetLocalId());
1303         Json accountIndexElement;
1304         accountIndexElement[Constants::LOCAL_NAME] = account->GetLocalName();
1305         accountIndexElement[Constants::SHORT_NAME] = account->GetShortName();
1306         accountIndexJson[localIdStr] = accountIndexElement;
1307     }
1308     accountIndexInfo = accountIndexJson.dump();
1309     return ERR_OK;
1310 }
1311 
GetBaseOAConstraintsFromFile(Json & baseOAConstraintsJson)1312 ErrCode OsAccountControlFileManager::GetBaseOAConstraintsFromFile(Json &baseOAConstraintsJson)
1313 {
1314     baseOAConstraintsJson.clear();
1315     std::string baseOAConstraints;
1316     std::lock_guard<std::mutex> lock(baseOAConstraintsFileLock_);
1317     ErrCode errCode = accountFileOperator_->GetFileContentByPath(
1318         Constants::BASE_OSACCOUNT_CONSTRAINTS_JSON_PATH, baseOAConstraints);
1319     if (errCode != ERR_OK) {
1320         ACCOUNT_LOGE("GetFileContentByPath failed! error code %{public}d.", errCode);
1321         return errCode;
1322     }
1323     baseOAConstraintsJson = Json::parse(baseOAConstraints, nullptr, false);
1324     if (baseOAConstraintsJson.is_discarded() || !baseOAConstraintsJson.is_object()) {
1325         ACCOUNT_LOGE("Base constraints json data parse failed code.");
1326         return errCode;
1327     }
1328 
1329     return ERR_OK;
1330 }
1331 
GetGlobalOAConstraintsFromFile(Json & globalOAConstraintsJson)1332 ErrCode OsAccountControlFileManager::GetGlobalOAConstraintsFromFile(Json &globalOAConstraintsJson)
1333 {
1334     globalOAConstraintsJson.clear();
1335     std::string globalOAConstraints;
1336     std::lock_guard<std::mutex> lock(globalOAConstraintsFileLock_);
1337     ErrCode errCode = accountFileOperator_->GetFileContentByPath(
1338         Constants::GLOBAL_OSACCOUNT_CONSTRAINTS_JSON_PATH, globalOAConstraints);
1339     if (errCode != ERR_OK) {
1340         ACCOUNT_LOGE("GetFileContentByPath failed! error code %{public}d.", errCode);
1341         return errCode;
1342     }
1343     globalOAConstraintsJson = Json::parse(globalOAConstraints, nullptr, false);
1344     if (globalOAConstraintsJson.is_discarded() || !globalOAConstraintsJson.is_object()) {
1345         ACCOUNT_LOGE("Global constraints json data parse failed code.");
1346         return errCode;
1347     }
1348 
1349     return ERR_OK;
1350 }
1351 
GetSpecificOAConstraintsFromFile(Json & specificOAConstraintsJson)1352 ErrCode OsAccountControlFileManager::GetSpecificOAConstraintsFromFile(Json &specificOAConstraintsJson)
1353 {
1354     specificOAConstraintsJson.clear();
1355     std::string specificOAConstraints;
1356     std::lock_guard<std::mutex> lock(specificOAConstraintsFileLock_);
1357     ErrCode errCode = accountFileOperator_->GetFileContentByPath(
1358         Constants::SPECIFIC_OSACCOUNT_CONSTRAINTS_JSON_PATH, specificOAConstraints);
1359     if (errCode != ERR_OK) {
1360         ACCOUNT_LOGE("GetFileContentByPath failed! error code %{public}d.", errCode);
1361         return errCode;
1362     }
1363     specificOAConstraintsJson = Json::parse(specificOAConstraints, nullptr, false);
1364     if (specificOAConstraintsJson.is_discarded() || !specificOAConstraintsJson.is_object()) {
1365         ACCOUNT_LOGE("Specific constraints json data parse failed code.");
1366         return errCode;
1367     }
1368 
1369     return ERR_OK;
1370 }
1371 
IsFromBaseOAConstraintsList(const int32_t id,const std::string constraint,bool & isExist)1372 ErrCode OsAccountControlFileManager::IsFromBaseOAConstraintsList(
1373     const int32_t id, const std::string constraint, bool &isExist)
1374 {
1375     isExist = false;
1376     std::vector<std::string> constraintsList;
1377     std::lock_guard<std::mutex> lock(baseOAConstraintsFileLock_);
1378     ErrCode errCode = osAccountFileOperator_->GetBaseOAConstraintsList(id, constraintsList);
1379     if (errCode != ERR_OK) {
1380         ACCOUNT_LOGE("GetBaseOAConstraintsList failed! error code %{public}d.", errCode);
1381         return errCode;
1382     }
1383 
1384     if (std::find(constraintsList.begin(), constraintsList.end(), constraint) != constraintsList.end()) {
1385         isExist = true;
1386     }
1387 
1388     return ERR_OK;
1389 }
1390 
IsFromGlobalOAConstraintsList(const int32_t id,const int32_t deviceOwnerId,const std::string constraint,std::vector<ConstraintSourceTypeInfo> & globalSourceList)1391 ErrCode OsAccountControlFileManager::IsFromGlobalOAConstraintsList(const int32_t id, const int32_t deviceOwnerId,
1392     const std::string constraint, std::vector<ConstraintSourceTypeInfo> &globalSourceList)
1393 {
1394     globalSourceList.clear();
1395     std::vector<std::string> constraintsList;
1396     ErrCode errCode = ERR_OK;
1397     {
1398         std::lock_guard<std::mutex> lock(globalOAConstraintsFileLock_);
1399         errCode = osAccountFileOperator_->GetGlobalOAConstraintsList(constraintsList);
1400     }
1401     if (errCode != ERR_OK) {
1402         ACCOUNT_LOGE("GetGlobalOAConstraintsList failed! error code %{public}d.", errCode);
1403         return errCode;
1404     }
1405     if (constraintsList.size() == 0) {
1406         return ERR_OK;
1407     }
1408     if (std::find(constraintsList.begin(), constraintsList.end(), constraint) != constraintsList.end()) {
1409         Json globalOAConstraintsJson;
1410         errCode = GetGlobalOAConstraintsFromFile(globalOAConstraintsJson);
1411         if (errCode != ERR_OK) {
1412             ACCOUNT_LOGE("Get globalOAConstraints from file failed!");
1413             return errCode;
1414         }
1415         std::vector<std::string> globalOAConstraintsList;
1416         OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
1417             globalOAConstraintsJson,
1418             globalOAConstraintsJson.end(),
1419             constraint,
1420             globalOAConstraintsList,
1421             OHOS::AccountSA::JsonType::ARRAY);
1422         ConstraintSourceTypeInfo constraintSourceTypeInfo;
1423         for (auto it = globalOAConstraintsList.begin(); it != globalOAConstraintsList.end(); it++) {
1424             int32_t localId = 0;
1425             if (!StrToInt(*it, localId)) {
1426                 ACCOUNT_LOGE("Convert localId failed");
1427                 continue;
1428             }
1429             if (localId == deviceOwnerId) {
1430                 constraintSourceTypeInfo.localId = localId;
1431                 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_TYPE_DEVICE_OWNER;
1432                 globalSourceList.push_back(constraintSourceTypeInfo);
1433             } else {
1434                 constraintSourceTypeInfo.localId = localId;
1435                 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_TYPE_PROFILE_OWNER;
1436                 globalSourceList.push_back(constraintSourceTypeInfo);
1437             }
1438         }
1439     }
1440     return ERR_OK;
1441 }
1442 
IsFromSpecificOAConstraintsList(const int32_t id,const int32_t deviceOwnerId,const std::string constraint,std::vector<ConstraintSourceTypeInfo> & specificSourceList)1443 ErrCode OsAccountControlFileManager::IsFromSpecificOAConstraintsList(const int32_t id, const int32_t deviceOwnerId,
1444     const std::string constraint, std::vector<ConstraintSourceTypeInfo> &specificSourceList)
1445 {
1446     specificSourceList.clear();
1447     std::vector<std::string> constraintsList;
1448     ErrCode errCode = ERR_OK;
1449     {
1450         std::lock_guard<std::mutex> lock(specificOAConstraintsFileLock_);
1451         errCode = osAccountFileOperator_->GetSpecificOAConstraintsList(id, constraintsList);
1452     }
1453     if (errCode != ERR_OK) {
1454         ACCOUNT_LOGE("GetSpecificOAConstraintsList failed! error code %{public}d.", errCode);
1455         return errCode;
1456     }
1457 
1458     if (std::find(constraintsList.begin(), constraintsList.end(), constraint) != constraintsList.end()) {
1459         Json specificOAConstraintsJson;
1460         errCode = GetSpecificOAConstraintsFromFile(specificOAConstraintsJson);
1461         if (errCode != ERR_OK) {
1462             ACCOUNT_LOGE("Get specificOAConstraints from file failed!");
1463             return errCode;
1464         }
1465         Json specificOAConstraintsInfo;
1466         OHOS::AccountSA::GetDataByType<Json>(specificOAConstraintsJson, specificOAConstraintsJson.end(),
1467             std::to_string(id), specificOAConstraintsInfo, OHOS::AccountSA::JsonType::OBJECT);
1468         std::vector<std::string> specificConstraintSource;
1469         OHOS::AccountSA::GetDataByType<std::vector<std::string>>(specificOAConstraintsInfo,
1470             specificOAConstraintsInfo.end(), constraint,
1471             specificConstraintSource, OHOS::AccountSA::JsonType::ARRAY);
1472         ConstraintSourceTypeInfo constraintSourceTypeInfo;
1473         for (auto it = specificConstraintSource.begin(); it != specificConstraintSource.end(); it++) {
1474             int32_t localId = 0;
1475             if (!StrToInt(*it, localId)) {
1476                 ACCOUNT_LOGE("Convert localId failed");
1477                 continue;
1478             }
1479             if (localId == deviceOwnerId) {
1480                 constraintSourceTypeInfo.localId = localId;
1481                 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_TYPE_DEVICE_OWNER;
1482                 specificSourceList.push_back(constraintSourceTypeInfo);
1483             } else {
1484                 constraintSourceTypeInfo.localId = localId;
1485                 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_TYPE_PROFILE_OWNER;
1486                 specificSourceList.push_back(constraintSourceTypeInfo);
1487             }
1488         }
1489     }
1490     return ERR_OK;
1491 }
1492 
SaveAccountListToFile(const Json & accountListJson)1493 ErrCode OsAccountControlFileManager::SaveAccountListToFile(const Json &accountListJson)
1494 {
1495     std::lock_guard<std::mutex> lock(accountListFileLock_);
1496     ErrCode result =
1497         accountFileOperator_->InputFileByPathAndContent(Constants::ACCOUNT_LIST_FILE_JSON_PATH, accountListJson.dump());
1498     if (result != ERR_OK) {
1499         ACCOUNT_LOGE("Cannot save save account list file content!");
1500         return result;
1501     }
1502 #ifdef ENABLE_FILE_WATCHER
1503     accountFileWatcherMgr_.AddAccountInfoDigest(accountListJson.dump(), Constants::ACCOUNT_LIST_FILE_JSON_PATH);
1504 #endif // ENABLE_FILE_WATCHER
1505     ACCOUNT_LOGD("Save account list file succeed!");
1506     return ERR_OK;
1507 }
1508 
SaveBaseOAConstraintsToFile(const Json & baseOAConstraints)1509 ErrCode OsAccountControlFileManager::SaveBaseOAConstraintsToFile(const Json &baseOAConstraints)
1510 {
1511     std::lock_guard<std::mutex> lock(baseOAConstraintsFileLock_);
1512     ErrCode result = accountFileOperator_->InputFileByPathAndContent(
1513         Constants::BASE_OSACCOUNT_CONSTRAINTS_JSON_PATH, baseOAConstraints.dump());
1514     if (result != ERR_OK) {
1515         ACCOUNT_LOGE("Cannot save base osaccount constraints file content!");
1516         return result;
1517     }
1518 #ifdef ENABLE_FILE_WATCHER
1519     accountFileWatcherMgr_.AddAccountInfoDigest(
1520         baseOAConstraints.dump(), Constants::BASE_OSACCOUNT_CONSTRAINTS_JSON_PATH);
1521 #endif // ENABLE_FILE_WATCHER
1522     return ERR_OK;
1523 }
1524 
SaveGlobalOAConstraintsToFile(const Json & globalOAConstraints)1525 ErrCode OsAccountControlFileManager::SaveGlobalOAConstraintsToFile(const Json &globalOAConstraints)
1526 {
1527     std::lock_guard<std::mutex> lock(globalOAConstraintsFileLock_);
1528     ErrCode result = accountFileOperator_->InputFileByPathAndContent(
1529         Constants::GLOBAL_OSACCOUNT_CONSTRAINTS_JSON_PATH, globalOAConstraints.dump());
1530     if (result != ERR_OK) {
1531         ACCOUNT_LOGE("Cannot save global osAccount constraints file content!");
1532         return result;
1533     }
1534 #ifdef ENABLE_FILE_WATCHER
1535     accountFileWatcherMgr_.AddAccountInfoDigest(
1536         globalOAConstraints.dump(), Constants::GLOBAL_OSACCOUNT_CONSTRAINTS_JSON_PATH);
1537 #endif // ENABLE_FILE_WATCHER
1538     return ERR_OK;
1539 }
1540 
SaveSpecificOAConstraintsToFile(const Json & specificOAConstraints)1541 ErrCode OsAccountControlFileManager::SaveSpecificOAConstraintsToFile(const Json &specificOAConstraints)
1542 {
1543     std::lock_guard<std::mutex> lock(specificOAConstraintsFileLock_);
1544     ErrCode result = accountFileOperator_->InputFileByPathAndContent(
1545         Constants::SPECIFIC_OSACCOUNT_CONSTRAINTS_JSON_PATH, specificOAConstraints.dump());
1546     if (result != ERR_OK) {
1547         ACCOUNT_LOGE("Cannot save specific osAccount constraints file content!");
1548         return result;
1549     }
1550 #ifdef ENABLE_FILE_WATCHER
1551     accountFileWatcherMgr_.AddAccountInfoDigest(
1552         specificOAConstraints.dump(), Constants::SPECIFIC_OSACCOUNT_CONSTRAINTS_JSON_PATH);
1553 #endif // ENABLE_FILE_WATCHER
1554     return ERR_OK;
1555 }
1556 
GetDeviceOwnerId(int & deviceOwnerId)1557 ErrCode OsAccountControlFileManager::GetDeviceOwnerId(int &deviceOwnerId)
1558 {
1559     Json globalOAConstraintsJson;
1560     ErrCode result = GetGlobalOAConstraintsFromFile(globalOAConstraintsJson);
1561     if (result != ERR_OK) {
1562         ACCOUNT_LOGE("Get global json data from file failed!");
1563         return result;
1564     }
1565     OHOS::AccountSA::GetDataByType<int>(
1566         globalOAConstraintsJson,
1567         globalOAConstraintsJson.end(),
1568         DEVICE_OWNER_ID,
1569         deviceOwnerId,
1570         OHOS::AccountSA::JsonType::NUMBER);
1571     return ERR_OK;
1572 }
1573 
UpdateDeviceOwnerId(const int deviceOwnerId)1574 ErrCode OsAccountControlFileManager::UpdateDeviceOwnerId(const int deviceOwnerId)
1575 {
1576     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
1577     Json globalOAConstraintsJson;
1578     ErrCode result = GetGlobalOAConstraintsFromFile(globalOAConstraintsJson);
1579     if (result != ERR_OK) {
1580         ACCOUNT_LOGE("Get global json data from file failed!");
1581         return result;
1582     }
1583     globalOAConstraintsJson[DEVICE_OWNER_ID] = deviceOwnerId;
1584     return SaveGlobalOAConstraintsToFile(globalOAConstraintsJson);
1585 }
1586 
SetDefaultActivatedOsAccount(const int32_t id)1587 ErrCode OsAccountControlFileManager::SetDefaultActivatedOsAccount(const int32_t id)
1588 {
1589     std::lock_guard<std::mutex> lock(accountInfoFileLock_);
1590     Json accountListJson;
1591     ErrCode result = GetAccountListFromFile(accountListJson);
1592     if (result != ERR_OK) {
1593         ACCOUNT_LOGE("Get account list failed!");
1594         return result;
1595     }
1596 
1597     accountListJson[DEFAULT_ACTIVATED_ACCOUNT_ID] = id;
1598     return SaveAccountListToFileAndDataBase(accountListJson);
1599 }
1600 
GetDefaultActivatedOsAccount(int32_t & id)1601 ErrCode OsAccountControlFileManager::GetDefaultActivatedOsAccount(int32_t &id)
1602 {
1603     Json accountListJsonData;
1604     ErrCode result = GetAccountListFromFile(accountListJsonData);
1605     if (result != ERR_OK) {
1606         return result;
1607     }
1608     OHOS::AccountSA::GetDataByType<int>(accountListJsonData,
1609         accountListJsonData.end(),
1610         DEFAULT_ACTIVATED_ACCOUNT_ID,
1611         id,
1612         OHOS::AccountSA::JsonType::NUMBER);
1613     return ERR_OK;
1614 }
1615 
SaveAccountListToFileAndDataBase(const Json & accountListJson)1616 ErrCode OsAccountControlFileManager::SaveAccountListToFileAndDataBase(const Json &accountListJson)
1617 {
1618 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1619     osAccountDataBaseOperator_->UpdateOsAccountIDListInDatabase(accountListJson);
1620 #endif // defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1621     return SaveAccountListToFile(accountListJson);
1622 }
1623 
IsOsAccountExists(const int id,bool & isExists)1624 ErrCode OsAccountControlFileManager::IsOsAccountExists(const int id, bool &isExists)
1625 {
1626     isExists = false;
1627     std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) +
1628                        Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
1629     // check exist
1630     if (!accountFileOperator_->IsExistFile(path)) {
1631         ACCOUNT_LOGI("IsOsAccountExists path %{public}s does not exist!", path.c_str());
1632         return ERR_OK;
1633     }
1634 
1635     // check format
1636     if (!accountFileOperator_->IsJsonFormat(path)) {
1637         ACCOUNT_LOGI("IsOsAccountExists path %{public}s wrong format!", path.c_str());
1638         return ERR_OK;
1639     }
1640 
1641     isExists = true;
1642     return ERR_OK;
1643 }
1644 
GetPhotoById(const int id,std::string & photo)1645 ErrCode OsAccountControlFileManager::GetPhotoById(const int id, std::string &photo)
1646 {
1647     if ((photo != USER_PHOTO_FILE_JPG_NAME) && (photo != USER_PHOTO_FILE_PNG_NAME)
1648         && (photo != Constants::USER_PHOTO_FILE_TXT_NAME)) {
1649         return ERR_OK;
1650     }
1651     std::string path =
1652         Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) + Constants::PATH_SEPARATOR + photo;
1653     std::string byteStr = "";
1654     ErrCode errCode = accountFileOperator_->GetFileContentByPath(path, byteStr);
1655     if (errCode != ERR_OK) {
1656         ACCOUNT_LOGE("GetPhotoById cannot find photo file error");
1657         return errCode;
1658     }
1659     if (photo == Constants::USER_PHOTO_FILE_TXT_NAME) {
1660         photo = byteStr;
1661         return ERR_OK;
1662     }
1663     // USER_PHOTO_FILE_JPG_NAME and USER_PHOTO_FILE_PNG_NAME are compatible with previous data
1664     if (photo == USER_PHOTO_FILE_JPG_NAME) {
1665         photo = USER_PHOTO_BASE_JPG_HEAD + osAccountPhotoOperator_->EnCode(byteStr.c_str(), byteStr.length());
1666     } else {
1667         photo = USER_PHOTO_BASE_PNG_HEAD + osAccountPhotoOperator_->EnCode(byteStr.c_str(), byteStr.length());
1668     }
1669     std::string substr = "\r\n";
1670     while (photo.find(substr) != std::string::npos) {
1671         photo.erase(photo.find(substr), substr.length());
1672     }
1673     return ERR_OK;
1674 }
1675 
SetPhotoById(const int id,const std::string & photo)1676 ErrCode OsAccountControlFileManager::SetPhotoById(const int id, const std::string &photo)
1677 {
1678     std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id)
1679         + Constants::PATH_SEPARATOR + Constants::USER_PHOTO_FILE_TXT_NAME;
1680     ErrCode errCode = accountFileOperator_->InputFileByPathAndContent(path, photo);
1681     if (errCode != ERR_OK) {
1682         return errCode;
1683     }
1684     return ERR_OK;
1685 }
1686 
GetGlobalOAConstraintsList(std::vector<std::string> & constraintsList)1687 ErrCode OsAccountControlFileManager::GetGlobalOAConstraintsList(std::vector<std::string> &constraintsList)
1688 {
1689     std::lock_guard<std::mutex> lock(globalOAConstraintsFileLock_);
1690     return osAccountFileOperator_->GetGlobalOAConstraintsList(constraintsList);
1691 }
1692 
GetSpecificOAConstraintsList(const int32_t id,std::vector<std::string> & constraintsList)1693 ErrCode OsAccountControlFileManager::GetSpecificOAConstraintsList(
1694     const int32_t id, std::vector<std::string> &constraintsList)
1695 {
1696     std::lock_guard<std::mutex> lock(specificOAConstraintsFileLock_);
1697     return osAccountFileOperator_->GetSpecificOAConstraintsList(id, constraintsList);
1698 }
1699 
GetIsMultiOsAccountEnable(bool & isMultiOsAccountEnable)1700 ErrCode OsAccountControlFileManager::GetIsMultiOsAccountEnable(bool &isMultiOsAccountEnable)
1701 {
1702     return osAccountFileOperator_->GetIsMultiOsAccountEnable(isMultiOsAccountEnable);
1703 }
CheckConstraintsList(const std::vector<std::string> & constraints,bool & isExists,bool & isOverSize)1704 ErrCode OsAccountControlFileManager::CheckConstraintsList(
1705     const std::vector<std::string> &constraints, bool &isExists, bool &isOverSize)
1706 {
1707     return osAccountFileOperator_->CheckConstraintsList(constraints, isExists, isOverSize);
1708 }
1709 
IsAllowedCreateAdmin(bool & isAllowedCreateAdmin)1710 ErrCode OsAccountControlFileManager::IsAllowedCreateAdmin(bool &isAllowedCreateAdmin)
1711 {
1712     return osAccountFileOperator_->IsAllowedCreateAdmin(isAllowedCreateAdmin);
1713 }
1714 
GetCreatedOsAccountNumFromDatabase(const std::string & storeID,int & createdOsAccountNum)1715 ErrCode OsAccountControlFileManager::GetCreatedOsAccountNumFromDatabase(const std::string& storeID,
1716     int &createdOsAccountNum)
1717 {
1718 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1719     return osAccountDataBaseOperator_->GetCreatedOsAccountNumFromDatabase(storeID, createdOsAccountNum);
1720 #else
1721     return ERR_ACCOUNT_COMMON_INTERFACE_NOT_SUPPORT_ERROR;
1722 #endif // defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1723 }
1724 
GetSerialNumberFromDatabase(const std::string & storeID,int64_t & serialNumber)1725 ErrCode OsAccountControlFileManager::GetSerialNumberFromDatabase(const std::string& storeID,
1726     int64_t &serialNumber)
1727 {
1728 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1729     return osAccountDataBaseOperator_->GetSerialNumberFromDatabase(storeID, serialNumber);
1730 #else
1731     return ERR_ACCOUNT_COMMON_INTERFACE_NOT_SUPPORT_ERROR;
1732 #endif // defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1733 }
1734 
GetMaxAllowCreateIdFromDatabase(const std::string & storeID,int & id)1735 ErrCode OsAccountControlFileManager::GetMaxAllowCreateIdFromDatabase(const std::string& storeID,
1736     int &id)
1737 {
1738 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1739     return osAccountDataBaseOperator_->GetMaxAllowCreateIdFromDatabase(storeID, id);
1740 #else
1741     return ERR_ACCOUNT_COMMON_INTERFACE_NOT_SUPPORT_ERROR;
1742 #endif // defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1743 }
1744 
GetOsAccountFromDatabase(const std::string & storeID,const int id,OsAccountInfo & osAccountInfo)1745 ErrCode OsAccountControlFileManager::GetOsAccountFromDatabase(const std::string& storeID,
1746     const int id, OsAccountInfo &osAccountInfo)
1747 {
1748 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1749     return osAccountDataBaseOperator_->GetOsAccountFromDatabase(storeID, id, osAccountInfo);
1750 #else
1751     return ERR_ACCOUNT_COMMON_INTERFACE_NOT_SUPPORT_ERROR;
1752 #endif // defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1753 }
1754 
GetOsAccountListFromDatabase(const std::string & storeID,std::vector<OsAccountInfo> & osAccountList)1755 ErrCode OsAccountControlFileManager::GetOsAccountListFromDatabase(const std::string& storeID,
1756     std::vector<OsAccountInfo> &osAccountList)
1757 {
1758 #if defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1759     return osAccountDataBaseOperator_->GetOsAccountListFromDatabase(storeID, osAccountList);
1760 #else
1761     return ERR_ACCOUNT_COMMON_INTERFACE_NOT_SUPPORT_ERROR;
1762 #endif // defined(HAS_KV_STORE_PART) && defined(DISTRIBUTED_FEATURE_ENABLED)
1763 }
1764 }  // namespace AccountSA
1765 }  // namespace OHOS