1 /*
2 * Copyright (c) 2021-2024 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 "access_token_db_util.h"
17
18 #include <map>
19
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23 namespace {
24 static const std::vector<std::string> g_StringTypeColumns = {
25 "description", "permission_name", "device_id", "bundle_name", "app_id",
26 "process_name", "dcap", "native_acls", "label", "value", "name", "app_distribution_type"
27 };
28
29 static const std::map<AtmDataType, std::string> g_DateTypeToTableName = {
30 {AtmDataType::ACCESSTOKEN_HAP_INFO, "hap_token_info_table"},
31 {AtmDataType::ACCESSTOKEN_NATIVE_INFO, "native_token_info_table"},
32 {AtmDataType::ACCESSTOKEN_PERMISSION_DEF, "permission_definition_table"},
33 {AtmDataType::ACCESSTOKEN_PERMISSION_STATE, "permission_state_table"},
34 {AtmDataType::ACCESSTOKEN_PERMISSION_REQUEST_TOGGLE_STATUS, "permission_request_toggle_status_table"},
35 {AtmDataType::ACCESSTOKEN_PERMISSION_EXTEND_VALUE, "permission_extend_value_table"},
36 {AtmDataType::ACCESSTOKEN_HAP_UNDEFINE_INFO, "hap_undefine_info_table"},
37 {AtmDataType::ACCESSTOKEN_SYSTEM_CONFIG, "system_config_table"},
38 };
39 }
40
GetTableNameByType(const AtmDataType type,std::string & tableName)41 void AccessTokenDbUtil::GetTableNameByType(const AtmDataType type, std::string& tableName)
42 {
43 auto iterator = g_DateTypeToTableName.find(type);
44 if (iterator != g_DateTypeToTableName.end()) {
45 tableName = iterator->second;
46 }
47 }
48
IsColumnStringType(const std::string & column)49 bool AccessTokenDbUtil::IsColumnStringType(const std::string& column)
50 {
51 auto iterator = std::find(g_StringTypeColumns.begin(), g_StringTypeColumns.end(), column);
52 if (iterator != g_StringTypeColumns.end()) {
53 return true;
54 }
55
56 return false;
57 }
58
ToRdbValueBucket(const GenericValues & value,NativeRdb::ValuesBucket & bucket)59 void AccessTokenDbUtil::ToRdbValueBucket(const GenericValues& value, NativeRdb::ValuesBucket& bucket)
60 {
61 std::vector<std::string> columnNames = value.GetAllKeys();
62 uint32_t size = columnNames.size(); // size 0 means insert or update nonthing, this should ignore
63
64 for (uint32_t i = 0; i < size; ++i) {
65 std::string column = columnNames[i];
66
67 if (IsColumnStringType(column)) {
68 bucket.PutString(column, value.GetString(column));
69 } else {
70 bucket.PutInt(column, value.GetInt(column));
71 }
72 }
73 }
74
ToRdbValueBuckets(const std::vector<GenericValues> & values,std::vector<NativeRdb::ValuesBucket> & buckets)75 void AccessTokenDbUtil::ToRdbValueBuckets(const std::vector<GenericValues>& values,
76 std::vector<NativeRdb::ValuesBucket>& buckets)
77 {
78 for (const auto& value : values) {
79 NativeRdb::ValuesBucket bucket;
80
81 ToRdbValueBucket(value, bucket);
82 if (bucket.IsEmpty()) {
83 continue;
84 }
85 buckets.emplace_back(bucket);
86 }
87 }
88
ToRdbPredicates(const GenericValues & conditionValue,NativeRdb::RdbPredicates & predicates)89 void AccessTokenDbUtil::ToRdbPredicates(const GenericValues& conditionValue, NativeRdb::RdbPredicates& predicates)
90 {
91 std::vector<std::string> columnNames = conditionValue.GetAllKeys();
92 uint32_t size = columnNames.size(); // size 0 is possible, maybe delete or query or update all records
93 for (uint32_t i = 0; i < size; ++i) {
94 std::string column = columnNames[i];
95
96 if (IsColumnStringType(column)) {
97 predicates.EqualTo(column, conditionValue.GetString(column));
98 } else {
99 predicates.EqualTo(column, conditionValue.GetInt(column));
100 }
101
102 if (i != size - 1) {
103 predicates.And();
104 }
105 }
106 }
107
ResultToGenericValues(const std::shared_ptr<NativeRdb::ResultSet> & resultSet,GenericValues & value)108 void AccessTokenDbUtil::ResultToGenericValues(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
109 GenericValues& value)
110 {
111 if (resultSet == nullptr) {
112 return;
113 }
114 std::vector<std::string> columnNames;
115 resultSet->GetAllColumnNames(columnNames);
116 uint32_t size = columnNames.size(); // size 0 means insert or update nonthing, this should ignore
117
118 for (uint32_t i = 0; i < size; ++i) {
119 std::string columnName = columnNames[i];
120 int32_t columnIndex = 0;
121 resultSet->GetColumnIndex(columnName, columnIndex);
122
123 NativeRdb::ColumnType type;
124 resultSet->GetColumnType(columnIndex, type);
125
126 if (type == NativeRdb::ColumnType::TYPE_INTEGER) {
127 int32_t data = 0;
128 resultSet->GetInt(columnIndex, data);
129 value.Put(columnName, data);
130 } else if (type == NativeRdb::ColumnType::TYPE_STRING) {
131 std::string data;
132 resultSet->GetString(columnIndex, data);
133 value.Put(columnName, data);
134 }
135 }
136 }
137 } // namespace AccessToken
138 } // namespace Security
139 } // namespace OHOS
140