From 5aa56d293127bc2d8dcc38a042f929c3a4b06de8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com> Date: Thu, 20 Oct 2022 15:08:43 +0200 Subject: [PATCH] extranet.domain -> host ; registration context in devise mailer --- .../admin/communication/extranets_controller.rb | 2 +- .../users/add_context_to_request_params.rb | 16 ++++++++++++++++ .../users/add_university_to_request_params.rb | 14 -------------- .../users/confirmations_controller.rb | 2 +- app/controllers/users/passwords_controller.rb | 2 +- .../users/registrations_controller.rb | 2 +- app/controllers/users/sessions_controller.rb | 2 +- app/mailers/devise_mailer.rb | 8 +++++++- app/models/communication/extranet.rb | 10 +++++----- app/models/user/with_authentication.rb | 17 +++++++++++++++++ app/models/user/with_registration_context.rb | 2 +- .../communication/extranets/_form.html.erb | 2 +- config/locales/communication/en.yml | 2 +- config/locales/communication/fr.yml | 2 +- ...domain_to_host_in_communication_extranets.rb | 5 +++++ db/schema.rb | 6 +++--- test/fixtures/communication/extranets.yml | 6 +++--- test/models/communication/extranet_test.rb | 2 +- 18 files changed, 66 insertions(+), 36 deletions(-) create mode 100644 app/controllers/concerns/users/add_context_to_request_params.rb delete mode 100644 app/controllers/concerns/users/add_university_to_request_params.rb create mode 100644 db/migrate/20221020102322_rename_domain_to_host_in_communication_extranets.rb diff --git a/app/controllers/admin/communication/extranets_controller.rb b/app/controllers/admin/communication/extranets_controller.rb index 2a1123239..c86a894ca 100644 --- a/app/controllers/admin/communication/extranets_controller.rb +++ b/app/controllers/admin/communication/extranets_controller.rb @@ -64,6 +64,6 @@ class Admin::Communication::ExtranetsController < Admin::Communication::Applicat def extranet_params params.require(:communication_extranet) - .permit(:name, :domain, :about_type, :about_id, :registration_contact, :logo, :logo_delete) + .permit(:name, :host, :about_type, :about_id, :registration_contact, :logo, :logo_delete) end end diff --git a/app/controllers/concerns/users/add_context_to_request_params.rb b/app/controllers/concerns/users/add_context_to_request_params.rb new file mode 100644 index 000000000..d0e8c900c --- /dev/null +++ b/app/controllers/concerns/users/add_context_to_request_params.rb @@ -0,0 +1,16 @@ +module Users::AddContextToRequestParams + extend ActiveSupport::Concern + + included do + prepend_before_action :add_context_to_request_params, only: :create + end + + protected + + def add_context_to_request_params + # inject university_id & context in users params submitted + return if request.params[:user].nil? + request.params[:user][:university_id] = current_university.id + request.params[:user][:registration_context] = current_context + end +end diff --git a/app/controllers/concerns/users/add_university_to_request_params.rb b/app/controllers/concerns/users/add_university_to_request_params.rb deleted file mode 100644 index 754392681..000000000 --- a/app/controllers/concerns/users/add_university_to_request_params.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Users::AddUniversityToRequestParams - extend ActiveSupport::Concern - - included do - prepend_before_action :add_university_to_request_params, only: :create - end - - protected - - def add_university_to_request_params - # inject university_id in users params submitted - request.params[:user][:university_id] = current_university.id unless request.params[:user].nil? - end -end diff --git a/app/controllers/users/confirmations_controller.rb b/app/controllers/users/confirmations_controller.rb index 2d4b6116e..14962a300 100644 --- a/app/controllers/users/confirmations_controller.rb +++ b/app/controllers/users/confirmations_controller.rb @@ -1,5 +1,5 @@ class Users::ConfirmationsController < Devise::ConfirmationsController - include Users::AddUniversityToRequestParams + include Users::AddContextToRequestParams include Users::LayoutChoice def resend diff --git a/app/controllers/users/passwords_controller.rb b/app/controllers/users/passwords_controller.rb index 49d967b4a..4902f4ed6 100644 --- a/app/controllers/users/passwords_controller.rb +++ b/app/controllers/users/passwords_controller.rb @@ -1,5 +1,5 @@ class Users::PasswordsController < Devise::PasswordsController - include Users::AddUniversityToRequestParams + include Users::AddContextToRequestParams include Users::LayoutChoice def update diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 8df0d6018..840ef1be6 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -1,5 +1,5 @@ class Users::RegistrationsController < Devise::RegistrationsController - include Users::AddUniversityToRequestParams + include Users::AddContextToRequestParams include Users::LayoutChoice before_action :configure_sign_up_params, only: :create diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb index 41758961c..166de839e 100644 --- a/app/controllers/users/sessions_controller.rb +++ b/app/controllers/users/sessions_controller.rb @@ -1,5 +1,5 @@ class Users::SessionsController < Devise::SessionsController - include Users::AddUniversityToRequestParams + include Users::AddContextToRequestParams include Users::LayoutChoice # DELETE /resource/sign_out diff --git a/app/mailers/devise_mailer.rb b/app/mailers/devise_mailer.rb index 430eee58f..5796201ee 100644 --- a/app/mailers/devise_mailer.rb +++ b/app/mailers/devise_mailer.rb @@ -4,6 +4,7 @@ class DeviseMailer < Devise::Mailer default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views def confirmation_instructions(record, token, opts={}) + @record = record opts = merge_with_university_infos(record.university, opts) I18n.with_locale(record.language.iso_code.to_sym) do super @@ -11,6 +12,7 @@ class DeviseMailer < Devise::Mailer end def reset_password_instructions(record, token, opts={}) + @record = record opts = merge_with_university_infos(record.university, opts) I18n.with_locale(record.language.iso_code.to_sym) do super @@ -18,6 +20,7 @@ class DeviseMailer < Devise::Mailer end def unlock_instructions(record, token, opts={}) + @record = record opts = merge_with_university_infos(record.university, opts) I18n.with_locale(record.language.iso_code.to_sym) do super @@ -25,6 +28,7 @@ class DeviseMailer < Devise::Mailer end def email_changed(record, opts={}) + @record = record opts = merge_with_university_infos(record.university, opts) I18n.with_locale(record.language.iso_code.to_sym) do super @@ -32,6 +36,7 @@ class DeviseMailer < Devise::Mailer end def password_change(record, opts={}) + @record = record opts = merge_with_university_infos(record.university, opts) I18n.with_locale(record.language.iso_code.to_sym) do super @@ -39,6 +44,7 @@ class DeviseMailer < Devise::Mailer end def two_factor_authentication_code(record, code, opts = {}) + @record = record opts = merge_with_university_infos(record.university, opts) @code = code @duration = ActiveSupport::Duration.build(Rails.application.config.devise.direct_otp_valid_for).inspect @@ -49,7 +55,7 @@ class DeviseMailer < Devise::Mailer def default_url_options { - host: @university.host, + host: @record.registration_context.present? ? @record.registration_context.host : @university.host, port: Rails.env.development? ? 3000 : nil } end diff --git a/app/models/communication/extranet.rb b/app/models/communication/extranet.rb index 01b34fd10..adb13abbc 100644 --- a/app/models/communication/extranet.rb +++ b/app/models/communication/extranet.rb @@ -4,7 +4,7 @@ # # id :uuid not null, primary key # about_type :string indexed => [about_id] -# domain :string +# host :string # name :string # registration_contact :string # created_at :datetime not null @@ -25,20 +25,20 @@ class Communication::Extranet < ApplicationRecord include WithAbouts include WithUniversity - validates_presence_of :name, :domain + validates_presence_of :name, :host has_one_attached_deletable :logo scope :ordered, -> { order(:name) } scope :for_search_term, -> (term) { where(" - unaccent(communication_extranets.domain) ILIKE unaccent(:term) OR + unaccent(communication_extranets.host) ILIKE unaccent(:term) OR unaccent(communication_extranets.name) ILIKE unaccent(:term) ", term: "%#{sanitize_sql_like(term)}%") } def self.with_host(host) - find_by domain: host + find_by host: host end def should_show_years? @@ -65,7 +65,7 @@ class Communication::Extranet < ApplicationRecord end def url - "https://#{domain}" + @url ||= Rails.env.development? ? "http://#{host}:3000" : "https://#{host}" end def to_s diff --git a/app/models/user/with_authentication.rb b/app/models/user/with_authentication.rb index 5e68ea750..27d1d9b2b 100644 --- a/app/models/user/with_authentication.rb +++ b/app/models/user/with_authentication.rb @@ -24,6 +24,23 @@ module User::WithAuthentication where(email: warden_conditions[:email].downcase, university_id: warden_conditions[:university_id]).first end + def self.send_confirmation_instructions(attributes = {}) + confirmable = find_by_unconfirmed_email_with_errors(attributes) if reconfirmable + unless confirmable.try(:persisted?) + confirmable = find_or_initialize_with_errors(confirmation_keys, attributes, :not_found) + end + confirmable.registration_context = attributes[:registration_context] if attributes.has_key?(:registration_context) + confirmable.resend_confirmation_instructions if confirmable.persisted? + confirmable + end + + def self.send_unlock_instructions(attributes = {}) + lockable = find_or_initialize_with_errors(unlock_keys, attributes, :not_found) + lockable.registration_context = attributes[:registration_context] if attributes.has_key?(:registration_context) + lockable.resend_unlock_instructions if lockable.persisted? + lockable + end + # Inject a session_token in user salt to prevent Cookie session hijacking # https://makandracards.com/makandra/53562-devise-invalidating-all-sessions-for-a-user def authenticatable_salt diff --git a/app/models/user/with_registration_context.rb b/app/models/user/with_registration_context.rb index 61a8902bd..f737c298b 100644 --- a/app/models/user/with_registration_context.rb +++ b/app/models/user/with_registration_context.rb @@ -4,7 +4,7 @@ module User::WithRegistrationContext included do attr_accessor :registration_context - validate :extranet_access, if: -> { registration_context.is_a?(Communication::Extranet) } + validate :extranet_access, on: :create, if: -> { registration_context.is_a?(Communication::Extranet) } private diff --git a/app/views/admin/communication/extranets/_form.html.erb b/app/views/admin/communication/extranets/_form.html.erb index b6c8ab182..c819a61af 100644 --- a/app/views/admin/communication/extranets/_form.html.erb +++ b/app/views/admin/communication/extranets/_form.html.erb @@ -10,7 +10,7 @@ </div> <div class="card-body"> <%= f.input :name %> - <%= f.input :domain %> + <%= f.input :host %> <%= render 'admin/communication/abouts', f: f, i18n_key: 'activerecord.attributes.communication/extranet.about_' %> <%= f.input :registration_contact %> </div> diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml index 85bef999d..1000cb08a 100644 --- a/config/locales/communication/en.yml +++ b/config/locales/communication/en.yml @@ -48,7 +48,7 @@ en: about_Research::Journal: Journal extranet about_Research::Laboratory: Laboratory extranet about_type: About - domain: Domain + host: Domain name: Name registration_contact: Contact mail for registrations problems communication/website: diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml index bf7a104bc..911fe0aa5 100644 --- a/config/locales/communication/fr.yml +++ b/config/locales/communication/fr.yml @@ -48,7 +48,7 @@ fr: about_Research::Journal: Extranet d'une revue scientifique about_Research::Laboratory: Extranet d'un laboratoire about_type: Type d'extranet - domain: Domaine + host: Domaine name: Nom registration_contact: Mail de contact pour les problèmes d'inscription communication/website: diff --git a/db/migrate/20221020102322_rename_domain_to_host_in_communication_extranets.rb b/db/migrate/20221020102322_rename_domain_to_host_in_communication_extranets.rb new file mode 100644 index 000000000..92ee954d7 --- /dev/null +++ b/db/migrate/20221020102322_rename_domain_to_host_in_communication_extranets.rb @@ -0,0 +1,5 @@ +class RenameDomainToHostInCommunicationExtranets < ActiveRecord::Migration[6.1] + def change + rename_column :communication_extranets, :domain, :host + end +end diff --git a/db/schema.rb b/db/schema.rb index 5876144fc..e96a5a770 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.define(version: 2022_10_17_142108) do +ActiveRecord::Schema.define(version: 2022_10_20_102322) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -44,7 +44,7 @@ ActiveRecord::Schema.define(version: 2022_10_17_142108) do t.text "metadata" t.string "service_name", null: false t.bigint "byte_size", null: false - t.string "checksum", null: false + t.string "checksum" t.datetime "created_at", null: false t.uuid "university_id" t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true @@ -97,7 +97,7 @@ ActiveRecord::Schema.define(version: 2022_10_17_142108) do create_table "communication_extranets", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t| t.string "name" t.uuid "university_id", null: false - t.string "domain" + t.string "host" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.string "about_type" diff --git a/test/fixtures/communication/extranets.yml b/test/fixtures/communication/extranets.yml index cba2a17ed..d052b0ea7 100644 --- a/test/fixtures/communication/extranets.yml +++ b/test/fixtures/communication/extranets.yml @@ -4,7 +4,7 @@ # # id :uuid not null, primary key # about_type :string indexed => [about_id] -# domain :string +# host :string # name :string # registration_contact :string # created_at :datetime not null @@ -25,9 +25,9 @@ one: name: MyString university: default_university - domain: MyString + host: MyString two: name: MyString university: default_university - domain: MyString + host: MyString diff --git a/test/models/communication/extranet_test.rb b/test/models/communication/extranet_test.rb index c51aec18a..dc82d6c69 100644 --- a/test/models/communication/extranet_test.rb +++ b/test/models/communication/extranet_test.rb @@ -4,7 +4,7 @@ # # id :uuid not null, primary key # about_type :string indexed => [about_id] -# domain :string +# host :string # name :string # registration_contact :string # created_at :datetime not null -- GitLab