Browse Source

Enable support users to invite users to any organisation

pull/576/head
baarkerlounger 3 years ago
parent
commit
be1c780189
  1. 16
      app/controllers/users_controller.rb
  2. 13
      app/views/users/new.html.erb
  3. 19
      spec/requests/users_controller_spec.rb

16
app/controllers/users_controller.rb

@ -51,7 +51,7 @@ class UsersController < ApplicationController
user = User.create(user_params.merge(org_params).merge(password_params)) user = User.create(user_params.merge(org_params).merge(password_params))
if user.persisted? if user.persisted?
user.send_reset_password_instructions user.send_reset_password_instructions
redirect_to users_organisation_path(current_user.organisation) redirect_to created_user_redirect_path
else else
@resource.errors.add :email, I18n.t("validations.email.taken") @resource.errors.add :email, I18n.t("validations.email.taken")
render :new, status: :unprocessable_entity render :new, status: :unprocessable_entity
@ -83,6 +83,8 @@ private
end end
def org_params def org_params
return {} if current_user.support?
{ organisation: current_user.organisation } { organisation: current_user.organisation }
end end
@ -93,8 +95,18 @@ private
else else
params.require(:user).permit(:email, :name, :password, :password_confirmation) params.require(:user).permit(:email, :name, :password, :password_confirmation)
end end
elsif current_user.data_coordinator? || current_user.support? elsif current_user.data_coordinator?
params.require(:user).permit(:email, :name, :role, :is_dpo, :is_key_contact) params.require(:user).permit(:email, :name, :role, :is_dpo, :is_key_contact)
elsif current_user.support?
params.require(:user).permit(:email, :name, :role, :is_dpo, :is_key_contact, :organisation_id)
end
end
def created_user_redirect_path
if current_user.support?
users_path
else
users_organisation_path(current_user.organisation)
end end
end end

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

@ -26,6 +26,19 @@
spellcheck: "false", spellcheck: "false",
value: @resource.email %> value: @resource.email %>
<% if current_user.support? %>
<% null_option = [OpenStruct.new(id: "", name: "Select an option")] %>
<% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %>
<% answer_options = null_option + organisations %>
<%= f.govuk_collection_select :organisation_id,
answer_options,
:id,
:name,
label: { text: "Organisation", size: "m" },
options: { disabled: [""], selected: "" } %>
<% end %>
<% roles = current_user.assignable_roles.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> <% roles = current_user.assignable_roles.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %>
<%= f.govuk_collection_radio_buttons :role, <%= f.govuk_collection_radio_buttons :role,

19
spec/requests/users_controller_spec.rb

@ -1071,12 +1071,15 @@ RSpec.describe UsersController, type: :request do
end end
describe "#create" do describe "#create" do
let(:organisation) { FactoryBot.create(:organisation) }
let(:email) { "new_user@example.com" }
let(:params) do let(:params) do
{ {
"user": { "user": {
name: "new user", name: "new user",
email: "new_user@example.com", email:,
role: "data_coordinator", role: "data_coordinator",
organisation_id: organisation.id,
}, },
} }
end end
@ -1090,9 +1093,14 @@ RSpec.describe UsersController, type: :request do
expect { request }.to change(User, :count).by(1) expect { request }.to change(User, :count).by(1)
end end
it "redirects back to organisation users page" do it "adds the user to the correct organisation" do
request request
expect(response).to redirect_to("/organisations/#{user.organisation.id}/users") expect(User.find_by(email:).organisation).to eq(organisation)
end
it "redirects back to users page" do
request
expect(response).to redirect_to("/users")
end end
context "when the email is already taken" do context "when the email is already taken" do
@ -1133,6 +1141,11 @@ RSpec.describe UsersController, type: :request do
get "/users/new" get "/users/new"
expect(page).to have_field("user-role-support-field") expect(page).to have_field("user-role-support-field")
end end
it "can assign organisation to the new user" do
get "/users/new"
expect(page).to have_field("user-organisation-id-field")
end
end end
end end

Loading…
Cancel
Save