Continuing to fix and update code to reflect proper placing within the app. Moved admin controller to its proper home.

This commit is contained in:
Ben 2024-04-29 14:35:03 -05:00
parent 2103713552
commit f0d741cae3
7 changed files with 69 additions and 72 deletions

View File

@ -1,4 +1,5 @@
class AdminController < ApplicationController module Admin
class UsersController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
before_action :check_admin before_action :check_admin

View File

@ -0,0 +1,15 @@
module AccessControlValidations
extend ActiveSupport::Concern
included do
validate :end_date_after_start_date, if: -> { access_revoked && access_end_date.present? }
end
private
def end_date_after_start_date
if access_start_date.present? && access_end_date.present? && access_end_date < access_start_date
errors.add(:access_end_date, 'must be after the start date')
end
end
end

View File

@ -1,4 +1,5 @@
class User < ApplicationRecord class User < ApplicationRecord
include AccessControlValidations
rolify rolify
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable
has_many :access_periods, dependent: :destroy has_many :access_periods, dependent: :destroy
@ -11,12 +12,13 @@ class User < ApplicationRecord
# Callback to update the admin attribute based on Rolify role # Callback to update the admin attribute based on Rolify role
before_save :update_admin_attribute before_save :update_admin_attribute
# Validation for date fields
validate :end_date_after_start_date, if: -> { access_revoked && access_end_date.present? }
# Override Devise method to consider access_revoked # Override Devise method to consider access_revoked
def active_for_authentication? def active_for_authentication?
super && !access_revoked super && !access_revoked && allowed_to_login?
end
def admin?
has_role?(:admin)
end end
private private
@ -34,6 +36,11 @@ class User < ApplicationRecord
self.admin = has_role?(:admin) self.admin = has_role?(:admin)
end end
def allowed_to_login?
# Assuming 'suspended' is a role that should not log in
!has_role?(:suspended)
end
def handle_access_revocation def handle_access_revocation
if access_revoked_changed? && access_revoked if access_revoked_changed? && access_revoked
access_periods.find_or_initialize_by(end_date: nil).update(end_date: Date.today) access_periods.find_or_initialize_by(end_date: nil).update(end_date: Date.today)
@ -42,12 +49,4 @@ class User < ApplicationRecord
end end
end end
def end_date_after_start_date
if access_start_date.present?
errors.add(:access_end_date, 'must be provided when access is revoked') unless access_end_date.present?
if access_end_date && access_end_date < access_start_date
errors.add(:access_end_date, 'must be after the start date')
end
end
end
end end

View File

@ -41,7 +41,7 @@
<div class="col-12 d-flex justify-content-between mb-4"> <div class="col-12 d-flex justify-content-between mb-4">
<!-- Button for Admins to add a new user --> <!-- Button for Admins to add a new user -->
<% if can?(:create, User) %> <!-- Checks if the current user has the permission to create new users --> <% if can?(:create, User) %> <!-- Checks if the current user has the permission to create new users -->
<%= link_to 'Add New User', new_admin_user_path, class: "btn btn-dark" %> <%= link_to 'Add New User', admin_new_user_path, class: "btn btn-dark" %>
<% end %> <% end %>
<%= link_to 'Back to Home', root_path, class: "btn btn-secondary" %> <!-- Adjust as needed --> <%= link_to 'Back to Home', root_path, class: "btn btn-secondary" %> <!-- Adjust as needed -->

View File

@ -1,19 +1,23 @@
Rails.application.routes.draw do Rails.application.routes.draw do
devise_for :users devise_for :users
authenticated :user do # Unauthenticated root path (Typically the login page)
root 'home#index', as: :authenticated_root
end
authenticate :user, lambda { |u| u.admin? } do
resources :users, only: [:index, :edit, :update, :destroy]
end
devise_scope :user do devise_scope :user do
root to: 'devise/sessions#new' root to: 'devise/sessions#new'
end end
# Authenticated root, accessible to any logged-in user
authenticated :user do
root 'home#index', as: :authenticated_root
end
# Admin-specific resources
authenticate :user, ->(u) { u.admin? } do
# Restrict these paths to admins only
resources :users, only: [:index, :edit, :update, :destroy]
end
# General resources accessible based on role, not restricted in routes but rather in controller actions
resources :participants do resources :participants do
resources :onboardings, only: [:index, :create, :update] do resources :onboardings, only: [:index, :create, :update] do
collection do collection do
@ -21,16 +25,13 @@ Rails.application.routes.draw do
end end
end end
resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy] resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy]
collection do collection { get 'search' }
get 'search' # Define search on the collection, not on a member
end
member do member do
post 'link_worker' # Other member routes post 'link_worker'
post 'link_vendor' post 'link_vendor'
end end
end end
resources :workers do resources :workers do
resources :onboardings, only: [:index, :create, :update] do resources :onboardings, only: [:index, :create, :update] do
collection do collection do
@ -38,14 +39,13 @@ Rails.application.routes.draw do
end end
end end
resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy] resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy]
member do collection do
get 'search'
post 'link_participant' post 'link_participant'
post 'link_employer' post 'link_employer'
end end
get 'search', on: :collection
end end
resources :vendors do resources :vendors do
resources :onboardings, only: [:index, :create, :update] do resources :onboardings, only: [:index, :create, :update] do
collection do collection do
@ -53,19 +53,8 @@ Rails.application.routes.draw do
end end
end end
resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy] resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy]
collection do collection { get 'search' }
get 'search'
end end
end
resources :employments, only: [:edit, :update, :destroy]
resources :employer_records, only: [:edit, :update, :destroy] do
post 'link_participant', on: :collection
end
resources :service_contracts
resources :employers do resources :employers do
resources :onboardings, only: [:index, :create, :update] do resources :onboardings, only: [:index, :create, :update] do
@ -73,35 +62,24 @@ Rails.application.routes.draw do
post 'submit_onboarding' post 'submit_onboarding'
end end
end end
member do collection { get 'search' }
post 'link_worker' member { post 'link_worker' }
end
collection do
get 'search'
end
end end
resources :employments, only: [:edit, :update, :destroy]
resources :employer_records, only: [:edit, :update, :destroy] do
collection { post 'link_participant' }
end
resources :service_contracts
resources :forms resources :forms
resources :users
# Setup the routes under a scope to mimic namespace-like behavior without actual namespacing
scope '/admin', controller: :admin do namespace :admin do
get 'users/new', as: 'new_admin_user', action: :new_user # for displaying the new user form get 'users/new', to: 'admin#new_user', as: 'new_user'
post 'users', as: 'admin_users', action: :create_user # for submitting the new user form post 'users', to: 'admin#create_user', as: 'create_user'
# You can add more admin routes here as needed # Add other admin routes as needed
resources :users, only: [:index, :edit, :update, :destroy]
end end
get 'home/index'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", as: :rails_health_check
# Defines the root path route ("/")
# root "posts#index"
end end

View File

@ -0,0 +1,5 @@
class RemoveAdminFromUsers < ActiveRecord::Migration[7.1]
def change
remove_column :users, :admin, :boolean
end
end

3
db/schema.rb generated
View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_04_11_215819) do ActiveRecord::Schema[7.1].define(version: 2024_04_29_184834) do
create_table "access_periods", force: :cascade do |t| create_table "access_periods", force: :cascade do |t|
t.date "start_date" t.date "start_date"
t.date "end_date" t.date "end_date"
@ -188,7 +188,6 @@ ActiveRecord::Schema[7.1].define(version: 2024_04_11_215819) do
t.datetime "remember_created_at" t.datetime "remember_created_at"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.boolean "admin", default: false, null: false
t.string "first_name" t.string "first_name"
t.string "last_name" t.string "last_name"
t.string "phone" t.string "phone"