From 4bdb78887bbc76e0de7df5155e1f442034e9883f Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Fri, 23 Feb 2018 15:14:10 +0100
Subject: [PATCH] keep logic in code, not in templates

---
 lib/AccountManager/WebRequest.pm         | 94 ++++++++++++++++--------
 templates/Makefile.am                    |  4 -
 templates/web/en/content.tt2.html        | 41 -----------
 templates/web/en/index-nobanner.tt2.html |  8 +-
 templates/web/en/index.tt2.html          |  7 +-
 templates/web/fr/content.tt2.html        | 41 -----------
 templates/web/fr/index-nobanner.tt2.html |  8 +-
 templates/web/fr/index.tt2.html          |  6 +-
 8 files changed, 67 insertions(+), 142 deletions(-)
 delete mode 100644 templates/web/en/content.tt2.html
 delete mode 100644 templates/web/fr/content.tt2.html

diff --git a/lib/AccountManager/WebRequest.pm b/lib/AccountManager/WebRequest.pm
index a1ba26a..54a0bd5 100644
--- a/lib/AccountManager/WebRequest.pm
+++ b/lib/AccountManager/WebRequest.pm
@@ -96,7 +96,8 @@ sub run {
                 $self->respond(
                     template => 'index.tt2.html',
                     data     => {
-                        errors => [ "format_$parameter" ]
+                        content => 'errors.tt2.html',
+                        errors  => [ "format_$parameter" ]
                     }
                 );
             }
@@ -127,7 +128,8 @@ sub run {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "Unknown action '$action'" ]
+                content => 'errors.tt2.html',
+                errors  => [ "Unknown action '$action'" ]
             }
         );
     }
@@ -185,6 +187,7 @@ sub req_select_sp {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
+                content => 'errors.tt2.html',
                 errors  => [ "internal" ]
             }
         );
@@ -197,7 +200,7 @@ sub req_select_sp {
                 SCRIPT_NAME => $ENV{SCRIPT_NAME}
             },
             metadata => $metadata->parse(type => 'sp'),
-            action   => 'select_sp'
+            content  => 'select_sp.tt2.html'
         }
     );
 }
@@ -210,7 +213,8 @@ sub req_select_email {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "missing_entityid" ]
+                content => 'errors.tt2.html',
+                errors  => [ "missing_entityid" ]
             }
         );
     }
@@ -237,7 +241,8 @@ sub req_select_email {
             $self->respond(
                 template => 'index.tt2.html',
                 data     => {
-                    errors => [ "internal" ]
+                    content => 'errors.tt2.html',
+                    errors  => [ "internal" ]
                 }
             );
         }
@@ -250,7 +255,8 @@ sub req_select_email {
             $self->respond(
                 template => 'index.tt2.html',
                 data     => {
-                    errors => [ "no_such_entity" ]
+                    content => 'errors.tt2.html',
+                    errors  => [ "no_such_entity" ]
                 }
             );
         }
@@ -267,7 +273,8 @@ sub req_select_email {
             $self->respond(
                 template => 'index.tt2.html',
                 data     => {
-                    errors => [ "internal" ]
+                    content => 'errors.tt2.html',
+                    errors  => [ "internal" ]
                 }
             );
         }
@@ -292,7 +299,7 @@ sub req_select_email {
         template => 'index-nobanner.tt2.html',
         data     => {
             provider => $provider,
-            action   => 'select_email'
+            content  => 'select_email.tt2.html'
         }
     );
 }
@@ -315,7 +322,8 @@ sub req_complete_challenge {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "missing_email" ]
+                content => 'errors.tt2.html',
+                errors  => [ "missing_email" ]
             }
         );
     }
@@ -330,7 +338,8 @@ sub req_complete_challenge {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "no_such_entity" ]
+                content => 'errors.tt2.html',
+                errors  => [ "no_such_entity" ]
             }
         );
     }
@@ -361,7 +370,8 @@ sub req_complete_challenge {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "internal" ]
+                content => 'errors.tt2.html',
+                errors  => [ "internal" ]
             }
         );
     }
@@ -382,7 +392,8 @@ sub req_complete_challenge {
             $self->respond(
                 template => 'index.tt2.html',
                 data     => {
-                    errors => [ "internal" ]
+                    content => 'errors.tt2.html',
+                    errors  => [ "internal" ]
                 }
             );
         }
@@ -405,7 +416,8 @@ sub req_complete_challenge {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "internal" ]
+                content => 'errors.tt2.html',
+                errors  => [ "internal" ]
             }
         );
     }
@@ -420,7 +432,8 @@ sub req_complete_challenge {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "mail_notification_error" ] 
+                content => 'errors.tt2.html',
+                errors  => [ "mail_notification_error" ]
             }
         );
     };
@@ -450,7 +463,8 @@ sub req_complete_challenge {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "mail_notification_error" ]
+                content => 'errors.tt2.html',
+                errors  => [ "mail_notification_error" ]
             }
         );
     }
@@ -469,7 +483,7 @@ sub req_complete_challenge {
         data     => {
             email    => $self->{in}->{email},
             entityid => $self->{in}->{entityid},
-            action   => 'complete_challenge'
+            content  => 'complete_challenge.tt2.html'
         }
     );
 }
@@ -482,14 +496,21 @@ sub req_create_accounts {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "missing_entityid" ]
+                content => 'errors.tt2.html',
+                errors  => [ "missing_entityid" ]
             }
         );
     }
 
     unless ($self->{in}->{token}) {
         $self->{logger}->error("Missing parameter token");
-        $self->respond({ errors => [ "missing_token" ] });
+        $self->respond(
+            template => 'index.tt2.html',
+            data => {
+                content => 'errors.tt2.html',
+                errors  => [ "missing_token" ]
+            }
+        );
     }
 
     unless ($self->{in}->{email}) {
@@ -497,7 +518,8 @@ sub req_create_accounts {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "missing_email" ]
+                content => 'errors.tt2.html',
+                errors  => [ "missing_email" ]
             }
         );
     }
@@ -516,7 +538,8 @@ sub req_create_accounts {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "wrong_token" ]
+                content => 'errors.tt2.html',
+                errors  => [ "wrong_token" ]
             }
         );
     }
@@ -530,7 +553,8 @@ sub req_create_accounts {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "wrong_token_for_sp" ]
+                content => 'errors.tt2.html',
+                errors  => [ "wrong_token_for_sp" ]
             }
         );
     }
@@ -569,7 +593,8 @@ sub req_create_accounts {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "internal" ]
+                content => 'errors.tt2.html',
+                errors  => [ "internal" ]
             }
         );
     }
@@ -602,7 +627,8 @@ sub req_create_accounts {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "accounts_creation_failed" ]
+                content => 'errors.tt2.html',
+                errors  => [ "accounts_creation_failed" ]
             }
         );
     }
@@ -627,7 +653,8 @@ sub req_create_accounts {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "accounts_creation_failed" ]
+                content => 'errors.tt2.html',
+                errors  => [ "accounts_creation_failed" ]
             }
         );
     }
@@ -645,7 +672,7 @@ sub req_create_accounts {
             entityid => $self->{in}->{entityid},
             key      => $key,
             token    => $download_token->token(),
-            action   => 'create_accounts'
+            content  => 'create_accounts.tt2.html'
         }
     );
 }
@@ -658,7 +685,8 @@ sub req_download_accounts {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "missing_entityid" ]
+                content => 'errors.tt2.html',
+                errors  => [ "missing_entityid" ]
             }
         );
     }
@@ -668,7 +696,8 @@ sub req_download_accounts {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "missing_token" ]
+                content => 'errors.tt2.html',
+                errors  => [ "missing_token" ]
             }
         );
     }
@@ -678,7 +707,8 @@ sub req_download_accounts {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "missing_key" ]
+                content => 'errors.tt2.html',
+                errors  => [ "missing_key" ]
             }
         );
     }
@@ -696,7 +726,8 @@ sub req_download_accounts {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "wrong_token" ]
+                content => 'errors.tt2.html',
+                errors  => [ "wrong_token" ]
             }
         );
     }
@@ -710,7 +741,8 @@ sub req_download_accounts {
         $self->respond(
             template => 'index.tt2.html',
             data     => {
-                errors => [ "wrong_token_for_sp" ]
+                content => 'errors.tt2.html',
+                errors  => [ "wrong_token_for_sp" ]
             }
         );
     }
@@ -784,7 +816,7 @@ sub req_home {
     $self->respond(
         template => 'index.tt2.html',
         data     => {
-            action => 'home'
+            content => 'home.tt2.html'
         }
     );
 }
diff --git a/templates/Makefile.am b/templates/Makefile.am
index 7a2d950..60d93b7 100644
--- a/templates/Makefile.am
+++ b/templates/Makefile.am
@@ -1,22 +1,18 @@
 nobase_templates_DATA = \
 	accountProfiles/valid-accounts.php.tt2 \
 	mail/send_authentication_token.tt2.eml \
-	web/en/content.tt2.html \
 	web/en/errors.tt2.html \
 	web/en/home.tt2.html \
 	web/en/index.tt2.html \
 	web/en/index-nobanner.tt2.html \
-	web/en/notices.tt2.html \
 	web/en/select_sp.tt2.html \
 	web/en/select_email.tt2.html \
 	web/en/complete_challenge.tt2.html \
 	web/en/create_accounts.tt2.html \
-	web/fr/content.tt2.html \
 	web/fr/errors.tt2.html \
 	web/fr/home.tt2.html \
 	web/fr/index.tt2.html \
 	web/fr/index-nobanner.tt2.html \
-	web/fr/notices.tt2.html \
 	web/fr/select_sp.tt2.html \
 	web/fr/select_email.tt2.html \
 	web/fr/complete_challenge.tt2.html \
diff --git a/templates/web/en/content.tt2.html b/templates/web/en/content.tt2.html
deleted file mode 100644
index 6327b7b..0000000
--- a/templates/web/en/content.tt2.html
+++ /dev/null
@@ -1,41 +0,0 @@
-
-
-[% IF action == 'select_sp' %]
- [% TRY %]
-  [% PROCESS 'select_sp.tt2.html' %]
- [% CATCH %]
-  An error occured
- [% END %]
-
- [% ELSIF action == 'select_email' %]
- [% TRY %]
-  [% PROCESS 'select_email.tt2.html' %]
- [% CATCH %]
-  An error occured
- [% END %]
-
-[% ELSIF action == 'complete_challenge' %]
- [% TRY %]
-  [% PROCESS 'complete_challenge.tt2.html' %]
- [% CATCH %]
-  An error occured
- [% END %]
-
-[% ELSIF action == 'create_accounts' %]
- [% TRY %]
-  [% PROCESS 'create_accounts.tt2.html' %]
- [% CATCH %]
-  An error occured
- [% END %]
-
- [% ELSIF action == 'home' %]
- [% TRY %]
-  [% PROCESS 'home.tt2.html' %]
- [% CATCH %]
-  An error occured
- [% END %]
-
-[% ELSE %]
-Error: unknown action
-
-[% END %]
diff --git a/templates/web/en/index-nobanner.tt2.html b/templates/web/en/index-nobanner.tt2.html
index f13a0d1..9539198 100644
--- a/templates/web/en/index-nobanner.tt2.html
+++ b/templates/web/en/index-nobanner.tt2.html
@@ -1,9 +1,3 @@
 <DOCTYPE! html>
 
-[% IF errors %]
-<div class="ui-widget">
-[% PROCESS 'errors.tt2.html' %]
-</div>
-[% ELSE %]
-[% PROCESS 'content.tt2.html' %]
-[% END %]
+[% INCLUDE $content %]
diff --git a/templates/web/en/index.tt2.html b/templates/web/en/index.tt2.html
index 9e47400..74a34cd 100644
--- a/templates/web/en/index.tt2.html
+++ b/templates/web/en/index.tt2.html
@@ -34,12 +34,7 @@
         </header>
 
         <main>
-
-            [% IF errors %]
-                [% PROCESS 'errors.tt2.html' %]
-            [% ELSE %]
-                [% PROCESS 'content.tt2.html' %]
-            [% END %]
+            [% INCLUDE $content %]
         </main>
 
         <footer>
diff --git a/templates/web/fr/content.tt2.html b/templates/web/fr/content.tt2.html
deleted file mode 100644
index 6327b7b..0000000
--- a/templates/web/fr/content.tt2.html
+++ /dev/null
@@ -1,41 +0,0 @@
-
-
-[% IF action == 'select_sp' %]
- [% TRY %]
-  [% PROCESS 'select_sp.tt2.html' %]
- [% CATCH %]
-  An error occured
- [% END %]
-
- [% ELSIF action == 'select_email' %]
- [% TRY %]
-  [% PROCESS 'select_email.tt2.html' %]
- [% CATCH %]
-  An error occured
- [% END %]
-
-[% ELSIF action == 'complete_challenge' %]
- [% TRY %]
-  [% PROCESS 'complete_challenge.tt2.html' %]
- [% CATCH %]
-  An error occured
- [% END %]
-
-[% ELSIF action == 'create_accounts' %]
- [% TRY %]
-  [% PROCESS 'create_accounts.tt2.html' %]
- [% CATCH %]
-  An error occured
- [% END %]
-
- [% ELSIF action == 'home' %]
- [% TRY %]
-  [% PROCESS 'home.tt2.html' %]
- [% CATCH %]
-  An error occured
- [% END %]
-
-[% ELSE %]
-Error: unknown action
-
-[% END %]
diff --git a/templates/web/fr/index-nobanner.tt2.html b/templates/web/fr/index-nobanner.tt2.html
index f13a0d1..9539198 100644
--- a/templates/web/fr/index-nobanner.tt2.html
+++ b/templates/web/fr/index-nobanner.tt2.html
@@ -1,9 +1,3 @@
 <DOCTYPE! html>
 
-[% IF errors %]
-<div class="ui-widget">
-[% PROCESS 'errors.tt2.html' %]
-</div>
-[% ELSE %]
-[% PROCESS 'content.tt2.html' %]
-[% END %]
+[% INCLUDE $content %]
diff --git a/templates/web/fr/index.tt2.html b/templates/web/fr/index.tt2.html
index ebfd655..86e8aa0 100644
--- a/templates/web/fr/index.tt2.html
+++ b/templates/web/fr/index.tt2.html
@@ -34,11 +34,7 @@
         </header>
 
         <main>
-            [% IF errors %]
-                [% PROCESS 'errors.tt2.html' %]
-            [% ELSE %]
-                [% PROCESS 'content.tt2.html' %]
-            [% END %]
+            [% INCLUDE $content %]
         </main>
 
         <footer>
-- 
GitLab