-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathauthentication_concern.rb
More file actions
115 lines (92 loc) · 3.06 KB
/
authentication_concern.rb
File metadata and controls
115 lines (92 loc) · 3.06 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# frozen_string_literal: true
module AuthenticationConcern
extend ActiveSupport::Concern
CIS2_WORKGROUP = "schoolagedimmunisations"
included do
private
def authenticate_user!
if !user_signed_in?
if request.path != start_path
store_location_for(:user, request.fullpath)
end
if Settings.cis2.enabled || request.path != new_user_session_path
flash[:info] = "You must be logged in to access this page."
redirect_to start_path
end
elsif cis2_session?
if !selected_cis2_workgroup_is_valid?
redirect_to users_workgroup_not_found_path
elsif !selected_cis2_role_is_valid?
redirect_to users_role_not_found_path
elsif !selected_cis2_org_is_registered?
redirect_to users_organisation_not_found_path
end
end
end
def cis2_session?
session.key?(:cis2_info)
end
def selected_cis2_org_is_registered?
Organisation.exists?(
ods_code: session["cis2_info"]["selected_org"]["code"]
)
end
def selected_cis2_workgroup_is_valid?
workgroups = session.dig("cis2_info", "selected_role", "workgroups")
workgroups.present? && CIS2_WORKGROUP.in?(workgroups)
end
def valid_cis2_roles
%w[S8000:G8000:R8001 S8000:G8001:R8006]
end
def selected_cis2_role_is_valid?
session["cis2_info"]["selected_role"]["code"].in? valid_cis2_roles
end
def storable_location?
request.get? && is_navigational_format? && !devise_controller? &&
!request.xhr? && !turbo_frame_request?
end
def store_user_location!
return unless user_signed_in?
return unless storable_location?
store_location_for(:user, request.fullpath)
end
def authenticate_basic
if Flipper.enabled? :basic_auth
authenticated =
authenticate_with_http_basic do |username, password|
username == Rails.application.credentials.support_username &&
password == Rails.application.credentials.support_password
end
unless authenticated
request_http_basic_authentication "Application", <<~MESSAGE
Access is currently restricted to authorised users only.
MESSAGE
end
end
end
def after_sign_in_path_for(scope)
stored_location_for(scope) || dashboard_path
end
def user_signed_in?
super && (Settings.cis2.enabled ? cis2_session? : true)
end
def set_user_cis2_info
return unless current_user
current_user.cis2_info = session["cis2_info"]
end
def selected_cis2_nrbac_role
return {} if raw_cis2_info["selected_roleid"].blank?
@selected_cis2_nrbac_role ||=
raw_cis2_info["nhsid_nrbac_roles"].find do
_1["person_roleid"] == raw_cis2_info["selected_roleid"]
end
end
def selected_cis2_org
return {} if selected_cis2_nrbac_role.empty?
@selected_cis2_org ||=
raw_cis2_info["nhsid_user_orgs"].find do
_1["org_code"] == selected_cis2_nrbac_role["org_code"]
end
end
end
end