From 99571be9a1387be8271afadd9c58e2e7fb7b61cb Mon Sep 17 00:00:00 2001 From: David Gwilliam Date: Fri, 27 Mar 2026 13:37:07 -0700 Subject: [PATCH] feat: Add admin user creation support and fix user entity --- app/repositories/users.rb | 9 +-- db/migrate/20240915000008_create_users.rb | 2 +- lib/euchre_camp/entities/user.rb | 1 + .../persistence/relations/users.rb | 28 +++++++++ scripts/create_admin.rb | 59 +++++++++++++++++++ 5 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 lib/euchre_camp/persistence/relations/users.rb create mode 100755 scripts/create_admin.rb diff --git a/app/repositories/users.rb b/app/repositories/users.rb index fd6b441..e7af3f5 100644 --- a/app/repositories/users.rb +++ b/app/repositories/users.rb @@ -20,14 +20,15 @@ module EuchreCamp end # Create new user - def create(email:, password_digest:, player_id:) + def create(email:, password_digest:, player_id:, role: 'player', confirmed: false) users.changeset(:create, { email: email, password_digest: password_digest, player_id: player_id, - confirmed: false, - confirmation_token: generate_token, - confirmation_sent_at: Time.now + role: role, + confirmed: confirmed, + confirmation_token: confirmed ? nil : generate_token, + confirmation_sent_at: confirmed ? nil : Time.now }).commit end diff --git a/db/migrate/20240915000008_create_users.rb b/db/migrate/20240915000008_create_users.rb index 9e911b4..776e3a9 100644 --- a/db/migrate/20240915000008_create_users.rb +++ b/db/migrate/20240915000008_create_users.rb @@ -7,7 +7,7 @@ ROM::SQL.migration do foreign_key :player_id, :players, null: false column :email, String, null: false, unique: true column :password_digest, String, null: false - column :confirmed, Boolean, default: false + column :confirmed, TrueClass, default: false column :confirmation_token, String column :confirmation_sent_at, DateTime column :reset_password_token, String diff --git a/lib/euchre_camp/entities/user.rb b/lib/euchre_camp/entities/user.rb index 60722f4..1dac0f5 100644 --- a/lib/euchre_camp/entities/user.rb +++ b/lib/euchre_camp/entities/user.rb @@ -7,6 +7,7 @@ module EuchreCamp attribute :player_id, Types::Integer attribute :email, Types::String attribute :password_digest, Types::String + attribute :role, Types::String, default: 'player' attribute :confirmed, Types::Bool attribute? :confirmation_token, Types::String.optional attribute? :confirmation_sent_at, Types::Time.optional diff --git a/lib/euchre_camp/persistence/relations/users.rb b/lib/euchre_camp/persistence/relations/users.rb new file mode 100644 index 0000000..888c502 --- /dev/null +++ b/lib/euchre_camp/persistence/relations/users.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module EuchreCamp + module Persistence + module Relations + class Users < ROM::Relation[:sql] + schema(:users, infer: true) do + attribute :id, ROM::Types::Integer, primary_key: true + attribute :player_id, ROM::Types::Integer + attribute :email, ROM::Types::String + attribute :password_digest, ROM::Types::String + attribute :role, ROM::Types::String + attribute :confirmed, ROM::Types::Bool + attribute :confirmation_token, ROM::Types::String.optional + attribute :confirmation_sent_at, ROM::Types::Time.optional + attribute :reset_password_token, ROM::Types::String.optional + attribute :reset_password_sent_at, ROM::Types::Time.optional + attribute :failed_login_attempts, ROM::Types::Integer + attribute :locked_until, ROM::Types::Time.optional + attribute :last_login_at, ROM::Types::Time.optional + attribute :last_login_ip, ROM::Types::String.optional + attribute :created_at, ROM::Types::Time + attribute :updated_at, ROM::Types::Time + end + end + end + end +end \ No newline at end of file diff --git a/scripts/create_admin.rb b/scripts/create_admin.rb new file mode 100755 index 0000000..0d06274 --- /dev/null +++ b/scripts/create_admin.rb @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require_relative '../config/app' +require 'hanami/prepare' +require 'bcrypt' + +# Get arguments +email = ARGV[0] || 'david@dhg.lol' +password = ARGV[1] || 'admin' + +if password.nil? + puts "Error: Password required" + puts "Usage: ruby scripts/create_admin.rb [email] [password]" + exit 1 +end + +puts "Creating admin user..." +puts "Email: #{email}" + +# Initialize repositories +players_repo = EuchreCamp::App['repositories.players'] +users_repo = EuchreCamp::App['repositories.users'] + +# Check if user already exists +existing_user = users_repo.by_email(email) +if existing_user + puts "User #{email} already exists with ID #{existing_user.id}" + puts "Updating role to club_admin..." + users_repo.users.where(id: existing_user.id).update(role: 'club_admin', confirmed: true) + puts "Done!" + exit 0 +end + +# Create player +player = players_repo.create(name: email.split('@').first.capitalize, rating: 1000) +puts "Created player with ID: #{player[:id]}" + +# Hash password +password_digest = BCrypt::Password.create(password) +puts "Password hashed" + +# Create user +user = users_repo.create( + email: email, + password_digest: password_digest, + player_id: player[:id], + role: 'club_admin', + confirmed: true +) + +puts "" +puts "✅ Admin user created successfully!" +puts " Email: #{email}" +puts " Player ID: #{player[:id]}" +puts " Role: club_admin" +puts " Confirmed: true" +puts "" +puts "Login at: http://localhost:2300/login"