From 0c7f279c616408247c5a5c017e414f54d8a5f2b4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Thu, 23 Jan 2025 16:33:32 +0100
Subject: [PATCH] Invitation Extranet (#2596)

* add extranet invitation

* fix
---
 app/mailers/application_mailer.rb             |  4 ++++
 app/mailers/extranet_mailer.rb                | 24 +++++++++++++++++++
 .../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, 84 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 7674c9445..36682a1b5 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 000000000..51b714e9f
--- /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 5bf79a6e2..3a229aa27 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 c39158c0e..8165e5839 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 000000000..e9ffed92a
--- /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 8b0da04a9..7a0e4aee8 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 0eb6e1be0..e5935d087 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 e1e3e1f02..c907f0e8c 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 000000000..9fc5b800b
--- /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
-- 
GitLab