aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Access/Common/AccessRightsElement.cpp
blob: 835f414df375df4170e9e505114946094b10008e (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <Access/Common/AccessRightsElement.h>
#include <Common/quoteString.h>
#include <boost/range/algorithm_ext/erase.hpp>


namespace DB
{
namespace
{
    void formatColumnNames(const Strings & columns, String & result)
    {
        result += "(";
        bool need_comma = false;
        for (const auto & column : columns)
        {
            if (need_comma)
                result += ", ";
            need_comma = true;
            result += backQuoteIfNeed(column);
        }
        result += ")";
    }

    void formatONClause(const AccessRightsElement & element, String & result)
    {
        result += "ON ";
        if (element.isGlobalWithParameter())
        {
            if (element.any_parameter)
                result += "*";
            else
                result += backQuoteIfNeed(element.parameter);
        }
        else if (element.any_database)
        {
            result += "*.*";
        }
        else
        {
            if (!element.database.empty())
            {
                result += backQuoteIfNeed(element.database);
                result += ".";
            }
            if (element.any_table)
                result += "*";
            else
                result += backQuoteIfNeed(element.table);
        }
    }

    void formatOptions(bool grant_option, bool is_partial_revoke, String & result)
    {
        if (is_partial_revoke)
        {
            if (grant_option)
                result.insert(0, "REVOKE GRANT OPTION ");
            else
                result.insert(0, "REVOKE ");
        }
        else
        {
            if (grant_option)
                result.insert(0, "GRANT ").append(" WITH GRANT OPTION");
            else
                result.insert(0, "GRANT ");
        }
    }

    void formatAccessFlagsWithColumns(const AccessFlags & access_flags, const Strings & columns, bool any_column, String & result)
    {
        String columns_as_str;
        if (!any_column)
        {
            if (columns.empty())
            {
                result += "USAGE";
                return;
            }
            formatColumnNames(columns, columns_as_str);
        }

        auto keywords = access_flags.toKeywords();
        if (keywords.empty())
        {
            result += "USAGE";
            return;
        }

        bool need_comma = false;
        for (std::string_view keyword : keywords)
        {
            if (need_comma)
                result.append(", ");
            need_comma = true;
            result += keyword;
            result += columns_as_str;
        }
    }

    String toStringImpl(const AccessRightsElement & element, bool with_options)
    {
        String result;
        formatAccessFlagsWithColumns(element.access_flags, element.columns, element.any_column, result);
        result += " ";
        formatONClause(element, result);
        if (with_options)
            formatOptions(element.grant_option, element.is_partial_revoke, result);
        return result;
    }

    String toStringImpl(const AccessRightsElements & elements, bool with_options)
    {
        if (elements.empty())
            return with_options ? "GRANT USAGE ON *.*" : "USAGE ON *.*";

        String result;
        String part;

        for (size_t i = 0; i != elements.size(); ++i)
        {
            const auto & element = elements[i];

            if (!part.empty())
                part += ", ";
            formatAccessFlagsWithColumns(element.access_flags, element.columns, element.any_column, part);

            bool next_element_uses_same_table_and_options = false;
            if (i != elements.size() - 1)
            {
                const auto & next_element = elements[i + 1];
                if (element.sameDatabaseAndTableAndParameter(next_element) && element.sameOptions(next_element))
                {
                    next_element_uses_same_table_and_options = true;
                }
            }

            if (!next_element_uses_same_table_and_options)
            {
                part += " ";
                formatONClause(element, part);
                if (with_options)
                    formatOptions(element.grant_option, element.is_partial_revoke, part);
                if (result.empty())
                    result = std::move(part);
                else
                    result.append(", ").append(part);
                part.clear();
            }
        }

        return result;
    }
}


AccessRightsElement::AccessRightsElement(AccessFlags access_flags_, std::string_view database_)
    : access_flags(access_flags_), database(database_), parameter(database_), any_database(false), any_parameter(false)
{
}

AccessRightsElement::AccessRightsElement(AccessFlags access_flags_, std::string_view database_, std::string_view table_)
    : access_flags(access_flags_), database(database_), table(table_), any_database(false), any_table(false)
{
}

AccessRightsElement::AccessRightsElement(
    AccessFlags access_flags_, std::string_view database_, std::string_view table_, std::string_view column_)
    : access_flags(access_flags_)
    , database(database_)
    , table(table_)
    , columns({String{column_}})
    , any_database(false)
    , any_table(false)
    , any_column(false)
    , any_parameter(false)
{
}

AccessRightsElement::AccessRightsElement(
    AccessFlags access_flags_,
    std::string_view database_,
    std::string_view table_,
    const std::vector<std::string_view> & columns_)
    : access_flags(access_flags_), database(database_), table(table_), any_database(false), any_table(false), any_column(false)
{
    columns.resize(columns_.size());
    for (size_t i = 0; i != columns_.size(); ++i)
        columns[i] = String{columns_[i]};
}

AccessRightsElement::AccessRightsElement(
    AccessFlags access_flags_, std::string_view database_, std::string_view table_, const Strings & columns_)
    : access_flags(access_flags_)
    , database(database_)
    , table(table_)
    , columns(columns_)
    , any_database(false)
    , any_table(false)
    , any_column(false)
    , any_parameter(false)
{
}

void AccessRightsElement::eraseNonGrantable()
{
    if (isGlobalWithParameter() && !any_parameter)
        access_flags &= AccessFlags::allFlagsGrantableOnGlobalWithParameterLevel();
    else if (!any_column)
        access_flags &= AccessFlags::allFlagsGrantableOnColumnLevel();
    else if (!any_table)
        access_flags &= AccessFlags::allFlagsGrantableOnTableLevel();
    else if (!any_database)
        access_flags &= AccessFlags::allFlagsGrantableOnDatabaseLevel();
    else
        access_flags &= AccessFlags::allFlagsGrantableOnGlobalLevel();
}

void AccessRightsElement::replaceEmptyDatabase(const String & current_database)
{
    if (isEmptyDatabase())
        database = current_database;
}

String AccessRightsElement::toString() const { return toStringImpl(*this, true); }
String AccessRightsElement::toStringWithoutOptions() const { return toStringImpl(*this, false); }


bool AccessRightsElements::empty() const { return std::all_of(begin(), end(), [](const AccessRightsElement & e) { return e.empty(); }); }

bool AccessRightsElements::sameDatabaseAndTableAndParameter() const
{
    return (size() < 2) || std::all_of(std::next(begin()), end(), [this](const AccessRightsElement & e) { return e.sameDatabaseAndTableAndParameter(front()); });
}

bool AccessRightsElements::sameDatabaseAndTable() const
{
    return (size() < 2) || std::all_of(std::next(begin()), end(), [this](const AccessRightsElement & e) { return e.sameDatabaseAndTable(front()); });
}

bool AccessRightsElements::sameOptions() const
{
    return (size() < 2) || std::all_of(std::next(begin()), end(), [this](const AccessRightsElement & e) { return e.sameOptions(front()); });
}

void AccessRightsElements::eraseNonGrantable()
{
    boost::range::remove_erase_if(*this, [](AccessRightsElement & element)
    {
        element.eraseNonGrantable();
        return element.empty();
    });
}

void AccessRightsElements::replaceEmptyDatabase(const String & current_database)
{
    for (auto & element : *this)
        element.replaceEmptyDatabase(current_database);
}

String AccessRightsElements::toString() const { return toStringImpl(*this, true); }
String AccessRightsElements::toStringWithoutOptions() const { return toStringImpl(*this, false); }

}