diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb new file mode 100644 index 000000000..139e79952 --- /dev/null +++ b/app/controllers/errors_controller.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class ErrorsController < ApplicationController + layout false + + skip_before_action :authenticate_auth_user! + + def auth_error + # Renders app/views/errors/auth_error.html.erb + end +end diff --git a/app/controllers/omniauth_callbacks_controller.rb b/app/controllers/omniauth_callbacks_controller.rb index b10a54bd6..d0ed242e3 100644 --- a/app/controllers/omniauth_callbacks_controller.rb +++ b/app/controllers/omniauth_callbacks_controller.rb @@ -6,7 +6,8 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController def keycloak_openid omniauth_auth = request.env['omniauth.auth'] @user = AuthUser.from_omniauth(omniauth_auth) - if @user.persisted? + @relevant_role = AuthConfig.relevant_keycloak_role + if @user.persisted? && omniauth_auth[:extra][:raw_info][:pitc][:roles].include?(@relevant_role) sign_in_and_redirect @user, event: :authentication set_flash_message(:notice, :success, kind: 'Keycloak') if is_navigational_format? else @@ -15,6 +16,6 @@ def keycloak_openid end def failure - redirect_to root_path + redirect_to '/auth_error' end end diff --git a/app/views/errors/auth_error.html.erb b/app/views/errors/auth_error.html.erb new file mode 100644 index 000000000..5623a2a09 --- /dev/null +++ b/app/views/errors/auth_error.html.erb @@ -0,0 +1,10 @@ +
You do not have the necessary permissions to access this application.
+Please contact an administrator if you believe this is an error.
+ <%= link_to 'Go back to sign in', '/sign_in', class: 'btn btn-primary' %> +