From 76785a5859135e233ffc48ca7123d0d08f581524 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Thu, 20 Oct 2022 14:54:29 +0200
Subject: [PATCH] wip extranet sso

---
 app/assets/javascripts/admin.js               |  1 -
 .../{university/edit.js => commons/sso.js}    | 11 +++----
 .../javascripts/admin/university/init.js      |  4 ---
 app/models/communication/extranet.rb          |  2 +-
 .../communication/extranets/_form.html.erb    | 17 ++++++++++
 app/views/server/universities/_form.html.erb  |  2 +-
 .../server/universities/_sso_mapping.html.erb | 32 ++++++++++++-------
 config/locales/communication/en.yml           |  6 ++++
 config/locales/communication/fr.yml           |  6 ++++
 ...935_change_extranet_sso_target_url_kind.rb |  5 +++
 db/schema.rb                                  |  4 +--
 test/fixtures/communication/extranets.yml     |  2 +-
 test/models/communication/extranet_test.rb    |  2 +-
 13 files changed, 64 insertions(+), 30 deletions(-)
 rename app/assets/javascripts/admin/{university/edit.js => commons/sso.js} (70%)
 delete mode 100644 app/assets/javascripts/admin/university/init.js
 create mode 100644 db/migrate/20221020124935_change_extranet_sso_target_url_kind.rb

diff --git a/app/assets/javascripts/admin.js b/app/assets/javascripts/admin.js
index 20f2a4a00..cd6e7e2d2 100644
--- a/app/assets/javascripts/admin.js
+++ b/app/assets/javascripts/admin.js
@@ -16,6 +16,5 @@
 //= require_tree ./admin/commons
 //= require_tree ./admin/plugins
 //= require ./admin/communication/init
-//= require ./admin/university/init
 
 window.osuny = {};
diff --git a/app/assets/javascripts/admin/university/edit.js b/app/assets/javascripts/admin/commons/sso.js
similarity index 70%
rename from app/assets/javascripts/admin/university/edit.js
rename to app/assets/javascripts/admin/commons/sso.js
index f28aff778..1009ce910 100644
--- a/app/assets/javascripts/admin/university/edit.js
+++ b/app/assets/javascripts/admin/commons/sso.js
@@ -1,7 +1,7 @@
-window.osuny.university.edit = {
+window.osuny.sso = {
     init: function () {
         'use strict';
-        this.hasSsoInput = document.querySelector('input[type="checkbox"][name="university[has_sso]"]');
+        this.hasSsoInput = document.querySelector('input[type="checkbox"][name$="[has_sso]"]');
         this.hasSsoInput.addEventListener('change', this.onHasSsoChange.bind(this));
         this.ssoFields = document.querySelectorAll('.sso-inputs');
         this.onHasSsoChange();
@@ -30,10 +30,7 @@ window.osuny.university.edit = {
 
 window.addEventListener('DOMContentLoaded', function () {
     'use strict';
-    if (document.body.classList.contains('universities-new')
-        || document.body.classList.contains('universities-create')
-        || document.body.classList.contains('universities-edit')
-        || document.body.classList.contains('universities-update')) {
-        window.osuny.university.edit.init();
+    if (document.querySelector('[name$="[has_sso]"]')) {
+        window.osuny.sso.init();
     }
 });
diff --git a/app/assets/javascripts/admin/university/init.js b/app/assets/javascripts/admin/university/init.js
deleted file mode 100644
index 1c33bd9eb..000000000
--- a/app/assets/javascripts/admin/university/init.js
+++ /dev/null
@@ -1,4 +0,0 @@
-//= require_self
-//= require ./edit
-
-window.osuny.university = {};
diff --git a/app/models/communication/extranet.rb b/app/models/communication/extranet.rb
index 2bb62c7e0..a560c3917 100644
--- a/app/models/communication/extranet.rb
+++ b/app/models/communication/extranet.rb
@@ -13,7 +13,7 @@
 #  sso_mapping                 :jsonb
 #  sso_name_identifier_format  :string
 #  sso_provider                :integer          default("saml")
-#  sso_target_url              :integer          default(0)
+#  sso_target_url              :string
 #  created_at                  :datetime         not null
 #  updated_at                  :datetime         not null
 #  about_id                    :uuid             indexed => [about_type]
diff --git a/app/views/admin/communication/extranets/_form.html.erb b/app/views/admin/communication/extranets/_form.html.erb
index b6c8ab182..c878aa055 100644
--- a/app/views/admin/communication/extranets/_form.html.erb
+++ b/app/views/admin/communication/extranets/_form.html.erb
@@ -30,6 +30,23 @@
         </div>
       </div>
     </div>
+    <h3 class="mt-5"><%= t('university.sso') %></h3>
+    <div class="row">
+      <div class="col-md-6">
+        <%= f.input :has_sso %>
+        <div class="sso-inputs">
+          <%= f.input :sso_inherit_from_university if current_university.has_sso? %>
+          <%= f.input :sso_target_url, required: true %>
+          <%= f.input :sso_cert, required: true %>
+          <%= f.input :sso_name_identifier_format, required: true %>
+        </div>
+      </div>
+      <div class="col-md-6 sso-inputs">
+        <h4 class="mb-4"><%= University.human_attribute_name('sso_mapping') %></h4>
+        <%= f.error_notification message: f.object.errors[:sso_mapping].to_sentence if f.object.errors[:sso_mapping].present? %>
+        <%= render 'server/universities/sso_mapping', kind: 'extranet', object: extranet %>
+      </div>
+    </div>
   </div>
   <% content_for :action_bar_right do %>
     <%= submit f %>
diff --git a/app/views/server/universities/_form.html.erb b/app/views/server/universities/_form.html.erb
index a08d955d5..0a8fbdf1d 100644
--- a/app/views/server/universities/_form.html.erb
+++ b/app/views/server/universities/_form.html.erb
@@ -44,7 +44,7 @@
     <div class="col-md-6 sso-inputs">
       <h4 class="mb-4"><%= University.human_attribute_name('sso_mapping') %></h4>
       <%= f.error_notification message: f.object.errors[:sso_mapping].to_sentence if f.object.errors[:sso_mapping].present? %>
-      <%= render 'sso_mapping', university: university %>
+      <%= render 'sso_mapping', kind: 'university', object: university %>
     </div>
   </div>
 
diff --git a/app/views/server/universities/_sso_mapping.html.erb b/app/views/server/universities/_sso_mapping.html.erb
index 7bdb320c2..ad00cbc2e 100644
--- a/app/views/server/universities/_sso_mapping.html.erb
+++ b/app/views/server/universities/_sso_mapping.html.erb
@@ -1,4 +1,10 @@
-<% mapping_keys = ['email', 'first_name', 'last_name', 'role', 'mobile_phone', 'language', 'picture_url'] %>
+<%
+if kind == 'university'
+  mapping_keys = ['email', 'first_name', 'last_name', 'role', 'mobile_phone', 'language', 'picture_url']
+else
+  mapping_keys = ['email', 'first_name', 'last_name', 'mobile_phone', 'language', 'picture_url'] 
+end
+%>
 
 <%# Include vue.js before call Vue.createApp %>
 <%= javascript_include_tag 'vue' %>
@@ -40,15 +46,17 @@
                 <option v-for="(label, key) in keys" :value="key">{{ label }}</option>
               </select>
             </div>
-            <div v-if="field.internal_key === 'role'">
-              <hr class="mt-4">
-              <% User.roles.keys.each do |role| %>
-                <div class="form-group">
-                  <label for="" class="form-label"><%= t("activerecord.attributes.user.roles.#{role}") %></label>
-                  <input v-model="field.roles.<%= role %>" type="text" class="form-control">
-                </div>
-              <% end %>
-            </div>
+            <% if kind == 'university' %>
+              <div v-if="field.internal_key === 'role'">
+                <hr class="mt-4">
+                <% User.roles.keys.each do |role| %>
+                  <div class="form-group">
+                    <label for="" class="form-label"><%= t("activerecord.attributes.user.roles.#{role}") %></label>
+                    <input v-model="field.roles.<%= role %>" type="text" class="form-control">
+                  </div>
+                <% end %>
+              </div>
+            <% end %>
           </div>
         </div>
       </div>
@@ -59,7 +67,7 @@
     </a>
   </div>
 
-  <textarea name="university[sso_mapping]" id="university_sso_mapping" rows="20" cols="200" class="d-none">
+  <textarea name="<%= kind %>[sso_mapping]" id="<%= kind %>_sso_mapping" rows="20" cols="200" class="d-none">
     {{ JSON.stringify(fields) }}
   </textarea>
 
@@ -72,7 +80,7 @@
     },
     data() {
       return {
-        fields: <%= university.sso_mapping.blank? ? '[]' : university.sso_mapping.to_json.html_safe %>,
+        fields: <%= object.sso_mapping.blank? ? '[]' : object.sso_mapping.to_json.html_safe %>,
         keys: <%= mapping_keys.map { |key| [key, User.human_attribute_name(key)] }.to_h.to_json.html_safe %>
       }
     }
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index 85bef999d..1b4f745ec 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -49,8 +49,14 @@ en:
         about_Research::Laboratory: Laboratory extranet
         about_type: About
         domain: Domain
+        has_sso: Has SSO?
         name: Name
         registration_contact: Contact mail for registrations problems
+        sso_cert: Certificate
+        sso_inherit_from_university: SSO inherited from University
+        sso_mapping: Mapping
+        sso_name_identifier_format: Name Identifier Format
+        sso_target_url: Target URL
       communication/website:
         about: About
         about_: Independent website
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index bf7a104bc..0646d6c9c 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -49,8 +49,14 @@ fr:
         about_Research::Laboratory: Extranet d'un laboratoire
         about_type: Type d'extranet
         domain: Domaine
+        has_sso: A un SSO ?
         name: Nom
         registration_contact: Mail de contact pour les problèmes d'inscription
+        sso_cert: Certificat
+        sso_inherit_from_university: SSO hérité de l'Université
+        sso_mapping: Mapping
+        sso_name_identifier_format: Name Identifier Format
+        sso_target_url: URL cible
       communication/website:
         about: Sujet du site
         about_: Site indépendant
diff --git a/db/migrate/20221020124935_change_extranet_sso_target_url_kind.rb b/db/migrate/20221020124935_change_extranet_sso_target_url_kind.rb
new file mode 100644
index 000000000..b8c3ffaa3
--- /dev/null
+++ b/db/migrate/20221020124935_change_extranet_sso_target_url_kind.rb
@@ -0,0 +1,5 @@
+class ChangeExtranetSsoTargetUrlKind < ActiveRecord::Migration[6.1]
+  def change
+    change_column :communication_extranets, :sso_target_url, :string, default: nil
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 2d3b4b3f9..378d4e33b 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_20_102138) do
+ActiveRecord::Schema.define(version: 2022_10_20_124935) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -109,7 +109,7 @@ ActiveRecord::Schema.define(version: 2022_10_20_102138) do
     t.jsonb "sso_mapping"
     t.string "sso_name_identifier_format"
     t.integer "sso_provider", default: 0
-    t.integer "sso_target_url", default: 0
+    t.string "sso_target_url"
     t.index ["about_type", "about_id"], name: "index_communication_extranets_on_about"
     t.index ["university_id"], name: "index_communication_extranets_on_university_id"
   end
diff --git a/test/fixtures/communication/extranets.yml b/test/fixtures/communication/extranets.yml
index c1b066ad3..4007d6530 100644
--- a/test/fixtures/communication/extranets.yml
+++ b/test/fixtures/communication/extranets.yml
@@ -13,7 +13,7 @@
 #  sso_mapping                 :jsonb
 #  sso_name_identifier_format  :string
 #  sso_provider                :integer          default("saml")
-#  sso_target_url              :integer          default(0)
+#  sso_target_url              :string
 #  created_at                  :datetime         not null
 #  updated_at                  :datetime         not null
 #  about_id                    :uuid             indexed => [about_type]
diff --git a/test/models/communication/extranet_test.rb b/test/models/communication/extranet_test.rb
index a15009595..b8f875645 100644
--- a/test/models/communication/extranet_test.rb
+++ b/test/models/communication/extranet_test.rb
@@ -13,7 +13,7 @@
 #  sso_mapping                 :jsonb
 #  sso_name_identifier_format  :string
 #  sso_provider                :integer          default("saml")
-#  sso_target_url              :integer          default(0)
+#  sso_target_url              :string
 #  created_at                  :datetime         not null
 #  updated_at                  :datetime         not null
 #  about_id                    :uuid             indexed => [about_type]
-- 
GitLab