From 23df742a2f49f91a4651e45160c70dbb09d2bfb0 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Thu, 28 Apr 2022 17:24:12 +0200
Subject: [PATCH] wip #343

---
 app/models/university.rb                      | 39 +++++++++++--------
 app/models/university/with_sso.rb             | 18 +++++++++
 .../20220428151735_add_sso_to_universities.rb | 10 +++++
 db/schema.rb                                  |  8 +++-
 test/fixtures/universities.yml                | 38 ++++++++++--------
 5 files changed, 80 insertions(+), 33 deletions(-)
 create mode 100644 app/models/university/with_sso.rb
 create mode 100644 db/migrate/20220428151735_add_sso_to_universities.rb

diff --git a/app/models/university.rb b/app/models/university.rb
index 4c1b31dc8..67325b683 100644
--- a/app/models/university.rb
+++ b/app/models/university.rb
@@ -2,22 +2,28 @@
 #
 # Table name: universities
 #
-#  id                :uuid             not null, primary key
-#  address           :string
-#  city              :string
-#  country           :string
-#  identifier        :string
-#  invoice_amount    :string
-#  invoice_date      :date
-#  invoice_date_yday :integer
-#  mail_from_address :string
-#  mail_from_name    :string
-#  name              :string
-#  private           :boolean
-#  sms_sender_name   :string
-#  zipcode           :string
-#  created_at        :datetime         not null
-#  updated_at        :datetime         not null
+#  id                         :uuid             not null, primary key
+#  address                    :string
+#  city                       :string
+#  country                    :string
+#  has_sso                    :boolean          default(FALSE)
+#  identifier                 :string
+#  invoice_amount             :string
+#  invoice_date               :date
+#  invoice_date_yday          :integer
+#  mail_from_address          :string
+#  mail_from_name             :string
+#  name                       :string
+#  private                    :boolean
+#  sms_sender_name            :string
+#  sso_cert                   :text
+#  sso_mapping                :jsonb
+#  sso_name_identifier_format :string
+#  sso_provider               :integer          default("saml")
+#  sso_target_url             :string
+#  zipcode                    :string
+#  created_at                 :datetime         not null
+#  updated_at                 :datetime         not null
 #
 class University < ApplicationRecord
   include WithPeopleAndOrganizations
@@ -26,6 +32,7 @@ class University < ApplicationRecord
   include WithIdentifier
   include WithInvoice
   include WithResearch
+  include WithSso
   include WithUsers
 
   has_one_attached_deletable :logo
diff --git a/app/models/university/with_sso.rb b/app/models/university/with_sso.rb
new file mode 100644
index 000000000..34f6afb8b
--- /dev/null
+++ b/app/models/university/with_sso.rb
@@ -0,0 +1,18 @@
+module University::WithSso
+  extend ActiveSupport::Concern
+
+  included do
+    enum sso_provider: { saml: 0, oauth2: 10 }, _prefix: :with_sso_via
+  end
+
+  # Setter to serialize data as JSON
+  def sso_mapping=(value)
+    if value.empty?
+      value = nil
+    else
+      value = JSON.parse value if value.is_a? String
+    end
+    super(value)
+  end
+
+end
diff --git a/db/migrate/20220428151735_add_sso_to_universities.rb b/db/migrate/20220428151735_add_sso_to_universities.rb
new file mode 100644
index 000000000..f94567dec
--- /dev/null
+++ b/db/migrate/20220428151735_add_sso_to_universities.rb
@@ -0,0 +1,10 @@
+class AddSsoToUniversities < ActiveRecord::Migration[6.1]
+  def change
+    add_column :universities, :has_sso, :boolean, default: false
+    add_column :universities, :sso_provider, :integer, default: 0
+    add_column :universities, :sso_target_url, :string
+    add_column :universities, :sso_cert, :text
+    add_column :universities, :sso_name_identifier_format, :string
+    add_column :universities, :sso_mapping, :jsonb
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index c2f1cfe20..651bf52d9 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_04_27_094234) do
+ActiveRecord::Schema.define(version: 2022_04_28_151735) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -629,6 +629,12 @@ ActiveRecord::Schema.define(version: 2022_04_27_094234) do
     t.date "invoice_date"
     t.integer "invoice_date_yday"
     t.string "invoice_amount"
+    t.boolean "has_sso", default: false
+    t.integer "sso_provider", default: 0
+    t.string "sso_target_url"
+    t.text "sso_cert"
+    t.string "sso_name_identifier_format"
+    t.jsonb "sso_mapping"
   end
 
   create_table "university_organization_imports", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
diff --git a/test/fixtures/universities.yml b/test/fixtures/universities.yml
index 0ce6f9287..112c47a9b 100644
--- a/test/fixtures/universities.yml
+++ b/test/fixtures/universities.yml
@@ -2,22 +2,28 @@
 #
 # Table name: universities
 #
-#  id                :uuid             not null, primary key
-#  address           :string
-#  city              :string
-#  country           :string
-#  identifier        :string
-#  invoice_amount    :string
-#  invoice_date      :date
-#  invoice_date_yday :integer
-#  mail_from_address :string
-#  mail_from_name    :string
-#  name              :string
-#  private           :boolean
-#  sms_sender_name   :string
-#  zipcode           :string
-#  created_at        :datetime         not null
-#  updated_at        :datetime         not null
+#  id                         :uuid             not null, primary key
+#  address                    :string
+#  city                       :string
+#  country                    :string
+#  has_sso                    :boolean          default(FALSE)
+#  identifier                 :string
+#  invoice_amount             :string
+#  invoice_date               :date
+#  invoice_date_yday          :integer
+#  mail_from_address          :string
+#  mail_from_name             :string
+#  name                       :string
+#  private                    :boolean
+#  sms_sender_name            :string
+#  sso_cert                   :text
+#  sso_mapping                :jsonb
+#  sso_name_identifier_format :string
+#  sso_provider               :integer          default("saml")
+#  sso_target_url             :string
+#  zipcode                    :string
+#  created_at                 :datetime         not null
+#  updated_at                 :datetime         not null
 #
 default_university:
   name: Université de test
-- 
GitLab