• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "os_account_control_file_manager.h"
16 #include <dirent.h>
17 #include <sstream>
18 #include <sys/types.h>
19 #include "account_log_wrapper.h"
20 #include "os_account_constants.h"
21 #include "os_account_standard_interface.h"
22 
23 namespace OHOS {
24 namespace AccountSA {
25 namespace {
GetValidAccountID(const std::string & dirName,std::int32_t & accountID)26 bool GetValidAccountID(const std::string& dirName, std::int32_t& accountID)
27 {
28     // check length first
29     if (dirName.empty() || dirName.size() > Constants::MAX_USER_ID_LENGTH) {
30         return false;
31     }
32 
33     for (char c : dirName) {
34         if (c < '0' || c > '9') {  // check whether it is digit
35             return false;
36         }
37     }
38 
39     // convert to osaccount id
40     std::stringstream sstream;
41     sstream << dirName;
42     sstream >> accountID;
43     return (accountID >= Constants::ADMIN_LOCAL_ID && accountID <= Constants::MAX_USER_ID);
44 }
45 }
46 
OsAccountControlFileManager()47 OsAccountControlFileManager::OsAccountControlFileManager()
48 {
49     accountFileOperator_ = std::make_shared<AccountFileOperator>();
50     osAccountDataBaseOperator_ = std::make_shared<OsAccountDatabaseOperator>();
51     osAccountFileOperator_ = std::make_shared<OsAccountFileOperator>();
52     osAccountPhotoOperator_ = std::make_shared<OsAccountPhotoOperator>();
53 }
~OsAccountControlFileManager()54 OsAccountControlFileManager::~OsAccountControlFileManager()
55 {}
Init()56 void OsAccountControlFileManager::Init()
57 {
58     ACCOUNT_LOGI("OsAccountControlFileManager Init start");
59     osAccountDataBaseOperator_->Init();
60     osAccountFileOperator_->Init();
61     if (!accountFileOperator_->IsExistFile(Constants::ACCOUNT_LIST_FILE_JSON_PATH) ||
62         !accountFileOperator_->IsJsonFormat(Constants::ACCOUNT_LIST_FILE_JSON_PATH)) {
63         ACCOUNT_LOGI("OsAccountControlFileManager there is not have valid account list, create!");
64         RecoverAccountListJsonFile();
65     }
66     ACCOUNT_LOGI("OsAccountControlFileManager Init end");
67 }
68 
BuildAndSaveAccountListJsonFile(const std::vector<std::string> & accounts)69 void OsAccountControlFileManager::BuildAndSaveAccountListJsonFile(const std::vector<std::string>& accounts)
70 {
71     ACCOUNT_LOGI("enter.");
72     Json accountList = Json {
73         {Constants::ACCOUNT_LIST, accounts},
74         {Constants::COUNT_ACCOUNT_NUM, accounts.size()},
75         {Constants::MAX_ALLOW_CREATE_ACCOUNT_ID, Constants::MAX_USER_ID},
76         {Constants::SERIAL_NUMBER_NUM, Constants::SERIAL_NUMBER_NUM_START},
77         {Constants::IS_SERIAL_NUMBER_FULL, Constants::IS_SERIAL_NUMBER_FULL_INIT_VALUE},
78     };
79     SaveAccountListToFile(accountList);
80 }
81 
RecoverAccountListJsonFile()82 void OsAccountControlFileManager::RecoverAccountListJsonFile()
83 {
84     // get account list
85     std::vector<std::string> accounts;
86     DIR* rootDir = opendir(Constants::USER_INFO_BASE.c_str());
87     if (rootDir == nullptr) {
88         accounts.push_back(std::to_string(Constants::START_USER_ID));  // account 100 always exists
89         BuildAndSaveAccountListJsonFile(accounts);
90         ACCOUNT_LOGE("cannot open dir %{public}s, err %{public}d.", Constants::USER_INFO_BASE.c_str(), errno);
91         return;
92     }
93 
94     struct dirent* curDir = nullptr;
95     while ((curDir = readdir(rootDir)) != nullptr) {
96         std::string curDirName(curDir->d_name);
97         if (curDirName == "." || curDirName == ".." || curDir->d_type != DT_DIR) {
98             continue;
99         }
100 
101         // get and check os account id
102         std::int32_t accountID = Constants::INVALID_OS_ACCOUNT_ID;
103         if (!GetValidAccountID(curDirName, accountID)) {
104             ACCOUNT_LOGE("invalid account id %{public}s detected in %{public}s.", curDirName.c_str(),
105                 Constants::USER_INFO_BASE.c_str());
106             continue;
107         }
108 
109         // check repeat
110         bool sameAccountID = false;
111         std::string curAccountIDStr = std::to_string(accountID);
112         for (size_t i = 0; i < accounts.size(); ++i) {
113             if (accounts[i] == curAccountIDStr) {
114                 ACCOUNT_LOGE("repeate account id %{public}s detected in %{public}s.", curAccountIDStr.c_str(),
115                     Constants::USER_INFO_BASE.c_str());
116                 sameAccountID = true;
117                 break;
118             }
119         }
120 
121         if (!sameAccountID && accountID >= Constants::START_USER_ID) {
122             accounts.push_back(curAccountIDStr);
123         }
124     }
125     BuildAndSaveAccountListJsonFile(accounts);
126 }
127 
GetOsAccountList(std::vector<OsAccountInfo> & osAccountList)128 ErrCode OsAccountControlFileManager::GetOsAccountList(std::vector<OsAccountInfo> &osAccountList)
129 {
130     ACCOUNT_LOGI("OsAccountControlFileManager GetOsAccountList  start");
131     osAccountList.clear();
132     Json accountListJson;
133     if (GetAccountListFromFile(accountListJson) != ERR_OK) {
134         ACCOUNT_LOGE(
135             "OsAccountControlFileManager GetOsAccountList ERR_OSACCOUNT_SERVICE_CONTROL_GET_OS_ACCOUNT_LIST_ERROR");
136         return ERR_OSACCOUNT_SERVICE_CONTROL_GET_OS_ACCOUNT_LIST_ERROR;
137     }
138     const auto &jsonObjectEnd = accountListJson.end();
139     std::vector<std::string> idList;
140     OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
141         accountListJson, jsonObjectEnd, Constants::ACCOUNT_LIST, idList, OHOS::AccountSA::JsonType::ARRAY);
142     if (!idList.empty()) {
143         for (auto it : idList) {
144             OsAccountInfo osAccountInfo;
145             if (GetOsAccountInfoById(std::atoi(it.c_str()), osAccountInfo) == ERR_OK) {
146                 if (osAccountInfo.GetPhoto() != "") {
147                     std::string photo = osAccountInfo.GetPhoto();
148                     GetPhotoById(osAccountInfo.GetLocalId(), photo);
149                     osAccountInfo.SetPhoto(photo);
150                 }
151                 osAccountList.push_back(osAccountInfo);
152             }
153         }
154     }
155     ACCOUNT_LOGI("OsAccountControlFileManager GetOsAccountList  end");
156     return ERR_OK;
157 }
158 
GetOsAccountInfoById(const int id,OsAccountInfo & osAccountInfo)159 ErrCode OsAccountControlFileManager::GetOsAccountInfoById(const int id, OsAccountInfo &osAccountInfo)
160 {
161     ACCOUNT_LOGI("OsAccountControlFileManager GetOsAccountInfoById start");
162     std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) +
163                        Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
164     if (!accountFileOperator_->IsExistFile(path)) {
165         ACCOUNT_LOGE("OsAccountControlFileManager GetOsAccountInfoById file donnot exists err");
166         return ERR_OSACCOUNT_SERVICE_CONTROL_SELECT_OS_ACCOUNT_ERROR;
167     }
168     std::string accountInfoStr;
169     if (accountFileOperator_->GetFileContentByPath(path, accountInfoStr) != ERR_OK) {
170         ACCOUNT_LOGE("OsAccountControlFileManager GetOsAccountInfoById file cannot get info err");
171         return ERR_OSACCOUNT_SERVICE_CONTROL_SELECT_OS_ACCOUNT_ERROR;
172     }
173     osAccountInfo.FromJson(Json::parse(accountInfoStr, nullptr, false));
174     ACCOUNT_LOGI("OsAccountControlFileManager GetOsAccountInfoById end");
175     return ERR_OK;
176 }
177 
GetConstraintsByType(const OsAccountType type,std::vector<std::string> & constratins)178 ErrCode OsAccountControlFileManager::GetConstraintsByType(
179     const OsAccountType type, std::vector<std::string> &constratins)
180 {
181     int typeInit = static_cast<int>(type);
182     return osAccountFileOperator_->GetConstraintsByType(typeInit, constratins);
183 }
184 
UpdateAccountList(const std::string & idStr,bool isAdd)185 ErrCode OsAccountControlFileManager::UpdateAccountList(const std::string& idStr, bool isAdd)
186 {
187     Json accountListJson;
188     if (GetAccountListFromFile(accountListJson) != ERR_OK) {
189         ACCOUNT_LOGE("get account list failed!");
190         return ERR_OSACCOUNT_SERVICE_CONTROL_GET_OS_ACCOUNT_LIST_ERROR;
191     }
192 
193     std::vector<std::string> accountIdList;
194     auto jsonEnd = accountListJson.end();
195     OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
196         accountListJson, jsonEnd, Constants::ACCOUNT_LIST, accountIdList, OHOS::AccountSA::JsonType::ARRAY);
197 
198     if (isAdd) {
199         // check repeat
200         if (std::find(accountIdList.begin(), accountIdList.end(), idStr) != accountIdList.end()) {
201             return ERR_OK;  // already exist, no need to add.
202         }
203         accountIdList.emplace_back(idStr);
204     } else {
205         accountIdList.erase(std::remove(accountIdList.begin(), accountIdList.end(), idStr), accountIdList.end());
206     }
207     accountListJson[Constants::ACCOUNT_LIST] = accountIdList;
208     accountListJson[Constants::COUNT_ACCOUNT_NUM] = accountIdList.size();
209 
210     if (SaveAccountListToFileAndDataBase(accountListJson) != ERR_OK) {
211         ACCOUNT_LOGE("SaveAccountListToFileAndDataBase failed!");
212         return ERR_OSACCOUNT_SERVICE_CONTROL_INSERT_OS_ACCOUNT_LIST_ERROR;
213     }
214     return ERR_OK;
215 }
216 
InsertOsAccount(OsAccountInfo & osAccountInfo)217 ErrCode OsAccountControlFileManager::InsertOsAccount(OsAccountInfo &osAccountInfo)
218 {
219     ACCOUNT_LOGI("enter");
220     if (osAccountInfo.GetLocalId() < Constants::ADMIN_LOCAL_ID ||
221         osAccountInfo.GetLocalId() > Constants::MAX_USER_ID) {
222         ACCOUNT_LOGE("error id %{public}d cannot insert", osAccountInfo.GetLocalId());
223         return ERR_OSACCOUNT_SERVICE_CONTROL_ID_CANNOT_CREATE_ERROR;
224     }
225 
226     std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + osAccountInfo.GetPrimeKey() +
227                        Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
228     if (accountFileOperator_->IsExistFile(path) && accountFileOperator_->IsJsonFormat(path)) {
229         ACCOUNT_LOGE("OsAccountControlFileManagerInsertOsAccountControlFileManagerCreateAccountDir ERR");
230         return ERR_OSACCOUNT_SERVICE_CONTROL_INSERT_FILE_EXISTS_ERROR;
231     }
232 
233     if (accountFileOperator_->InputFileByPathAndContent(path, osAccountInfo.ToString()) != ERR_OK) {
234         ACCOUNT_LOGE("OsAccountControlFileManager InsertOsAccount");
235         return ERR_OSACCOUNT_SERVICE_CONTROL_INSERT_OS_ACCOUNT_FILE_ERROR;
236     }
237     osAccountDataBaseOperator_->InsertOsAccountIntoDataBase(osAccountInfo);
238 
239     if (osAccountInfo.GetLocalId() >= Constants::START_USER_ID) {
240         return UpdateAccountList(osAccountInfo.GetPrimeKey(), true);
241     }
242     return ERR_OK;
243 }
244 
DelOsAccount(const int id)245 ErrCode OsAccountControlFileManager::DelOsAccount(const int id)
246 {
247     ACCOUNT_LOGI("enter");
248     if (id <= Constants::START_USER_ID || id > Constants::MAX_USER_ID) {
249         ACCOUNT_LOGE("invalid input id %{public}d to delete!", id);
250         return ERR_OSACCOUNT_SERVICE_CONTROL_CANNOT_DELETE_ID_ERROR;
251     }
252 
253     std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id);
254     if (accountFileOperator_->DeleteDirOrFile(path) != ERR_OK) {
255         ACCOUNT_LOGE("OsAccountControlFileManager DelOsAccount delete dir error");
256         return ERR_OSACCOUNT_SERVICE_CONTROL_DEL_OS_ACCOUNT_INFO_ERROR;
257     }
258     osAccountDataBaseOperator_->DelOsAccountFromDatabase(id);
259     return UpdateAccountList(std::to_string(id), false);
260 }
261 
UpdateOsAccount(OsAccountInfo & osAccountInfo)262 ErrCode OsAccountControlFileManager::UpdateOsAccount(OsAccountInfo &osAccountInfo)
263 {
264     ACCOUNT_LOGI("OsAccountControlFileManager UpdateOsAccount start");
265     std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + osAccountInfo.GetPrimeKey() +
266                        Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
267     if (!accountFileOperator_->IsExistFile(path)) {
268         ACCOUNT_LOGE("path %{public}s does not exist!", path.c_str());
269         return ERR_OSACCOUNT_SERVICE_CONTROL_UPDATE_FILE_NOT_EXISTS_ERROR;
270     }
271     if (accountFileOperator_->InputFileByPathAndContent(path, osAccountInfo.ToString()) != ERR_OK) {
272         return ERR_OSACCOUNT_SERVICE_CONTROL_UPDATE_FILE_ERROR;
273     }
274 
275     // update in database
276     if (osAccountInfo.GetLocalId() >= Constants::START_USER_ID) {
277         osAccountDataBaseOperator_->UpdateOsAccountInDatabase(osAccountInfo);
278     }
279 
280     ACCOUNT_LOGI("OsAccountControlFileManager UpdateOsAccount end");
281     return ERR_OK;
282 }
283 
GetMaxCreatedOsAccountNum(int & maxCreatedOsAccountNum)284 ErrCode OsAccountControlFileManager::GetMaxCreatedOsAccountNum(int &maxCreatedOsAccountNum)
285 {
286     ACCOUNT_LOGI("OsAccountControlFileManager GetMaxCreatedOsAccountNum start");
287     Json accountListJson;
288     if (GetAccountListFromFile(accountListJson) != ERR_OK) {
289         return ERR_OSACCOUNT_SERVICE_CONTROL_GET_OS_ACCOUNT_LIST_ERROR;
290     }
291     OHOS::AccountSA::GetDataByType<int>(accountListJson,
292         accountListJson.end(),
293         Constants::MAX_ALLOW_CREATE_ACCOUNT_ID,
294         maxCreatedOsAccountNum,
295         OHOS::AccountSA::JsonType::NUMBER);
296     maxCreatedOsAccountNum -= Constants::START_USER_ID;
297     ACCOUNT_LOGI("OsAccountControlFileManager GetMaxCreatedOsAccountNum end");
298     return ERR_OK;
299 }
300 
GetSerialNumber(int64_t & serialNumber)301 ErrCode OsAccountControlFileManager::GetSerialNumber(int64_t &serialNumber)
302 {
303     Json accountListJson;
304     if (GetAccountListFromFile(accountListJson) != ERR_OK) {
305         ACCOUNT_LOGE("GetSerialNumber get accountList error");
306         return ERR_OSACCOUNT_SERVICE_CONTROL_GET_OS_ACCOUNT_LIST_ERROR;
307     }
308     OHOS::AccountSA::GetDataByType<int64_t>(accountListJson,
309         accountListJson.end(),
310         Constants::SERIAL_NUMBER_NUM,
311         serialNumber,
312         OHOS::AccountSA::JsonType::NUMBER);
313     if (serialNumber == Constants::CARRY_NUM) {
314         accountListJson[Constants::IS_SERIAL_NUMBER_FULL] = true;
315         serialNumber = Constants::SERIAL_NUMBER_NUM_START;
316     }
317     bool isSerialNumberFull = false;
318     OHOS::AccountSA::GetDataByType<bool>(accountListJson,
319         accountListJson.end(),
320         Constants::IS_SERIAL_NUMBER_FULL,
321         isSerialNumberFull,
322         OHOS::AccountSA::JsonType::BOOLEAN);
323     if (isSerialNumberFull) {
324         std::vector<OsAccountInfo> osAccountInfos;
325         if (GetOsAccountList(osAccountInfos) != ERR_OK) {
326             ACCOUNT_LOGE("GetSerialNumber get accountList error");
327             return ERR_OSACCOUNT_SERVICE_CONTROL_GET_OS_ACCOUNT_LIST_ERROR;
328         }
329         while (serialNumber < Constants::CARRY_NUM) {
330             bool exists = false;
331             for (auto it = osAccountInfos.begin(); it != osAccountInfos.end(); it++) {
332                 if (it->GetSerialNumber() ==
333                     Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN * Constants::CARRY_NUM + serialNumber) {
334                     exists = true;
335                     break;
336                 }
337             }
338             if (!exists) {
339                 break;
340             }
341             serialNumber++;
342             serialNumber = (serialNumber == Constants::CARRY_NUM) ? Constants::SERIAL_NUMBER_NUM_START : serialNumber;
343         }
344     }
345     accountListJson[Constants::SERIAL_NUMBER_NUM] = serialNumber + 1;
346     if (SaveAccountListToFileAndDataBase(accountListJson) != ERR_OK) {
347         return ERR_OSACCOUNT_SERVICE_CONTROL_INSERT_OS_ACCOUNT_LIST_ERROR;
348     }
349     serialNumber = serialNumber + Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN * Constants::CARRY_NUM;
350     return ERR_OK;
351 }
352 
GetAllowCreateId(int & id)353 ErrCode OsAccountControlFileManager::GetAllowCreateId(int &id)
354 {
355     Json accountListJson;
356     if (GetAccountListFromFile(accountListJson) != ERR_OK) {
357         ACCOUNT_LOGE("GetAllowCreateId get accountList error");
358         return ERR_OSACCOUNT_SERVICE_CONTROL_GET_OS_ACCOUNT_LIST_ERROR;
359     }
360     int countCreatedNum = 0;
361     auto jsonEnd = accountListJson.end();
362     OHOS::AccountSA::GetDataByType<int>(
363         accountListJson, jsonEnd, Constants::COUNT_ACCOUNT_NUM, countCreatedNum, OHOS::AccountSA::JsonType::NUMBER);
364     if (countCreatedNum >= Constants::MAX_USER_ID - Constants::START_USER_ID) {
365         ACCOUNT_LOGE("GetAllowCreateId cannot create more account error");
366         return ERR_OSACCOUNT_SERVICE_CONTROL_MAX_CAN_CREATE_ERROR;
367     }
368     std::vector<std::string> accountIdList;
369     OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
370         accountListJson, jsonEnd, Constants::ACCOUNT_LIST, accountIdList, OHOS::AccountSA::JsonType::ARRAY);
371     id = Constants::START_USER_ID + 1;
372     while (std::find(accountIdList.begin(), accountIdList.end(), std::to_string(id)) != accountIdList.end() &&
373            id != Constants::MAX_USER_ID + 1) {
374         id++;
375     }
376     if (id == Constants::MAX_USER_ID + 1) {
377         id = -1;
378         return ERR_OSACCOUNT_SERVICE_CONTROL_SELECT_CAN_USE_ID_ERROR;
379     }
380     return ERR_OK;
381 }
382 
GetAccountListFromFile(Json & accountListJson)383 ErrCode OsAccountControlFileManager::GetAccountListFromFile(Json &accountListJson)
384 {
385     ACCOUNT_LOGI("enter");
386     accountListJson.clear();
387     std::string accountList;
388     std::lock_guard<std::mutex> lock(accountListFileLock_);
389     ErrCode errCode = accountFileOperator_->GetFileContentByPath(Constants::ACCOUNT_LIST_FILE_JSON_PATH,
390         accountList);
391     if (errCode != ERR_OK) {
392         ACCOUNT_LOGE("GetFileContentByPath failed! error code %{public}d.", errCode);
393         return ERR_OSACCOUNT_SERVICE_CONTROL_GET_ACCOUNT_LIST_ERROR;
394     }
395     accountListJson = Json::parse(accountList, nullptr, false);
396     ACCOUNT_LOGI("end");
397     return ERR_OK;
398 }
399 
SaveAccountListToFile(const Json & accountListJson)400 ErrCode OsAccountControlFileManager::SaveAccountListToFile(const Json &accountListJson)
401 {
402     ACCOUNT_LOGI("enter!");
403     std::lock_guard<std::mutex> lock(accountListFileLock_);
404     if (accountFileOperator_->InputFileByPathAndContent(Constants::ACCOUNT_LIST_FILE_JSON_PATH,
405         accountListJson.dump()) != ERR_OK) {
406         ACCOUNT_LOGE("cannot save save account list file content!");
407         return ERR_OSACCOUNT_SERVICE_CONTROL_SET_ACCOUNT_LIST_ERROR;
408     }
409     ACCOUNT_LOGI("save account list file succeed!");
410     return ERR_OK;
411 }
412 
SaveAccountListToFileAndDataBase(const Json & accountListJson)413 ErrCode OsAccountControlFileManager::SaveAccountListToFileAndDataBase(const Json &accountListJson)
414 {
415     osAccountDataBaseOperator_->UpdateOsAccountIDListInDatabase(accountListJson);
416     return SaveAccountListToFile(accountListJson);
417 }
418 
IsOsAccountExists(const int id,bool & isExists)419 ErrCode OsAccountControlFileManager::IsOsAccountExists(const int id, bool &isExists)
420 {
421     ACCOUNT_LOGI("OsAccountControlFileManager IsOsAccountExists start");
422     isExists = false;
423     std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) +
424                        Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
425     // check exist
426     if (!accountFileOperator_->IsExistFile(path)) {
427         ACCOUNT_LOGI("IsOsAccountExists path %{public}s does not exist!", path.c_str());
428         return ERR_OK;
429     }
430 
431     // check format
432     if (!accountFileOperator_->IsJsonFormat(path)) {
433         ACCOUNT_LOGI("IsOsAccountExists path %{public}s wrong format!", path.c_str());
434         return ERR_OK;
435     }
436 
437     isExists = true;
438     ACCOUNT_LOGI("OsAccountControlFileManager IsOsAccountExists path is %{public}d", isExists);
439     return ERR_OK;
440 }
441 
GetPhotoById(const int id,std::string & photo)442 ErrCode OsAccountControlFileManager::GetPhotoById(const int id, std::string &photo)
443 {
444     std::string path =
445         Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) + Constants::PATH_SEPARATOR + photo;
446     std::string byteStr = "";
447     ErrCode errCode = accountFileOperator_->GetFileContentByPath(path, byteStr);
448     if (errCode != ERR_OK) {
449         ACCOUNT_LOGE("GetPhotoById cannot find photo file error");
450         return errCode;
451     }
452     if (photo == Constants::USER_PHOTO_FILE_JPG_NAME) {
453         photo =
454             Constants::USER_PHOTO_BASE_JPG_HEAD + osAccountPhotoOperator_->EnCode(byteStr.c_str(), byteStr.length());
455     } else {
456         photo =
457             Constants::USER_PHOTO_BASE_PNG_HEAD + osAccountPhotoOperator_->EnCode(byteStr.c_str(), byteStr.length());
458     }
459     std::string substr = "\r\n";
460     while (photo.find(substr) != std::string::npos) {
461         photo.erase(photo.find(substr), substr.length());
462     }
463     return ERR_OK;
464 }
465 
SetPhotoById(const int id,const std::string & photo)466 ErrCode OsAccountControlFileManager::SetPhotoById(const int id, const std::string &photo)
467 {
468     std::string path = "";
469     std::string type = "";
470     std::string subPhoto = "";
471     if (photo.find(Constants::USER_PHOTO_BASE_JPG_HEAD) != std::string::npos) {
472         type = "image/jpeg";
473         path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) + Constants::PATH_SEPARATOR +
474                Constants::USER_PHOTO_FILE_JPG_NAME;
475         subPhoto = photo.substr(Constants::USER_PHOTO_BASE_JPG_HEAD.size());
476     } else if (photo.find(Constants::USER_PHOTO_BASE_PNG_HEAD) != std::string::npos) {
477         type = "image/png";
478         path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) + Constants::PATH_SEPARATOR +
479                Constants::USER_PHOTO_FILE_PNG_NAME;
480         subPhoto = photo.substr(Constants::USER_PHOTO_BASE_PNG_HEAD.size());
481     } else {
482         ACCOUNT_LOGE("SetPhotoById photo str error");
483         return ERR_OSACCOUNT_SERVICE_CONTROL_PHOTO_STR_ERROR;
484     }
485     std::string bytePhoto = osAccountPhotoOperator_->DeCode(subPhoto);
486     ErrCode errCode = accountFileOperator_->InputFileByPathAndContent(path, bytePhoto);
487     if (errCode != ERR_OK) {
488         return errCode;
489     }
490     return ERR_OK;
491 }
492 
GetIsMultiOsAccountEnable(bool & isMultiOsAccountEnable)493 ErrCode OsAccountControlFileManager::GetIsMultiOsAccountEnable(bool &isMultiOsAccountEnable)
494 {
495     return osAccountFileOperator_->GetIsMultiOsAccountEnable(isMultiOsAccountEnable);
496 }
IsConstrarionsInTypeList(const std::vector<std::string> & constrains,bool & isExists)497 ErrCode OsAccountControlFileManager::IsConstrarionsInTypeList(
498     const std::vector<std::string> &constrains, bool &isExists)
499 {
500     return osAccountFileOperator_->IsConstrarionsInTypeList(constrains, isExists);
501 }
502 
IsAllowedCreateAdmin(bool & isAllowedCreateAdmin)503 ErrCode OsAccountControlFileManager::IsAllowedCreateAdmin(bool &isAllowedCreateAdmin)
504 {
505     return osAccountFileOperator_->IsAllowedCreateAdmin(isAllowedCreateAdmin);
506 }
507 
GetCreatedOsAccountNumFromDatabase(const std::string & storeID,int & createdOsAccountNum)508 ErrCode OsAccountControlFileManager::GetCreatedOsAccountNumFromDatabase(const std::string& storeID,
509     int &createdOsAccountNum)
510 {
511     return osAccountDataBaseOperator_->GetCreatedOsAccountNumFromDatabase(storeID, createdOsAccountNum);
512 }
513 
GetSerialNumberFromDatabase(const std::string & storeID,int64_t & serialNumber)514 ErrCode OsAccountControlFileManager::GetSerialNumberFromDatabase(const std::string& storeID,
515     int64_t &serialNumber)
516 {
517     return osAccountDataBaseOperator_->GetSerialNumberFromDatabase(storeID, serialNumber);
518 }
519 
GetMaxAllowCreateIdFromDatabase(const std::string & storeID,int & id)520 ErrCode OsAccountControlFileManager::GetMaxAllowCreateIdFromDatabase(const std::string& storeID,
521     int &id)
522 {
523     return osAccountDataBaseOperator_->GetMaxAllowCreateIdFromDatabase(storeID, id);
524 }
525 
GetOsAccountFromDatabase(const std::string & storeID,const int id,OsAccountInfo & osAccountInfo)526 ErrCode OsAccountControlFileManager::GetOsAccountFromDatabase(const std::string& storeID,
527     const int id, OsAccountInfo &osAccountInfo)
528 {
529     return osAccountDataBaseOperator_->GetOsAccountFromDatabase(storeID, id, osAccountInfo);
530 }
531 
GetOsAccountListFromDatabase(const std::string & storeID,std::vector<OsAccountInfo> & osAccountList)532 ErrCode OsAccountControlFileManager::GetOsAccountListFromDatabase(const std::string& storeID,
533     std::vector<OsAccountInfo> &osAccountList)
534 {
535     return osAccountDataBaseOperator_->GetOsAccountListFromDatabase(storeID, osAccountList);
536 }
537 }  // namespace AccountSA
538 }  // namespace OHOS