From 23da23cc6b98ae29f58ceea3099402228cc24dab Mon Sep 17 00:00:00 2001
From: Joris Masson <joris.masson@enalean.com>
Date: Tue, 13 Jun 2017 14:01:03 +0200
Subject: [PATCH] request #10313: New homepage does not allow users to log in
 when an OpenID Connect provider is the only authentication source

How to test:
- Configure an OpenID Connect provider as the only authentication source
- You should see only one login button on the homepage
- The "Create account" link at the bottom and button on the right should
not be displayed

Change-Id: Ie79ddef377891001e02bac9fc4a06392fcd7b466
---
 .../include/openidconnectclientPlugin.class.php    |  9 --
 src/common/event/Event.class.php                   |  8 --
 src/templates/homepage/homepage-signin.mustache    | 99 ++++++++++++----------
 src/templates/homepage/homepage.mustache           |  8 +-
 src/www/index.php                                  | 13 ++-
 .../themes/BurningParrot/BurningParrotTheme.php    |  9 +-
 src/www/themes/BurningParrot/HomePagePresenter.php |  9 +-
 7 files changed, 81 insertions(+), 74 deletions(-)

diff --git a/plugins/openidconnectclient/include/openidconnectclientPlugin.class.php b/plugins/openidconnectclient/include/openidconnectclientPlugin.class.php
index 81c0c32..8e20e3b 100644
--- a/plugins/openidconnectclient/include/openidconnectclientPlugin.class.php
+++ b/plugins/openidconnectclient/include/openidconnectclientPlugin.class.php
@@ -70,7 +70,6 @@ class openidconnectclientPlugin extends Plugin {
         $this->addHook(Event::IS_OLD_PASSWORD_REQUIRED_FOR_PASSWORD_CHANGE);
         $this->addHook(Event::GET_LOGIN_URL);
         $this->addHook('display_newaccount');
-        $this->addHook(Event::DISPLAY_HOMEPAGE_LOGIN_FORM);
     }
 
     /**
@@ -198,14 +197,6 @@ class openidconnectclientPlugin extends Plugin {
         }
     }
 
-    public function display_homepage_login_form($params)
-    {
-        $provider_manager = $this->getProviderManager();
-        if ($this->isLoginConfiguredToUseAProviderAsUniqueAuthenticationEndpoint($provider_manager)) {
-            $params['is_displayed'] = false;
-        }
-    }
-
     /**
      * @return Flow
      */
diff --git a/src/common/event/Event.class.php b/src/common/event/Event.class.php
index 1c9d4b7..a594b49 100644
--- a/src/common/event/Event.class.php
+++ b/src/common/event/Event.class.php
@@ -780,14 +780,6 @@ class Event {
     const GET_LOGIN_URL = 'get_login_url';
 
     /**
-     * Determine if the homepage login form should be displayed
-     *
-     * Parameters:
-     *   'is_displayed' => boolean (OUT)
-     */
-    const DISPLAY_HOMEPAGE_LOGIN_FORM = 'display_homepage_login_form';
-
-    /**
      * Allow plugin to add fields in the user register form
      *
      * Parameters:
diff --git a/src/templates/homepage/homepage-signin.mustache b/src/templates/homepage/homepage-signin.mustache
index 702eda7..4029625 100644
--- a/src/templates/homepage/homepage-signin.mustache
+++ b/src/templates/homepage/homepage-signin.mustache
@@ -1,53 +1,66 @@
 {{# user_is_anonymous }}
-    <form class="homepage-sign-in" action="{{ most_secure_url }}/account/login.php" method="POST">
-        {{# login }}
-            <div class="sign-in-avatar">
-                <div class="tlp-avatar-extra-large"></div>
-            </div>
+    <div class="homepage-sign-in">
+        <div class="sign-in-avatar">
+            <div class="tlp-avatar-extra-large"></div>
+        </div>
+        {{# login_url }}
+            <a
+                href="{{ login_url }}"
+                class="tlp-button-primary tlp-button-large tlp-button-wide sign-in-button"
+            >{{ sign_in_submit }}</a>
+        {{/login_url}}
 
-            <div class="tlp-form-element">
-                <label for="form_loginname" class="tlp-label">{{ username_placeholder }}</label>
-                <input
-                    type="text"
-                    class="tlp-input"
-                    id="form_loginname"
-                    name="form_loginname"
-                    placeholder="{{ username_placeholder }}"
-                    tabindex="1"
-                >
+        {{^ login_url }}
+            <form action="{{ most_secure_url }}/account/login.php" method="POST">
+                {{# login }}
 
-                {{# allow_password_recovery }}
-                    <p class="tlp-text-info sign-in-forgot-password">
-                        <a href="{{ most_secure_url }}/account/lostpw.php">{{ forgot_password }}</a>
-                    </p>
-                {{/ allow_password_recovery }}
-            </div>
+                    <div class="tlp-form-element">
+                        <label for="form_loginname" class="tlp-label">{{ username_placeholder }}</label>
+                        <input
+                            type="text"
+                            class="tlp-input"
+                            id="form_loginname"
+                            name="form_loginname"
+                            placeholder="{{ username_placeholder }}"
+                            tabindex="1"
+                        >
 
-            <div class="tlp-form-element">
-                <label for="form_pw" class="tlp-label">{{ password_placeholder }}</label>
-                <input
-                    type="password"
-                    class="tlp-input"
-                    id="form_pw"
-                    name="form_pw"
-                    placeholder="{{ password_placeholder }}"
-                    tabindex="2"
-                >
-            </div>
+                        {{# allow_password_recovery }}
+                            <p class="tlp-text-info sign-in-forgot-password">
+                                <a href="{{ most_secure_url }}/account/lostpw.php">{{ forgot_password }}</a>
+                            </p>
+                        {{/ allow_password_recovery }}
+                    </div>
 
-            <input type="hidden" name="{{ csrf_token_name }}" value="{{ csrf_token_value }}">
-            <button
-                type="submit"
-                name="login"
-                class="tlp-button-primary tlp-button-large tlp-button-wide sign-in-button"
-                tabindex="3"
-            >{{ sign_in_submit }}</button>
+                    <div class="tlp-form-element">
+                        <label for="form_pw" class="tlp-label">{{ password_placeholder }}</label>
+                        <input
+                            type="password"
+                            class="tlp-input"
+                            id="form_pw"
+                            name="form_pw"
+                            placeholder="{{ password_placeholder }}"
+                            tabindex="2"
+                        >
+                    </div>
 
-            {{{ additional_connectors }}}
+                    <input type="hidden" name="{{ csrf_token_name }}" value="{{ csrf_token_value }}">
+                    <button
+                        type="submit"
+                        name="login"
+                        class="tlp-button-primary tlp-button-large tlp-button-wide sign-in-button"
+                        tabindex="3"
+                    >{{ sign_in_submit }}</button>
 
-            <span class="sign-in-register">{{ not_a_member }} <a href="{{ most_secure_url }}/account/register.php">{{ create_account_label }}</a></span>
-        {{/ login }}
-    </form>
+                    {{{ additional_connectors }}}
+
+                    {{# display_new_account_button }}
+                        <span class="sign-in-register">{{ not_a_member }} <a href="{{ most_secure_url }}/account/register.php">{{ create_account_label }}</a></span>
+                    {{/display_new_account_button}}
+                {{/ login }}
+            </form>
+        {{/ login_url }}
+    </div>
 {{/ user_is_anonymous }}
 
 {{^ user_is_anonymous }}
diff --git a/src/templates/homepage/homepage.mustache b/src/templates/homepage/homepage.mustache
index 14a7095..b916c13 100644
--- a/src/templates/homepage/homepage.mustache
+++ b/src/templates/homepage/homepage.mustache
@@ -11,22 +11,20 @@
                     </svg>
                 {{/is_custom_logo_used}}
             </div>
-            {{# display_homepage_login_form }}
-                {{> homepage-signin }}
-            {{/display_homepage_login_form}}
+            {{> homepage-signin }}
         </div>
     </section>
     <section class="homepage-create-account-section">
         <div class="homepage-welcome-wrapper">
             <h2 class="homepage-welcome-title">{{{ purified_welcome_message }}}</h2>
             <p class="homepage-headline">{{ headline }}</p>
-            {{# user_is_anonymous }}
+            {{# display_new_account_button }}
                 <a href="{{ most_secure_url }}/account/register.php"
                    class="tlp-button-secondary tlp-button-large homepage-create-account-button"
                    role="button"
                 >{{ create_account_label }}<i class="fa fa-heart tlp-button-icon-right homepage-create-account-button-icon"></i>
                 </a>
-            {{/ user_is_anonymous }}
+            {{/ display_new_account_button }}
         </div>
         <img
             class="homepage-screenshots"
diff --git a/src/www/index.php b/src/www/index.php
index 6133cb0..1b2e867 100644
--- a/src/www/index.php
+++ b/src/www/index.php
@@ -29,8 +29,11 @@ $login_form_url .= '/account/login.php';
 
 $display_homepage_boxes      = !isset($GLOBALS['sys_display_homepage_boxes']) || (isset($GLOBALS['sys_display_homepage_boxes']) && $GLOBALS['sys_display_homepage_boxes'] == 1);
 $display_homepage_news       = !isset($GLOBALS['sys_display_homepage_news'])  || (isset($GLOBALS['sys_display_homepage_news'])  && $GLOBALS['sys_display_homepage_news']  == 1);
-$display_homepage_login_form = true;
-$event_manager->processEvent(Event::DISPLAY_HOMEPAGE_LOGIN_FORM, array('is_displayed' => &$display_homepage_login_form));
+$display_new_account_button  = true;
+$event_manager->processEvent('display_newaccount', array('allow' => &$display_new_account_button));
+$login_url = '';
+$event_manager->processEvent(Event::GET_LOGIN_URL, array('return_to' => '', 'login_url' => &$login_url));
+
 $header_params = array(
     'title' => $Language->getText('homepage', 'title'),
 );
@@ -43,7 +46,11 @@ if ($homepage_dao->isStandardHomepageUsed()) {
 
     $HTML->header($header_params);
     $request = HTTPRequest::instance();
-    $HTML->displayStandardHomepage($display_homepage_news, $display_homepage_login_form, $request->isSecure());
+    $HTML->displayStandardHomepage(
+        $display_new_account_button,
+        $login_url,
+        $request->isSecure()
+    );
 } else {
     $HTML->header($header_params);
 
diff --git a/src/www/themes/BurningParrot/BurningParrotTheme.php b/src/www/themes/BurningParrot/BurningParrotTheme.php
index 719f513..7733dda 100644
--- a/src/www/themes/BurningParrot/BurningParrotTheme.php
+++ b/src/www/themes/BurningParrot/BurningParrotTheme.php
@@ -200,8 +200,8 @@ class BurningParrotTheme extends Layout
     }
 
     public function displayStandardHomepage(
-        $display_homepage_news,
-        $display_homepage_login_form,
+        $display_new_account_button,
+        $login_url,
         $is_secure
     ) {
         $homepage_dao = $this->getAdminHomepageDao();
@@ -218,6 +218,8 @@ class BurningParrotTheme extends Layout
         $login_csrf              = new CSRFSynchronizerToken('/account/login.php');
         $login_presenter         = $login_presenter_builder->buildForHomepage($is_secure, $login_csrf);
 
+        $display_new_account_button = ($current_user->isAnonymous() && $display_new_account_button);
+
         $templates_dir = ForgeConfig::get('codendi_dir') . '/src/templates/homepage/';
         $renderer      = TemplateRendererFactory::build()->getRenderer($templates_dir);
         $presenter     = new HomePagePresenter(
@@ -225,7 +227,8 @@ class BurningParrotTheme extends Layout
             $current_user,
             $most_secure_url,
             $login_presenter,
-            $display_homepage_login_form
+            $display_new_account_button,
+            $login_url
         );
         $renderer->renderToPage('homepage', $presenter);
     }
diff --git a/src/www/themes/BurningParrot/HomePagePresenter.php b/src/www/themes/BurningParrot/HomePagePresenter.php
index 4410090..e54e053 100644
--- a/src/www/themes/BurningParrot/HomePagePresenter.php
+++ b/src/www/themes/BurningParrot/HomePagePresenter.php
@@ -42,8 +42,9 @@ class HomePagePresenter
     public $sign_in_submit;
     public $forgot_password;
     public $not_a_member;
-    public $display_homepage_login_form;
     public $is_custom_logo_used;
+    public $display_new_account_button;
+    public $login_url;
 
     /**
      * HomePagePresenter constructor.
@@ -53,7 +54,8 @@ class HomePagePresenter
         PFUser $user,
         $most_secure_url,
         User_LoginPresenter $login,
-        $display_homepage_login_form
+        $display_new_account_button,
+        $login_url
     ) {
         $this->headline                    = $headline;
         $this->is_custom_logo_used         = Admin_Homepage_LogoFinder::isCustomLogoUsed();
@@ -62,7 +64,8 @@ class HomePagePresenter
         $this->user_is_anonymous           = $user->isAnonymous();
         $this->most_secure_url             = $most_secure_url;
         $this->login                       = $login;
-        $this->display_homepage_login_form = $display_homepage_login_form;
+        $this->display_new_account_button  = $display_new_account_button;
+        $this->login_url                   = $login_url;
 
         $purifier        = Codendi_HTMLPurifier::instance();
         $welcome_message = $GLOBALS['Language']->getText('homepage', 'welcome_title');
-- 
2.9.4

