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 "permission_used_record_db.h"
17
18 #include <mutex>
19 #include "accesstoken_log.h"
20 #include "active_change_response_info.h"
21 #include "constant.h"
22 #include "permission_used_type.h"
23 #include "privacy_field_const.h"
24
25 namespace OHOS {
26 namespace Security {
27 namespace AccessToken {
28 namespace {
29 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
30 LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PermissionUsedRecordDb"
31 };
32 static const std::string FIELD_COUNT_NUMBER = "count";
33 static const std::string INTEGER_STR = " integer not null,";
34 std::recursive_mutex g_instanceMutex;
35 }
36
GetInstance()37 PermissionUsedRecordDb& PermissionUsedRecordDb::GetInstance()
38 {
39 static PermissionUsedRecordDb* instance = nullptr;
40 if (instance == nullptr) {
41 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
42 if (instance == nullptr) {
43 instance = new PermissionUsedRecordDb();
44 }
45 }
46 return *instance;
47 }
48
~PermissionUsedRecordDb()49 PermissionUsedRecordDb::~PermissionUsedRecordDb()
50 {
51 Close();
52 }
53
OnCreate()54 void PermissionUsedRecordDb::OnCreate()
55 {
56 ACCESSTOKEN_LOG_INFO(LABEL, "Entry");
57 CreatePermissionRecordTable();
58 CreatePermissionUsedTypeTable();
59 }
60
OnUpdate(int32_t version)61 void PermissionUsedRecordDb::OnUpdate(int32_t version)
62 {
63 ACCESSTOKEN_LOG_INFO(LABEL, "Entry");
64 if (version == DataBaseVersion::VERISION_1) {
65 InsertLockScreenStatusColumn();
66 InsertPermissionUsedTypeColumn();
67 CreatePermissionUsedTypeTable();
68 } else if (version == DataBaseVersion::VERISION_2) {
69 InsertPermissionUsedTypeColumn();
70 CreatePermissionUsedTypeTable();
71 }
72 }
73
PermissionUsedRecordDb()74 PermissionUsedRecordDb::PermissionUsedRecordDb() : SqliteHelper(DATABASE_NAME, DATABASE_PATH, DATABASE_VERSION)
75 {
76 SqliteTable permissionRecordTable;
77 permissionRecordTable.tableName_ = PERMISSION_RECORD_TABLE;
78 permissionRecordTable.tableColumnNames_ = {
79 PrivacyFiledConst::FIELD_TOKEN_ID,
80 PrivacyFiledConst::FIELD_OP_CODE,
81 PrivacyFiledConst::FIELD_STATUS,
82 PrivacyFiledConst::FIELD_TIMESTAMP,
83 PrivacyFiledConst::FIELD_ACCESS_DURATION,
84 PrivacyFiledConst::FIELD_ACCESS_COUNT,
85 PrivacyFiledConst::FIELD_REJECT_COUNT,
86 PrivacyFiledConst::FIELD_LOCKSCREEN_STATUS,
87 PrivacyFiledConst::FIELD_USED_TYPE
88 };
89
90 SqliteTable permissionUsedTypeTable;
91 permissionUsedTypeTable.tableName_ = PERMISSION_USED_TYPE_TABLE;
92 permissionUsedTypeTable.tableColumnNames_ = {
93 PrivacyFiledConst::FIELD_TOKEN_ID,
94 PrivacyFiledConst::FIELD_PERMISSION_CODE,
95 /**
96 * bit operation:
97 * 1 -> 001, NORMAL_TYPE
98 * 2 -> 010, PICKER_TYPE
99 * 3 -> 011, NORMAL_TYPE + PICKER_TYPE
100 * 4 -> 100, SECURITY_COMPONENT_TYPE
101 * 5 -> 101, NORMAL_TYPE + SECURITY_COMPONENT_TYPE
102 * 6 -> 110, PICKER_TYPE + SECURITY_COMPONENT_TYPE
103 * 7 -> 111, NORMAL_TYPE + PICKER_TYPE + SECURITY_COMPONENT_TYPE
104 */
105 PrivacyFiledConst::FIELD_USED_TYPE
106 };
107
108 dataTypeToSqlTable_ = {
109 {PERMISSION_RECORD, permissionRecordTable},
110 {PERMISSION_USED_TYPE, permissionUsedTypeTable},
111 };
112 Open();
113 }
114
Add(DataType type,const std::vector<GenericValues> & values)115 int32_t PermissionUsedRecordDb::Add(DataType type, const std::vector<GenericValues>& values)
116 {
117 OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lock(this->rwLock_);
118 std::string prepareSql = CreateInsertPrepareSqlCmd(type);
119 if (prepareSql.empty()) {
120 ACCESSTOKEN_LOG_ERROR(LABEL, "Type %{public}u invalid", type);
121 return FAILURE;
122 }
123
124 auto statement = Prepare(prepareSql);
125 BeginTransaction();
126 bool isAddSuccessfully = true;
127 for (const auto& value : values) {
128 std::vector<std::string> columnNames = value.GetAllKeys();
129 for (const auto& name : columnNames) {
130 statement.Bind(name, value.Get(name));
131 }
132 int32_t ret = statement.Step();
133 if (ret != Statement::State::DONE) {
134 ACCESSTOKEN_LOG_ERROR(LABEL, "failed, errorMsg: %{public}s", SpitError().c_str());
135 isAddSuccessfully = false;
136 }
137 statement.Reset();
138 }
139 if (!isAddSuccessfully) {
140 ACCESSTOKEN_LOG_ERROR(LABEL, "rollback transaction.");
141 RollbackTransaction();
142 return FAILURE;
143 }
144 ACCESSTOKEN_LOG_DEBUG(LABEL, "commit transaction.");
145 CommitTransaction();
146 return SUCCESS;
147 }
148
Remove(DataType type,const GenericValues & conditions)149 int32_t PermissionUsedRecordDb::Remove(DataType type, const GenericValues& conditions)
150 {
151 OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lock(this->rwLock_);
152 std::vector<std::string> columnNames = conditions.GetAllKeys();
153 std::string prepareSql = CreateDeletePrepareSqlCmd(type, columnNames);
154 if (prepareSql.empty()) {
155 ACCESSTOKEN_LOG_ERROR(LABEL, "Type %{public}u invalid", type);
156 return FAILURE;
157 }
158
159 auto statement = Prepare(prepareSql);
160 for (const auto& columnName : columnNames) {
161 statement.Bind(columnName, conditions.Get(columnName));
162 }
163 int32_t ret = statement.Step();
164 return (ret == Statement::State::DONE) ? SUCCESS : FAILURE;
165 }
166
FindByConditions(DataType type,const std::set<int32_t> & opCodeList,const GenericValues & andConditions,std::vector<GenericValues> & results,int32_t databaseQueryCount)167 int32_t PermissionUsedRecordDb::FindByConditions(DataType type, const std::set<int32_t>& opCodeList,
168 const GenericValues& andConditions, std::vector<GenericValues>& results, int32_t databaseQueryCount)
169 {
170 OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lock(this->rwLock_);
171 std::vector<std::string> andColumns = andConditions.GetAllKeys();
172 std::string prepareSql = CreateSelectByConditionPrepareSqlCmd(type, opCodeList, andColumns, databaseQueryCount);
173 if (prepareSql.empty()) {
174 ACCESSTOKEN_LOG_ERROR(LABEL, "Type %{public}u invalid", type);
175 return FAILURE;
176 }
177
178 auto statement = Prepare(prepareSql);
179
180 for (const auto& columnName : andColumns) {
181 statement.Bind(columnName, andConditions.Get(columnName));
182 }
183
184 while (statement.Step() == Statement::State::ROW) {
185 int32_t columnCount = statement.GetColumnCount();
186 GenericValues value;
187 for (int32_t i = 0; i < columnCount; i++) {
188 if ((statement.GetColumnName(i) == PrivacyFiledConst::FIELD_TIMESTAMP) ||
189 (statement.GetColumnName(i) == PrivacyFiledConst::FIELD_ACCESS_DURATION)) {
190 value.Put(statement.GetColumnName(i), statement.GetValue(i, true));
191 } else {
192 value.Put(statement.GetColumnName(i), statement.GetValue(i, false));
193 }
194 }
195 results.emplace_back(value);
196 }
197 return SUCCESS;
198 }
199
GetDistinctValue(DataType type,const std::string & condition,std::vector<GenericValues> & results)200 int32_t PermissionUsedRecordDb::GetDistinctValue(DataType type,
201 const std::string& condition, std::vector<GenericValues>& results)
202 {
203 OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lock(this->rwLock_);
204 std::string getDistinctValueSql = CreateGetDistinctValue(type, condition);
205 auto statement = Prepare(getDistinctValueSql);
206 while (statement.Step() == Statement::State::ROW) {
207 int32_t columnCount = statement.GetColumnCount();
208 GenericValues value;
209 for (int32_t i = 0; i < columnCount; i++) {
210 if (statement.GetColumnName(i) == PrivacyFiledConst::FIELD_TOKEN_ID) {
211 value.Put(statement.GetColumnName(i), statement.GetValue(i, false));
212 } else if (statement.GetColumnName(i) == PrivacyFiledConst::FIELD_DEVICE_ID) {
213 value.Put(statement.GetColumnName(i), statement.GetColumnString(i));
214 }
215 }
216 results.emplace_back(value);
217 }
218 return SUCCESS;
219 }
220
Count(DataType type,GenericValues & result)221 void PermissionUsedRecordDb::Count(DataType type, GenericValues& result)
222 {
223 OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lock(this->rwLock_);
224 std::string countSql = CreateCountPrepareSqlCmd(type);
225 auto countStatement = Prepare(countSql);
226 if (countStatement.Step() == Statement::State::ROW) {
227 int32_t column = 0;
228 result.Put(FIELD_COUNT_NUMBER, countStatement.GetValue(column, true));
229 }
230 }
231
DeleteExpireRecords(DataType type,const GenericValues & andConditions)232 int32_t PermissionUsedRecordDb::DeleteExpireRecords(DataType type,
233 const GenericValues& andConditions)
234 {
235 OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lock(this->rwLock_);
236 std::vector<std::string> andColumns = andConditions.GetAllKeys();
237 if (!andColumns.empty()) {
238 std::string deleteExpireSql = CreateDeleteExpireRecordsPrepareSqlCmd(type, andColumns);
239 auto deleteExpireStatement = Prepare(deleteExpireSql);
240 for (const auto& columnName : andColumns) {
241 deleteExpireStatement.Bind(columnName, andConditions.Get(columnName));
242 }
243 if (deleteExpireStatement.Step() != Statement::State::DONE) {
244 return FAILURE;
245 }
246 }
247 return SUCCESS;
248 }
249
DeleteExcessiveRecords(DataType type,uint32_t excessiveSize)250 int32_t PermissionUsedRecordDb::DeleteExcessiveRecords(DataType type, uint32_t excessiveSize)
251 {
252 OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lock(this->rwLock_);
253 std::string deleteExcessiveSql = CreateDeleteExcessiveRecordsPrepareSqlCmd(type, excessiveSize);
254 auto deleteExcessiveStatement = Prepare(deleteExcessiveSql);
255 if (deleteExcessiveStatement.Step() != Statement::State::DONE) {
256 return FAILURE;
257 }
258 return SUCCESS;
259 }
260
Update(DataType type,const GenericValues & modifyValue,const GenericValues & conditionValue)261 int32_t PermissionUsedRecordDb::Update(DataType type, const GenericValues& modifyValue,
262 const GenericValues& conditionValue)
263 {
264 std::vector<std::string> modifyNames = modifyValue.GetAllKeys();
265 std::vector<std::string> conditionNames = conditionValue.GetAllKeys();
266
267 OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lock(this->rwLock_);
268 std::string prepareSql = CreateUpdatePrepareSqlCmd(type, modifyNames, conditionNames);
269 if (prepareSql.empty()) {
270 ACCESSTOKEN_LOG_ERROR(LABEL, "Type %{public}u invalid", type);
271 return FAILURE;
272 }
273
274 auto statement = Prepare(prepareSql);
275
276 for (const auto& modifyName : modifyNames) {
277 statement.Bind(modifyName, modifyValue.Get(modifyName));
278 }
279
280 for (const auto& conditionName : conditionNames) {
281 statement.Bind(conditionName, conditionValue.Get(conditionName));
282 }
283
284 int32_t ret = statement.Step();
285 return (ret == Statement::State::DONE) ? SUCCESS : FAILURE;
286 }
287
Query(DataType type,const GenericValues & conditionValue,std::vector<GenericValues> & results)288 int32_t PermissionUsedRecordDb::Query(DataType type, const GenericValues& conditionValue,
289 std::vector<GenericValues>& results)
290 {
291 std::vector<std::string> conditionColumns = conditionValue.GetAllKeys();
292
293 OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lock(this->rwLock_);
294 std::string prepareSql = CreateQueryPrepareSqlCmd(type, conditionColumns);
295 if (prepareSql.empty()) {
296 ACCESSTOKEN_LOG_ERROR(LABEL, "Type %{public}u invalid", type);
297 return FAILURE;
298 }
299
300 auto statement = Prepare(prepareSql);
301 for (const auto& conditionColumn : conditionColumns) {
302 statement.Bind(conditionColumn, conditionValue.Get(conditionColumn));
303 }
304
305 while (statement.Step() == Statement::State::ROW) {
306 int32_t columnCount = statement.GetColumnCount();
307 GenericValues value;
308
309 for (int32_t i = 0; i < columnCount; i++) {
310 value.Put(statement.GetColumnName(i), statement.GetValue(i, false));
311 }
312
313 results.emplace_back(value);
314 }
315
316 return SUCCESS;
317 }
318
CreateInsertPrepareSqlCmd(DataType type) const319 std::string PermissionUsedRecordDb::CreateInsertPrepareSqlCmd(DataType type) const
320 {
321 auto it = dataTypeToSqlTable_.find(type);
322 if (it == dataTypeToSqlTable_.end()) {
323 return std::string();
324 }
325 std::string sql = "insert into " + it->second.tableName_ + " values(";
326 int32_t i = 1;
327 for (const auto& name : it->second.tableColumnNames_) {
328 sql.append(":" + name);
329 if (i < static_cast<int32_t>(it->second.tableColumnNames_.size())) {
330 sql.append(",");
331 }
332 i += 1;
333 }
334 sql.append(")");
335 return sql;
336 }
337
CreateQueryPrepareSqlCmd(DataType type,const std::vector<std::string> & conditionColumns) const338 std::string PermissionUsedRecordDb::CreateQueryPrepareSqlCmd(DataType type,
339 const std::vector<std::string>& conditionColumns) const
340 {
341 auto it = dataTypeToSqlTable_.find(type);
342 if (it == dataTypeToSqlTable_.end()) {
343 return std::string();
344 }
345 std::string sql = "select * from " + it->second.tableName_ + " where 1 = 1";
346
347 for (const auto& andColumn : conditionColumns) {
348 sql.append(" and ");
349 sql.append(andColumn + "=:" + andColumn);
350 }
351
352 return sql;
353 }
354
CreateDeletePrepareSqlCmd(DataType type,const std::vector<std::string> & columnNames) const355 std::string PermissionUsedRecordDb::CreateDeletePrepareSqlCmd(
356 DataType type, const std::vector<std::string>& columnNames) const
357 {
358 auto it = dataTypeToSqlTable_.find(type);
359 if (it == dataTypeToSqlTable_.end()) {
360 return std::string();
361 }
362 std::string sql = "delete from " + it->second.tableName_ + " where 1 = 1";
363 for (const auto& name : columnNames) {
364 sql.append(" and ");
365 sql.append(name + "=:" + name);
366 }
367 return sql;
368 }
369
CreateUpdatePrepareSqlCmd(DataType type,const std::vector<std::string> & modifyColumns,const std::vector<std::string> & conditionColumns) const370 std::string PermissionUsedRecordDb::CreateUpdatePrepareSqlCmd(DataType type,
371 const std::vector<std::string>& modifyColumns, const std::vector<std::string>& conditionColumns) const
372 {
373 if (modifyColumns.empty()) {
374 return std::string();
375 }
376
377 auto it = dataTypeToSqlTable_.find(type);
378 if (it == dataTypeToSqlTable_.end()) {
379 return std::string();
380 }
381
382 std::string sql = "update " + it->second.tableName_ + " set ";
383 int32_t i = 1;
384 for (const auto& name : modifyColumns) {
385 sql.append(name + "=:" + name);
386 if (i < static_cast<int32_t>(modifyColumns.size())) {
387 sql.append(",");
388 }
389 i += 1;
390 }
391
392 if (!conditionColumns.empty()) {
393 sql.append(" where 1 = 1");
394 for (const auto& columnName : conditionColumns) {
395 sql.append(" and ");
396 sql.append(columnName + "=:" + columnName);
397 }
398 }
399 return sql;
400 }
401
CreateSelectByConditionPrepareSqlCmd(DataType type,const std::set<int32_t> & opCodeList,const std::vector<std::string> & andColumns,int32_t databaseQueryCount) const402 std::string PermissionUsedRecordDb::CreateSelectByConditionPrepareSqlCmd(DataType type,
403 const std::set<int32_t>& opCodeList, const std::vector<std::string>& andColumns, int32_t databaseQueryCount) const
404 {
405 auto it = dataTypeToSqlTable_.find(type);
406 if (it == dataTypeToSqlTable_.end()) {
407 return std::string();
408 }
409
410 std::string sql = "select * from " + it->second.tableName_ + " where 1 = 1";
411
412 for (const auto& andColName : andColumns) {
413 if (andColName == PrivacyFiledConst::FIELD_TIMESTAMP_BEGIN) {
414 sql.append(" and ");
415 sql.append(PrivacyFiledConst::FIELD_TIMESTAMP);
416 sql.append(" >=:" + andColName);
417 } else if (andColName == PrivacyFiledConst::FIELD_TIMESTAMP_END) {
418 sql.append(" and ");
419 sql.append(PrivacyFiledConst::FIELD_TIMESTAMP);
420 sql.append(" <=:" + andColName);
421 } else {
422 sql.append(" and ");
423 sql.append(andColName + "=:" + andColName);
424 }
425 }
426 if (!opCodeList.empty()) {
427 sql.append(" and (");
428 for (const auto& opCode : opCodeList) {
429 if (opCode != Constant::OP_INVALID) {
430 sql.append(PrivacyFiledConst::FIELD_OP_CODE);
431 sql.append(+ " = " + std::to_string(opCode));
432 sql.append(" or ");
433 }
434 }
435 sql.append("0)");
436 }
437 sql.append(" order by timestamp desc");
438 sql.append(" limit " + std::to_string(databaseQueryCount));
439 return sql;
440 }
441
CreateCountPrepareSqlCmd(DataType type) const442 std::string PermissionUsedRecordDb::CreateCountPrepareSqlCmd(DataType type) const
443 {
444 auto it = dataTypeToSqlTable_.find(type);
445 if (it == dataTypeToSqlTable_.end()) {
446 return std::string();
447 }
448 std::string sql = "select count(*) from " + it->second.tableName_;
449 return sql;
450 }
451
CreateDeleteExpireRecordsPrepareSqlCmd(DataType type,const std::vector<std::string> & andColumns) const452 std::string PermissionUsedRecordDb::CreateDeleteExpireRecordsPrepareSqlCmd(DataType type,
453 const std::vector<std::string>& andColumns) const
454 {
455 auto it = dataTypeToSqlTable_.find(type);
456 if (it == dataTypeToSqlTable_.end()) {
457 return std::string();
458 }
459 std::string sql = "delete from " + it->second.tableName_ + " where ";
460 sql.append(PrivacyFiledConst::FIELD_TIMESTAMP);
461 sql.append(" in (select ");
462 sql.append(PrivacyFiledConst::FIELD_TIMESTAMP);
463 sql.append(" from " + it->second.tableName_ + " where 1 = 1");
464 for (const auto& name : andColumns) {
465 if (name == PrivacyFiledConst::FIELD_TIMESTAMP_BEGIN) {
466 sql.append(" and ");
467 sql.append(PrivacyFiledConst::FIELD_TIMESTAMP);
468 sql.append(" >=:" + name);
469 } else if (name == PrivacyFiledConst::FIELD_TIMESTAMP_END) {
470 sql.append(" and ");
471 sql.append(PrivacyFiledConst::FIELD_TIMESTAMP);
472 sql.append(" <=:" + name);
473 } else {
474 sql.append(" and ");
475 sql.append(name + "=:" + name);
476 }
477 }
478 sql.append(" )");
479 return sql;
480 }
481
CreateDeleteExcessiveRecordsPrepareSqlCmd(DataType type,uint32_t excessiveSize) const482 std::string PermissionUsedRecordDb::CreateDeleteExcessiveRecordsPrepareSqlCmd(DataType type,
483 uint32_t excessiveSize) const
484 {
485 auto it = dataTypeToSqlTable_.find(type);
486 if (it == dataTypeToSqlTable_.end()) {
487 return std::string();
488 }
489 std::string sql = "delete from " + it->second.tableName_ + " where ";
490 sql.append(PrivacyFiledConst::FIELD_TIMESTAMP);
491 sql.append(" in (select ");
492 sql.append(PrivacyFiledConst::FIELD_TIMESTAMP);
493 sql.append(" from " + it->second.tableName_ + " order by ");
494 sql.append(PrivacyFiledConst::FIELD_TIMESTAMP);
495 sql.append(" limit ");
496 sql.append(std::to_string(excessiveSize) + " )");
497 return sql;
498 }
499
CreateGetDistinctValue(DataType type,const std::string conditionColumns) const500 std::string PermissionUsedRecordDb::CreateGetDistinctValue(DataType type,
501 const std::string conditionColumns) const
502 {
503 auto it = dataTypeToSqlTable_.find(type);
504 if (it == dataTypeToSqlTable_.end()) {
505 return std::string();
506 }
507 std::string sql = "select distinct ";
508 sql.append(conditionColumns + " from "+ it->second.tableName_);
509 return sql;
510 }
511
CreatePermissionRecordTable() const512 int32_t PermissionUsedRecordDb::CreatePermissionRecordTable() const
513 {
514 auto it = dataTypeToSqlTable_.find(DataType::PERMISSION_RECORD);
515 if (it == dataTypeToSqlTable_.end()) {
516 return FAILURE;
517 }
518 std::string sql = "create table if not exists ";
519 sql.append(it->second.tableName_ + " (")
520 .append(PrivacyFiledConst::FIELD_TOKEN_ID)
521 .append(" integer not null,")
522 .append(PrivacyFiledConst::FIELD_OP_CODE)
523 .append(" integer not null,")
524 .append(PrivacyFiledConst::FIELD_STATUS)
525 .append(" integer not null,")
526 .append(PrivacyFiledConst::FIELD_TIMESTAMP)
527 .append(" integer not null,")
528 .append(PrivacyFiledConst::FIELD_ACCESS_DURATION)
529 .append(" integer not null,")
530 .append(PrivacyFiledConst::FIELD_ACCESS_COUNT)
531 .append(" integer not null,")
532 .append(PrivacyFiledConst::FIELD_REJECT_COUNT)
533 .append(" integer not null,")
534 .append(PrivacyFiledConst::FIELD_LOCKSCREEN_STATUS)
535 .append(" integer not null,")
536 .append(PrivacyFiledConst::FIELD_USED_TYPE)
537 .append(" integer not null,")
538 .append("primary key(")
539 .append(PrivacyFiledConst::FIELD_TOKEN_ID)
540 .append(",")
541 .append(PrivacyFiledConst::FIELD_OP_CODE)
542 .append(",")
543 .append(PrivacyFiledConst::FIELD_STATUS)
544 .append(",")
545 .append(PrivacyFiledConst::FIELD_TIMESTAMP)
546 .append("))");
547 return ExecuteSql(sql);
548 }
549
CreatePermissionUsedTypeTable() const550 int32_t PermissionUsedRecordDb::CreatePermissionUsedTypeTable() const
551 {
552 auto it = dataTypeToSqlTable_.find(DataType::PERMISSION_USED_TYPE);
553 if (it == dataTypeToSqlTable_.end()) {
554 return FAILURE;
555 }
556 std::string sql = "create table if not exists ";
557 sql.append(it->second.tableName_ + " (")
558 .append(PrivacyFiledConst::FIELD_TOKEN_ID)
559 .append(" integer not null,")
560 .append(PrivacyFiledConst::FIELD_PERMISSION_CODE)
561 .append(" integer not null,")
562 .append(PrivacyFiledConst::FIELD_USED_TYPE)
563 .append(" integer not null,")
564 .append("primary key(")
565 .append(PrivacyFiledConst::FIELD_TOKEN_ID)
566 .append(",")
567 .append(PrivacyFiledConst::FIELD_PERMISSION_CODE)
568 .append("))");
569 return ExecuteSql(sql);
570 }
571
InsertLockScreenStatusColumn() const572 int32_t PermissionUsedRecordDb::InsertLockScreenStatusColumn() const
573 {
574 ACCESSTOKEN_LOG_INFO(LABEL, "Entry");
575 auto it = dataTypeToSqlTable_.find(DataType::PERMISSION_RECORD);
576 if (it == dataTypeToSqlTable_.end()) {
577 return FAILURE;
578 }
579 std::string checkSql = "SELECT 1 FROM " + it->second.tableName_ + " WHERE " +
580 PrivacyFiledConst::FIELD_LOCKSCREEN_STATUS + "=" +
581 std::to_string(LockScreenStatusChangeType::PERM_ACTIVE_IN_UNLOCKED);
582 int32_t checkResult = ExecuteSql(checkSql);
583 ACCESSTOKEN_LOG_INFO(LABEL, "check result:%{public}d", checkResult);
584 if (checkResult != -1) {
585 return SUCCESS;
586 }
587
588 std::string sql = "alter table ";
589 sql.append(it->second.tableName_ + " add column ")
590 .append(PrivacyFiledConst::FIELD_LOCKSCREEN_STATUS)
591 .append(" integer default ")
592 .append(std::to_string(LockScreenStatusChangeType::PERM_ACTIVE_IN_UNLOCKED));
593 int32_t insertResult = ExecuteSql(sql);
594 ACCESSTOKEN_LOG_INFO(LABEL, "insert column result:%{public}d", insertResult);
595 return insertResult;
596 }
597
InsertPermissionUsedTypeColumn() const598 int32_t PermissionUsedRecordDb::InsertPermissionUsedTypeColumn() const
599 {
600 ACCESSTOKEN_LOG_INFO(LABEL, "Entry");
601 auto it = dataTypeToSqlTable_.find(DataType::PERMISSION_RECORD);
602 if (it == dataTypeToSqlTable_.end()) {
603 return FAILURE;
604 }
605 std::string checkSql = "SELECT 1 FROM " + it->second.tableName_ + " WHERE " +
606 PrivacyFiledConst::FIELD_USED_TYPE + "=" +
607 std::to_string(PermissionUsedType::NORMAL_TYPE);
608 int32_t checkResult = ExecuteSql(checkSql);
609 ACCESSTOKEN_LOG_INFO(LABEL, "check result:%{public}d", checkResult);
610 if (checkResult != -1) {
611 return SUCCESS;
612 }
613
614 std::string sql = "alter table ";
615 sql.append(it->second.tableName_ + " add column ")
616 .append(PrivacyFiledConst::FIELD_USED_TYPE)
617 .append(" integer default ")
618 .append(std::to_string(PermissionUsedType::NORMAL_TYPE));
619 int32_t insertResult = ExecuteSql(sql);
620 ACCESSTOKEN_LOG_INFO(LABEL, "insert column result:%{public}d", insertResult);
621 return insertResult;
622 }
623 } // namespace AccessToken
624 } // namespace Security
625 } // namespace OHOS
626