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
16 #include "os_account_file_operator.h"
17
18 #include "account_log_wrapper.h"
19 #include "os_account_constants.h"
20
21 namespace OHOS {
22 namespace AccountSA {
23 namespace {
24 const char KEY_TYPE_TO_CONSTRAINTS[] = "Type2Constraints";
25 const char KEY_CONSTRAINTS_LIST[] = "constraints";
26 const char IS_ALLOWED_CREATE_ADMIN[] = "IsAllowedCreateAdmin";
27 }
28
OsAccountFileOperator()29 OsAccountFileOperator::OsAccountFileOperator()
30 {
31 accountFileOperator_ = std::make_shared<AccountFileOperator>();
32 }
~OsAccountFileOperator()33 OsAccountFileOperator::~OsAccountFileOperator()
34 {}
35
GetConstraintsByType(const int type,std::vector<std::string> & constraints)36 ErrCode OsAccountFileOperator::GetConstraintsByType(const int type, std::vector<std::string> &constraints)
37 {
38 ACCOUNT_LOGD("Start");
39 constraints.clear();
40 std::string str;
41 ErrCode errCode = accountFileOperator_->GetFileContentByPath(Constants::OS_ACCOUNT_CONSTRAINT_CONFIG_PATH, str);
42 if (errCode != ERR_OK) {
43 return errCode;
44 }
45 auto configJson = CreateJsonFromString(str);
46 CJson *typeJson = nullptr;
47 bool ret = GetDataByType<CJson *>(configJson, KEY_TYPE_TO_CONSTRAINTS, typeJson);
48 if (!ret) {
49 ACCOUNT_LOGE("Failed to parse %{public}s", KEY_TYPE_TO_CONSTRAINTS);
50 return ERR_ACCOUNT_COMMON_BAD_JSON_FORMAT_ERROR;
51 }
52 GetDataByType<std::vector<std::string>>(typeJson, std::to_string(type), constraints);
53 ACCOUNT_LOGD("End");
54 return ERR_OK;
55 }
56
GetBaseOAConstraintsList(const int id,std::vector<std::string> & constraints)57 ErrCode OsAccountFileOperator::GetBaseOAConstraintsList(const int id, std::vector<std::string> &constraints)
58 {
59 auto baseOsAccountConstraintsConfig = CreateJson();
60 if (accountFileOperator_->IsExistFile(Constants::BASE_OSACCOUNT_CONSTRAINTS_JSON_PATH)) {
61 std::string baseUserConstraintsConfigStr;
62 accountFileOperator_->GetFileContentByPath(
63 Constants::BASE_OSACCOUNT_CONSTRAINTS_JSON_PATH, baseUserConstraintsConfigStr);
64 baseOsAccountConstraintsConfig = CreateJsonFromString(baseUserConstraintsConfigStr);
65 if (baseOsAccountConstraintsConfig == nullptr) {
66 return ERR_OSACCOUNT_SERVICE_GET_DATA_FROM_BASE_CONSTRAINTS_FILE_EMPTY;
67 }
68 }
69 if (GetItemNum(baseOsAccountConstraintsConfig) == 0) {
70 ACCOUNT_LOGE("BaseOsAccountConstraints data is empty");
71 return ERR_OSACCOUNT_SERVICE_GET_DATA_FROM_BASE_CONSTRAINTS_FILE_EMPTY;
72 }
73 GetDataByType<std::vector<std::string>>(baseOsAccountConstraintsConfig, std::to_string(id), constraints);
74 return ERR_OK;
75 }
76
GetGlobalOAConstraintsList(std::vector<std::string> & constraints)77 ErrCode OsAccountFileOperator::GetGlobalOAConstraintsList(std::vector<std::string> &constraints)
78 {
79 auto globalOsAccountConstraintsConfig = CreateJson();
80 if (accountFileOperator_->IsExistFile(Constants::GLOBAL_OSACCOUNT_CONSTRAINTS_JSON_PATH)) {
81 std::string globalOsAccountConstraintsConfigStr;
82 accountFileOperator_->GetFileContentByPath(
83 Constants::GLOBAL_OSACCOUNT_CONSTRAINTS_JSON_PATH, globalOsAccountConstraintsConfigStr);
84 globalOsAccountConstraintsConfig = CreateJsonFromString(globalOsAccountConstraintsConfigStr);
85 if (globalOsAccountConstraintsConfig == nullptr) {
86 return ERR_OSACCOUNT_SERVICE_GET_DATA_FROM_GLOBAL_CONSTRAINTS_FILE_EMPTY;
87 }
88 }
89
90 if (GetItemNum(globalOsAccountConstraintsConfig) == 0) {
91 ACCOUNT_LOGE("GlobalOsAccountConstraints data is empty");
92 return ERR_OSACCOUNT_SERVICE_GET_DATA_FROM_GLOBAL_CONSTRAINTS_FILE_EMPTY;
93 }
94 GetDataByType<std::vector<std::string>>(globalOsAccountConstraintsConfig, Constants::ALL_GLOBAL_CONSTRAINTS,
95 constraints);
96 return ERR_OK;
97 }
98
GetSpecificOAConstraintsList(const int id,std::vector<std::string> & constraints)99 ErrCode OsAccountFileOperator::GetSpecificOAConstraintsList(const int id, std::vector<std::string> &constraints)
100 {
101 auto specificOsAccountConstraintsConfig = CreateJson();
102 if (accountFileOperator_->IsExistFile(Constants::SPECIFIC_OSACCOUNT_CONSTRAINTS_JSON_PATH)) {
103 std::string specificOsAccountConstraintsConfigStr;
104 accountFileOperator_->GetFileContentByPath(
105 Constants::SPECIFIC_OSACCOUNT_CONSTRAINTS_JSON_PATH, specificOsAccountConstraintsConfigStr);
106 specificOsAccountConstraintsConfig = CreateJsonFromString(specificOsAccountConstraintsConfigStr);
107 if (specificOsAccountConstraintsConfig == nullptr) {
108 return ERR_OSACCOUNT_SERVICE_GET_DATA_FROM_SPECIFIC_CONSTRAINTS_FILE_EMPTY;
109 }
110 }
111
112 if (GetItemNum(specificOsAccountConstraintsConfig) == 0) {
113 ACCOUNT_LOGE("GlobalOsAccountConstraints data is empty");
114 return ERR_OSACCOUNT_SERVICE_GET_DATA_FROM_SPECIFIC_CONSTRAINTS_FILE_EMPTY;
115 }
116 CJson *SpecificOAConstraintsData = nullptr;
117 GetDataByType<CJson *>(specificOsAccountConstraintsConfig, std::to_string(id), SpecificOAConstraintsData);
118 GetDataByType<std::vector<std::string>>(SpecificOAConstraintsData, Constants::ALL_SPECIFIC_CONSTRAINTS,
119 constraints);
120 return ERR_OK;
121 }
122
GetIsMultiOsAccountEnable(bool & isMultiOsAccountEnable)123 ErrCode OsAccountFileOperator::GetIsMultiOsAccountEnable(bool &isMultiOsAccountEnable)
124 {
125 isMultiOsAccountEnable = true;
126 std::string str;
127 ErrCode errCode = accountFileOperator_->GetFileContentByPath(Constants::OS_ACCOUNT_CONSTRAINT_CONFIG_PATH, str);
128 if (errCode != ERR_OK) {
129 ACCOUNT_LOGE("Failed to get file content, errCode: %{public}d", errCode);
130 return errCode;
131 }
132 auto configJson = CreateJsonFromString(str);
133 bool ret = GetDataByType<bool>(configJson, Constants::IS_MULTI_OS_ACCOUNT_ENABLE, isMultiOsAccountEnable);
134 if (!ret) {
135 ACCOUNT_LOGE("Failed to parse IsMultiOsAccountEnabled");
136 }
137 return ERR_OK;
138 }
139
IsAllowedCreateAdmin(bool & isAllowedCreateAdmin)140 ErrCode OsAccountFileOperator::IsAllowedCreateAdmin(bool &isAllowedCreateAdmin)
141 {
142 isAllowedCreateAdmin = false;
143 std::string str;
144 ErrCode errCode = accountFileOperator_->GetFileContentByPath(Constants::OS_ACCOUNT_CONSTRAINT_CONFIG_PATH, str);
145 if (errCode != ERR_OK) {
146 ACCOUNT_LOGE("Failed to get file content, errCode: %{public}d", errCode);
147 return errCode;
148 }
149 auto configJson = CreateJsonFromString(str);
150 bool ret = GetDataByType<bool>(configJson, IS_ALLOWED_CREATE_ADMIN, isAllowedCreateAdmin);
151 if (!ret) {
152 ACCOUNT_LOGE("Failed to parse IsAllowedCreateAdmin");
153 }
154 return ERR_OK;
155 }
156
CheckConstraints(const std::vector<std::string> & constraints)157 bool OsAccountFileOperator::CheckConstraints(const std::vector<std::string> &constraints)
158 {
159 std::string str;
160 ErrCode errCode =
161 accountFileOperator_->GetFileContentByPath(Constants::OS_ACCOUNT_CONSTRAINT_DEFINITION_PATH, str);
162 if (errCode != ERR_OK) {
163 ACCOUNT_LOGE("Failed to get the list of constraints, errCode: %{public}d", errCode);
164 return false;
165 }
166 auto constraintSetJson = CreateJsonFromString(str);
167 std::set<std::string> constraintSet;
168 bool ret = GetDataByType<std::set<std::string>>(constraintSetJson, KEY_CONSTRAINTS_LIST, constraintSet);
169 if (!ret) {
170 ACCOUNT_LOGE("Failed to parse constraint definition");
171 return false;
172 }
173 for (auto it = constraints.begin(); it != constraints.end(); it++) {
174 if (std::find(constraintSet.begin(), constraintSet.end(), *it) == constraintSet.end()) {
175 ACCOUNT_LOGE("Invalid constraint: %{public}s", (*it).c_str());
176 return false;
177 }
178 }
179 return true;
180 }
181 } // namespace AccountSA
182 } // namespace OHOS