-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
Current
per-request attributes singleton
This creates an idiomatic Rails per-request attributes singleton `Current`, using [`ActiveSupport::CurrentAttributes`], and stores the current user in it. This will allow us to access the currently active user in cross-cutting concerns, without having to deeply pass it around. `CurrentAttributes` is a "sharp tool" that should be used with caution as it creates global state for a request, but is an ideal tool for dealing with auditing and logging concerns (which we'll soon introduce) where you need to have global awareness of certain request attributes outside of just the controller layer. We have prior art for this pattern, for example in Whitehall. [`ActiveSupport::CurrentAttributes`]: https://edgeapi.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html
- Loading branch information
Showing
5 changed files
with
27 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
class ApplicationController < ActionController::Base | ||
include AuthenticatesUser | ||
|
||
# Prevent CSRF attacks by raising an exception. | ||
# For APIs, you may want to use :null_session instead. | ||
protect_from_forgery with: :exception | ||
|
||
include GDS::SSO::ControllerMethods | ||
before_action :authenticate_user! | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Enforces user authentication for a controller and stores the authenticated user in `Current` | ||
module AuthenticatesUser | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
include GDS::SSO::ControllerMethods | ||
|
||
before_action do | ||
authenticate_user! | ||
|
||
Current.user = current_user | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Stores cross-cutting concerns for every request | ||
class Current < ActiveSupport::CurrentAttributes | ||
attribute :user | ||
|
||
def user? | ||
user.present? | ||
end | ||
end |