Skip to content

Commit

Permalink
Allow non-support users to add other users to their organisation
Browse files Browse the repository at this point in the history
  • Loading branch information
elceebee committed Jan 29, 2024
1 parent 2d140d0 commit 3243083
Show file tree
Hide file tree
Showing 29 changed files with 793 additions and 21 deletions.
44 changes: 44 additions & 0 deletions app/controllers/placements/providers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Placements::Providers::UsersController < ApplicationController
before_action :set_provider

def index
@users = @provider.users.order("LOWER(first_name)")
end

def show
@user = @provider.users.find(params.require(:id))
end

def new
@user_form = params[:user_invite_form].present? ? user_form : UserInviteForm.new
end

def check
render :new unless user_form.valid?
end

def create
if user_form.invite
redirect_to placements_provider_users_path(@provider)
flash[:success] = t(".user_added")
else
render :new
end
end

private

def set_provider
@provider = Placements::Provider.find(params.fetch(:provider_id))
end

def user_params
params.require(:user_invite_form)
.permit(:first_name, :last_name, :email)
.merge({ service: current_service, organisation: @provider })
end

def user_form
@user_form ||= UserInviteForm.new(user_params)
end
end
44 changes: 44 additions & 0 deletions app/controllers/placements/schools/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Placements::Schools::UsersController < ApplicationController
before_action :set_school

def index
@users = @school.users.order("LOWER(first_name)")
end

def show
@user = @school.users.find(params.require(:id))
end

def new
@user_form = params[:user_invite_form].present? ? user_form : UserInviteForm.new
end

def check
render :new unless user_form.valid?
end

def create
if user_form.invite
redirect_to placements_school_users_path(@school)
flash[:success] = t(".user_added")
else
render :new
end
end

private

def set_school
@school = Placements::School.find(params.fetch(:school_id))
end

def user_params
params.require(:user_invite_form)
.permit(:first_name, :last_name, :email)
.merge({ service: current_service, organisation: @school })
end

def user_form
@user_form ||= UserInviteForm.new(user_params)
end
end
45 changes: 45 additions & 0 deletions app/helpers/routes_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ def organisations_path
}.fetch current_service
end

def organisation_users_path(organisation)
case organisation
when School
placements_school_users_path(organisation)
when Provider
placements_provider_users_path(organisation)
end
end

def placements_support_organisation_path(organisation)
case organisation
when School
Expand All @@ -43,6 +52,15 @@ def placements_support_organisation_path(organisation)
end
end

def placements_organisation_user_path(organisation, user)
case organisation
when School
placements_school_user_path(organisation, user)
when Provider
placements_provider_user_path(organisation, user)
end
end

def placements_organisation_path(organisation)
case organisation
when School
Expand All @@ -60,4 +78,31 @@ def placements_support_users_path(organisation)
placements_support_provider_users_path(organisation)
end
end

def check_placements_organisation_users_path(organisation)
case organisation
when School
check_placements_school_users_path
when Provider
check_placements_provider_users_path
end
end

def placements_organisation_users_path(organisation)
case organisation
when School
placements_school_users_path(organisation)
when Provider
placements_provider_users_path(organisation)
end
end

def new_placements_organisation_user_path(organisation, params = {})
case organisation
when School
new_placements_school_user_path(organisation, params)
when Provider
new_placements_provider_user_path(organisation, params)
end
end
end
4 changes: 4 additions & 0 deletions app/models/placements/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ class Placements::User < User
def service
:placements
end

def organisation_count
providers.count + schools.count
end
end
2 changes: 1 addition & 1 deletion app/views/placements/_primary_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<%= render PrimaryNavigationComponent.new do |component| %>
<% component.with_navigation_item t(".placements"), "#", current: current_navigation == :placements %>
<% component.with_navigation_item t(".mentors"), "#", current: current_navigation == :mentors %>
<% component.with_navigation_item t(".users"), "#", current: current_navigation == :users %>
<% component.with_navigation_item t(".users"), organisation_users_path(organisation), current: current_navigation == :users %>
<% component.with_navigation_item t(".organisation_details"),
placements_organisation_path(organisation),
current: current_navigation == :organisation_details %>
Expand Down
58 changes: 58 additions & 0 deletions app/views/placements/organisations/users/_check_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<%# locals: (user: nil, organisation: nil) -%>
<div class="govuk-width-container">
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= form_with(model: user, url: placements_organisation_users_path(organisation), method: :post) do |f| %>
<%= f.hidden_field :first_name, value: user.first_name %>
<%= f.hidden_field :last_name, value: user.last_name %>
<%= f.hidden_field :email, value: user.email %>

<label class="govuk-label govuk-label--l">
<span class="govuk-caption-l"><%= t(".add_user", organisation_name: organisation.name) %></span>
<%= t(".check_your_answers") %>
</label>

<%= govuk_summary_list do |summary_list| %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: User.human_attribute_name(:first_name)) %>
<% row.with_value(text: user.first_name) %>
<% row.with_action(text: t(".change"),
href: new_placements_organisation_user_path(organisation, user.as_form_params),
html_attributes: {
class: "govuk-link--no-visited-state",
}) %>
<% end %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: User.human_attribute_name(:last_name)) %>
<% row.with_value(text: user.last_name) %>
<% row.with_action(text: t(".change"),
href: new_placements_organisation_user_path(organisation, user.as_form_params),
html_attributes: {
class: "govuk-link--no-visited-state",
}) %>
<% end %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: User.human_attribute_name(:email)) %>
<% row.with_value(text: user.email) %>
<% row.with_action(text: t(".change"),
href: new_placements_organisation_user_path(organisation, user.as_form_params),
html_attributes: {
class: "govuk-link--no-visited-state",
}) %>
<% end %>
<% end %>

<div class="govuk-warning-text">
<span class="govuk-warning-text__icon" aria-hidden="true">!</span>
<strong class="govuk-warning-text__text"><%= t(".warning", organisation_name: organisation.name) %></strong>
</div>

<%= f.govuk_submit t(".add_user") %>

<p class="govuk-body">
<%= govuk_link_to(t(".cancel"), placements_organisation_users_path(organisation)) %>
</p>
<% end %>
<div>
<div>
</div>
33 changes: 33 additions & 0 deletions app/views/placements/organisations/users/_new_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%# locals: (organisation: nil, user: nil) -%>
<%= form_with(model: user, url: check_placements_organisation_users_path(organisation), method: "get") do |f| %>
<%= f.govuk_error_summary %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<span class="govuk-caption-l"><%= t(".add_user", organisation_name: organisation.name) %></span>
<h2 class="govuk-heading-l"><%= t(".personal_details") %></h2>

<div class="govuk-form-group">
<%= f.govuk_text_field :first_name,
class: "govuk-input--width-20",
label: { text: User.human_attribute_name(:first_name), size: "s" } %>
</div>

<div class="govuk-form-group">
<%= f.govuk_text_field :last_name,
class: "govuk-input--width-20",
label: { text: User.human_attribute_name(:last_name), size: "s" } %>
</div>

<div class="govuk-form-group">
<%= f.govuk_text_field :email, label: { text: User.human_attribute_name(:email), size: "s" } %>
</div>

<%= f.govuk_submit t(".continue") %>

<p class="govuk-body">
<%= govuk_link_to(t(".cancel"), placements_organisation_users_path(organisation)) %>
</p>
</div>
</div>
<% end %>
14 changes: 14 additions & 0 deletions app/views/placements/organisations/users/_user_details.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%= govuk_summary_list do |summary_list| %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: User.human_attribute_name(:first_name)) %>
<% row.with_value(text: @user.first_name) %>
<% end %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: User.human_attribute_name(:last_name)) %>
<% row.with_value(text: @user.last_name) %>
<% end %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: User.human_attribute_name(:email)) %>
<% row.with_value(text: @user.email) %>
<% end %>
<% end %>
20 changes: 20 additions & 0 deletions app/views/placements/organisations/users/_users_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<% if users.any? %>
<%= govuk_table do |table| %>
<% table.with_head do |head| %>
<% head.with_row do |row| %>
<% row.with_cell(header: true, text: t(".name")) %>
<% row.with_cell(header: true, text: User.human_attribute_name(:email)) %>
<% end %>
<% end %>
<% table.with_body do |body| %>
<% users.each do |user| %>
<% body.with_row do |row| %>
<% row.with_cell(text: govuk_link_to(user.full_name, placements_organisation_user_path(organisation, user))) %>
<% row.with_cell(text: user.email) %>
<% end %>
<% end %>
<% end %>
<% end %>
<% else %>
<p><%= t("no_users", organisation_name: organisation.name) %></p>
<% end %>
28 changes: 28 additions & 0 deletions app/views/placements/providers/users/check.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<%= content_for :page_title, sanitize(t(".check_your_answers", provider_name: @provider.name)) %>
<% if current_user.organisation_count > 1 %>
<%= content_for(:header_content) do %>
<%= render(ContentHeaderComponent.new(
title: @provider.name,
actions: [govuk_link_to(t(".change_organisation"), placements_organisations_path, no_visited_state: true)],
)) %>
<% end %>
<% end %>
<%= render "placements/primary_navigation", current_navigation: :users, organisation: @provider %>
<%= content_for(:before_content) do %>
<%= govuk_back_link(href: new_placements_provider_user_path(
@user_form.as_form_params.merge(provider_id: @provider.id),
)) %>
<% end %>

<div class="govuk-width-container">
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<label class="govuk-label govuk-label--l">
<span class="govuk-caption-l"><%= t(".add_user") %></span>
<%= t(".check_your_answers") %>
</label>

<%= render "placements/organisations/users/check_form", user: @user_form, organisation: @provider %>
</div>
</div>
</div>
20 changes: 20 additions & 0 deletions app/views/placements/providers/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<% content_for(:page_title) { t(".users") } %>
<% if current_user.organisation_count > 1 %>
<%= content_for(:header_content) do %>
<%= render(ContentHeaderComponent.new(
title: @provider.name,
actions: [govuk_link_to(t(".change_organisation"), placements_organisations_path, no_visited_state: true)],
)) %>
<% end %>
<% end %>
<%= render "placements/primary_navigation", organisation: @provider, current_navigation: :users %>

<div class="govuk-width-container">
<h1 class="govuk-heading-l"><%= t(".users") %></h1>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= govuk_button_to(t(".add_user"), new_placements_provider_user_path, method: :get) %>
<%= render "placements/organisations/users/users_list", users: @users, organisation: @provider %>
</div>
</div>
</div>
18 changes: 18 additions & 0 deletions app/views/placements/providers/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%= content_for :page_title, t(".page_title") %>
<%= render "placements/primary_navigation", current_navigation: :users, organisation: @provider %>
<% if current_user.organisation_count > 1 %>
<%= content_for(:header_content) do %>
<%= render(ContentHeaderComponent.new(
title: @provider.name,
actions: [govuk_link_to(t(".change_organisation"), placements_organisations_path, no_visited_state: true)],
)) %>
<% end %>
<% end %>

<%= content_for(:before_content) do %>
<%= govuk_back_link(href: placements_provider_users_path(@provider)) %>
<% end %>

<div class="govuk-width-container">
<%= render "placements/organisations/users/new_form", user: @user_form, organisation: @provider %>
</div>
22 changes: 22 additions & 0 deletions app/views/placements/providers/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<% if current_user.organisation_count > 1 %>
<%= content_for(:header_content) do %>
<%= render(ContentHeaderComponent.new(
title: @provider.name,
actions: [govuk_link_to(t(".change_organisation"), placements_organisations_path, no_visited_state: true)],
)) %>
<% end %>
<% end %>
<%= render "placements/primary_navigation", organisation: @provider, current_navigation: :users %>
<%= content_for :page_title, sanitize(@user.full_name) %>
<%= content_for(:before_content) do %>
<%= govuk_back_link(href: placements_provider_users_path(@provider)) %>
<% end %>

<div class="govuk-width-container">
<h1 class="govuk-heading-l"><%= @user.full_name %></h1>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= render "placements/organisations/user_details", user: @user %>
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/placements/schools/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= content_for :page_title, t(".organisation_details") %>
<% if current_user.memberships.many? %>
<% if current_user.organisation_count > 1 %>
<%= content_for(:header_content) do %>
<%= render(ContentHeaderComponent.new(
title: @school.name,
Expand Down
Loading

0 comments on commit 3243083

Please sign in to comment.