Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/controllers/account/organisations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions app/models/org_domain.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions app/models/organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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
2 changes: 1 addition & 1 deletion app/views/account/organisations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
9 changes: 9 additions & 0 deletions app/views/account/organisations/unsupported_email.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l">We do not recognise your email address</h1>
<p class="govuk-body">
Your email address (<%= current_user.email %>) is not associated with a supported organisation.
</p>
<%= govuk_details(summary_text: t('account.organisation_details_summary'), text: t('account.organisation_text_html', contact_link: contact_link)) %>
</div>
</div>
12 changes: 12 additions & 0 deletions db/migrate/20260622151619_create_org_domains.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateOrgDomains < ActiveRecord::Migration[8.1]
def change
create_table :org_domains do |t|
t.references :organisation, null: false, foreign_key: true
t.string :domain, null: false
t.timestamps
end

add_index :org_domains, [:organisation_id, :domain], unique: true, name: "index_org_domains_unique"
add_index :org_domains, :domain
end
end
13 changes: 12 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading