/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/ptree/pt_grant_revoke.h
Line | Count | Source |
1 | | //-------------------------------------------------------------------------------------------------- |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. 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 distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | // |
15 | | // Tree node definitions for GRANT statement. |
16 | | //-------------------------------------------------------------------------------------------------- |
17 | | |
18 | | #ifndef YB_YQL_CQL_QL_PTREE_PT_GRANT_REVOKE_H_ |
19 | | #define YB_YQL_CQL_QL_PTREE_PT_GRANT_REVOKE_H_ |
20 | | |
21 | | #include <inttypes.h> |
22 | | #include <stddef.h> |
23 | | #include <stdint.h> |
24 | | #include <string.h> |
25 | | |
26 | | #include <limits> |
27 | | #include <string> |
28 | | |
29 | | #include "yb/client/client_fwd.h" |
30 | | |
31 | | #include "yb/common/roles_permissions.h" |
32 | | |
33 | | #include "yb/gutil/integral_types.h" |
34 | | |
35 | | #include "yb/util/enums.h" |
36 | | |
37 | | #include "yb/yql/cql/ql/ptree/ptree_fwd.h" |
38 | | #include "yb/yql/cql/ql/ptree/list_node.h" |
39 | | #include "yb/yql/cql/ql/ptree/pt_name.h" |
40 | | #include "yb/yql/cql/ql/ptree/tree_node.h" |
41 | | |
42 | | namespace yb { |
43 | | namespace ql { |
44 | | |
45 | | //-------------------------------------------------------------------------------------------------- |
46 | | // GRANT Role Statement. |
47 | | |
48 | | class PTGrantRevokeRole : public TreeNode { |
49 | | public: |
50 | | //------------------------------------------------------------------------------------------------ |
51 | | // Public types. |
52 | | typedef MCSharedPtr<PTGrantRevokeRole> SharedPtr; |
53 | | typedef MCSharedPtr<const PTGrantRevokeRole> SharedPtrConst; |
54 | | |
55 | | //------------------------------------------------------------------------------------------------ |
56 | | // Constructor and destructor. |
57 | | PTGrantRevokeRole(MemoryContext* memctx, YBLocationPtr loc, |
58 | | client::GrantRevokeStatementType statement_type, |
59 | | const MCSharedPtr<MCString>& granted_role_name, |
60 | | const MCSharedPtr<MCString>& recipient_role_name); |
61 | | virtual ~PTGrantRevokeRole(); |
62 | | |
63 | | // Node type. |
64 | 289 | virtual TreeNodeOpcode opcode() const override { |
65 | 289 | return TreeNodeOpcode::kPTGrantRevokeRole; |
66 | 289 | } |
67 | | |
68 | | // Support for shared_ptr. |
69 | | template<typename... TypeArgs> |
70 | 98 | inline static PTGrantRevokeRole::SharedPtr MakeShared(MemoryContext* memctx, TypeArgs&&... args) { |
71 | 98 | return MCMakeShared<PTGrantRevokeRole>(memctx, std::forward<TypeArgs>(args)...); |
72 | 98 | } |
73 | | |
74 | | // Node semantics analysis. |
75 | | virtual CHECKED_STATUS Analyze(SemContext* sem_context) override; |
76 | | |
77 | | void PrintSemanticAnalysisResult(SemContext *sem_context); |
78 | | |
79 | | // Name of role that is being granted. |
80 | 112 | std::string granted_role_name() const { |
81 | 112 | return granted_role_name_->c_str(); |
82 | 112 | } |
83 | | |
84 | | // Name of role that is receiving the grant statement. |
85 | 112 | std::string recipient_role_name() const { |
86 | 112 | return recipient_role_name_->c_str(); |
87 | 112 | } |
88 | | |
89 | | // Type of statement: GRANT or REVOKE. |
90 | 52 | client::GrantRevokeStatementType statement_type() const { |
91 | 52 | return statement_type_; |
92 | 52 | } |
93 | | |
94 | | private: |
95 | | const client::GrantRevokeStatementType statement_type_; |
96 | | const MCSharedPtr<MCString> granted_role_name_; |
97 | | const MCSharedPtr<MCString> recipient_role_name_; |
98 | | }; |
99 | | |
100 | | //-------------------------------------------------------------------------------------------------- |
101 | | // GRANT Permission Statement. |
102 | | |
103 | | class PTGrantRevokePermission : public TreeNode { |
104 | | public: |
105 | | //------------------------------------------------------------------------------------------------ |
106 | | // Public types. |
107 | | typedef MCSharedPtr<PTGrantRevokePermission> SharedPtr; |
108 | | typedef MCSharedPtr<const PTGrantRevokePermission> SharedPtrConst; |
109 | | |
110 | | //------------------------------------------------------------------------------------------------ |
111 | | // Constructor and destructor. |
112 | | PTGrantRevokePermission(MemoryContext* memctx, YBLocationPtr loc, |
113 | | client::GrantRevokeStatementType statement_type, |
114 | | const MCSharedPtr<MCString>& permission_name, |
115 | | const ResourceType& resource_type, |
116 | | const PTQualifiedName::SharedPtr& resource_name, |
117 | | const PTQualifiedName::SharedPtr& role_name); |
118 | | virtual ~PTGrantRevokePermission(); |
119 | | |
120 | | static const std::map<std::string, PermissionType> kPermissionMap; |
121 | | |
122 | | // Node type. |
123 | 3.65k | virtual TreeNodeOpcode opcode() const override { |
124 | 3.65k | return TreeNodeOpcode::kPTGrantRevokePermission; |
125 | 3.65k | } |
126 | | |
127 | | // Support for shared_ptr. |
128 | | template<typename... TypeArgs> |
129 | | inline static PTGrantRevokePermission::SharedPtr MakeShared(MemoryContext* memctx, |
130 | 815 | TypeArgs&&... args) { |
131 | 815 | return MCMakeShared<PTGrantRevokePermission>(memctx, std::forward<TypeArgs>(args)...); |
132 | 815 | } _ZN2yb2ql23PTGrantRevokePermission10MakeSharedIJNSt3__110shared_ptrINS0_8LocationEEENS_6client24GrantRevokeStatementTypeERNS4_INS3_12basic_stringIcNS3_11char_traitsIcEENS_8internal18ArenaAllocatorBaseIcNSC_11ArenaTraitsEEEEEEENS_12ResourceTypeEDnRNS4_INS0_15PTQualifiedNameEEEEEENS4_IS1_EEPNSC_9ArenaBaseISE_EEDpOT_ Line | Count | Source | 130 | 219 | TypeArgs&&... args) { | 131 | 219 | return MCMakeShared<PTGrantRevokePermission>(memctx, std::forward<TypeArgs>(args)...); | 132 | 219 | } |
_ZN2yb2ql23PTGrantRevokePermission10MakeSharedIJNSt3__110shared_ptrINS0_8LocationEEENS_6client24GrantRevokeStatementTypeERNS4_INS3_12basic_stringIcNS3_11char_traitsIcEENS_8internal18ArenaAllocatorBaseIcNSC_11ArenaTraitsEEEEEEENS_12ResourceTypeERNS4_INS0_15PTQualifiedNameEEESM_EEENS4_IS1_EEPNSC_9ArenaBaseISE_EEDpOT_ Line | Count | Source | 130 | 596 | TypeArgs&&... args) { | 131 | 596 | return MCMakeShared<PTGrantRevokePermission>(memctx, std::forward<TypeArgs>(args)...); | 132 | 596 | } |
|
133 | | |
134 | | // Node semantics analysis. |
135 | | virtual CHECKED_STATUS Analyze(SemContext *sem_context) override; |
136 | | void PrintSemanticAnalysisResult(SemContext *sem_context); |
137 | | |
138 | | // Type of statement: GRANT or REVOKE. |
139 | 724 | client::GrantRevokeStatementType statement_type() const { |
140 | 724 | return statement_type_; |
141 | 724 | } |
142 | | |
143 | | // Name of Role the permission is being granted to. |
144 | 1.46k | const PTQualifiedName::SharedPtr role_name() const { |
145 | 1.46k | return role_name_; |
146 | 1.46k | } |
147 | | |
148 | 1.45k | ResourceType resource_type() const { |
149 | 1.45k | return resource_type_; |
150 | 1.45k | } |
151 | | |
152 | | // Name of resource, e.g. role_eng, table_salaries, keyspace_qa. |
153 | 3.11k | const char* resource_name() const { |
154 | 3.11k | if (resource_type_ == ResourceType::ALL_ROLES || |
155 | 2.98k | resource_type_ == ResourceType::ALL_KEYSPACES) { |
156 | 406 | return nullptr; |
157 | 406 | } |
158 | 2.71k | return complete_resource_name_->last_name().c_str(); |
159 | 2.71k | } |
160 | | |
161 | | // Keyspace name for tables and keyspaces. |
162 | 2.05k | const char* namespace_name() const { |
163 | 2.05k | if (resource_type_ == ResourceType::TABLE || resource_type_ == ResourceType::KEYSPACE) { |
164 | 1.48k | return complete_resource_name_->first_name().c_str(); |
165 | 1.48k | } |
166 | 578 | return nullptr; |
167 | 578 | } |
168 | | |
169 | 721 | PermissionType permission() const { |
170 | 721 | return permission_; |
171 | 721 | } |
172 | | |
173 | 2.19k | const string canonical_resource() const { |
174 | 2.19k | std::string prefix = ""; |
175 | 2.19k | std::string suffix = ""; |
176 | 2.19k | if (complete_resource_name_ != nullptr) { |
177 | 1.57k | if (resource_type_ == ResourceType::TABLE) { |
178 | 603 | suffix = "/" + string(namespace_name()); |
179 | 603 | } |
180 | 1.57k | suffix = suffix + "/" + string(resource_name()); |
181 | 1.57k | } |
182 | | |
183 | 2.19k | if (resource_type_ == ResourceType::ALL_ROLES || resource_type_ == ResourceType::ROLE) { |
184 | 462 | prefix = kRolesRoleResource; |
185 | 1.73k | } else { |
186 | 1.73k | prefix = kRolesDataResource; |
187 | 1.73k | } |
188 | 2.19k | std::string full_name = prefix + suffix; |
189 | 2.19k | return full_name; |
190 | 2.19k | } |
191 | | |
192 | | protected: |
193 | | const client::GrantRevokeStatementType statement_type_; |
194 | | // Just need an arbitrary default value here. |
195 | | PermissionType permission_ = PermissionType::ALL_PERMISSION; |
196 | | const MCSharedPtr<MCString> permission_name_; |
197 | | // complete_resource_name_ is nullptr for ALL KEYSPACES and ALL ROLES. |
198 | | const PTQualifiedName::SharedPtr complete_resource_name_; |
199 | | const PTQualifiedName::SharedPtr role_name_; |
200 | | const ResourceType resource_type_; |
201 | | }; |
202 | | |
203 | | } // namespace ql |
204 | | } // namespace yb |
205 | | |
206 | | #endif // YB_YQL_CQL_QL_PTREE_PT_GRANT_REVOKE_H_ |