From c3fde925e50acf4239aa7cee31395e85ac608c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= Date: Thu, 23 Jan 2025 10:14:04 +0100 Subject: [PATCH 1/2] add extranet invitation --- app/mailers/application_mailer.rb | 4 ++++ app/mailers/extranet_mailer.rb | 23 +++++++++++++++++++ .../communication/extranet/connection.rb | 10 ++++++++ app/views/devise/registrations/new.html.erb | 2 +- .../extranet/invitation_message.html.erb | 15 ++++++++++++ config/locales/en.yml | 6 +++++ config/locales/fr.yml | 6 +++++ test/mailers/previews/base_mailer_preview.rb | 8 +++++++ .../previews/extranet_mailer_preview.rb | 10 ++++++++ 9 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 app/mailers/extranet_mailer.rb create mode 100644 app/views/mailers/extranet/invitation_message.html.erb create mode 100644 test/mailers/previews/extranet_mailer_preview.rb diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 7674c94453..36682a1b56 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -18,4 +18,8 @@ def merge_with_university_infos(university, opts) opts[:from] = opts[:reply_to] = university.mail_from[:full] opts end + + def should_send?(email) + Rails.env.production? || email.end_with?(*Rails.application.config.internal_domains) + end end diff --git a/app/mailers/extranet_mailer.rb b/app/mailers/extranet_mailer.rb new file mode 100644 index 0000000000..c2cbfdb85e --- /dev/null +++ b/app/mailers/extranet_mailer.rb @@ -0,0 +1,23 @@ +class ExtranetMailer < ApplicationMailer + helper :application # gives access to all helpers defined within `application_helper`. + default template_path: 'mailers/extranet' + + def invitation_message(extranet, person) + @extranet = extranet + @person = person + university = @extranet.university + merge_with_university_infos(university, {}) + + @user = @person.user + # If the person has a user, we use the user's email in priority as it can be used for login. + @email = @user.present? ? @user.email : @person.email + language = @user.present? ? @user.language : university.default_language + @extranet_name = @extranet.to_s_in(language) + + I18n.with_locale(language.iso_code) do + subject = t('mailers.extranet.invitation_message.subject', extranet: @extranet_name) + mail(from: university.mail_from[:full], to: @email, subject: subject) if should_send?(@email) + end + end + +end diff --git a/app/models/communication/extranet/connection.rb b/app/models/communication/extranet/connection.rb index 5bf79a6e21..3a229aa276 100644 --- a/app/models/communication/extranet/connection.rb +++ b/app/models/communication/extranet/connection.rb @@ -26,7 +26,17 @@ class Communication::Extranet::Connection < ApplicationRecord belongs_to :extranet, class_name: 'Communication::Extranet' belongs_to :about, polymorphic: true + after_create_commit :send_invitation_to_person, if: -> { about.is_a?(University::Person) } + def self.permitted_about_classes [University::Organization, University::Person] end + + protected + + def send_invitation_to_person + # Do not send invitation if there is no email on the person's user or the person itself + return unless about.user.try(:email).present? || about.email.present? + ExtranetMailer.invitation_message(extranet, about).deliver_later + end end diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index c39158c0e5..8165e5839e 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -9,7 +9,7 @@
<%= f.input :email, required: true, - input_html: { autocomplete: "email" } %> + input_html: { autocomplete: "email", value: params[:email] } %> <%= f.input :first_name, required: true, autofocus: true, diff --git a/app/views/mailers/extranet/invitation_message.html.erb b/app/views/mailers/extranet/invitation_message.html.erb new file mode 100644 index 0000000000..e9ffed92af --- /dev/null +++ b/app/views/mailers/extranet/invitation_message.html.erb @@ -0,0 +1,15 @@ +

<%= t('mailers.extranet.invitation_message.text_line_1_html', extranet: @extranet_name) %>

+<% if @user.present? %> +

+ <%= t('mailers.extranet.invitation_message.text_line_2_with_user_html', + email: @email, + sign_in_url: new_user_session_url(email: @email, host: @extranet.host)) %> +

+<% else %> +

+ <%= t('mailers.extranet.invitation_message.text_line_2_without_user_html', + email: @email, + sign_up_url: new_user_registration_url(email: @email, host: @extranet.host)) %> +

+<% end %> +

<%= t('mailers.yours') %>

diff --git a/config/locales/en.yml b/config/locales/en.yml index 8b0da04a9c..7a0e4aee8f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -409,6 +409,12 @@ en: subtitle: Sign in to your account to continue look_feel: Look & feel mailers: + extranet: + invitation_message: + subject: You now have access to the “%{extranet}” extranet + text_line_1_html: You now have access to the “%{extranet}” extranet. + text_line_2_with_user_html: You can sign in with your email address %{email} by clicking here. + text_line_2_without_user_html: You can sign up with your email address %{email} by clicking here. notifications: import: subject_with_errors: Your import has been processed with errors diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 0eb6e1be0c..e5935d087b 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -409,6 +409,12 @@ fr: subtitle: Vous devez être authentifié pour continuer look_feel: Look & feel mailers: + extranet: + invitation_message: + subject: Vous avez désormais accès à l'extranet « %{extranet} » + text_line_1_html: Vous avez désormais accès à l'extranet « %{extranet} ». + text_line_2_with_user_html: Vous pouvez vous y connecter avec votre adresse email %{email} en cliquant ici. + text_line_2_without_user_html: Vous pouvez vous y inscrire avec votre adresse email %{email} en cliquant ici. notifications: import: subject_with_errors: Votre import a bien été traité mais comporte des erreurs diff --git a/test/mailers/previews/base_mailer_preview.rb b/test/mailers/previews/base_mailer_preview.rb index e1e3e1f02c..c907f0e8c7 100644 --- a/test/mailers/previews/base_mailer_preview.rb +++ b/test/mailers/previews/base_mailer_preview.rb @@ -10,10 +10,18 @@ def user @user ||= university.users.first end + def person + @person ||= university.people.first + end + def website @website ||= university.communication_websites.first end + def extranet + @extranet ||= university.communication_extranets.first + end + def organizations_import @organizations_import ||= Import.new( id: SecureRandom.uuid, diff --git a/test/mailers/previews/extranet_mailer_preview.rb b/test/mailers/previews/extranet_mailer_preview.rb new file mode 100644 index 0000000000..9fc5b800b2 --- /dev/null +++ b/test/mailers/previews/extranet_mailer_preview.rb @@ -0,0 +1,10 @@ +# Preview all emails at http://localhost:3000/rails/mailers/extranet_mailer + +class ExtranetMailerPreview < BaseMailerPreview + + # Preview this email at http://localhost:3000/rails/mailers/extranet_mailer/invitation_message + def invitation_message + ExtranetMailer.invitation_message(extranet, person) + end + +end From 2b48469dad1df13c669c140cf1b3783f3f902869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= Date: Thu, 23 Jan 2025 11:06:28 +0100 Subject: [PATCH 2/2] fix --- app/mailers/extranet_mailer.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/mailers/extranet_mailer.rb b/app/mailers/extranet_mailer.rb index c2cbfdb85e..51b714e9f4 100644 --- a/app/mailers/extranet_mailer.rb +++ b/app/mailers/extranet_mailer.rb @@ -10,8 +10,9 @@ def invitation_message(extranet, person) @user = @person.user # If the person has a user, we use the user's email in priority as it can be used for login. - @email = @user.present? ? @user.email : @person.email - language = @user.present? ? @user.language : university.default_language + @email = @user.try(:email) || @person.email + + language = @user.try(:language) || university.default_language @extranet_name = @extranet.to_s_in(language) I18n.with_locale(language.iso_code) do