Permission: move code to ParsePermission() and a table
This commit is contained in:
parent
c5194d0a8f
commit
daa47546c7
@ -35,15 +35,32 @@
|
|||||||
static constexpr char PERMISSION_PASSWORD_CHAR = '@';
|
static constexpr char PERMISSION_PASSWORD_CHAR = '@';
|
||||||
#define PERMISSION_SEPERATOR ","
|
#define PERMISSION_SEPERATOR ","
|
||||||
|
|
||||||
#define PERMISSION_READ_STRING "read"
|
static constexpr struct {
|
||||||
#define PERMISSION_ADD_STRING "add"
|
const char *name;
|
||||||
#define PERMISSION_CONTROL_STRING "control"
|
unsigned value;
|
||||||
#define PERMISSION_ADMIN_STRING "admin"
|
} permission_names[] = {
|
||||||
|
{ "read", PERMISSION_READ },
|
||||||
|
{ "add", PERMISSION_ADD },
|
||||||
|
{ "control", PERMISSION_CONTROL },
|
||||||
|
{ "admin", PERMISSION_ADMIN },
|
||||||
|
{ nullptr, 0 },
|
||||||
|
};
|
||||||
|
|
||||||
static std::map<std::string, unsigned> permission_passwords;
|
static std::map<std::string, unsigned> permission_passwords;
|
||||||
|
|
||||||
static unsigned permission_default;
|
static unsigned permission_default;
|
||||||
|
|
||||||
|
gcc_pure
|
||||||
|
static unsigned
|
||||||
|
ParsePermission(const char *p)
|
||||||
|
{
|
||||||
|
for (auto i = permission_names; i->name != nullptr; ++i)
|
||||||
|
if (strcmp(p, i->name) == 0)
|
||||||
|
return i->value;
|
||||||
|
|
||||||
|
FormatFatalError("unknown permission \"%s\"", p);
|
||||||
|
}
|
||||||
|
|
||||||
static unsigned parsePermissions(const char *string)
|
static unsigned parsePermissions(const char *string)
|
||||||
{
|
{
|
||||||
assert(string != nullptr);
|
assert(string != nullptr);
|
||||||
@ -54,18 +71,7 @@ static unsigned parsePermissions(const char *string)
|
|||||||
tokens = g_strsplit(string, PERMISSION_SEPERATOR, 0);
|
tokens = g_strsplit(string, PERMISSION_SEPERATOR, 0);
|
||||||
for (unsigned i = 0; tokens[i] != NULL; ++i) {
|
for (unsigned i = 0; tokens[i] != NULL; ++i) {
|
||||||
char *temp = tokens[i];
|
char *temp = tokens[i];
|
||||||
|
permission |= ParsePermission(temp);
|
||||||
if (strcmp(temp, PERMISSION_READ_STRING) == 0) {
|
|
||||||
permission |= PERMISSION_READ;
|
|
||||||
} else if (strcmp(temp, PERMISSION_ADD_STRING) == 0) {
|
|
||||||
permission |= PERMISSION_ADD;
|
|
||||||
} else if (strcmp(temp, PERMISSION_CONTROL_STRING) == 0) {
|
|
||||||
permission |= PERMISSION_CONTROL;
|
|
||||||
} else if (strcmp(temp, PERMISSION_ADMIN_STRING) == 0) {
|
|
||||||
permission |= PERMISSION_ADMIN;
|
|
||||||
} else {
|
|
||||||
FormatFatalError("unknown permission \"%s\"", temp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
g_strfreev(tokens);
|
g_strfreev(tokens);
|
||||||
|
Loading…
Reference in New Issue
Block a user