diff --git a/include/maxscale/users.h b/include/maxscale/users.h index 83ad092c8..202d7adae 100644 --- a/include/maxscale/users.h +++ b/include/maxscale/users.h @@ -99,7 +99,7 @@ bool users_auth(USERS* users, const char* user, const char* password); bool users_find(USERS* users, const char* user); /** - * Check if user is an administrator + * Check if user is an administrator * * @param users The users table * @param user User to check @@ -109,24 +109,13 @@ bool users_find(USERS* users, const char* user); bool users_is_admin(USERS* users, const char* user); /** - * Promote a user to an administrator + * Check if at least one admin account exists * - * @param users The users table - * @param user User to promote + * @param users Users to check * - * @return True if user was found and promoted + * @return True if at least one admin account exists */ -bool users_promote(USERS* users, const char* user); - -/** - * Demote an administrative user to a normal user - * - * @param users The users table - * @param user User to demote - * - * @return True if user was found and demoted - */ -bool users_demote(USERS* users, const char* user); +bool users_have_admin(USERS* users); /** * Dump users as JSON diff --git a/server/core/users.cc b/server/core/users.cc index b5bbfec05..20b174234 100644 --- a/server/core/users.cc +++ b/server/core/users.cc @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -45,6 +46,7 @@ static const char* account_type_to_str(account_type type) return "unknown"; } } + static account_type json_to_account_type(json_t* json) { std::string str = json_string_value(json); @@ -62,6 +64,7 @@ static account_type json_to_account_type(json_t* json) ss_dassert(!true); return ACCOUNT_UNKNOWN; } + struct UserInfo { UserInfo(): @@ -136,6 +139,11 @@ public: return rval; } + bool have_admin() const + { + return std::find_if(m_data.begin(), m_data.end(), is_admin) != m_data.end(); + } + bool check_permissions(std::string user, account_type perm) const { mxs::SpinLockGuard guard(m_lock); @@ -225,6 +233,12 @@ public: } private: + + static bool is_admin(const UserMap::value_type& value) + { + return value.second.permissions == ACCOUNT_ADMIN; + } + void load_json(json_t* json) { // This function is always called in a single-threaded context @@ -254,7 +268,6 @@ private: mxs::SpinLock m_lock; UserMap m_data; - }; } @@ -321,16 +334,10 @@ bool users_is_admin(USERS* users, const char* user) return u->check_permissions(user, ACCOUNT_ADMIN); } -bool users_promote(USERS* users, const char* user) +bool users_have_admin(USERS* users) { Users* u = reinterpret_cast(users); - return u->set_permissions(user, ACCOUNT_ADMIN); -} - -bool users_demote(USERS* users, const char* user) -{ - Users* u = reinterpret_cast(users); - return u->set_permissions(user, ACCOUNT_BASIC); + return u->have_admin(); } void users_diagnostic(DCB* dcb, USERS* users)