From 90ccd50c80fa776124db564e1d3f7384f2712cd1 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Tue, 13 Oct 2020 05:33:49 +0200 Subject: [PATCH] Deletion: WIP --- src/authd.cr | 30 ++++++++++++++++++++++++++++++ utils/authc.cr | 37 +++++++++++++++++++++++++++++-------- utils/better-parser.cr | 4 +++- 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/src/authd.cr b/src/authd.cr index 036d63d..906922a 100644 --- a/src/authd.cr +++ b/src/authd.cr @@ -344,6 +344,19 @@ class AuthD::Request property phone : String? end + class Delete < Request + # Deletion can be triggered by either an admin or the user. + property shared_key : String? + + property login : String? + property password : String? + + property user : String | Int32 + + initialize :user, :login, :password + initialize :user, :shared_key + end + # This creates a Request::Type enumeration. One entry for each request type. {% begin %} enum Type @@ -608,6 +621,23 @@ module AuthD raise Exception.new "unexpected response" end end + + def delete(user : Int32 | String, key : String) + send Request::Delete.new user, key + delete_ + end + def delete(user : Int32 | String, login : String, pass : String) + send Request::Delete.new user, login, pass + delete_ + end + def delete_ + response = Response.from_ipc read + case response + when Response::Error + raise Exception.new response.reason + end + response + end end end diff --git a/utils/authc.cr b/utils/authc.cr index ab8234e..196bbe3 100644 --- a/utils/authc.cr +++ b/utils/authc.cr @@ -79,15 +79,8 @@ class Actions # For all functions: the number of arguments is already tested. # - def user_registration - # pp! authd.register login, password.not_nil!, email, phone, profile: profile - rescue e : AuthD::Exception - puts "error: #{e.message}" - end - def user_deletion - end - def user_add + puts "User add!!!" args = Context.args.not_nil! login, email, phone = args[0..2] profile = Context.user_profile @@ -100,6 +93,20 @@ class Actions puts "error: #{e.message}" end + def user_registration + args = Context.args.not_nil! + login, email, phone = args[0..2] + profile = Context.user_profile + + password = Actions.ask_password + exit 1 unless password + + res = authd.register login, password.not_nil!, email, phone, profile: profile + puts res + rescue e : AuthD::Exception + puts "error: #{e.message}" + end + # TODO def user_mod args = Context.args.not_nil! @@ -127,6 +134,20 @@ class Actions # puts res end + def user_deletion + args = Context.args.not_nil! + userid = args[0] + + # Check if the request comes from an admin or the user. + res = if Context.shared_key.nil? + authd.delete userid, Context.authd_login, Context.authd_pass + else + authd.delete userid, Context.shared_key + end + + puts res + end + # TODO def validate # pp! r = authd.validate_user login.not_nil!, activation_key.not_nil! diff --git a/utils/better-parser.cr b/utils/better-parser.cr index 3896ef7..b74cb38 100644 --- a/utils/better-parser.cr +++ b/utils/better-parser.cr @@ -111,13 +111,15 @@ parser = OptionParser.new do |parser| end parser.on "delete", "Remove user." do - parser.banner = "Usage: user delete -u userid [opt]" + parser.banner = "Usage: user delete userid [opt]" Baguette::Log.info "Remove user." Context.command = "delete" # You can either be the owner of the account, or an admin. opt_authd_login.call parser opt_authd_admin.call parser opt_help.call parser + # userid + unrecognized_args_to_context_args.call parser, 1 end # Do not require to be admin.