diff options
-rw-r--r-- | src/Client/CommandParser.cpp | 5 | ||||
-rw-r--r-- | src/Client/UserCommands.cpp | 97 | ||||
-rw-r--r-- | src/Client/UserCommands.h | 4 | ||||
-rw-r--r-- | src/mad-server.conf | 7 | ||||
-rw-r--r-- | src/modules/UserBackendMysql/UserBackendMysql.cpp | 71 | ||||
-rw-r--r-- | src/modules/UserBackendMysql/UserBackendMysql.h | 5 |
6 files changed, 188 insertions, 1 deletions
diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 838c831..b06f4b4 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -47,9 +47,12 @@ const CommandParser::Command CommandParser::commands[] = { {{"user_info", "user", 0}, "user_info uid|name", "Search for a user id", "Search for a user.", &UserCommands::userInfoCommand}, {{"list_groups", "groups", 0}, "list_groups", "Show the user group database", "Show the user group database.", &UserCommands::listGroupsCommand}, {{"group_info", "group", 0}, "group_info gid|name", "Search for a group id", "Search for a group.", &UserCommands::groupInfoCommand}, - {{"add_user", 0}, "add_user uid gid username full_name", "Add a new user", "Add a new user with the given info to the account database.", &UserCommands::addUserCommand}, + {{"add_user", 0}, "add_user uid gid username full_name", "Add a new user", "Add a new user with the given information to the account database.", &UserCommands::addUserCommand}, {{"update_user", 0}, "update_user uid new_uid gid username full_name", "Update a user", "Update user data in the account database.", &UserCommands::updateUserCommand}, {{"delete_user", 0}, "delete_user uid", "Delete user", "Delete a user from the account database.", &UserCommands::deleteUserCommand}, + {{"add_group", 0}, "add_group gid name", "Add a new group", "Add a new user group with the given name to the account database.", &UserCommands::addGroupCommand}, + {{"update_group", 0}, "update_group gid new_gid name", "Update a group", "Update group data in the account database.", &UserCommands::updateGroupCommand}, + {{"delete_group", 0}, "delete_group did", "Delete group", "Delete a user group from the account database.", &UserCommands::deleteGroupCommand}, {{"add_user_to_group", 0}, "add_user_to_group uid gid", "Add a user to a group", "Add a user to a group.", &UserCommands::addUserToGroupCommand}, {{"delete_user_from_group", 0}, "delete_user_from_group uid gid", "Remove a user from a group", "Remove a user from a group.", &UserCommands::deleteUserFromGroupCommand}, {{"exit", "quit", 0}, "exit", "Close the connection and quit the client", "Close the connection and quit the client.", &CommandParser::exitCommand}, diff --git a/src/Client/UserCommands.cpp b/src/Client/UserCommands.cpp index 43131c7..b93de94 100644 --- a/src/Client/UserCommands.cpp +++ b/src/Client/UserCommands.cpp @@ -320,6 +320,103 @@ void UserCommands::deleteUserCommand(CommandParser *commandParser, const std::ve } } +void UserCommands::addGroupCommand(CommandParser *commandParser, const std::vector<std::string> &args) { + if(args.size() < 3) { + std::cerr << args[0] << ": Too few arguments." << std::endl; + commandParser->printUsage("add_group"); + return; + } + if(args.size() > 3) { + std::cerr << args[0] << ": Too many arguments." << std::endl; + commandParser->printUsage("add_group"); + return; + } + + char *endptr; + unsigned long gid = std::strtoul(args[1].c_str(), &endptr, 10); + if(args[2].empty() || *endptr != '\0') { + std::cerr << args[0] << ": Unable to parse group id." << std::endl; + commandParser->printUsage("add_group"); + return; + } + + try { + commandParser->application->getUserManager()->addGroup(Common::GroupInfo(gid, args[2])); + + std::cout << "Group added." << std::endl; + } + catch(Core::Exception e) { + std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + } +} + +void UserCommands::updateGroupCommand(CommandParser *commandParser, const std::vector<std::string> &args) { + if(args.size() < 4) { + std::cerr << args[0] << ": Too few arguments." << std::endl; + commandParser->printUsage("update_group"); + return; + } + if(args.size() > 4) { + std::cerr << args[0] << ": Too many arguments." << std::endl; + commandParser->printUsage("update_group"); + return; + } + + char *endptr; + unsigned long origGid = std::strtoul(args[1].c_str(), &endptr, 10); + if(args[1].empty() || *endptr != '\0') { + std::cerr << args[0] << ": Unable to parse the old group id." << std::endl; + commandParser->printUsage("update_group"); + return; + } + + unsigned long gid = std::strtoul(args[2].c_str(), &endptr, 10); + if(args[2].empty() || *endptr != '\0') { + std::cerr << args[0] << ": Unable to parse the new group id." << std::endl; + commandParser->printUsage("update_group"); + return; + } + + try { + commandParser->application->getUserManager()->updateGroup(origGid, Common::GroupInfo(gid, args[3])); + + std::cout << "Group updated." << std::endl; + } + catch(Core::Exception e) { + std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + } +} + +void UserCommands::deleteGroupCommand(CommandParser *commandParser, const std::vector<std::string> &args) { + if(args.size() < 2) { + std::cerr << args[0] << ": No group id given." << std::endl; + commandParser->printUsage("delete_group"); + return; + } + if(args.size() > 2) { + std::cerr << args[0] << ": Too many arguments." << std::endl; + commandParser->printUsage("delete_group"); + return; + } + + char *endptr; + unsigned long gid = std::strtoul(args[1].c_str(), &endptr, 10); + if(args[1].empty() || *endptr != '\0') { + std::cerr << args[0] << ": Unable to parse group id." << std::endl; + commandParser->printUsage("delete_group"); + return; + } + + try { + commandParser->application->getUserManager()->deleteGroup(gid); + + std::cout << "Group deleted." << std::endl; + } + catch(Core::Exception e) { + std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + } +} + void UserCommands::addUserToGroupCommand(CommandParser *commandParser, const std::vector<std::string> &args) { if(args.size() < 3) { std::cerr << args[0] << ": Too few arguments." << std::endl; diff --git a/src/Client/UserCommands.h b/src/Client/UserCommands.h index d5793fa..62d42c1 100644 --- a/src/Client/UserCommands.h +++ b/src/Client/UserCommands.h @@ -46,6 +46,10 @@ class UserCommands { static void updateUserCommand(CommandParser *commandParser, const std::vector<std::string> &args); static void deleteUserCommand(CommandParser *commandParser, const std::vector<std::string> &args); + static void addGroupCommand(CommandParser *commandParser, const std::vector<std::string> &args); + static void updateGroupCommand(CommandParser *commandParser, const std::vector<std::string> &args); + static void deleteGroupCommand(CommandParser *commandParser, const std::vector<std::string> &args); + static void addUserToGroupCommand(CommandParser *commandParser, const std::vector<std::string> &args); static void deleteUserFromGroupCommand(CommandParser *commandParser, const std::vector<std::string> &args); }; diff --git a/src/mad-server.conf b/src/mad-server.conf index c19fb0f..58a5116 100644 --- a/src/mad-server.conf +++ b/src/mad-server.conf @@ -31,14 +31,21 @@ UserManager { ListGroups "SELECT id, name FROM groups" ListUserGroups "SELECT gid FROM usergroups WHERE uid = {UID}" ListGroupUsers "SELECT uid FROM usergroups WHERE gid = {GID}" + UserById "SELECT id, gid, username, fullname FROM users WHERE id = {UID}" UserByName "SELECT id, gid, username, fullname FROM users WHERE username = {USER}" GroupById "SELECT id, name FROM groups WHERE id = {GID}" GroupByName "SELECT id, name FROM groups WHERE name = {GROUP}" UserGroupTable "SELECT uid, gid FROM usergroups" + AddUser "INSERT INTO users (id, gid, username, fullname) VALUES ({UID}, {GID}, {USER}, {FULL_NAME})" UpdateUser "UPDATE users SET id = {UID}, gid = {GID}, username = {USER}, fullname = {FULL_NAME} WHERE id = {ORIG_UID}" DeleteUser "DELETE FROM users WHERE id = {UID}" + + AddGroup "INSERT INTO groups (id, name) VALUES ({GID}, {GROUP})" + UpdateGroup "UPDATE groups SET id = {GID}, name = {GROUP} WHERE id = {ORIG_GID}" + DeleteGroup "DELETE FROM groups WHERE id = {GID}" + AddUserToGroup "INSERT INTO usergroups (uid, gid) VALUES ({UID}, {GID})" DeleteUserFromGroup "DELETE FROM usergroups WHERE uid = {UID} AND gid = {GID}" } diff --git a/src/modules/UserBackendMysql/UserBackendMysql.cpp b/src/modules/UserBackendMysql/UserBackendMysql.cpp index 3ef44d1..f2729ab 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.cpp +++ b/src/modules/UserBackendMysql/UserBackendMysql.cpp @@ -128,6 +128,18 @@ bool UserBackendMysql::handleConfigEntry(const Core::ConfigEntry &entry, bool /* if(entry[4].empty()) queryDeleteUser = entry[3][0]; } + else if(entry[3].getKey().matches("AddGroup")) { + if(entry[4].empty()) + queryAddGroup = entry[3][0]; + } + else if(entry[3].getKey().matches("UpdateGroup")) { + if(entry[4].empty()) + queryUpdateGroup = entry[3][0]; + } + else if(entry[3].getKey().matches("DeleteGroup")) { + if(entry[4].empty()) + queryDeleteGroup = entry[3][0]; + } else if(entry[3].getKey().matches("AddUserToGroup")) { if(entry[4].empty()) queryAddUserToGroup = entry[3][0]; @@ -538,6 +550,65 @@ void UserBackendMysql::deleteUser(unsigned long uid) throw(Core::Exception) { lastUpdate = boost::posix_time::microsec_clock::universal_time(); } +void UserBackendMysql::addGroup(const Common::GroupInfo &groupInfo) throw(Core::Exception) { + application->getThreadManager()->detach(); + + boost::lock_guard<boost::mutex> lock(mutex); + + ArgumentMap args; + args.insert(std::make_pair("GID", groupInfo.getGid())); + args.insert(std::make_pair("GROUP", groupInfo.getName())); + + Result result = query(queryAddGroup, args); + + if(result.getErrno()) { + if(result.getErrno() == ER_DUP_ENTRY) + throw Core::Exception(Core::Exception::DUPLICATE_ENTRY); + else + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + } + + lastUpdate = boost::posix_time::microsec_clock::universal_time(); +} + +void UserBackendMysql::updateGroup(unsigned long gid, const Common::GroupInfo &groupInfo) throw(Core::Exception) { + application->getThreadManager()->detach(); + + boost::lock_guard<boost::mutex> lock(mutex); + + ArgumentMap args; + args.insert(std::make_pair("ORIG_GID", gid)); + args.insert(std::make_pair("GID", groupInfo.getGid())); + args.insert(std::make_pair("GROUP", groupInfo.getName())); + + Result result = query(queryUpdateGroup, args); + + if(result.getErrno()) { + if(result.getErrno() == ER_DUP_ENTRY) + throw Core::Exception(Core::Exception::DUPLICATE_ENTRY); + else + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + } + + lastUpdate = boost::posix_time::microsec_clock::universal_time(); +} + +void UserBackendMysql::deleteGroup(unsigned long gid) throw(Core::Exception) { + application->getThreadManager()->detach(); + + boost::lock_guard<boost::mutex> lock(mutex); + + ArgumentMap args; + args.insert(std::make_pair("GID", gid)); + + Result result = query(queryDeleteGroup, args); + + if(result.getErrno()) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + + lastUpdate = boost::posix_time::microsec_clock::universal_time(); +} + void UserBackendMysql::addUserToGroup(unsigned long uid, unsigned long gid) throw(Core::Exception) { application->getThreadManager()->detach(); diff --git a/src/modules/UserBackendMysql/UserBackendMysql.h b/src/modules/UserBackendMysql/UserBackendMysql.h index 85bc678..c3848c0 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.h +++ b/src/modules/UserBackendMysql/UserBackendMysql.h @@ -97,6 +97,7 @@ class UserBackendMysql : public Common::UserDBBackend, private Core::Configurabl std::string queryGroupById, queryGroupByName; std::string queryUserGroupTable; std::string queryAddUser, queryUpdateUser, queryDeleteUser; + std::string queryAddGroup, queryUpdateGroup, queryDeleteGroup; std::string queryAddUserToGroup, queryDeleteUserFromGroup; MYSQL *mysql; @@ -125,6 +126,10 @@ class UserBackendMysql : public Common::UserDBBackend, private Core::Configurabl virtual void updateUser(unsigned long uid, const Common::UserInfo &userInfo) throw(Core::Exception); virtual void deleteUser(unsigned long uid) throw(Core::Exception); + virtual void addGroup(const Common::GroupInfo &groupInfo) throw(Core::Exception); + virtual void updateGroup(unsigned long gid, const Common::GroupInfo &groupInfo) throw(Core::Exception); + virtual void deleteGroup(unsigned long gid) throw(Core::Exception); + virtual void addUserToGroup(unsigned long uid, unsigned long gid) throw(Core::Exception); virtual void deleteUserFromGroup(unsigned long uid, unsigned long gid) throw(Core::Exception); |