MXS-1354: Add utility functions to User class
Added a utility function for checking if an admin user has been created. Removed unused promote and demote commands which can be replaced with a call to remove and add.
This commit is contained in:
@ -99,7 +99,7 @@ bool users_auth(USERS* users, const char* user, const char* password);
|
|||||||
bool users_find(USERS* users, const char* user);
|
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 users The users table
|
||||||
* @param user User to check
|
* @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);
|
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 users Users to check
|
||||||
* @param user User to promote
|
|
||||||
*
|
*
|
||||||
* @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);
|
bool users_have_admin(USERS* users);
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dump users as JSON
|
* Dump users as JSON
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
#include <new>
|
#include <new>
|
||||||
#include <tr1/unordered_map>
|
#include <tr1/unordered_map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#include <maxscale/users.h>
|
#include <maxscale/users.h>
|
||||||
#include <maxscale/authenticator.h>
|
#include <maxscale/authenticator.h>
|
||||||
@ -45,6 +46,7 @@ static const char* account_type_to_str(account_type type)
|
|||||||
return "unknown";
|
return "unknown";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static account_type json_to_account_type(json_t* json)
|
static account_type json_to_account_type(json_t* json)
|
||||||
{
|
{
|
||||||
std::string str = json_string_value(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);
|
ss_dassert(!true);
|
||||||
return ACCOUNT_UNKNOWN;
|
return ACCOUNT_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct UserInfo
|
struct UserInfo
|
||||||
{
|
{
|
||||||
UserInfo():
|
UserInfo():
|
||||||
@ -136,6 +139,11 @@ public:
|
|||||||
return rval;
|
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
|
bool check_permissions(std::string user, account_type perm) const
|
||||||
{
|
{
|
||||||
mxs::SpinLockGuard guard(m_lock);
|
mxs::SpinLockGuard guard(m_lock);
|
||||||
@ -225,6 +233,12 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
static bool is_admin(const UserMap::value_type& value)
|
||||||
|
{
|
||||||
|
return value.second.permissions == ACCOUNT_ADMIN;
|
||||||
|
}
|
||||||
|
|
||||||
void load_json(json_t* json)
|
void load_json(json_t* json)
|
||||||
{
|
{
|
||||||
// This function is always called in a single-threaded context
|
// This function is always called in a single-threaded context
|
||||||
@ -254,7 +268,6 @@ private:
|
|||||||
|
|
||||||
mxs::SpinLock m_lock;
|
mxs::SpinLock m_lock;
|
||||||
UserMap m_data;
|
UserMap m_data;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -321,16 +334,10 @@ bool users_is_admin(USERS* users, const char* user)
|
|||||||
return u->check_permissions(user, ACCOUNT_ADMIN);
|
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*>(users);
|
Users* u = reinterpret_cast<Users*>(users);
|
||||||
return u->set_permissions(user, ACCOUNT_ADMIN);
|
return u->have_admin();
|
||||||
}
|
|
||||||
|
|
||||||
bool users_demote(USERS* users, const char* user)
|
|
||||||
{
|
|
||||||
Users* u = reinterpret_cast<Users*>(users);
|
|
||||||
return u->set_permissions(user, ACCOUNT_BASIC);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void users_diagnostic(DCB* dcb, USERS* users)
|
void users_diagnostic(DCB* dcb, USERS* users)
|
||||||
|
|||||||
Reference in New Issue
Block a user