From e29c060a39bcd33b48e113572382904e7f1fe1e3 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sat, 1 Apr 2023 02:29:59 +0200 Subject: [PATCH] Add user statuses and seed admin user --- README.md | 2 +- app/models/user.rb | 9 +++++++++ config/application.rb.dist | 4 ++++ config/initializers/devise.rb | 2 +- db/seeds.rb | 20 +++++++++++++------- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 962ab4c..6cd1b67 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Copy config template and update database configuration: Run database creation and migration tasks: - RAILS_ENV="production" bundle exec rake db:create db:migrate + RAILS_ENV="production" bundle exec rake db:create db:migrate db:seed ## Running diff --git a/app/models/user.rb b/app/models/user.rb index 4756799..9c27d47 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,13 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable + + # Statuses ordered by decreasing privileges + enum :status, { + admin: 1, # admin level access + active: 2, # read-write user level access + restricted: 3, # read-only user level access + locked: 4, # disallowed to sign in due to failed logins + disabled: 5 # administratively disallowed to sign in + }, default: :active end diff --git a/config/application.rb.dist b/config/application.rb.dist index d534f8c..5be786e 100644 --- a/config/application.rb.dist +++ b/config/application.rb.dist @@ -35,8 +35,12 @@ module FixinMe # # URL to use in sent e-mails. config.action_mailer.default_url_options = {host: 'localhost'} + # List of hosts this app is available at. # https://guides.rubyonrails.org/configuring.html#actiondispatch-hostauthorization config.hosts << 'localhost' + + # Email address of admin account + config.admin = 'admin@localhost' end end diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 7ed1ab3..46edb3a 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -178,7 +178,7 @@ Devise.setup do |config| # ==> Configuration for :validatable # Range for password length. - config.password_length = 6..128 + config.password_length = 5..128 # Email regex used to validate email formats. It simply asserts that # one (and only one) @ exists in the given string. This is mainly diff --git a/db/seeds.rb b/db/seeds.rb index bc25fce..d8f19e9 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,7 +1,13 @@ -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup). -# -# Examples: -# -# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }]) -# Character.create(name: "Luke", movie: movies.first) +# This file should contain all the record creation needed to seed the database +# with its default values. The data can then be loaded with the +# bin/rails db:seed +# command (or created alongside the database with db:setup). +# Seeding process should be idempotent. + +User.transaction do + User.find_or_create_by!(status: :admin) do |user| + user.email = Rails.configuration.admin + user.password = 'admin' + puts "Admin account '#{user.email}' created with default password '#{user.password}'" + end +end