ClickHouse
99 строк · 3.4 Кб
1#include <base/sort.h>
2#include <Functions/IFunction.h>
3#include <Functions/FunctionFactory.h>
4#include <Interpreters/Context.h>
5#include <Access/AccessControl.h>
6#include <Access/EnabledRolesInfo.h>
7#include <Access/User.h>
8#include <Columns/ColumnArray.h>
9#include <Columns/ColumnConst.h>
10#include <Columns/ColumnString.h>
11#include <DataTypes/DataTypeString.h>
12#include <DataTypes/DataTypeArray.h>
13
14
15namespace DB
16{
17
18namespace
19{
20enum class Kind
21{
22CURRENT_ROLES,
23ENABLED_ROLES,
24DEFAULT_ROLES,
25};
26
27template <Kind kind>
28class FunctionCurrentRoles : public IFunction
29{
30public:
31static constexpr auto name = (kind == Kind::CURRENT_ROLES) ? "currentRoles" : ((kind == Kind::ENABLED_ROLES) ? "enabledRoles" : "defaultRoles");
32static FunctionPtr create(const ContextPtr & context) { return std::make_shared<FunctionCurrentRoles>(context); }
33
34bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
35
36String getName() const override { return name; }
37
38explicit FunctionCurrentRoles(const ContextPtr & context_)
39: context(context_)
40{}
41
42size_t getNumberOfArguments() const override { return 0; }
43bool isDeterministic() const override { return false; }
44
45DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
46{
47return std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>());
48}
49
50ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
51{
52std::call_once(initialized_flag, [&]{ initialize(); });
53
54auto col_res = ColumnArray::create(ColumnString::create());
55ColumnString & res_strings = typeid_cast<ColumnString &>(col_res->getData());
56ColumnArray::Offsets & res_offsets = col_res->getOffsets();
57for (const String & role_name : role_names)
58res_strings.insertData(role_name.data(), role_name.length());
59res_offsets.push_back(res_strings.size());
60return ColumnConst::create(std::move(col_res), input_rows_count);
61}
62
63private:
64void initialize() const
65{
66if constexpr (kind == Kind::CURRENT_ROLES)
67{
68role_names = context->getRolesInfo()->getCurrentRolesNames();
69}
70else if constexpr (kind == Kind::ENABLED_ROLES)
71{
72role_names = context->getRolesInfo()->getEnabledRolesNames();
73}
74else
75{
76static_assert(kind == Kind::DEFAULT_ROLES);
77const auto & manager = context->getAccessControl();
78auto user = context->getUser();
79role_names = manager.tryReadNames(user->granted_roles.findGranted(user->default_roles));
80}
81
82/// We sort the names because the result of the function should not depend on the order of UUIDs.
83::sort(role_names.begin(), role_names.end());
84}
85
86mutable std::once_flag initialized_flag;
87ContextPtr context;
88mutable Strings role_names;
89};
90}
91
92REGISTER_FUNCTION(CurrentRoles)
93{
94factory.registerFunction<FunctionCurrentRoles<Kind::CURRENT_ROLES>>();
95factory.registerFunction<FunctionCurrentRoles<Kind::ENABLED_ROLES>>();
96factory.registerFunction<FunctionCurrentRoles<Kind::DEFAULT_ROLES>>();
97}
98
99}
100