Skip to content

wip: redirect user to login and back if trying to access blocked resource #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ class ApplicationController < ActionController::Base

before_action :still_authenticated?

# https://cancancan.dev/handling_access_denied
rescue_from CanCan::AccessDenied do |exception|
if current_user.nil?
session[:redirect_url] = request.fullpath
redirect_to root_url, alert: 'You have to log in to continue.'
else
respond_to do |format|
format.json { render nothing: true, status: :not_found }
format.html { redirect_to root_url, alert: exception.message }
end
end
end

private

def still_authenticated?
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ def create
user = User.upsert_from_auth_hash(request.env['omniauth.auth'])
log_in user
flash[:success] = 'You are now logged in!'
if session[:redirect_url]
redirect_to session.delete(:redirect_url)
return
end
redirect_to user_path user
end

Expand Down
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class UsersController < ApplicationController
protect_from_forgery unless: -> { request.format.json? }

def index
authorize! :index, User
@users = User.accessible_by(current_ability)
end

Expand Down
4 changes: 4 additions & 0 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ def logged_in?
end

def log_in(user)
# Keep redirect_url around when resetting the session
next_url = session[:redirect_url]
reset_session # For security reasons, we clear the session data before login
session[:user_id] = user.id
# TODO: Increase expires_at as the user stays active in the application
session[:expires_at] = Time.current + SESSION_DURATION_TIME
session[:groups] = user.groups
session[:redirect_url] = next_url if next_url
end

# TODO: also logout of sso
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<li>
<%= link_to "Lea5", root_path, class: "logo" %>
</li>
<% if logged_in? %>
<% if can? :read, User %>
<li>
<%= link_to "Users", users_path %>
</li>
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
root 'static_pages#home'

get AUTH_CALLBACK_PATH, to: 'sessions#create', as: 'auth_callback'
delete '/logout', to: 'sessions#destroy', as: 'logout'
root 'static_pages#home'

resources :users do
resources :machines, shallow: true, except: [:index]
Expand Down