1 | #pragma once |
2 | |
3 | #include <Access/RowPolicy.h> |
4 | #include <Core/Types.h> |
5 | #include <Core/UUID.h> |
6 | #include <common/StringRef.h> |
7 | #include <memory> |
8 | #include <unordered_map> |
9 | |
10 | |
11 | namespace DB |
12 | { |
13 | class IAST; |
14 | using ASTPtr = std::shared_ptr<IAST>; |
15 | |
16 | |
17 | /// Provides fast access to row policies' conditions for a specific user and tables. |
18 | class RowPolicyContext |
19 | { |
20 | public: |
21 | /// Default constructor makes a row policy usage context which restricts nothing. |
22 | RowPolicyContext(); |
23 | |
24 | ~RowPolicyContext(); |
25 | |
26 | using ConditionIndex = RowPolicy::ConditionIndex; |
27 | |
28 | /// Returns prepared filter for a specific table and operations. |
29 | /// The function can return nullptr, that means there is no filters applied. |
30 | /// The returned filter can be a combination of the filters defined by multiple row policies. |
31 | ASTPtr getCondition(const String & database, const String & table_name, ConditionIndex index) const; |
32 | |
33 | /// Returns IDs of all the policies used by the current user. |
34 | std::vector<UUID> getCurrentPolicyIDs() const; |
35 | |
36 | /// Returns IDs of the policies used by a concrete table. |
37 | std::vector<UUID> getCurrentPolicyIDs(const String & database, const String & table_name) const; |
38 | |
39 | private: |
40 | friend class RowPolicyContextFactory; |
41 | friend struct ext::shared_ptr_helper<RowPolicyContext>; |
42 | RowPolicyContext(const String & user_name_); /// RowPolicyContext should be created by RowPolicyContextFactory. |
43 | |
44 | using DatabaseAndTableName = std::pair<String, String>; |
45 | using DatabaseAndTableNameRef = std::pair<StringRef, StringRef>; |
46 | struct Hash |
47 | { |
48 | size_t operator()(const DatabaseAndTableNameRef & database_and_table_name) const; |
49 | }; |
50 | static constexpr size_t MAX_CONDITION_INDEX = RowPolicy::MAX_CONDITION_INDEX; |
51 | using ParsedConditions = std::array<ASTPtr, MAX_CONDITION_INDEX>; |
52 | struct MixedConditions |
53 | { |
54 | std::unique_ptr<DatabaseAndTableName> database_and_table_name_keeper; |
55 | ParsedConditions mixed_conditions; |
56 | std::vector<UUID> policy_ids; |
57 | }; |
58 | using MapOfMixedConditions = std::unordered_map<DatabaseAndTableNameRef, MixedConditions, Hash>; |
59 | |
60 | const String user_name; |
61 | std::shared_ptr<const MapOfMixedConditions> atomic_map_of_mixed_conditions; /// Changed atomically, not protected by `mutex`. |
62 | }; |
63 | |
64 | |
65 | using RowPolicyContextPtr = std::shared_ptr<RowPolicyContext>; |
66 | } |
67 | |