Skip to content

Commit 3e4c92b

Browse files
committed
Add routing constraints for admins and authenticated users
1 parent bcd1bd0 commit 3e4c92b

File tree

7 files changed

+41
-13
lines changed

7 files changed

+41
-13
lines changed

app/controllers/concerns/authentication.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
module Authentication
22
extend ActiveSupport::Concern
33

4+
# Routing constraints
5+
Authenticated = ->(request) { Current.session ||= Session.find_by(id: request.cookie_jar.signed[:session_id]) }
6+
Admin = ->(request) { Authenticated.call(request) && Current.user&.admin? }
7+
48
included do
59
before_action :require_authentication
610
helper_method :authenticated?
@@ -10,12 +14,13 @@ module Authentication
1014
class_methods do
1115
def allow_unauthenticated_access(**options)
1216
skip_before_action :require_authentication, **options
17+
before_action :resume_session
1318
end
1419
end
1520

1621
private
1722
def authenticated?
18-
Current.session.present?
23+
resume_session
1924
end
2025

2126
def require_authentication
@@ -29,7 +34,9 @@ def resume_session
2934
end
3035

3136
def find_session_by_cookie
32-
Session.find_by(id: cookies.signed[:session_id])
37+
if (id = request.cookie_jar.signed[:session_id])
38+
Session.find_by(id: id)
39+
end
3340
end
3441

3542

app/controllers/main_controller.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
class MainController < ApplicationController
2+
allow_unauthenticated_access only: [:about]
3+
24
def index
35
end
6+
7+
def admin
8+
render plain: "Admin area"
9+
end
10+
11+
def dashboard
12+
render plain: "Dashboard"
13+
end
14+
15+
def about
16+
render plain: "About"
17+
end
418
end

app/models/current.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
class Current < ActiveSupport::CurrentAttributes
22
attribute :session
3-
# delegate :user, to: :session, allow_nil: true
4-
53
attribute :impersonated_user
64

7-
def user
8-
impersonated_user || true_user
9-
end
10-
11-
def true_user
12-
session&.user
13-
end
5+
def user = impersonated_user || true_user
6+
def true_user = session&.user
147
end

app/models/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ class User < ApplicationRecord
22
has_secure_password
33
has_many :sessions, dependent: :destroy
44

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

config/routes.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
Rails.application.routes.draw do
2+
constraints Authentication::Admin do
3+
get "admin", to: "main#admin"
4+
end
5+
6+
constraints Authentication::Authenticated do
7+
get "dashboard", to: "main#dashboard"
8+
end
9+
210
resource :impersonate
311
resource :session
412
resources :passwords, param: :token
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddAdminToUsers < ActiveRecord::Migration[8.0]
2+
def change
3+
add_column :users, :admin, :boolean
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)