2019-11-17 11:57:02 +00:00
|
|
|
#include <Access/RowPolicyContext.h>
|
2020-02-12 20:47:37 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
2020-02-12 23:59:49 +00:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
2019-11-17 11:57:02 +00:00
|
|
|
#include <boost/range/adaptor/map.hpp>
|
|
|
|
#include <boost/range/algorithm/copy.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
size_t RowPolicyContext::Hash::operator()(const DatabaseAndTableNameRef & database_and_table_name) const
|
|
|
|
{
|
2020-02-12 23:59:49 +00:00
|
|
|
return std::hash<std::string_view>{}(database_and_table_name.first) - std::hash<std::string_view>{}(database_and_table_name.second);
|
2019-11-17 11:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RowPolicyContext::RowPolicyContext()
|
2020-02-12 23:59:49 +00:00
|
|
|
: map_of_mixed_conditions(boost::make_shared<MapOfMixedConditions>())
|
2019-11-17 11:57:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RowPolicyContext::~RowPolicyContext() = default;
|
|
|
|
|
|
|
|
|
2020-02-10 02:26:56 +00:00
|
|
|
RowPolicyContext::RowPolicyContext(const UUID & user_id_)
|
|
|
|
: user_id(user_id_)
|
2019-11-17 11:57:02 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
ASTPtr RowPolicyContext::getCondition(const String & database, const String & table_name, ConditionIndex index) const
|
|
|
|
{
|
|
|
|
/// We don't lock `mutex` here.
|
2020-02-12 23:59:49 +00:00
|
|
|
auto loaded = map_of_mixed_conditions.load();
|
|
|
|
auto it = loaded->find({database, table_name});
|
|
|
|
if (it == loaded->end())
|
2019-11-17 11:57:02 +00:00
|
|
|
return {};
|
|
|
|
return it->second.mixed_conditions[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-12 20:47:37 +00:00
|
|
|
ASTPtr RowPolicyContext::combineConditionsUsingAnd(const ASTPtr & lhs, const ASTPtr & rhs)
|
|
|
|
{
|
|
|
|
if (!lhs)
|
|
|
|
return rhs;
|
|
|
|
if (!rhs)
|
|
|
|
return lhs;
|
|
|
|
auto function = std::make_shared<ASTFunction>();
|
|
|
|
auto exp_list = std::make_shared<ASTExpressionList>();
|
|
|
|
function->name = "and";
|
|
|
|
function->arguments = exp_list;
|
|
|
|
function->children.push_back(exp_list);
|
|
|
|
exp_list->children.push_back(lhs);
|
|
|
|
exp_list->children.push_back(rhs);
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-17 11:57:02 +00:00
|
|
|
std::vector<UUID> RowPolicyContext::getCurrentPolicyIDs() const
|
|
|
|
{
|
|
|
|
/// We don't lock `mutex` here.
|
2020-02-12 23:59:49 +00:00
|
|
|
auto loaded = map_of_mixed_conditions.load();
|
2019-11-17 11:57:02 +00:00
|
|
|
std::vector<UUID> policy_ids;
|
2020-02-12 23:59:49 +00:00
|
|
|
for (const auto & mixed_conditions : *loaded | boost::adaptors::map_values)
|
2019-11-17 11:57:02 +00:00
|
|
|
boost::range::copy(mixed_conditions.policy_ids, std::back_inserter(policy_ids));
|
|
|
|
return policy_ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<UUID> RowPolicyContext::getCurrentPolicyIDs(const String & database, const String & table_name) const
|
|
|
|
{
|
|
|
|
/// We don't lock `mutex` here.
|
2020-02-12 23:59:49 +00:00
|
|
|
auto loaded = map_of_mixed_conditions.load();
|
|
|
|
auto it = loaded->find({database, table_name});
|
|
|
|
if (it == loaded->end())
|
2019-11-17 11:57:02 +00:00
|
|
|
return {};
|
|
|
|
return it->second.policy_ids;
|
|
|
|
}
|
|
|
|
}
|