Skip to content

Commit

Permalink
Add routing constraints for admins and authenticated users
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Oct 14, 2024
1 parent bcd1bd0 commit 3e4c92b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 13 deletions.
11 changes: 9 additions & 2 deletions app/controllers/concerns/authentication.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Authentication
extend ActiveSupport::Concern

# Routing constraints
Authenticated = ->(request) { Current.session ||= Session.find_by(id: request.cookie_jar.signed[:session_id]) }
Admin = ->(request) { Authenticated.call(request) && Current.user&.admin? }

included do
before_action :require_authentication
helper_method :authenticated?
Expand All @@ -10,12 +14,13 @@ module Authentication
class_methods do
def allow_unauthenticated_access(**options)
skip_before_action :require_authentication, **options
before_action :resume_session
end
end

private
def authenticated?
Current.session.present?
resume_session
end

def require_authentication
Expand All @@ -29,7 +34,9 @@ def resume_session
end

def find_session_by_cookie
Session.find_by(id: cookies.signed[:session_id])
if (id = request.cookie_jar.signed[:session_id])
Session.find_by(id: id)
end
end


Expand Down
14 changes: 14 additions & 0 deletions app/controllers/main_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
class MainController < ApplicationController
allow_unauthenticated_access only: [:about]

Check failure on line 2 in app/controllers/main_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 2 in app/controllers/main_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

def index
end

def admin
render plain: "Admin area"
end

def dashboard
render plain: "Dashboard"
end

def about
render plain: "About"
end
end
11 changes: 2 additions & 9 deletions app/models/current.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
class Current < ActiveSupport::CurrentAttributes
attribute :session
# delegate :user, to: :session, allow_nil: true

attribute :impersonated_user

def user
impersonated_user || true_user
end

def true_user
session&.user
end
def user = impersonated_user || true_user
def true_user = session&.user
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ class User < ApplicationRecord
has_secure_password
has_many :sessions, dependent: :destroy

normalizes :email_address, with: ->(e) { e.strip.downcase }
normalizes :email_address, with: ->{ _1.strip.downcase }

Check failure on line 5 in app/models/user.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
end
8 changes: 8 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
Rails.application.routes.draw do
constraints Authentication::Admin do
get "admin", to: "main#admin"
end

constraints Authentication::Authenticated do
get "dashboard", to: "main#dashboard"
end

resource :impersonate
resource :session
resources :passwords, param: :token
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20241007153451_add_admin_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAdminToUsers < ActiveRecord::Migration[8.0]
def change
add_column :users, :admin, :boolean
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3e4c92b

Please sign in to comment.