diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 7674c944537fe0436240130463b1a7553459bda3..36682a1b560baa6cd32cdc2149dd1f5af3d6133a 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -18,4 +18,8 @@ class ApplicationMailer < ActionMailer::Base 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 0000000000000000000000000000000000000000..51b714e9f4b8c9d9138651a128528f8a22bef850 --- /dev/null +++ b/app/mailers/extranet_mailer.rb @@ -0,0 +1,24 @@ +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.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 + 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 5bf79a6e213a75a80c348d64285abc4e38a104ae..3a229aa2769833e26fc069d1eba40fbeb3886f3d 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 c39158c0e5ee1601ea8562e2f2f301d5476cee5e..8165e5839e98e1695573ebfb4fbda3e76d486a3f 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -9,7 +9,7 @@ <div class="col-lg-6"> <%= 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 0000000000000000000000000000000000000000..e9ffed92af2c88fa493acbb06834bdd6cc8d961b --- /dev/null +++ b/app/views/mailers/extranet/invitation_message.html.erb @@ -0,0 +1,15 @@ +<p><%= t('mailers.extranet.invitation_message.text_line_1_html', extranet: @extranet_name) %></p> +<% if @user.present? %> + <p> + <%= 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)) %> + </p> +<% else %> + <p> + <%= 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)) %> + </p> +<% end %> +<p><%= t('mailers.yours') %></p> diff --git a/config/locales/en.yml b/config/locales/en.yml index 8b0da04a9c69c34ad57eac7d7187963a4f86fa33..7a0e4aee8f97a2b0c7e7943219c8bfb22cb6a137 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 <b>%{email}</b> by clicking <a href="%{sign_in_url}">here</a>. + text_line_2_without_user_html: You can sign up with your email address <b>%{email}</b> by clicking <a href="%{sign_up_url}">here</a>. 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 0eb6e1be0c763566c081c6075272255fd9475e9e..e5935d087bb6851d50fcfb5dd30b3937795e5a20 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 <b>%{email}</b> en cliquant <a href="%{sign_in_url}">ici</a>. + text_line_2_without_user_html: Vous pouvez vous y inscrire avec votre adresse email <b>%{email}</b> en cliquant <a href="%{sign_up_url}">ici</a>. 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 e1e3e1f02cf37894d50ec34be868762a9c718e9b..c907f0e8c7f9bb2f9bc6ac5f8acb2e6045bf1575 100644 --- a/test/mailers/previews/base_mailer_preview.rb +++ b/test/mailers/previews/base_mailer_preview.rb @@ -10,10 +10,18 @@ class BaseMailerPreview < ActionMailer::Preview @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 0000000000000000000000000000000000000000..9fc5b800b2347dc200075158bc98cce4b61dbe1f --- /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