• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "token_field_const.h"
21 
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 namespace {
26 static const std::vector<std::string> g_StringTypeColumns = {
27     "description", "permission_name", "device_id", "bundle_name",
28     "app_id", "process_name", "dcap", "native_acls", "label", "value",
29 };
30 
31 static const std::map<AtmDataType, std::string> g_DateTypeToTableName = {
32     {AtmDataType::ACCESSTOKEN_HAP_INFO, "hap_token_info_table"},
33     {AtmDataType::ACCESSTOKEN_NATIVE_INFO, "native_token_info_table"},
34     {AtmDataType::ACCESSTOKEN_PERMISSION_DEF, "permission_definition_table"},
35     {AtmDataType::ACCESSTOKEN_PERMISSION_STATE, "permission_state_table"},
36     {AtmDataType::ACCESSTOKEN_PERMISSION_REQUEST_TOGGLE_STATUS, "permission_request_toggle_status_table"},
37     {AtmDataType::ACCESSTOKEN_PERMISSION_EXTEND_VALUE, "permission_extend_value_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     std::vector<std::string> columnNames;
112     resultSet->GetAllColumnNames(columnNames);
113     uint32_t size = columnNames.size(); // size 0 means insert or update nonthing, this should ignore
114 
115     for (uint32_t i = 0; i < size; ++i) {
116         std::string columnName = columnNames[i];
117         int32_t columnIndex = 0;
118         resultSet->GetColumnIndex(columnName, columnIndex);
119 
120         NativeRdb::ColumnType type;
121         resultSet->GetColumnType(columnIndex, type);
122 
123         if (type == NativeRdb::ColumnType::TYPE_INTEGER) {
124             int32_t data = 0;
125             resultSet->GetInt(columnIndex, data);
126             value.Put(columnName, data);
127         } else if (type == NativeRdb::ColumnType::TYPE_STRING) {
128             std::string data;
129             resultSet->GetString(columnIndex, data);
130             value.Put(columnName, data);
131         }
132     }
133 }
134 } // namespace AccessToken
135 } // namespace Security
136 } // namespace OHOS
137