1 /*
2 * Copyright (c) 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 "admin_policies_storage_rdb.h"
17
18 #include "edm_constants.h"
19 #include "edm_log.h"
20 #include "edm_rdb_filed_const.h"
21
22 namespace OHOS {
23 namespace EDM {
24 std::shared_ptr<AdminPoliciesStorageRdb> AdminPoliciesStorageRdb::instance_ = nullptr;
25 std::mutex AdminPoliciesStorageRdb::mutexLock_;
26
AdminPoliciesStorageRdb()27 AdminPoliciesStorageRdb::AdminPoliciesStorageRdb()
28 {
29 EDMLOGD("AdminPoliciesStorageRdb::create database.");
30 std::string createTableSql = "CREATE TABLE IF NOT EXISTS ";
31 createTableSql.append(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME + " (")
32 .append(EdmRdbFiledConst::FILED_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,")
33 .append(EdmRdbFiledConst::FILED_USER_ID + " INTEGER NOT NULL,")
34 .append(EdmRdbFiledConst::FILED_ADMIN_TYPE + " INTEGER NOT NULL,")
35 .append(EdmRdbFiledConst::FILED_PACKAGE_NAME + " TEXT NOT NULL,")
36 .append(EdmRdbFiledConst::FILED_CLASS_NAME + " TEXT NOT NULL,")
37 .append(EdmRdbFiledConst::FILED_ENT_NAME + " TEXT,")
38 .append(EdmRdbFiledConst::FILED_ENT_DESC + " TEXT,")
39 .append(EdmRdbFiledConst::FILED_PERMISSIONS + " TEXT,")
40 .append(EdmRdbFiledConst::FILED_SUBSCRIBE_EVENTS + " TEXT,")
41 .append(EdmRdbFiledConst::FILED_PARENT_ADMIN + " TEXT);");
42 auto edmRdbDataManager = EdmRdbDataManager::GetInstance();
43 if (edmRdbDataManager != nullptr) {
44 edmRdbDataManager->CreateTable(createTableSql);
45 } else {
46 EDMLOGE("AdminPoliciesStorageRdb::create database admin_policies failed.");
47 }
48 }
49
GetInstance()50 std::shared_ptr<AdminPoliciesStorageRdb> AdminPoliciesStorageRdb::GetInstance()
51 {
52 if (instance_ == nullptr) {
53 std::lock_guard<std::mutex> lock(mutexLock_);
54 if (instance_ == nullptr) {
55 instance_ = std::make_shared<AdminPoliciesStorageRdb>();
56 }
57 }
58 return instance_;
59 }
60
InsertAdmin(int32_t userId,const AppExecFwk::ExtensionAbilityInfo & abilityInfo,const EntInfo & entInfo,AdminType role,const std::vector<std::string> & permissions)61 bool AdminPoliciesStorageRdb::InsertAdmin(int32_t userId, const AppExecFwk::ExtensionAbilityInfo &abilityInfo,
62 const EntInfo &entInfo, AdminType role, const std::vector<std::string> &permissions)
63 {
64 EDMLOGD("AdminPoliciesStorageRdb::Insert data start.");
65 auto edmRdbDataManager = EdmRdbDataManager::GetInstance();
66 if (edmRdbDataManager == nullptr) {
67 EDMLOGE("AdminPoliciesStorageRdb::InsertAdmin get edmRdbDataManager failed.");
68 return false;
69 }
70 // insert into admin_policies(user_id, admin_type, package_name, class_name, ent_name, ent_desc, permissions)
71 // values(?, ?, ?, ?, ?, ?, ?)
72 return edmRdbDataManager->Insert(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME,
73 CreateValuesBucket(userId, abilityInfo, entInfo, role, permissions));
74 }
75
UpdateAdmin(int32_t userId,const AppExecFwk::ExtensionAbilityInfo & abilityInfo,const EntInfo & entInfo,AdminType role,const std::vector<std::string> & permissions)76 bool AdminPoliciesStorageRdb::UpdateAdmin(int32_t userId, const AppExecFwk::ExtensionAbilityInfo &abilityInfo,
77 const EntInfo &entInfo, AdminType role, const std::vector<std::string> &permissions)
78 {
79 EDMLOGD("AdminPoliciesStorageRdb::Insert data start.");
80 auto edmRdbDataManager = EdmRdbDataManager::GetInstance();
81 if (edmRdbDataManager == nullptr) {
82 EDMLOGE("AdminPoliciesStorageRdb::UpdateAdmin get edmRdbDataManager failed.");
83 return false;
84 }
85 // update admin_policies set user_id=?, admin_type=?, package_name=?, class_name=?, ent_name=?, ent_desc=?,
86 // permissions=? where user_id=? and package_name=?
87 NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME);
88 predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId));
89 predicates.EqualTo(EdmRdbFiledConst::FILED_PACKAGE_NAME, abilityInfo.bundleName);
90 return edmRdbDataManager->Update(CreateValuesBucket(userId, abilityInfo, entInfo, role, permissions), predicates);
91 }
92
CreateValuesBucket(int32_t userId,const AppExecFwk::ExtensionAbilityInfo & abilityInfo,const EntInfo & entInfo,AdminType role,const std::vector<std::string> & permissions)93 NativeRdb::ValuesBucket AdminPoliciesStorageRdb::CreateValuesBucket(int32_t userId,
94 const AppExecFwk::ExtensionAbilityInfo &abilityInfo, const EntInfo &entInfo, AdminType role,
95 const std::vector<std::string> &permissions)
96 {
97 NativeRdb::ValuesBucket valuesBucket;
98 valuesBucket.PutInt(EdmRdbFiledConst::FILED_USER_ID, userId);
99 valuesBucket.PutInt(EdmRdbFiledConst::FILED_ADMIN_TYPE, static_cast<int>(role));
100 valuesBucket.PutString(EdmRdbFiledConst::FILED_PACKAGE_NAME, abilityInfo.bundleName);
101 valuesBucket.PutString(EdmRdbFiledConst::FILED_CLASS_NAME, abilityInfo.name);
102 valuesBucket.PutString(EdmRdbFiledConst::FILED_ENT_NAME, entInfo.enterpriseName);
103 valuesBucket.PutString(EdmRdbFiledConst::FILED_ENT_DESC, entInfo.description);
104
105 if (!permissions.empty()) {
106 Json::StreamWriterBuilder builder;
107 builder.settings_["indentation"] = "";
108 Json::Value permissionJson;
109 for (const auto &it : permissions) {
110 permissionJson.append(it);
111 }
112 valuesBucket.PutString(EdmRdbFiledConst::FILED_PERMISSIONS, Json::writeString(builder, permissionJson));
113 }
114 return valuesBucket;
115 }
116
DeleteAdmin(int32_t userId,const std::string & packageName)117 bool AdminPoliciesStorageRdb::DeleteAdmin(int32_t userId, const std::string &packageName)
118 {
119 EDMLOGD("AdminPoliciesStorageRdb::DeleteAdmin.");
120 auto edmRdbDataManager = EdmRdbDataManager::GetInstance();
121 if (edmRdbDataManager == nullptr) {
122 EDMLOGE("AdminPoliciesStorageRdb::DeleteAdmin get edmRdbDataManager failed.");
123 return false;
124 }
125 // delete from admin_policies where user_id=? and package_name=?
126 NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME);
127 predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId));
128 predicates.EqualTo(EdmRdbFiledConst::FILED_PACKAGE_NAME, packageName);
129 return edmRdbDataManager->Delete(predicates);
130 }
131
UpdateAdmin(int32_t userId,const std::string & packageName,const std::string & className,const std::vector<std::string> & permissions)132 bool AdminPoliciesStorageRdb::UpdateAdmin(int32_t userId, const std::string &packageName, const std::string &className,
133 const std::vector<std::string> &permissions)
134 {
135 EDMLOGD("AdminPoliciesStorageRdb::UpdateAdmin.");
136 auto edmRdbDataManager = EdmRdbDataManager::GetInstance();
137 if (edmRdbDataManager == nullptr) {
138 EDMLOGE("AdminPoliciesStorageRdb::UpdateAdmin get edmRdbDataManager failed.");
139 return false;
140 }
141 // update admin_policies set package_name=?, class_name=?, permissions=? where user_id=? and package_name=?
142 NativeRdb::ValuesBucket valuesBucket;
143 valuesBucket.PutString(EdmRdbFiledConst::FILED_PACKAGE_NAME, packageName);
144 valuesBucket.PutString(EdmRdbFiledConst::FILED_CLASS_NAME, className);
145
146 Json::StreamWriterBuilder builder;
147 builder.settings_["indentation"] = "";
148 Json::Value permissionJson;
149 for (const auto &it : permissions) {
150 permissionJson.append(it);
151 }
152 valuesBucket.PutString(EdmRdbFiledConst::FILED_PERMISSIONS, Json::writeString(builder, permissionJson));
153
154 NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME);
155 predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId));
156 predicates.EqualTo(EdmRdbFiledConst::FILED_PACKAGE_NAME, packageName);
157 return edmRdbDataManager->Update(valuesBucket, predicates);
158 }
159
UpdateEntInfo(int32_t userId,const std::string & packageName,const EntInfo & entInfo)160 bool AdminPoliciesStorageRdb::UpdateEntInfo(int32_t userId, const std::string &packageName, const EntInfo &entInfo)
161 {
162 EDMLOGD("AdminPoliciesStorageRdb::UpdateEntInfo.");
163 auto edmRdbDataManager = EdmRdbDataManager::GetInstance();
164 if (edmRdbDataManager == nullptr) {
165 EDMLOGE("AdminPoliciesStorageRdb::UpdateEntInfo get edmRdbDataManager failed.");
166 return false;
167 }
168 // update admin_policies set ent_name=?, ent_desc=? where user_id=? and package_name=?
169 NativeRdb::ValuesBucket valuesBucket;
170 valuesBucket.PutString(EdmRdbFiledConst::FILED_ENT_NAME, entInfo.enterpriseName);
171 valuesBucket.PutString(EdmRdbFiledConst::FILED_ENT_DESC, entInfo.description);
172
173 NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME);
174 predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId));
175 predicates.EqualTo(EdmRdbFiledConst::FILED_PACKAGE_NAME, packageName);
176 return edmRdbDataManager->Update(valuesBucket, predicates);
177 }
178
UpdateManagedEvents(int32_t userId,const std::string & packageName,const std::vector<ManagedEvent> & managedEvents)179 bool AdminPoliciesStorageRdb::UpdateManagedEvents(int32_t userId, const std::string &packageName,
180 const std::vector<ManagedEvent> &managedEvents)
181 {
182 EDMLOGD("AdminPoliciesStorageRdb::UpdateManagedEvents.");
183 auto edmRdbDataManager = EdmRdbDataManager::GetInstance();
184 if (edmRdbDataManager == nullptr) {
185 EDMLOGE("AdminPoliciesStorageRdb::UpdateManagedEvents get edmRdbDataManager failed.");
186 return false;
187 }
188 // update admin_policies set subscribe_events=? where user_id=? and package_name=?
189 NativeRdb::ValuesBucket valuesBucket;
190 Json::StreamWriterBuilder builder;
191 builder.settings_["indentation"] = "";
192 Json::Value managedEventsJson;
193 for (const auto &it : managedEvents) {
194 managedEventsJson.append(static_cast<uint32_t>(it));
195 }
196 valuesBucket.PutString(EdmRdbFiledConst::FILED_SUBSCRIBE_EVENTS, Json::writeString(builder, managedEventsJson));
197
198 NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME);
199 predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId));
200 predicates.EqualTo(EdmRdbFiledConst::FILED_PACKAGE_NAME, packageName);
201 return edmRdbDataManager->Update(valuesBucket, predicates);
202 }
203
QueryAllAdmin()204 std::unordered_map<int32_t, std::vector<std::shared_ptr<Admin>>> AdminPoliciesStorageRdb::QueryAllAdmin()
205 {
206 EDMLOGD("AdminPoliciesStorageRdb::QueryAllAdmin.");
207 auto edmRdbDataManager = EdmRdbDataManager::GetInstance();
208 std::unordered_map<int32_t, std::vector<std::shared_ptr<Admin>>> admins;
209 if (edmRdbDataManager == nullptr) {
210 EDMLOGE("AdminPoliciesStorageRdb::QueryAllAdmin get edmRdbDataManager failed.");
211 return admins;
212 }
213 // select * from admin_policies
214 NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME);
215 auto resultSet = edmRdbDataManager->Query(predicates, std::vector<std::string>());
216 if (resultSet == nullptr) {
217 return admins;
218 }
219 int resultSetNum = resultSet->GoToFirstRow();
220 while (resultSetNum == NativeRdb::E_OK) {
221 std::shared_ptr<Admin> item = std::make_shared<Admin>();
222 int32_t userId = 0;
223 resultSet->GetInt(EdmRdbFiledConst::FILED_COLUMN_INDEX_ONE, userId);
224 SetAdminItems(resultSet, item);
225 if (admins.find(userId) != admins.end()) {
226 admins[userId].push_back(item);
227 } else {
228 std::vector<std::shared_ptr<Admin>> adminItems{item};
229 admins[userId] = adminItems;
230 }
231 resultSetNum = resultSet->GoToNextRow();
232 }
233 resultSet->Close();
234 return admins;
235 }
236
SetAdminItems(std::shared_ptr<NativeRdb::ResultSet> resultSet,std::shared_ptr<Admin> item)237 void AdminPoliciesStorageRdb::SetAdminItems(std::shared_ptr<NativeRdb::ResultSet> resultSet,
238 std::shared_ptr<Admin> item)
239 {
240 int32_t adminType = 0;
241 resultSet->GetInt(EdmRdbFiledConst::FILED_COLUMN_INDEX_TWO, adminType);
242 item->adminInfo_.adminType_ = static_cast<AdminType>(adminType);
243 resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_THREE, item->adminInfo_.packageName_);
244 resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_FOUR, item->adminInfo_.className_);
245 resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_FIVE, item->adminInfo_.entInfo_.enterpriseName);
246 resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_SIX, item->adminInfo_.entInfo_.description);
247 std::string permissionStr;
248 resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_SEVEN, permissionStr);
249 if (!permissionStr.empty() && permissionStr != "null") {
250 Json::Value permissionJson;
251 ConvertStrToJson(permissionStr, permissionJson);
252 for (uint32_t i = 0; i < permissionJson.size(); i++) {
253 if (permissionJson[i].isString()) {
254 item->adminInfo_.permission_.push_back(permissionJson[i].asString());
255 }
256 }
257 }
258 std::string managedEventsStr;
259 resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_EIGHT, managedEventsStr);
260 if (!managedEventsStr.empty() && managedEventsStr != "null") {
261 Json::Value managedEventsJson;
262 ConvertStrToJson(managedEventsStr, managedEventsJson);
263 for (uint32_t i = 0; i < managedEventsJson.size(); i++) {
264 if (managedEventsJson[i].isUInt()) {
265 item->adminInfo_.managedEvents_.push_back(static_cast<ManagedEvent>(managedEventsJson[i].asUInt()));
266 }
267 }
268 }
269 resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_NINE, item->adminInfo_.parentAdminName_);
270 }
271
ConvertStrToJson(const std::string & str,Json::Value & json)272 void AdminPoliciesStorageRdb::ConvertStrToJson(const std::string &str, Json::Value &json)
273 {
274 Json::String err;
275 Json::CharReaderBuilder builder;
276 std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
277 reader->parse(str.c_str(), str.c_str() + str.length(), &json, &err);
278 }
279
InsertAuthorizedAdmin(const std::string & bundleName,const std::vector<std::string> & permissions,const std::string & parentName)280 bool AdminPoliciesStorageRdb::InsertAuthorizedAdmin(const std::string &bundleName,
281 const std::vector<std::string> &permissions, const std::string &parentName)
282 {
283 EDMLOGD("AdminPoliciesStorageRdb::InsertAuthorizedAdmin.");
284 // insert into admin_policies(user_id, admin_type, package_name, class_name, permissions, parent_admin)
285 // values(?, ?, ?, ?, ?, ?)
286 NativeRdb::ValuesBucket valuesBucket;
287 valuesBucket.PutInt(EdmRdbFiledConst::FILED_USER_ID, EdmConstants::DEFAULT_USER_ID);
288 valuesBucket.PutInt(EdmRdbFiledConst::FILED_ADMIN_TYPE, static_cast<int32_t>(AdminType::SUB_SUPER_ADMIN));
289 valuesBucket.PutString(EdmRdbFiledConst::FILED_PACKAGE_NAME, bundleName);
290 valuesBucket.PutString(EdmRdbFiledConst::FILED_CLASS_NAME, "");
291 Json::StreamWriterBuilder builder;
292 builder.settings_["indentation"] = "";
293 Json::Value permissionJson;
294 for (const auto &permission : permissions) {
295 permissionJson.append(permission);
296 }
297 valuesBucket.PutString(EdmRdbFiledConst::FILED_PERMISSIONS, Json::writeString(builder, permissionJson));
298 valuesBucket.PutString(EdmRdbFiledConst::FILED_PARENT_ADMIN, parentName);
299 return EdmRdbDataManager::GetInstance()->Insert(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME, valuesBucket);
300 }
301
UpdateAuthorizedAdmin(const std::string & bundleName,const std::vector<std::string> & permissions,const std::string & parentName)302 bool AdminPoliciesStorageRdb::UpdateAuthorizedAdmin(const std::string &bundleName,
303 const std::vector<std::string> &permissions, const std::string &parentName)
304 {
305 EDMLOGD("AdminPoliciesStorageRdb::SaveAuthorizedAdmin.");
306 // update admin_policies set permissions=? where user_id=100 and package_name=? and parent_name=?
307 NativeRdb::ValuesBucket valuesBucket;
308 Json::StreamWriterBuilder builder;
309 builder.settings_["indentation"] = "";
310 Json::Value permissionJson;
311 for (const auto &permission : permissions) {
312 permissionJson.append(permission);
313 }
314 valuesBucket.PutString(EdmRdbFiledConst::FILED_PERMISSIONS, Json::writeString(builder, permissionJson));
315
316 NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME);
317 predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(EdmConstants::DEFAULT_USER_ID));
318 predicates.EqualTo(EdmRdbFiledConst::FILED_PACKAGE_NAME, bundleName);
319 predicates.EqualTo(EdmRdbFiledConst::FILED_PARENT_ADMIN, parentName);
320 return EdmRdbDataManager::GetInstance()->Update(valuesBucket, predicates);
321 }
322 } // namespace EDM
323 } // namespace OHOS