diff --git a/app/controllers/account/organisations_controller.rb b/app/controllers/account/organisations_controller.rb index 30d215e0c2..6eb3908b35 100644 --- a/app/controllers/account/organisations_controller.rb +++ b/app/controllers/account/organisations_controller.rb @@ -6,6 +6,13 @@ class OrganisationsController < WebController skip_before_action :redirect_if_account_not_completed def edit + @organisations = Organisation.for_email(current_user.email).order(:name) + + if @organisations.empty? + render :unsupported_email + return + end + @organisation_input = OrganisationInput.new(user: current_user).assign_form_values end diff --git a/app/models/org_domain.rb b/app/models/org_domain.rb new file mode 100644 index 0000000000..aa966f2d49 --- /dev/null +++ b/app/models/org_domain.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class OrgDomain < ApplicationRecord + belongs_to :organisation + + validates :domain, presence: true + validates :domain, uniqueness: { scope: :organisation_id, case_sensitive: false } + + before_validation -> { self.domain = domain&.downcase&.strip } +end diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 1209911eee..ada22dcb8a 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -4,6 +4,7 @@ class Organisation < ApplicationRecord has_many :groups has_many :users + has_many :org_domains, dependent: :destroy has_many :mou_signatures scope :not_closed, -> { where(closed: false) } @@ -28,4 +29,13 @@ def as_json(options = {}) options[:methods] ||= %i[organisation_admin_users] super(options) end + + def self.for_email(email) + domain = email.split('@').last.downcase + + not_closed + .joins(:org_domains) + .where(org_domains: { domain: domain }) + .distinct + end end diff --git a/app/views/account/organisations/edit.html.erb b/app/views/account/organisations/edit.html.erb index 66261814e3..33560477e0 100644 --- a/app/views/account/organisations/edit.html.erb +++ b/app/views/account/organisations/edit.html.erb @@ -10,7 +10,7 @@ f, attribute_name: :organisation_id, form_field: f.govuk_collection_select(:organisation_id, - Organisation.not_closed.order(:name), + @organisations, :id, :name_with_abbreviation, class: ['govuk-!-width-three-quarters'], diff --git a/app/views/account/organisations/unsupported_email.html.erb b/app/views/account/organisations/unsupported_email.html.erb new file mode 100644 index 0000000000..55e76ac700 --- /dev/null +++ b/app/views/account/organisations/unsupported_email.html.erb @@ -0,0 +1,9 @@ +
+ Your email address (<%= current_user.email %>) is not associated with a supported organisation. +
+ <%= govuk_details(summary_text: t('account.organisation_details_summary'), text: t('account.organisation_text_html', contact_link: contact_link)) %> +