Skip to content
Snippets Groups Projects
Unverified Commit 0d5fb966 authored by Sébastien Gaya's avatar Sébastien Gaya
Browse files

validate sso mapping contains email + codeclimate from_omniauth

parent f6fdd517
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,7 @@ module University::WithSso
enum sso_provider: { saml: 0 }, _prefix: :with_sso_via
validates :sso_cert, :sso_name_identifier_format, :sso_target_url, presence: true, if: :has_sso?
validate :sso_mapping_should_have_email, if: :has_sso?
end
# Setter to serialize data as JSON
......@@ -18,4 +18,7 @@ module University::WithSso
super(value)
end
def sso_mapping_should_have_email
errors.add(:sso_mapping, :missing_email) unless (sso_mapping || []).detect { |sso_item| sso_item['internal_key'] == 'email' }
end
end
......@@ -7,13 +7,12 @@ module User::WithOmniauth
mapping = university.sso_mapping || []
# first step: we find the email (we are supposed to have an email mapping)
email_sso_key = mapping.select { |elmt| elmt['internal_key'] == 'email' }&.first&.dig('sso_key')
email_sso_key = mapping.detect { |elmt| elmt['internal_key'] == 'email' }&.dig('sso_key')
email = attributes.dig(email_sso_key)
return unless email
email = email.first if email.is_a?(Array)
email = email.downcase
user = User.where(university: university, email: email).first_or_create do |u|
user = User.where(university: university, email: email.downcase).first_or_create do |u|
u.password = "#{Devise.friendly_token[0,20]}!" # meets password complexity requirements
end
......
<%= simple_form_for [:server, university] do |f| %>
<%= f.error_notification %>
<div class="row">
<div class="col-md-4">
<%= f.input :name %>
......@@ -40,6 +42,7 @@
</div>
<div class="col-md-6">
<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 %>
</div>
</div>
......
......@@ -81,6 +81,12 @@ en:
university/role:
description: Description
people: People
errors:
models:
university:
attributes:
sso_mapping:
missing_email: doesn't handle the email
models:
university:
one: University
......
......@@ -81,6 +81,12 @@ fr:
university/role:
description: Description
people: Personnes
errors:
models:
university:
attributes:
sso_mapping:
missing_email: ne gère pas l'adresse email
models:
university:
one: Université
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment