From c4cbe4a94fd63e0da6e291a481b9a9ccc71e7843 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 7 Jul 2009 18:51:35 +0200 Subject: Added add_user command to client & UserBackendMysql --- src/Client/CommandParser.cpp | 3 +++ src/Client/UserCommands.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/Client/UserCommands.h | 2 ++ 3 files changed, 48 insertions(+) (limited to 'src/Client') diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 842be7f..92b1169 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -17,6 +17,8 @@ * with this program. If not, see . */ +#include + #include "CommandParser.h" #include "Application.h" #include "InformationManager.h" @@ -49,6 +51,7 @@ const CommandParser::Command CommandParser::commands[] = { {{"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}, {{"list_group_users", "group_users", 0}, "list_group_users gid", "List group's users", "List the users that are members of the group.", &UserCommands::listGroupUsersCommand}, + {{"add_user", 0}, "add_user uid gid username full_name", "Add a new user", "Adds a new user with the given info to the account database.", &UserCommands::addUserCommand}, {{"exit", "quit", 0}, "exit", "Close the connection and quit the client", "Close the connection and quit the client.", &CommandParser::exitCommand}, {{0}, 0, 0, 0, 0} }; diff --git a/src/Client/UserCommands.cpp b/src/Client/UserCommands.cpp index fec2a33..9fcaa36 100644 --- a/src/Client/UserCommands.cpp +++ b/src/Client/UserCommands.cpp @@ -17,6 +17,8 @@ * with this program. If not, see . */ +#include + #include "UserCommands.h" #include "Application.h" #include "CommandParser.h" @@ -221,5 +223,46 @@ void UserCommands::listGroupUsersCommand(CommandParser *commandParser, const std } } +void UserCommands::addUserCommand(CommandParser *commandParser, const std::vector &args) { + if(args.size() < 5) { + std::cerr << args[0] << ": Too few arguments." << std::endl; + commandParser->printUsage("add_user"); + return; + } + if(args.size() > 5) { + std::cerr << args[0] << ": Too many arguments." << std::endl; + commandParser->printUsage("add_user"); + return; + } + + char *endptr; + unsigned long uid = std::strtoul(args[1].c_str(), &endptr, 10); + if(args[1].empty() || *endptr != '\0') { + std::cerr << args[0] << ": Unable to parse user id." << std::endl; + commandParser->printUsage("add_user"); + 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 group id." << std::endl; + commandParser->printUsage("add_user"); + return; + } + + try { + Common::UserInfo user(uid, args[3]); + user.setGid(gid); + user.setFullName(args[4]); + + commandParser->application->getUserManager()->addUser(user); + + std::cout << "User added." << std::endl; + } + catch(Core::Exception e) { + std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + } +} + } } diff --git a/src/Client/UserCommands.h b/src/Client/UserCommands.h index 0f56a40..6e184b0 100644 --- a/src/Client/UserCommands.h +++ b/src/Client/UserCommands.h @@ -40,6 +40,8 @@ class UserCommands { static void listGroupsCommand(CommandParser *commandParser, const std::vector &args); static void groupInfoCommand(CommandParser *commandParser, const std::vector &args); static void listGroupUsersCommand(CommandParser *commandParser, const std::vector &args); + + static void addUserCommand(CommandParser *commandParser, const std::vector &args); }; } -- cgit v1.2.3