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",
26     "app_id", "process_name", "dcap", "native_acls", "label",
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 };
36 }
37 
GetTableNameByType(const AtmDataType type,std::string & tableName)38 void AccessTokenDbUtil::GetTableNameByType(const AtmDataType type, std::string& tableName)
39 {
40     auto iterator = g_DateTypeToTableName.find(type);
41     if (iterator != g_DateTypeToTableName.end()) {
42         tableName = iterator->second;
43     }
44 }
45 
IsColumnStringType(const std::string & column)46 bool AccessTokenDbUtil::IsColumnStringType(const std::string& column)
47 {
48     auto iterator = std::find(g_StringTypeColumns.begin(), g_StringTypeColumns.end(), column);
49     if (iterator != g_StringTypeColumns.end()) {
50         return true;
51     }
52 
53     return false;
54 }
55 
ToRdbValueBucket(const GenericValues & value,NativeRdb::ValuesBucket & bucket)56 void AccessTokenDbUtil::ToRdbValueBucket(const GenericValues& value, NativeRdb::ValuesBucket& bucket)
57 {
58     std::vector<std::string> columnNames = value.GetAllKeys();
59     uint32_t size = columnNames.size(); // size 0 means insert or update nonthing, this should ignore
60 
61     for (uint32_t i = 0; i < size; ++i) {
62         std::string column = columnNames[i];
63 
64         if (IsColumnStringType(column)) {
65             bucket.PutString(column, value.GetString(column));
66         } else {
67             bucket.PutInt(column, value.GetInt(column));
68         }
69     }
70 }
71 
ToRdbValueBuckets(const std::vector<GenericValues> & values,std::vector<NativeRdb::ValuesBucket> & buckets)72 void AccessTokenDbUtil::ToRdbValueBuckets(const std::vector<GenericValues>& values,
73     std::vector<NativeRdb::ValuesBucket>& buckets)
74 {
75     for (const auto& value : values) {
76         NativeRdb::ValuesBucket bucket;
77 
78         ToRdbValueBucket(value, bucket);
79         if (bucket.IsEmpty()) {
80             continue;
81         }
82         buckets.emplace_back(bucket);
83     }
84 }
85 
ToRdbPredicates(const GenericValues & conditionValue,NativeRdb::RdbPredicates & predicates)86 void AccessTokenDbUtil::ToRdbPredicates(const GenericValues& conditionValue, NativeRdb::RdbPredicates& predicates)
87 {
88     std::vector<std::string> columnNames = conditionValue.GetAllKeys();
89     uint32_t size = columnNames.size(); // size 0 is possible, maybe delete or query or update all records
90     for (uint32_t i = 0; i < size; ++i) {
91         std::string column = columnNames[i];
92 
93         if (IsColumnStringType(column)) {
94             predicates.EqualTo(column, conditionValue.GetString(column));
95         } else {
96             predicates.EqualTo(column, conditionValue.GetInt(column));
97         }
98 
99         if (i != size - 1) {
100             predicates.And();
101         }
102     }
103 }
104 
ResultToGenericValues(const std::shared_ptr<NativeRdb::ResultSet> & resultSet,GenericValues & value)105 void AccessTokenDbUtil::ResultToGenericValues(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
106     GenericValues& value)
107 {
108     std::vector<std::string> columnNames;
109     resultSet->GetAllColumnNames(columnNames);
110     uint32_t size = columnNames.size(); // size 0 means insert or update nonthing, this should ignore
111 
112     for (uint32_t i = 0; i < size; ++i) {
113         std::string columnName = columnNames[i];
114         int32_t columnIndex = 0;
115         resultSet->GetColumnIndex(columnName, columnIndex);
116 
117         NativeRdb::ColumnType type;
118         resultSet->GetColumnType(columnIndex, type);
119 
120         if (type == NativeRdb::ColumnType::TYPE_INTEGER) {
121             int32_t data = 0;
122             resultSet->GetInt(columnIndex, data);
123             value.Put(columnName, data);
124         } else if (type == NativeRdb::ColumnType::TYPE_STRING) {
125             std::string data;
126             resultSet->GetString(columnIndex, data);
127             value.Put(columnName, data);
128         }
129     }
130 }
131 } // namespace AccessToken
132 } // namespace Security
133 } // namespace OHOS
134