• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OSACCOUNT_OS_ACCOUNT_CONTROL_FILE_MANAGER_H
17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OSACCOUNT_OS_ACCOUNT_CONTROL_FILE_MANAGER_H
18 
19 #include <memory>
20 #include <mutex>
21 #include "ios_account_control.h"
22 #ifdef HAS_KV_STORE_PART
23 #include "os_account_database_operator.h"
24 #endif
25 #include "os_account_file_operator.h"
26 #include "os_account_photo_operator.h"
27 namespace OHOS {
28 namespace AccountSA {
29 
30 bool GetValidAccountID(const std::string& dirName, std::int32_t& accountID);
31 
32 class OsAccountControlFileManager : public IOsAccountControl {
33 public:
34     OsAccountControlFileManager();
35     virtual ~OsAccountControlFileManager();
36     void Init() override;
37     ErrCode GetOsAccountList(std::vector<OsAccountInfo> &osAccountList) override;
38     ErrCode GetOsAccountInfoById(const int id, OsAccountInfo &osAccountInfo) override;
39     ErrCode GetConstraintsByType(const OsAccountType type, std::vector<std::string> &constraints) override;
40     ErrCode InsertOsAccount(OsAccountInfo &osAccountInfo) override;
41     ErrCode DelOsAccount(const int id) override;
42     ErrCode UpdateOsAccount(OsAccountInfo &osAccountInfo) override;
43     ErrCode GetMaxCreatedOsAccountNum(int &maxCreatedOsAccountNum) override;
44     ErrCode GetSerialNumber(int64_t &serialNumber) override;
45     ErrCode GetAllowCreateId(int &id) override;
46     ErrCode IsOsAccountExists(const int id, bool &isExists) override;
47     ErrCode GetPhotoById(const int id, std::string &photo) override;
48     ErrCode SetPhotoById(const int id, const std::string &photo) override;
49     ErrCode GetIsMultiOsAccountEnable(bool &isMultiOsAccountEnable) override;
50     ErrCode CheckConstraintsList(const std::vector<std::string> &constraints,
51         bool &isExists, bool &isOverSize) override;
52     ErrCode IsAllowedCreateAdmin(bool &isAllowedCreateAdmin) override;
53 
54     ErrCode GetCreatedOsAccountNumFromDatabase(const std::string& storeID,
55         int &createdOsAccountNum) override;
56     ErrCode GetSerialNumberFromDatabase(const std::string& storeID,
57         int64_t &serialNumber) override;
58     ErrCode GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id) override;
59     ErrCode GetOsAccountFromDatabase(const std::string& storeID,
60         const int id, OsAccountInfo& osAccountInfo) override;
61     ErrCode GetOsAccountListFromDatabase(const std::string& storeID,
62         std::vector<OsAccountInfo>& osAccountList) override;
63 
64     ErrCode RemoveOAConstraintsInfo(const int32_t id) override;
65     ErrCode IsFromBaseOAConstraintsList(const int32_t id, const std::string constraint, bool &isExits) override;
66     ErrCode IsFromGlobalOAConstraintsList(const int32_t id, const int32_t deviceOwnerId,
67         const std::string constraint, std::vector<ConstraintSourceTypeInfo> &globalSourceList) override;
68     ErrCode IsFromSpecificOAConstraintsList(const int32_t id, const int32_t deviceOwnerId,
69         const std::string constraint, std::vector<ConstraintSourceTypeInfo> &specificSourceList) override;
70     ErrCode GetGlobalOAConstraintsList(std::vector<std::string> &constraintsList) override;
71     ErrCode GetSpecificOAConstraintsList(const int32_t id, std::vector<std::string> &constraintsList) override;
72     ErrCode UpdateBaseOAConstraints(const std::string& idStr,
73         const std::vector<std::string>& ConstraintStr, bool isAdd) override;
74     ErrCode UpdateGlobalOAConstraints(const std::string& idStr,
75         const std::vector<std::string>& ConstraintStr, bool isAdd) override;
76     ErrCode UpdateSpecificOAConstraints(const std::string& idStr,
77         const std::string& targetIdStr, const std::vector<std::string>& ConstraintStr, bool isAdd) override;
78     ErrCode GetDeviceOwnerId(int32_t &deviceOwnerId) override;
79     ErrCode UpdateDeviceOwnerId(const int32_t deviceOwnerId) override;
80 
81     ErrCode SetDefaultActivatedOsAccount(const int32_t id) override;
82     ErrCode GetDefaultActivatedOsAccount(int32_t &id) override;
83 
84 private:
85     ErrCode UpdateAccountList(const std::string& idStr, bool isAdd);
86     ErrCode GetAccountListFromFile(Json& accountListJson);
87     ErrCode SaveAccountListToFile(const Json& accountListJson);
88     ErrCode SaveAccountListToFileAndDataBase(const Json& accountListJson);
89     void BuildAndSaveAccountListJsonFile(const std::vector<std::string>& accounts);
90     void RecoverAccountListJsonFile();
91     void BuildAndSaveBaseOAConstraintsJsonFile();
92     void BuildAndSaveGlobalOAConstraintsJsonFile();
93     void BuildAndSaveSpecificOAConstraintsJsonFile();
94     void GlobalConstraintsDataOperate(const std::string& idStr,
95         const std::vector<std::string>& ConstraintStr, bool isAdd, Json &globalOAConstraintsJson);
96     void SpecificConstraintsDataOperate(const std::string& idStr, const std::string& targetIdStr,
97         const std::vector<std::string>& ConstraintStr, bool isAdd, Json& userPrivateConstraintsDataJson);
98 
99     ErrCode GetBaseOAConstraintsFromFile(Json &baseOAConstraintsJson);
100     ErrCode GetGlobalOAConstraintsFromFile(Json &globalOAConstraintsJson);
101     ErrCode GetSpecificOAConstraintsFromFile(Json &specificOAConstraintsJson);
102     ErrCode SaveBaseOAConstraintsToFile(const Json &baseOAConstraints);
103     ErrCode SaveGlobalOAConstraintsToFile(const Json &globalOAConstraints);
104     ErrCode SaveSpecificOAConstraintsToFile(const Json &specificOAConstraints);
105 
106     ErrCode RemoveOABaseConstraintsInfo(const int32_t id);
107     ErrCode RemoveOAGlobalConstraintsInfo(const int32_t id);
108     ErrCode RemoveOASpecificConstraintsInfo(const int32_t id);
109 
110 private:
111     std::shared_ptr<AccountFileOperator> accountFileOperator_;
112 #ifdef HAS_KV_STORE_PART
113     std::shared_ptr<OsAccountDatabaseOperator> osAccountDataBaseOperator_;
114 #endif
115     std::shared_ptr<OsAccountFileOperator> osAccountFileOperator_;
116     std::shared_ptr<OsAccountPhotoOperator> osAccountPhotoOperator_;
117     std::mutex accountListFileLock_;
118     std::mutex baseOAConstraintsFileLock_;
119     std::mutex globalOAConstraintsFileLock_;
120     std::mutex specificOAConstraintsFileLock_;
121 };
122 }  // namespace AccountSA
123 }  // namespace OHOS
124 
125 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OSACCOUNT_OS_ACCOUNT_CONTROL_FILE_MANAGER_H
126