From 20049973223848679d25c127b5dd01e35ea245a4 Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Tue, 7 Nov 2017 14:54:11 +0100
Subject: [PATCH] let ORM handle multi-valued attributes

---
 lib/IdPAccountManager/Data/ServiceProvider.pm | 29 ++++---------------
 lib/IdPAccountManager/WebRequest.pm           |  5 ++--
 templates/web/select_sp.tt2.html              |  2 +-
 3 files changed, 9 insertions(+), 27 deletions(-)

diff --git a/lib/IdPAccountManager/Data/ServiceProvider.pm b/lib/IdPAccountManager/Data/ServiceProvider.pm
index 6eee98a..bf8a4b7 100644
--- a/lib/IdPAccountManager/Data/ServiceProvider.pm
+++ b/lib/IdPAccountManager/Data/ServiceProvider.pm
@@ -12,7 +12,7 @@ __PACKAGE__->meta->setup(
         id          => { type => 'bigserial', not_null => 1 },
         entityid    => { type => 'varchar', length => 200, not_null => 1 },
         displayname => { type => 'varchar', length => 500 },
-        contacts    => { type => 'varchar', length => 2000 },
+        contacts    => { type => 'array' },
     ],
 
     primary_key_columns => [ 'id' ],
@@ -25,8 +25,6 @@ sub new {
 
     my $self = $pkg->SUPER::new(%args);
 
-    $self->{dev_sp_contact} = $args{dev_sp_contact};
-
     return $self;
 }
 
@@ -37,32 +35,17 @@ sub print {
 
     printf $fd
       "ServiceProvider ID=%s; entityid=%s; displayname=%s; contacts=%s\n",
-      $self->id(), $self->entityid(), $self->displayname(), $self->contacts();
-}
-
-## list contacts for this SP, including those listed in conf.dev_sp_contact
-sub list_contacts_as_array {
-    my ($self) = @_;
-
-    my %contact_list;
-
-    foreach my $contact_email (split /,/, $self->contacts()) {
-        $contact_list{$contact_email}++;
-    }
-
-    foreach my $contact_email (split /,/, $self->{dev_sp_contact}) {
-        $contact_list{$contact_email}++;
-    }
-
-    return keys %contact_list;
+      $self->id(), $self->entityid(), $self->displayname(), join(',', $self->contacts());
 }
 
 ## Check if email address is a known contact (or conf.dev_sp_contact)
 sub is_contact {
     my ($self, $email) = @_;
 
-    foreach my $known_contact ($self->list_contacts_as_array()) {
-        return 1 if (lc($email) eq lc($known_contact));
+    $email = lc($email);
+
+    foreach my $contact ($self->contacts()) {
+        return 1 if $email eq lc($contact);
     }
 
     return 0;
diff --git a/lib/IdPAccountManager/WebRequest.pm b/lib/IdPAccountManager/WebRequest.pm
index d7fec1e..9705304 100755
--- a/lib/IdPAccountManager/WebRequest.pm
+++ b/lib/IdPAccountManager/WebRequest.pm
@@ -277,9 +277,8 @@ sub req_select_sp {
 
         # complete persistent object
         $provider->displayname($sp->{display_name});
-        $provider->contacts(
-            join(',', map { $_->{EmailAddress} } @{$sp->{contacts}})
-        ) if $sp->{contacts};
+        $provider->contacts(map { $_->{EmailAddress} } @{$sp->{contacts}})
+            if $sp->{contacts};
 
         # save in DB
         unless ($provider->save()) {
diff --git a/templates/web/select_sp.tt2.html b/templates/web/select_sp.tt2.html
index ce4e56a..fe4356b 100644
--- a/templates/web/select_sp.tt2.html
+++ b/templates/web/select_sp.tt2.html
@@ -11,7 +11,7 @@ Before you can create test accounts at this Identity Provider, we need to ensure
 
 
 <div class="radio_inline">
-[% FOREACH email IN provider.list_contacts_as_array.sort %]
+[% FOREACH email IN provider.contacts.sort %]
 <input name="email_address" value="[% email %]" type="radio" class="required"/><label for="email_address">[% email %]</label><br/>
 
 <input type="hidden" name="sp_entityid" value="[% provider.entityid %]" id="sp_entityid"/>
-- 
GitLab