diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2013-05-27 10:57:28 +0100 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2013-05-27 10:57:28 +0100 |
commit | 6f07f66dd99f0fdc97daa402483a5838dd6d001a (patch) | |
tree | 626c2e9111cf7fdab9f8d918a5e95e742b10e363 | |
parent | 784ebcfb4d749b4d0cf36b3b3bf3bc839a013ee0 (diff) | |
download | gitano-6f07f66dd99f0fdc97daa402483a5838dd6d001a.tar.gz |
ADMINCOMMAND: Add user rename command
Add a rename command for users.
-rw-r--r-- | lib/gitano/admincommand.lua | 57 |
1 files changed, 50 insertions, 7 deletions
diff --git a/lib/gitano/admincommand.lua b/lib/gitano/admincommand.lua index 7d62b4b..0b7075a 100644 --- a/lib/gitano/admincommand.lua +++ b/lib/gitano/admincommand.lua @@ -103,6 +103,7 @@ usage: user [list] user del <username> [confirm token] user email <username> <email> user name <username> <real name> + user rename <username> <newusername> [confirm token] With no subcommand, or the subcommand 'list' the user command will show a list of all the users, along with their email addresses and @@ -113,11 +114,12 @@ With the 'del' subcommand, you can delete a user from the system. With the 'email' subcommand, you can change a user's email address. With the 'name' subcommand, you can change a user's real name. -If you try and delete a user, you will need to paste a confirmation -token which will be supplied if you try and delete the user without -it. That token is reliant on the state of the admin repository. Any -admin operations performed between the two delete attempts will -invalidate the token and you will have to retry. +If you try and delete or rename a user, you will need to paste a +confirmation token which will be supplied if you try and delete or +rename the user without it. That token is reliant on the state of +the admin repository. Any admin operations performed between the +two delete or rename attempts will invalidate the token and you will +have to retry. ]] local function builtin_user_validate(conf, _, cmdline) @@ -127,9 +129,10 @@ local function builtin_user_validate(conf, _, cmdline) if cmdline[2] ~= "list" and cmdline[2] ~= "add" and cmdline[2] ~= "del" and + cmdline[2] ~= "rename" and cmdline[2] ~= "email" and cmdline[2] ~= "name" then - log.error("user takes one of list, add, del, email or name") + log.error("user takes one of list, add, del, rename, email or name") return false end if cmdline[2] == "list" and #cmdline ~= 2 then @@ -155,6 +158,10 @@ local function builtin_user_validate(conf, _, cmdline) log.error("user name takes a username and a real name") return false end + if cmdline[2] == "rename" and (#cmdline < 4 or #cmdline > 5) then + log.error("user rename takes a username, a new username [and a token]") + return false + end return true end @@ -229,7 +236,7 @@ local function builtin_user_run(conf, _, cmdline, env) if not utab then log.fatal("Could not find user:", username) end - local token = conf.repo:generate_confirmation() + local token = conf.repo:generate_confirmation("delete " .. username) if not cmdline[4] then log.state("In order to delete", username, "you must supply the following token:") log.state(token) @@ -250,6 +257,42 @@ local function builtin_user_run(conf, _, cmdline, env) -- And explain what reason = "Delete user " .. username end + elseif cmdline[2] == "rename" then + local oldusername = cmdline[3] + local newusername = cmdline[4] + local utab = conf.users[oldusername] + if not utab then + log.fatal("Could not find user:", oldusername) + end + if conf.users[newusername] then + log.fatal("New username already exists:", newusername) + end + local token = conf.repo:generate_confirmation("rename " .. oldusername .. + " to " .. newusername) + if not cmdline[5] then + log.state("In order to rename", oldusername, "you must supply the following token:") + log.state(token) + elseif cmdline[5] ~= token then + log.error("Tokens do not match. Did someone else do administrative actions?") + else + -- Iterate groups and rename the user in any group it is + -- a direct member of + for g, gtab in pairs(conf.groups) do + if gtab.members[oldusername] then + gtab.members[gtab.members[oldusername]] = newusername + gtab.members[newusername] = gtab.members[oldusername] + gtab.members[oldusername] = nil + gtab.changed_tables() + log.state("Renamed:", oldusername, "to", newusername, + "in membership of group:", g) + end + end + -- Now rename the user itself + conf.users[newusername] = utab + conf.users[oldusername] = nil + -- And explain what + reason = "Rename user " .. oldusername .. " to " .. newusername + end end if reason then -- Need to try and make a config commit |