add default_database to user

This commit is contained in:
万康 2021-07-06 00:55:07 +08:00
parent 80eaf85301
commit 60f556be2d
11 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<yandex>
<users>
<u_01939_x>
<password></password>
<networks incl="networks" replace="replace">
<ip>::1</ip>
<ip>127.0.0.1</ip>
</networks>
<default_database>system</default_database>
<profile>readonly</profile>
<quota>default</quota>
</u_01939_x>
</users>
</yandex>

View File

@ -22,6 +22,7 @@ struct User : public IAccessEntity
RolesOrUsersSet default_roles = RolesOrUsersSet::AllTag{};
SettingsProfileElements settings;
RolesOrUsersSet grantees = RolesOrUsersSet::AllTag{};
String default_database;
bool equal(const IAccessEntity & other) const override;
std::shared_ptr<IAccessEntity> clone() const override { return cloneImpl<User>(); }

View File

@ -196,6 +196,12 @@ namespace
user->access.revokeGrantOption(AccessType::ALL);
}
String default_database = config.getString(user_config + ".default_database", "");
if (!default_database.empty())
{
user->default_database = default_database;
}
return user;
}

View File

@ -781,6 +781,11 @@ void Context::setUser(const Credentials & credentials, const Poco::Net::SocketAd
current_roles.clear();
use_default_roles = true;
UserPtr new_user = getAccessControlManager().read<User>(new_user_id);
String user_default_database = new_user->default_database;
if (!user_default_database.empty())
setCurrentDatabase(user_default_database);
setSettings(*access->getDefaultSettings());
}

View File

@ -59,6 +59,9 @@ namespace
else if (query.default_roles)
set_default_roles(*query.default_roles);
if (!query.default_database.empty())
user.default_database = query.default_database;
if (override_settings)
user.settings = *override_settings;
else if (query.settings)

View File

@ -82,6 +82,10 @@ namespace
query->grantees->use_keyword_any = true;
}
if (!user.default_database.empty())
{
query->default_database = user.default_database;
}
return query;
}

View File

@ -210,6 +210,11 @@ namespace
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " GRANTEES " << (settings.hilite ? IAST::hilite_none : "");
grantees.format(settings);
}
void formatDefaultDatabase(const String & default_database, const IAST::FormatSettings & settings)
{
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " DEFAULT DATABASE " << (settings.hilite ? IAST::hilite_none : "") << default_database;
}
}
@ -262,6 +267,9 @@ void ASTCreateUserQuery::formatImpl(const FormatSettings & format, FormatState &
if (remove_hosts)
formatHosts("DROP", *remove_hosts, format);
if (!default_database.empty())
formatDefaultDatabase(default_database, format);
if (default_roles)
formatDefaultRoles(*default_roles, format);
@ -270,5 +278,7 @@ void ASTCreateUserQuery::formatImpl(const FormatSettings & format, FormatState &
if (grantees)
formatGrantees(*grantees, format);
}
}

View File

@ -16,6 +16,7 @@ class ASTSettingsProfileElements;
* [NOT IDENTIFIED | IDENTIFIED {[WITH {no_password|plaintext_password|sha256_password|sha256_hash|double_sha1_password|double_sha1_hash}] BY {'password'|'hash'}}|{WITH ldap SERVER 'server_name'}|{WITH kerberos [REALM 'realm']}]
* [HOST {LOCAL | NAME 'name' | REGEXP 'name_regexp' | IP 'address' | LIKE 'pattern'} [,...] | ANY | NONE]
* [DEFAULT ROLE role [,...]]
* [DEFAULT DATABASE 'database']
* [SETTINGS variable [= value] [MIN [=] min_value] [MAX [=] max_value] [READONLY|WRITABLE] | PROFILE 'profile_name'] [,...]
* [GRANTEES {user | role | ANY | NONE} [,...] [EXCEPT {user | role} [,...]]]
*
@ -24,6 +25,7 @@ class ASTSettingsProfileElements;
* [NOT IDENTIFIED | IDENTIFIED {[WITH {no_password|plaintext_password|sha256_password|sha256_hash|double_sha1_password|double_sha1_hash}] BY {'password'|'hash'}}|{WITH ldap SERVER 'server_name'}|{WITH kerberos [REALM 'realm']}]
* [[ADD|DROP] HOST {LOCAL | NAME 'name' | REGEXP 'name_regexp' | IP 'address' | LIKE 'pattern'} [,...] | ANY | NONE]
* [DEFAULT ROLE role [,...] | ALL | ALL EXCEPT role [,...] ]
* [DEFAULT DATABASE 'database']
* [SETTINGS variable [= value] [MIN [=] min_value] [MAX [=] max_value] [READONLY|WRITABLE] | PROFILE 'profile_name'] [,...]
* [GRANTEES {user | role | ANY | NONE} [,...] [EXCEPT {user | role} [,...]]]
*/
@ -51,6 +53,8 @@ public:
std::shared_ptr<ASTSettingsProfileElements> settings;
std::shared_ptr<ASTRolesOrUsersSet> grantees;
String default_database;
String getID(char) const override;
ASTPtr clone() const override;
void formatImpl(const FormatSettings & format, FormatState &, FormatStateStacked) const override;

View File

@ -300,6 +300,17 @@ namespace
return ParserKeyword{"ON"}.ignore(pos, expected) && ASTQueryWithOnCluster::parse(pos, cluster, expected);
});
}
bool parseDefaultDatabase(IParserBase::Pos & pos, Expected & expected, String & default_database)
{
return IParserBase::wrapParseImpl(pos, [&]
{
if (!ParserKeyword{"DEFAULT DATABASE"}.ignore(pos, expected))
return false;
return parseIdentifierOrStringLiteral(pos, expected, default_database);
});
}
}
@ -350,6 +361,7 @@ bool ParserCreateUserQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
std::shared_ptr<ASTSettingsProfileElements> settings;
std::shared_ptr<ASTRolesOrUsersSet> grantees;
String cluster;
String default_database;
while (true)
{
@ -390,6 +402,9 @@ bool ParserCreateUserQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
if (!grantees && parseGrantees(pos, expected, attach_mode, grantees))
continue;
if (default_database.empty() && parseDefaultDatabase(pos, expected, default_database))
continue;
if (alter)
{
if (new_name.empty() && (names->size() == 1) && parseRenameTo(pos, expected, new_name))
@ -445,6 +460,7 @@ bool ParserCreateUserQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
query->default_roles = std::move(default_roles);
query->settings = std::move(settings);
query->grantees = std::move(grantees);
query->default_database = std::move(default_database);
return true;
}

View File

@ -0,0 +1,2 @@
default
db_01939

View File

@ -0,0 +1,19 @@
#!/usr/bin/env bash
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CUR_DIR"/../shell_config.sh
${CLICKHOUSE_CLIENT_BINARY} --query "drop database if exists db_01939"
${CLICKHOUSE_CLIENT_BINARY} --query "create database db_01939"
#create user by sql
${CLICKHOUSE_CLIENT_BINARY} --query "drop user if exists u_01939"
${CLICKHOUSE_CLIENT_BINARY} --query "create user u_01939 default database db_01939"
${CLICKHOUSE_CLIENT_BINARY} --query "SELECT currentDatabase();"
${CLICKHOUSE_CLIENT_BINARY} --user=u_01939 --query "SELECT currentDatabase();"
${CLICKHOUSE_CLIENT_BINARY} --query "drop user u_01939 "
${CLICKHOUSE_CLIENT_BINARY} --query "drop database db_01939"