CLI: implement user {list,show,perms}

このコミットが含まれているのは:
Albert S. 2021-09-26 17:27:51 +02:00
コミット 95c5c4e155
4個のファイルの変更77行の追加12行の削除

38
cli.cpp
ファイルの表示

@ -1,5 +1,6 @@
#include <map> #include <map>
#include <functional> #include <functional>
#include <iomanip>
#include "cli.h" #include "cli.h"
#include "utils.h" #include "utils.h"
#include "random.h" #include "random.h"
@ -87,14 +88,51 @@ bool CLI::user_rename(const std::vector<std::string> &args)
bool CLI::user_set_perms(const std::vector<std::string> &args) bool CLI::user_set_perms(const std::vector<std::string> &args)
{ {
auto userDao = this->db->createUserDao();
std::string username = args.at(0);
std::string permission_string = args.at(1);
Permissions perms{permission_string};
auto user = userDao->find(username);
if(user)
{
user->permissions = perms;
userDao->save(*user);
user_show({username});
return true;
}
std::cout << "User not found" << std::endl;
return false;
} }
bool CLI::user_list(const std::vector<std::string> &args) bool CLI::user_list(const std::vector<std::string> &args)
{ {
auto userDao = this->db->createUserDao();
QueryOption o;
auto result = userDao->list(o);
for(User &u : result)
{
std::cout << u.login << "\t" << std::string(u.enabled ? "enabled" : "disabled") << "\t"
<< u.permissions.toString() << std::endl;
}
return true;
} }
bool CLI::user_show(const std::vector<std::string> &args) bool CLI::user_show(const std::vector<std::string> &args)
{ {
std::string username = args.at(0);
auto userDao = this->db->createUserDao();
auto user = userDao->find(username);
if(user)
{
std::cout << "Username: " << user->login << std::endl;
std::cout << "Enabled: " << std::string(user->enabled ? "yes" : "no") << std::endl;
std::cout << "Permissions (general): " << user->permissions.toString() << std::endl;
return true;
}
std::cout << "User not found" << std::endl;
return false;
} }
bool CLI::cli_help(const std::vector<std::string> &args) bool CLI::cli_help(const std::vector<std::string> &args)

4
cli.h
ファイルの表示

@ -38,8 +38,8 @@ class CLI
{"changepw", "[user] [password] - changes the password of user", 2, {}, &CLI::user_change_pw}, {"changepw", "[user] [password] - changes the password of user", 2, {}, &CLI::user_change_pw},
{"rename", "[user] [new name] - renames a user", 2, {}, &CLI::user_rename}, {"rename", "[user] [new name] - renames a user", 2, {}, &CLI::user_rename},
{"setperms", "[user] [perms] - sets the permissions of the user", 2, {}, &CLI::user_set_perms}, {"setperms", "[user] [perms] - sets the permissions of the user", 2, {}, &CLI::user_set_perms},
{"list", "- lists users", 2, {}, &CLI::user_list}, {"list", "- lists users", 0, {}, &CLI::user_list},
{"show", "[user] - show detailed information about user", 2, {}, &CLI::user_show}}}, {"show", "[user] - show detailed information about user", 1, {}, &CLI::user_show}}},
&CLI::cli_help}, &CLI::cli_help},
{"exit", {"exit",
"exit cli", "exit cli",

ファイルの表示

@ -20,6 +20,17 @@ SOFTWARE.
*/ */
#include "permissions.h" #include "permissions.h"
static const std::map<std::string, int> permmap = {{"can_read", PERM_CAN_READ},
{"can_edit", PERM_CAN_EDIT},
{"can_page_history", PERM_CAN_PAGE_HISTORY},
{"can_global_history", PERM_CAN_GLOBAL_HISTORY},
{"can_delete", PERM_CAN_DELETE},
{"can_see_page_list", PERM_CAN_SEE_PAGE_LIST},
{"can_create", PERM_CAN_CREATE},
{"can_see_category_list", PERM_CAN_SEE_CATEGORY_LIST},
{"can_see_links_here", PERM_CAN_SEE_LINKS_HERE},
{"can_search", PERM_CAN_SEARCH}};
Permissions::Permissions(int permissions) Permissions::Permissions(int permissions)
{ {
this->permissions = permissions; this->permissions = permissions;
@ -36,3 +47,20 @@ Permissions::Permissions(const std::string &str)
} }
} }
} }
std::string Permissions::toString(int perms)
{
std::string result;
for(auto pair : permmap)
{
if(pair.second & perms)
{
result += pair.first + ",";
}
}
if(result.size() > 0)
{
result.pop_back();
}
return result;
}

ファイルの表示

@ -14,20 +14,12 @@
#include <string> #include <string>
#include <map> #include <map>
class Permissions class Permissions
{ {
private: private:
int permissions = 0; int permissions = 0;
const std::map<std::string, int> permmap = {{"can_read", PERM_CAN_READ},
{"can_edit", PERM_CAN_EDIT},
{"can_page_history", PERM_CAN_PAGE_HISTORY},
{"can_global_history", PERM_CAN_GLOBAL_HISTORY},
{"can_delete", PERM_CAN_DELETE},
{"can_see_page_list", PERM_CAN_SEE_PAGE_LIST},
{"can_create", PERM_CAN_CREATE},
{"can_see_category_list", PERM_CAN_SEE_CATEGORY_LIST},
{"can_see_links_here", PERM_CAN_SEE_LINKS_HERE},
{"can_search", PERM_CAN_SEARCH}};
public: public:
Permissions() Permissions()
@ -102,6 +94,13 @@ class Permissions
{ {
return this->permissions & PERM_CAN_SEE_PAGE_LIST; return this->permissions & PERM_CAN_SEE_PAGE_LIST;
} }
std::string toString() const
{
return Permissions::toString(this->permissions);
}
static std::string toString(int perms);
}; };
#endif // PERMISSIONS_H #endif // PERMISSIONS_H