Skip to content

Commit 00cf834

Browse files
authored
Merge pull request #4456 from nhsuk/seed-user-roles
Fix user roles in seeds
2 parents 169e58c + 3791454 commit 00cf834

File tree

3 files changed

+13
-22
lines changed

3 files changed

+13
-22
lines changed

app/controllers/users/sessions_controller.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,4 @@ class Users::SessionsController < Devise::SessionsController
88
before_action :store_redirect_uri!, only: :new
99

1010
layout "one_half"
11-
12-
def create
13-
super { |user| user.update!(show_in_suppliers: user.is_nurse?) }
14-
end
1511
end

config/initializers/devise.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,12 @@
407407
user&.update!(session_token: nil, reporting_api_session_token: nil)
408408
end
409409
else
410-
Warden::Manager.after_authentication do |user, _auth, _opts|
410+
Warden::Manager.after_authentication do |user, _warden, _opts|
411411
user&.reporting_api_session_token = SecureRandom.hex(32)
412+
user&.show_in_suppliers = user&.is_nurse?
412413
end
413414

414-
Warden::Manager.before_logout do |user, _auth, _opts|
415+
Warden::Manager.before_logout do |user, _warden, _opts|
415416
# we use this syntax to stop CodeQL complaining about bypassing validations
416417
# (which we have to do to support local development when password auth is enabled
417418
# and you're using the example user created in db/seeds)

db/seeds.rb

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,28 @@ def create_team(ods_code:)
3232
)
3333
end
3434

35-
def create_user(team:, email: nil, uid: nil, fallback_role: :nurse)
35+
def create_user(role, team:, email: nil, uid: nil)
3636
if uid
3737
User.find_by(uid:) ||
3838
FactoryBot.create(
39-
:user,
39+
role,
4040
uid:,
4141
family_name: "Flo",
4242
given_name: "Nurse",
4343
email: "nurse.flo@example.nhs.uk",
4444
provider: "cis2",
45-
team:,
46-
fallback_role:
45+
team:
4746
# password: Do not set this as they should not log in via password
4847
)
4948
elsif email
5049
User.find_by(email:) ||
5150
FactoryBot.create(
52-
:user,
51+
role,
5352
family_name: email.split("@").first.split(".").last.capitalize,
5453
given_name: email.split("@").first.split(".").first.capitalize,
5554
email:,
5655
password: email,
57-
team:,
58-
fallback_role:
56+
team:
5957
)
6058
else
6159
raise "No email or UID provided"
@@ -259,14 +257,10 @@ def create_team_sessions(user, team)
259257
# Don't create Nurse Joy's team on a CIS2 env, because password authentication
260258
# is not available and password= fails to run.
261259
team = create_team(ods_code: "R1L")
262-
user = create_user(team:, email: "nurse.joy@example.com")
263-
create_user(team:, email: "admin.hope@example.com", fallback_role: "admin")
264-
create_user(team:, email: "superuser@example.com", fallback_role: "superuser")
265-
create_user(
266-
team:,
267-
email: "hca@example.com",
268-
fallback_role: "healthcare_assistant"
269-
)
260+
user = create_user(:nurse, team:, email: "nurse.joy@example.com")
261+
create_user(:admin, team:, email: "admin.hope@example.com")
262+
create_user(:superuser, team:, email: "superuser@example.com")
263+
create_user(:healthcare_assistant, team:, email: "hca@example.com")
270264

271265
attach_sample_of_schools_to(team)
272266

@@ -285,7 +279,7 @@ def create_team_sessions(user, team)
285279

286280
# CIS2 team - the ODS code and user UID need to match the values in the CIS2 env
287281
team = create_team(ods_code: "A9A5A")
288-
user = create_user(team:, uid: "555057896106")
282+
user = create_user(:nurse, team:, uid: "555057896106")
289283

290284
attach_sample_of_schools_to(team)
291285

0 commit comments

Comments
 (0)