Skip to content

Commit 96c26a7

Browse files
committed
Choose team when signing in
This is the final part of the teams/organisations restructuring, which requires users to select the team they want to sign in to when they sign in. This is necessary as a single organisation can be part of multiple teams. Jira-Issue: MAV-1280
1 parent 0f3c0ca commit 96c26a7

22 files changed

+167
-121
lines changed

app/controllers/application_controller.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ApplicationController < ActionController::Base
66

77
before_action :store_user_location!
88
before_action :authenticate_user!
9-
before_action :set_selected_team
9+
before_action :ensure_team_is_selected
1010
before_action :set_user_cis2_info
1111
before_action :set_disable_cache_headers
1212
before_action :set_header_path
@@ -35,18 +35,16 @@ class UnprocessableEntity < StandardError
3535

3636
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
3737

38+
def current_organisation = current_user&.selected_organisation
39+
3840
def current_team = current_user&.selected_team
3941

40-
helper_method :current_team
42+
helper_method :current_organisation, :current_team
4143

4244
private
4345

44-
def set_selected_team
45-
return if Settings.cis2.enabled
46-
return unless current_user
47-
return if cis2_info.signed_in?
48-
49-
redirect_to new_users_teams_path
46+
def ensure_team_is_selected
47+
redirect_to new_users_teams_path if current_user && cis2_info.team.nil?
5048
end
5149

5250
def set_header_path

app/controllers/concerns/authentication_concern.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ def authenticate_user!
1212
store_location_for(:user, request.fullpath)
1313
end
1414

15-
if Settings.cis2.enabled || request.path != new_user_session_path
15+
if cis2_enabled? || request.path != new_user_session_path
1616
flash[:info] = "You must be logged in to access this page."
1717
redirect_to start_path
1818
end
19-
elsif cis2_session?
19+
elsif cis2_enabled?
2020
if !selected_cis2_workgroup_is_valid?
2121
redirect_to users_workgroup_not_found_path
2222
elsif !selected_cis2_role_is_valid?
@@ -27,9 +27,9 @@ def authenticate_user!
2727
end
2828
end
2929

30-
def cis2_info = CIS2Info.new(request_session: session)
30+
def cis2_enabled? = Settings.cis2.enabled
3131

32-
def cis2_session? = cis2_info.present?
32+
def cis2_info = CIS2Info.new(request_session: session)
3333

3434
def selected_cis2_org_is_registered?
3535
Organisation.exists?(ods_code: cis2_info.organisation_code)
@@ -76,7 +76,7 @@ def after_sign_in_path_for(scope)
7676
end
7777

7878
def user_signed_in?
79-
super && (Settings.cis2.enabled ? cis2_session? : true)
79+
super && (cis2_enabled? ? cis2_info.present? : true)
8080
end
8181

8282
def set_user_cis2_info

app/controllers/users/errors_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class Users::ErrorsController < ::ApplicationController
44
skip_before_action :store_user_location!
55
skip_before_action :authenticate_user!
6+
skip_before_action :ensure_team_is_selected
67
skip_after_action :verify_policy_scoped
78

89
before_action :set_cis2_info

app/controllers/users/omniauth_callbacks_controller.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
55
include CIS2LogoutConcern
66

77
skip_before_action :authenticate_user!
8+
skip_before_action :ensure_team_is_selected
89
skip_after_action :verify_policy_scoped
910
skip_before_action :verify_authenticity_token, only: [:cis2_logout]
1011
skip_before_action :authenticate_basic, only: [:cis2_logout]
@@ -21,7 +22,7 @@ def cis2
2122
elsif !selected_cis2_org_is_registered?
2223
redirect_to users_organisation_not_found_path
2324
else
24-
@user = User.find_or_create_from_cis2_oidc(user_cis2_info, teams)
25+
@user = User.find_or_create_from_cis2_oidc(user_cis2_info, valid_teams)
2526

2627
# Force is set to true because the `session_token` might have changed
2728
# even if the same user is logging in.
@@ -92,14 +93,13 @@ def raw_cis2_info
9293
user_cis2_info["extra"]["raw_info"]
9394
end
9495

95-
def organisation
96-
@organisation ||=
97-
Organisation.find_by(ods_code: selected_cis2_org["org_code"])
98-
end
99-
100-
def teams
101-
# TODO: Select the right team based on the user's workgroup.
102-
organisation.teams
96+
def valid_teams
97+
Team.joins(:organisation).where(
98+
workgroup: selected_cis2_nrbac_role["workgroups"],
99+
organisation: {
100+
ods_code: selected_cis2_org["org_code"]
101+
}
102+
)
103103
end
104104

105105
def set_cis2_session_info
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# frozen_string_literal: true
22

33
class Users::TeamsController < ApplicationController
4-
skip_before_action :set_selected_team
4+
skip_before_action :store_user_location!
5+
skip_before_action :ensure_team_is_selected
56
skip_after_action :verify_policy_scoped
67

7-
before_action :redirect_to_dashboard_if_cis2_is_enabled
8-
98
layout "two_thirds"
109

1110
def new
@@ -21,15 +20,9 @@ def create
2120
)
2221

2322
if @form.save
24-
redirect_to dashboard_path
23+
redirect_to session[:user_return_to] || dashboard_path
2524
else
2625
render :new, status: :unprocessable_content
2726
end
2827
end
29-
30-
private
31-
32-
def redirect_to_dashboard_if_cis2_is_enabled
33-
redirect_to dashboard_path if Settings.cis2.enabled
34-
end
3528
end

app/forms/select_team_form.rb

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,34 @@ class SelectTeamForm
1313
def save
1414
return false if invalid?
1515

16-
cis2_info.update!(
17-
organisation_name: team.name,
18-
organisation_code: organisation.ods_code,
19-
role_code: CIS2Info::NURSE_ROLE,
20-
workgroups: [CIS2Info::WORKGROUP]
21-
)
16+
team = teams.find(team_id)
17+
18+
cis2_info.update!(team_workgroup: team.workgroup)
19+
20+
unless Settings.cis2.enabled
21+
cis2_info.update!(
22+
organisation_code: team.organisation.ods_code,
23+
role_code: CIS2Info::NURSE_ROLE,
24+
workgroups: [CIS2Info::WORKGROUP] + [team.workgroup]
25+
)
26+
end
2227

2328
true
2429
end
2530

26-
private
27-
28-
def team = current_user.teams.includes(:organisation).find(team_id)
31+
def teams
32+
if Settings.cis2.enabled
33+
cis2_info
34+
.organisation
35+
.teams
36+
.where(workgroup: cis2_info.workgroups)
37+
.includes(:organisation)
38+
else
39+
current_user.teams.includes(:organisation)
40+
end
41+
end
2942

30-
delegate :organisation, to: :team
43+
private
3144

32-
def team_id_values = current_user.teams.pluck(:id)
45+
def team_id_values = teams.pluck(:id)
3346
end

app/models/cis2_info.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class CIS2Info
1414
attribute :role_name
1515
attribute :role_code
1616
attribute :workgroups, array: true
17+
attribute :team_workgroup
1718
attribute :has_other_roles, :boolean
1819

1920
def present? = attributes.compact_blank.present?
@@ -25,6 +26,14 @@ def organisation
2526
end
2627
end
2728

29+
def team
30+
@team ||=
31+
if (workgroup = team_workgroup).present? &&
32+
workgroups&.include?(workgroup)
33+
Team.find_by(organisation:, workgroup:)
34+
end
35+
end
36+
2837
def has_workgroup? = workgroups&.include?(WORKGROUP) || false
2938

3039
def is_admin? = role_code == ADMIN_ROLE

app/models/user.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class User < ApplicationRecord
3939
end
4040

4141
has_and_belongs_to_many :teams
42+
has_many :organisations, -> { distinct }, through: :teams
4243

4344
has_many :programmes, through: :teams
4445

@@ -96,13 +97,7 @@ def self.find_or_create_from_cis2_oidc(userinfo, teams)
9697

9798
def selected_organisation = cis2_info.organisation
9899

99-
def selected_team
100-
# TODO: Select the right team based on the user's workgroup.
101-
@selected_team ||=
102-
Team.includes(:location_programme_year_groups, :programmes).find_by(
103-
organisation: selected_organisation
104-
)
105-
end
100+
def selected_team = cis2_info.team
106101

107102
def requires_email_and_password?
108103
provider.blank? || uid.blank?

app/views/users/teams/new.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
<%= f.govuk_error_summary %>
77

88
<%= f.govuk_collection_radio_buttons :team_id,
9-
current_user.teams.includes(:organisation),
9+
@form.teams,
1010
:id,
1111
:name,
12-
-> { _1.organisation.ods_code },
12+
-> { "#{_1.workgroup} (#{_1.organisation.ods_code})" },
1313
legend: { text: legend, size: "xl", tag: "h1" } %>
1414

1515
<%= f.govuk_submit %>

spec/factories/users.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,18 @@
3838
uploaded_by
3939
] do
4040
transient do
41-
team { Team.first || create(:team) }
41+
team { Team.includes(:organisation).first || create(:team) }
4242

4343
role_code { CIS2Info::NURSE_ROLE }
4444
role_workgroups { [CIS2Info::WORKGROUP] }
4545

4646
cis2_info_hash do
4747
{
48-
"organisation_name" => team.name,
4948
"organisation_code" => team.organisation.ods_code,
49+
"organisation_name" => team.name,
5050
"role_code" => role_code,
51-
"workgroups" => role_workgroups
51+
"team_workgroup" => team.workgroup,
52+
"workgroups" => (role_workgroups || []) + [team.workgroup]
5253
}
5354
end
5455
end

0 commit comments

Comments
 (0)