From 1efeb48e0268e06e495ceb9615d1bd320417761e Mon Sep 17 00:00:00 2001 From: chao-xian Date: Mon, 22 Jun 2026 17:49:46 +0100 Subject: [PATCH] WIP --- app/controllers/account/organisations_controller.rb | 7 +++++++ app/models/org_domain.rb | 10 ++++++++++ app/models/organisation.rb | 10 ++++++++++ app/views/account/organisations/edit.html.erb | 2 +- .../organisations/unsupported_email.html.erb | 9 +++++++++ db/migrate/20260622151619_create_org_domains.rb | 12 ++++++++++++ db/schema.rb | 13 ++++++++++++- 7 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 app/models/org_domain.rb create mode 100644 app/views/account/organisations/unsupported_email.html.erb create mode 100644 db/migrate/20260622151619_create_org_domains.rb 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 @@ +
+
+

We do not recognise your email address

+

+ 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)) %> +
+
diff --git a/db/migrate/20260622151619_create_org_domains.rb b/db/migrate/20260622151619_create_org_domains.rb new file mode 100644 index 0000000000..ff0902bcdd --- /dev/null +++ b/db/migrate/20260622151619_create_org_domains.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index f9401740c5..1aee24074d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2026_06_08_112508) do +ActiveRecord::Schema[8.1].define(version: 2026_06_22_151619) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -199,6 +199,16 @@ t.index ["user_id"], name: "index_mou_signatures_on_user_id_unique_without_organisation_id", unique: true, where: "(organisation_id IS NULL)", comment: "Users can only sign a single MOU without an organisation" end + create_table "org_domains", force: :cascade do |t| + t.datetime "created_at", null: false + t.string "domain", null: false + t.bigint "organisation_id", null: false + t.datetime "updated_at", null: false + t.index ["domain"], name: "index_org_domains_on_domain" + t.index ["organisation_id", "domain"], name: "index_org_domains_unique", unique: true + t.index ["organisation_id"], name: "index_org_domains_on_organisation_id" + end + create_table "organisations", force: :cascade do |t| t.string "abbreviation" t.boolean "closed", default: false @@ -297,6 +307,7 @@ add_foreign_key "memberships", "users", column: "added_by_id" add_foreign_key "mou_signatures", "organisations" add_foreign_key "mou_signatures", "users" + add_foreign_key "org_domains", "organisations" add_foreign_key "page_translations", "pages" add_foreign_key "pages", "forms" add_foreign_key "users", "organisations"