aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/currentRoles.cpp
blob: 45d2000d0885cf5e457e45eccefc05163f548376 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <base/sort.h>
#include <Functions/IFunction.h>
#include <Functions/FunctionFactory.h>
#include <Interpreters/Context.h>
#include <Access/AccessControl.h>
#include <Access/EnabledRolesInfo.h>
#include <Access/User.h>
#include <Columns/ColumnArray.h>
#include <Columns/ColumnConst.h>
#include <Columns/ColumnString.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeArray.h>


namespace DB
{

namespace
{
    enum class Kind
    {
        CURRENT_ROLES,
        ENABLED_ROLES,
        DEFAULT_ROLES,
    };

    template <Kind kind>
    class FunctionCurrentRoles : public IFunction
    {
    public:
        static constexpr auto name = (kind == Kind::CURRENT_ROLES) ? "currentRoles" : ((kind == Kind::ENABLED_ROLES) ? "enabledRoles" : "defaultRoles");
        static FunctionPtr create(const ContextPtr & context) { return std::make_shared<FunctionCurrentRoles>(context); }

        bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }

        String getName() const override { return name; }

        explicit FunctionCurrentRoles(const ContextPtr & context)
        {
            if constexpr (kind == Kind::CURRENT_ROLES)
            {
                role_names = context->getRolesInfo()->getCurrentRolesNames();
            }
            else if constexpr (kind == Kind::ENABLED_ROLES)
            {
                role_names = context->getRolesInfo()->getEnabledRolesNames();
            }
            else
            {
                static_assert(kind == Kind::DEFAULT_ROLES);
                const auto & manager = context->getAccessControl();
                auto user = context->getUser();
                role_names = manager.tryReadNames(user->granted_roles.findGranted(user->default_roles));
            }

            /// We sort the names because the result of the function should not depend on the order of UUIDs.
            ::sort(role_names.begin(), role_names.end());
        }

        size_t getNumberOfArguments() const override { return 0; }
        bool isDeterministic() const override { return false; }

        DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
        {
            return std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>());
        }

        ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
        {
            auto col_res = ColumnArray::create(ColumnString::create());
            ColumnString & res_strings = typeid_cast<ColumnString &>(col_res->getData());
            ColumnArray::Offsets & res_offsets = col_res->getOffsets();
            for (const String & role_name : role_names)
                res_strings.insertData(role_name.data(), role_name.length());
            res_offsets.push_back(res_strings.size());
            return ColumnConst::create(std::move(col_res), input_rows_count);
        }

    private:
        Strings role_names;
    };
}

REGISTER_FUNCTION(CurrentRoles)
{
    factory.registerFunction<FunctionCurrentRoles<Kind::CURRENT_ROLES>>();
    factory.registerFunction<FunctionCurrentRoles<Kind::ENABLED_ROLES>>();
    factory.registerFunction<FunctionCurrentRoles<Kind::DEFAULT_ROLES>>();
}

}