From ca4cb9e2317697324c0c424a130e3d8c66ad77b5 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sat, 8 Jun 2024 20:35:41 +0200 Subject: [PATCH] Migration script: done. --- makefile | 4 ++++ src/better-parser.cr | 11 +++++++++++ src/client.cr | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/makefile b/makefile index 263232e..d9c1328 100644 --- a/makefile +++ b/makefile @@ -52,6 +52,10 @@ get-user: migrate-user: ./bin/authc user migrate $(NAME) $(PASSWORD_HASH) $(LOGIN_OPT) +USER_DB ?= user-db.txt +migrate-all-users: + ./bin/authc migration-script $(USER_DB) $(LOGIN_OPT) + SERVICE ?= 'auth' RESOURCE ?= '*' UID ?= 1000 diff --git a/src/better-parser.cr b/src/better-parser.cr index 1b82fbf..0f0ec30 100644 --- a/src/better-parser.cr +++ b/src/better-parser.cr @@ -79,6 +79,17 @@ parser = OptionParser.new do |parser| unrecognized_args_to_context_args.call parser, 2 end + parser.on "migration-script", "Adding a batch of users from old code base." do + parser.banner = "usage: migration-script user-db.txt" + Baguette::Log.info "Adding a batch of users." + Context.command = "migration-script" + opt_authd_login.call parser + opt_profile.call parser + opt_help.call parser + # user-db.txt + unrecognized_args_to_context_args.call parser, 1 + end + parser.on "user", "Operations on users." do parser.banner = "Usage: user [add | mod | delete | validate | search | get | recover | register ]" diff --git a/src/client.cr b/src/client.cr index 9804d53..74f0a9f 100644 --- a/src/client.cr +++ b/src/client.cr @@ -63,6 +63,7 @@ class Actions # Require admin privileges. @the_call["user-add"] = ->user_add @the_call["user-migrate"] = ->user_migrate + @the_call["migration-script"] = ->migration_script @the_call["user-mod"] = ->user_mod @the_call["permission-set"] = ->permission_set @@ -99,6 +100,26 @@ class Actions puts "error: #{e.message}" end + # Migrate a batch of users from dnsmanager v1. + # + # Usage: authc migration-script user-db.txt + # + # user-db.txt should be formated as "login old-hash". + def migration_script + args = Context.args.not_nil! + filename = args[0] + profile = Context.user_profile + + File.each_line(filename) do |line| + login, password_hash_brkn = line.split("\t") + Baguette::Log.info "adding user '#{login}', hash '#{password_hash_brkn}'" + pp! authd.migrate_user login, password_hash_brkn + end + + rescue e : AuthD::Exception + puts "error: #{e.message}" + end + def user_registration args = Context.args.not_nil! login, email = args[0..1]