2020-06-10 23:08:37 +00:00
|
|
|
#include <Interpreters/InterpreterShowAccessQuery.h>
|
|
|
|
|
|
|
|
#include <Parsers/formatAST.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/InterpreterShowCreateAccessEntityQuery.h>
|
|
|
|
#include <Interpreters/InterpreterShowGrantsQuery.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
2021-09-15 19:35:48 +00:00
|
|
|
#include <Processors/Sources/SourceFromSingleChunk.h>
|
2020-06-10 23:08:37 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Access/AccessFlags.h>
|
|
|
|
#include <Access/AccessControlManager.h>
|
2021-06-15 19:55:21 +00:00
|
|
|
#include <common/range.h>
|
2020-06-10 23:08:37 +00:00
|
|
|
#include <boost/range/algorithm/sort.hpp>
|
|
|
|
#include <boost/range/algorithm_ext/push_back.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
using EntityType = IAccessEntity::Type;
|
|
|
|
|
|
|
|
|
|
|
|
BlockIO InterpreterShowAccessQuery::execute()
|
|
|
|
{
|
|
|
|
BlockIO res;
|
2021-09-15 19:35:48 +00:00
|
|
|
res.pipeline = executeImpl();
|
2020-06-10 23:08:37 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-15 19:35:48 +00:00
|
|
|
QueryPipeline InterpreterShowAccessQuery::executeImpl() const
|
2020-06-10 23:08:37 +00:00
|
|
|
{
|
|
|
|
/// Build a create query.
|
|
|
|
ASTs queries = getCreateAndGrantQueries();
|
|
|
|
|
|
|
|
/// Build the result column.
|
|
|
|
MutableColumnPtr column = ColumnString::create();
|
2020-11-09 16:05:40 +00:00
|
|
|
WriteBufferFromOwnString buf;
|
2020-06-10 23:08:37 +00:00
|
|
|
for (const auto & query : queries)
|
|
|
|
{
|
2020-11-09 16:05:40 +00:00
|
|
|
buf.restart();
|
|
|
|
formatAST(*query, buf, false, true);
|
|
|
|
column->insert(buf.str());
|
2020-06-10 23:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String desc = "ACCESS";
|
2021-09-15 19:35:48 +00:00
|
|
|
return QueryPipeline(std::make_shared<SourceFromSingleChunk>(Block{{std::move(column), std::make_shared<DataTypeString>(), desc}}));
|
2020-06-10 23:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<AccessEntityPtr> InterpreterShowAccessQuery::getEntities() const
|
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
const auto & access_control = getContext()->getAccessControlManager();
|
|
|
|
getContext()->checkAccess(AccessType::SHOW_ACCESS);
|
2020-06-10 23:08:37 +00:00
|
|
|
|
|
|
|
std::vector<AccessEntityPtr> entities;
|
2021-06-15 19:55:21 +00:00
|
|
|
for (auto type : collections::range(EntityType::MAX))
|
2020-06-10 23:08:37 +00:00
|
|
|
{
|
|
|
|
auto ids = access_control.findAll(type);
|
|
|
|
for (const auto & id : ids)
|
|
|
|
{
|
|
|
|
if (auto entity = access_control.tryRead(id))
|
|
|
|
entities.push_back(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::range::sort(entities, IAccessEntity::LessByTypeAndName{});
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASTs InterpreterShowAccessQuery::getCreateAndGrantQueries() const
|
|
|
|
{
|
|
|
|
auto entities = getEntities();
|
2021-04-10 23:33:54 +00:00
|
|
|
const auto & access_control = getContext()->getAccessControlManager();
|
2020-06-10 23:08:37 +00:00
|
|
|
|
|
|
|
ASTs create_queries, grant_queries;
|
|
|
|
for (const auto & entity : entities)
|
|
|
|
{
|
|
|
|
create_queries.push_back(InterpreterShowCreateAccessEntityQuery::getCreateQuery(*entity, access_control));
|
2020-11-11 10:09:48 +00:00
|
|
|
if (entity->isTypeOf(EntityType::USER) || entity->isTypeOf(EntityType::ROLE))
|
2020-06-10 23:08:37 +00:00
|
|
|
boost::range::push_back(grant_queries, InterpreterShowGrantsQuery::getGrantQueries(*entity, access_control));
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTs result = std::move(create_queries);
|
|
|
|
boost::range::push_back(result, std::move(grant_queries));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|