From cde5e651a66f75aaa0101b7b4f349c65669e595b Mon Sep 17 00:00:00 2001 From: julien Date: Mon, 2 Nov 2026 15:46:50 +0100 Subject: [PATCH] Add first admin creation script --- bin/initialize-admin | 70 +++++++++++++++++++++++ db/migrations/003_add_admin_to_members.rb | 13 +++++ 2 files changed, 83 insertions(+) create mode 100644 bin/initialize-admin create mode 100644 db/migrations/003_add_admin_to_members.rb diff --git a/bin/initialize-admin b/bin/initialize-admin new file mode 100644 index 0000000..f14aa92 --- /dev/null +++ b/bin/initialize-admin @@ -0,0 +1,70 @@ +#!/usr/bin/env ruby + +require "dotenv/load" if ENV.fetch("RACK_ENV", "development") == "development" +require "io/console" +require "rodauth" + +require_relative "../lib/database" + +console = IO.console or abort "Terminal interactif requis." + +abort "Un administrateur existe déjà." if + DB[:members].where(admin: true).count.positive? + +console.print "Adresse électronique : " +email = console.gets&.strip + +abort "Adresse électronique obligatoire." if email.to_s.empty? + +abort "Ce compte existe déjà." if + DB[:accounts].where(email: email).count.positive? + +console.print "Prénom : " +first_name = console.gets&.strip + +console.print "Nom : " +last_name = console.gets&.strip + +abort "Prénom et nom obligatoires." if + first_name.to_s.empty? || last_name.to_s.empty? + +password = console.getpass("Mot de passe : ") +confirmation = console.getpass("Confirmer le mot de passe : ") + +abort "Mot de passe obligatoire." if password.empty? +abort "Les mots de passe ne correspondent pas." unless password == confirmation + +console.puts +console.puts "Administrateur : #{first_name} #{last_name} (#{email})" +console.print "Confirmer la création ? [o/N] " + +abort "Opération annulée." unless %w[o oui].include?( + console.gets&.strip&.downcase +) + +auth = Rodauth.lib do + db DB + enable :create_account + + already_logged_in { super() } +end + +DB.transaction do + auth.create_account( + login: email, + password: password + ) + + account_id = auth.account_id_for_login(login: email) + + DB[:members].insert( + account_id: account_id, + kind: "individual", + first_name: first_name, + last_name: last_name, + organization_name: nil, + admin: true + ) +end + +puts "Premier administrateur créé." diff --git a/db/migrations/003_add_admin_to_members.rb b/db/migrations/003_add_admin_to_members.rb new file mode 100644 index 0000000..4d58e86 --- /dev/null +++ b/db/migrations/003_add_admin_to_members.rb @@ -0,0 +1,13 @@ +Sequel.migration do + up do + alter_table(:members) do + add_column :admin, TrueClass, null: false, default: false + end + end + + down do + alter_table(:members) do + drop_column :admin + end + end +end