generated from dxw/rails-template
/
auth.rb
45 lines (37 loc) · 1.21 KB
/
auth.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class UserNotAuthorised < StandardError; end
module Auth
extend ActiveSupport::Concern
included do
helper_method :current_user
helper_method :authenticated?
rescue_from(UserNotAuthorised, Pundit::NotAuthorizedError) do |exception|
error_message = if exception.respond_to?(:policy)
t("#{exception.policy.class.to_s.underscore}.#{exception.query}", scope: "not_authorised", default: :default)
else
t("page_content.errors.not_authorised.explanation")
end
render "pages/errors/not_authorised", formats: [:html], status: 401, locals: {error_message: error_message}
end
end
def current_user
@current_user ||= if session.dig(:userinfo)
User.active.includes(:organisation).find_by!(identifier: signed_in_user_identifier) do |user|
user.name = session.dig(:userinfo, "info", "name")
user.email = session.dig(:userinfo, "info", "email")
end
end
rescue ActiveRecord::RecordNotFound
repudiate!
raise UserNotAuthorised
end
def signed_in_user_identifier
session.dig(:userinfo, "uid")
end
def authenticated?
current_user.present?
end
private def repudiate!
session.delete(:userinfo)
@current_user = nil
end
end