2020-05-30 20:10:45 +00:00
|
|
|
#include <Parsers/ParserRolesOrUsersSet.h>
|
2019-12-01 22:01:05 +00:00
|
|
|
#include <Parsers/CommonParsers.h>
|
2020-02-21 19:27:12 +00:00
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
2020-05-30 20:10:45 +00:00
|
|
|
#include <Parsers/ASTRolesOrUsersSet.h>
|
2020-02-04 22:37:04 +00:00
|
|
|
#include <Parsers/parseUserName.h>
|
2020-06-05 21:31:37 +00:00
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
2019-12-01 22:01:05 +00:00
|
|
|
#include <boost/range/algorithm/find.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-10 15:24:33 +00:00
|
|
|
namespace
|
2019-12-01 22:01:05 +00:00
|
|
|
{
|
2021-02-26 22:37:00 +00:00
|
|
|
bool parseNameOrID(IParserBase::Pos & pos, Expected & expected, bool id_mode, String & res)
|
2020-02-21 19:27:12 +00:00
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
2020-06-05 21:31:37 +00:00
|
|
|
if (!id_mode)
|
2020-02-21 19:27:12 +00:00
|
|
|
return parseRoleName(pos, expected, res);
|
|
|
|
|
|
|
|
if (!ParserKeyword{"ID"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
if (!ParserToken(TokenType::OpeningRoundBracket).ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
ASTPtr ast;
|
|
|
|
if (!ParserStringLiteral{}.parse(pos, ast, expected))
|
|
|
|
return false;
|
|
|
|
String id = ast->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
if (!ParserToken(TokenType::ClosingRoundBracket).ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
res = std::move(id);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseBeforeExcept(
|
|
|
|
IParserBase::Pos & pos,
|
|
|
|
Expected & expected,
|
|
|
|
bool id_mode,
|
2020-05-30 20:10:45 +00:00
|
|
|
bool allow_all,
|
2021-03-11 13:44:00 +00:00
|
|
|
bool allow_any,
|
2020-06-05 21:31:37 +00:00
|
|
|
bool allow_current_user,
|
2020-02-21 19:27:12 +00:00
|
|
|
bool & all,
|
2021-02-26 22:37:00 +00:00
|
|
|
Strings & names,
|
2020-02-21 19:27:12 +00:00
|
|
|
bool & current_user)
|
2019-12-01 22:01:05 +00:00
|
|
|
{
|
2020-06-05 21:31:37 +00:00
|
|
|
bool res_all = false;
|
|
|
|
Strings res_names;
|
2021-02-26 22:37:00 +00:00
|
|
|
bool res_current_user = false;
|
|
|
|
Strings res_with_roles_names;
|
2020-06-05 21:31:37 +00:00
|
|
|
|
|
|
|
auto parse_element = [&]
|
2019-12-01 22:01:05 +00:00
|
|
|
{
|
2020-06-05 21:31:37 +00:00
|
|
|
if (ParserKeyword{"NONE"}.ignore(pos, expected))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (allow_all && ParserKeyword{"ALL"}.ignore(pos, expected))
|
2019-12-01 22:01:05 +00:00
|
|
|
{
|
2020-06-05 21:31:37 +00:00
|
|
|
res_all = true;
|
|
|
|
return true;
|
2019-12-01 22:01:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 13:44:00 +00:00
|
|
|
if (allow_any && ParserKeyword{"ANY"}.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
res_all = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-05 21:31:37 +00:00
|
|
|
if (allow_current_user && parseCurrentUserTag(pos, expected))
|
|
|
|
{
|
|
|
|
res_current_user = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
String name;
|
2021-02-26 22:37:00 +00:00
|
|
|
if (parseNameOrID(pos, expected, id_mode, name))
|
2020-06-05 21:31:37 +00:00
|
|
|
{
|
|
|
|
res_names.emplace_back(std::move(name));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!ParserList::parseUtil(pos, expected, parse_element, false))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
names = std::move(res_names);
|
|
|
|
current_user = res_current_user;
|
2021-02-26 22:37:00 +00:00
|
|
|
all = res_all;
|
2020-06-05 21:31:37 +00:00
|
|
|
return true;
|
2019-12-01 22:01:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 19:27:12 +00:00
|
|
|
bool parseExceptAndAfterExcept(
|
|
|
|
IParserBase::Pos & pos,
|
|
|
|
Expected & expected,
|
|
|
|
bool id_mode,
|
2020-06-05 21:31:37 +00:00
|
|
|
bool allow_current_user,
|
2020-02-21 19:27:12 +00:00
|
|
|
Strings & except_names,
|
|
|
|
bool & except_current_user)
|
2019-12-01 22:01:05 +00:00
|
|
|
{
|
2021-02-26 22:37:00 +00:00
|
|
|
return IParserBase::wrapParseImpl(pos, [&] {
|
2020-02-10 15:24:33 +00:00
|
|
|
if (!ParserKeyword{"EXCEPT"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
2020-06-05 21:31:37 +00:00
|
|
|
bool unused;
|
2021-03-11 13:44:00 +00:00
|
|
|
return parseBeforeExcept(pos, expected, id_mode, false, false, allow_current_user, unused, except_names, except_current_user);
|
2020-02-10 15:24:33 +00:00
|
|
|
});
|
2019-12-01 22:01:05 +00:00
|
|
|
}
|
2020-02-10 15:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-30 20:10:45 +00:00
|
|
|
bool ParserRolesOrUsersSet::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2020-02-10 15:24:33 +00:00
|
|
|
{
|
2021-02-26 22:37:00 +00:00
|
|
|
bool all = false;
|
2020-02-10 15:24:33 +00:00
|
|
|
Strings names;
|
|
|
|
bool current_user = false;
|
|
|
|
Strings except_names;
|
|
|
|
bool except_current_user = false;
|
|
|
|
|
2021-03-11 13:44:00 +00:00
|
|
|
if (!parseBeforeExcept(pos, expected, id_mode, allow_all, allow_any, allow_current_user, all, names, current_user))
|
2020-02-10 15:24:33 +00:00
|
|
|
return false;
|
|
|
|
|
2020-05-30 20:10:45 +00:00
|
|
|
parseExceptAndAfterExcept(pos, expected, id_mode, allow_current_user, except_names, except_current_user);
|
2020-02-10 15:24:33 +00:00
|
|
|
|
|
|
|
if (all)
|
|
|
|
names.clear();
|
2019-12-01 22:01:05 +00:00
|
|
|
|
2020-05-30 20:10:45 +00:00
|
|
|
auto result = std::make_shared<ASTRolesOrUsersSet>();
|
2020-02-10 15:24:33 +00:00
|
|
|
result->names = std::move(names);
|
2019-12-01 22:01:05 +00:00
|
|
|
result->current_user = current_user;
|
2020-02-10 15:24:33 +00:00
|
|
|
result->all = all;
|
|
|
|
result->except_names = std::move(except_names);
|
2019-12-01 22:01:05 +00:00
|
|
|
result->except_current_user = except_current_user;
|
2021-02-26 22:37:00 +00:00
|
|
|
result->allow_users = allow_users;
|
|
|
|
result->allow_roles = allow_roles;
|
2020-02-21 19:27:12 +00:00
|
|
|
result->id_mode = id_mode;
|
2021-03-11 13:44:00 +00:00
|
|
|
result->use_keyword_any = all && allow_any && !allow_all;
|
2019-12-01 22:01:05 +00:00
|
|
|
node = result;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|