From 5c4803ddf21f48916e6561586425eff39eb195cb Mon Sep 17 00:00:00 2001
From: Duncan Sommerville <duncan.sommerville@sourcefabric.org>
Date: Wed, 22 Jul 2015 13:48:47 -0400
Subject: [PATCH] SAAS-781 - password reset

---
 .../controllers/LoginController.php           |  24 ++--
 .../forms/EmailServerPreferences.php          | 106 ------------------
 .../application/forms/PasswordRestore.php     |   4 +-
 airtime_mvc/application/models/Auth.php       |   4 +-
 airtime_mvc/application/models/Email.php      |  12 +-
 airtime_mvc/application/models/Preference.php |  81 -------------
 .../views/scripts/form/login.phtml            |  12 +-
 .../scripts/login/password-restore.phtml      |   5 +-
 8 files changed, 28 insertions(+), 220 deletions(-)
 delete mode 100644 airtime_mvc/application/forms/EmailServerPreferences.php

diff --git a/airtime_mvc/application/controllers/LoginController.php b/airtime_mvc/application/controllers/LoginController.php
index e810d4246..face87056 100644
--- a/airtime_mvc/application/controllers/LoginController.php
+++ b/airtime_mvc/application/controllers/LoginController.php
@@ -140,9 +140,6 @@ class LoginController extends Zend_Controller_Action
         
         Application_Model_Locale::configureLocalization($request->getcookie('airtime_locale', $stationLocale));
 
-//        if (!Application_Model_Preference::GetEnableSystemEmail()) {
-//            $this->_redirect('login');
-//        } else {
         //uses separate layout without a navigation.
         $this->_helper->layout->setLayout('login');
 
@@ -150,16 +147,16 @@ class LoginController extends Zend_Controller_Action
 
         $request = $this->getRequest();
         if ($request->isPost() && $form->isValid($request->getPost())) {
-            if (is_null($form->username->getValue()) || $form->username->getValue() == '') {
-                $user = CcSubjsQuery::create()
-                    ->filterByDbEmail($form->email->getValue())
-                    ->findOne();
+            $query = CcSubjsQuery::create();
+            if (empty($form->username->getValue())) {
+                $query->filterByDbEmail($form->email->getValue());
+            } else if (empty($form->email->getValue())) {
+                $query->filterByDbLogin($form->username->getValue());
             } else {
-                $user = CcSubjsQuery::create()
-                    ->filterByDbEmail($form->email->getValue())
-                    ->filterByDbLogin($form->username->getValue())
-                    ->findOne();
+                $query->filterByDbEmail($form->email->getValue())
+                      ->filterByDbLogin($form->username->getValue());
             }
+            $user = $query->findOne();
 
             if (!empty($user)) {
                 $auth = new Application_Model_Auth();
@@ -168,15 +165,14 @@ class LoginController extends Zend_Controller_Action
                 if ($success) {
                     $this->_helper->redirector('password-restore-after', 'login');
                 } else {
-                    $form->email->addError($this->view->translate(_("Email could not be sent. Check your mail server settings and ensure it has been configured properly.")));
+                    $form->email->addError($this->view->translate(_("There was a problem sending the recovery email.")));
                 }
             } else {
-                $form->email->addError($this->view->translate(_("Given email not found.")));
+                $form->email->addError($this->view->translate(_("We couldn't find the email you entered - you can also try <a href='https://account.sourcefabric.com/pwreset.php'>here</a>.")));
             }
         }
 
         $this->view->form = $form;
-//        }
     }
 
     public function passwordRestoreAfterAction()
diff --git a/airtime_mvc/application/forms/EmailServerPreferences.php b/airtime_mvc/application/forms/EmailServerPreferences.php
deleted file mode 100644
index 1e0feda79..000000000
--- a/airtime_mvc/application/forms/EmailServerPreferences.php
+++ /dev/null
@@ -1,106 +0,0 @@
-<?php
-require_once 'customvalidators/ConditionalNotEmpty.php';
-
-class Application_Form_EmailServerPreferences extends Zend_Form_SubForm
-{
-
-    public function init()
-    {
-        $this->setDecorators(array(
-            array('ViewScript', array('viewScript' => 'form/preferences_email_server.phtml'))
-        ));
-
-        // Enable system emails
-        $this->addElement('checkbox', 'enableSystemEmail', array(
-            'label' => _('Enable System Emails (Password Reset)'),
-            'required' => false,
-            'value' => Application_Model_Preference::GetEnableSystemEmail(),
-            'decorators' => array(
-                'ViewHelper'
-            )
-        ));
-
-        $this->addElement('text', 'systemEmail', array(
-            'class' => 'input_text',
-            'label' => _("Reset Password 'From' Email"),
-            'value' => Application_Model_Preference::GetSystemEmail(),
-            'readonly' => true,
-            'decorators' => array('viewHelper')
-        ));
-
-        $this->addElement('checkbox', 'configureMailServer', array(
-            'label' => _('Configure Mail Server'),
-            'required' => false,
-            'value' => Application_Model_Preference::GetMailServerConfigured(),
-            'decorators' => array (
-                'viewHelper'
-            )
-        ));
-
-        $this->addElement('checkbox', 'msRequiresAuth', array(
-            'label' => _('Requires Authentication'),
-            'required' => false,
-            'value' => Application_Model_Preference::GetMailServerRequiresAuth(),
-            'decorators' => array(
-                'viewHelper'
-            )
-        ));
-
-        $this->addElement('text', 'mailServer', array(
-            'class' => 'input_text',
-            'label' => _('Mail Server'),
-            'value' => Application_Model_Preference::GetMailServer(),
-            'readonly' => true,
-            'decorators' => array('viewHelper'),
-            'allowEmpty' => false,
-            'validators' => array(
-                new ConditionalNotEmpty(array(
-                    'configureMailServer' => '1'
-                ))
-            )
-        ));
-
-        $this->addElement('text', 'email', array(
-            'class' => 'input_text',
-            'label' => _('Email Address'),
-            'value' => Application_Model_Preference::GetMailServerEmailAddress(),
-            'readonly' => true,
-            'decorators' => array('viewHelper'),
-            'allowEmpty' => false,
-            'validators' => array(
-                new ConditionalNotEmpty(array(
-                    'configureMailServer' => '1',
-                    'msRequiresAuth' => '1'
-                ))
-            )
-        ));
-
-        $this->addElement('password', 'ms_password', array(
-            'class' => 'input_text',
-            'label' => _('Password'),
-            'value' => Application_Model_Preference::GetMailServerPassword(),
-            'readonly' => true,
-            'decorators' => array('viewHelper'),
-            'allowEmpty' => false,
-            'validators' => array(
-                new ConditionalNotEmpty(array(
-                    'configureMailServer' => '1',
-                    'msRequiresAuth' => '1'
-                ))
-            ),
-            'renderPassword' => true
-        ));
-
-        $port = new Zend_Form_Element_Text('port');
-        $port->class = 'input_text';
-        $port->setRequired(false)
-            ->setValue(Application_Model_Preference::GetMailServerPort())
-            ->setLabel(_('Port'))
-            ->setAttrib('readonly', true)
-            ->setDecorators(array('viewHelper'));
-
-        $this->addElement($port);
-
-    }
-
-}
diff --git a/airtime_mvc/application/forms/PasswordRestore.php b/airtime_mvc/application/forms/PasswordRestore.php
index 0e8f4ad9c..12a957617 100644
--- a/airtime_mvc/application/forms/PasswordRestore.php
+++ b/airtime_mvc/application/forms/PasswordRestore.php
@@ -11,7 +11,7 @@ class Application_Form_PasswordRestore extends Zend_Form
         ));
 
         $this->addElement('text', 'email', array(
-            'label' => _('E-mail'),
+            'label' => _('Email'),
             'required' => true,
             'filters' => array(
                 'stringTrim',
@@ -43,7 +43,7 @@ class Application_Form_PasswordRestore extends Zend_Form
 
         $cancel = new Zend_Form_Element_Button("cancel");
         $cancel->class = 'ui-button ui-widget ui-state-default ui-button-text-only center';
-        $cancel->setLabel(_("Cancel"))
+        $cancel->setLabel(_("Return to login"))
                ->setIgnore(True)
                ->setAttrib('onclick', 'redirectToLogin();')
                ->setDecorators(array('ViewHelper'));
diff --git a/airtime_mvc/application/models/Auth.php b/airtime_mvc/application/models/Auth.php
index 6c4c75edd..044972b37 100644
--- a/airtime_mvc/application/models/Auth.php
+++ b/airtime_mvc/application/models/Auth.php
@@ -34,9 +34,7 @@ class Application_Model_Auth
         $message .= "{$e_link_protocol}://{$e_link_base}:{$e_link_port}{$e_link_path}";
 
         $str = sprintf(_('%s Password Reset'), PRODUCT_NAME);
-        $success = Application_Model_Email::send($str, $message, $user->getDbEmail());
-
-        return $success;
+        return Application_Model_Email::send($str, $message, $user->getDbEmail());
     }
 
     public function invalidateTokens($user, $action)
diff --git a/airtime_mvc/application/models/Email.php b/airtime_mvc/application/models/Email.php
index 774617c1e..e1b399f4c 100644
--- a/airtime_mvc/application/models/Email.php
+++ b/airtime_mvc/application/models/Email.php
@@ -2,18 +2,20 @@
 
 class Application_Model_Email
 {
+
     /**
      * Send email
      *
      * @param  string $subject
      * @param  string $message
-     * @param  mixed  $tos
-     * @return void
+     * @param  mixed  $to
+     * @return boolean
      */
-    public static function send($subject, $message, $tos, $from = null)
-    {
+    public static function send($subject, $message, $to) {
 
-        return mail($tos, $subject, $message);
+        $headers = 'From: Airtime <noreply@account.sourcefabric.com>';
+        return mail($to, $subject, $message, $headers);
 
     }
+
 }
diff --git a/airtime_mvc/application/models/Preference.php b/airtime_mvc/application/models/Preference.php
index 6143058ba..6f899f439 100644
--- a/airtime_mvc/application/models/Preference.php
+++ b/airtime_mvc/application/models/Preference.php
@@ -1161,87 +1161,6 @@ class Application_Model_Preference
     {
         return self::getValue("auto_switch");
     }
-
-    public static function SetEnableSystemEmail($upload)
-    {
-        self::setValue("enable_system_email", $upload);
-    }
-
-    public static function GetEnableSystemEmail()
-    {
-        $v =  self::getValue("enable_system_email");
-        return ($v === "") ?  0 : $v;
-    }
-
-    public static function SetSystemEmail($value)
-    {
-        self::setValue("system_email", $value, false);
-    }
-
-    public static function GetSystemEmail()
-    {
-        return self::getValue("system_email");
-    }
-
-    public static function SetMailServerConfigured($value)
-    {
-        self::setValue("mail_server_configured", $value, false);
-    }
-
-    public static function GetMailServerConfigured()
-    {
-        return self::getValue("mail_server_configured");
-    }
-
-    public static function SetMailServer($value)
-    {
-        self::setValue("mail_server", $value, false);
-    }
-
-    public static function GetMailServer()
-    {
-        return self::getValue("mail_server");
-    }
-
-    public static function SetMailServerEmailAddress($value)
-    {
-        self::setValue("mail_server_email_address", $value, false);
-    }
-
-    public static function GetMailServerEmailAddress()
-    {
-        return self::getValue("mail_server_email_address");
-    }
-
-    public static function SetMailServerPassword($value)
-    {
-        self::setValue("mail_server_password", $value, false);
-    }
-
-    public static function GetMailServerPassword()
-    {
-        return self::getValue("mail_server_password");
-    }
-
-    public static function SetMailServerPort($value)
-    {
-        self::setValue("mail_server_port", $value, false);
-    }
-
-    public static function GetMailServerPort()
-    {
-        return self::getValue("mail_server_port");
-    }
-
-    public static function SetMailServerRequiresAuth($value)
-    {
-        self::setValue("mail_server_requires_auth", $value, false);
-    }
-
-    public static function GetMailServerRequiresAuth()
-    {
-        return self::getValue("mail_server_requires_auth");
-    }
     /* User specific preferences end */
 
     public static function ShouldShowPopUp()
diff --git a/airtime_mvc/application/views/scripts/form/login.phtml b/airtime_mvc/application/views/scripts/form/login.phtml
index 09893410d..0c96983ca 100644
--- a/airtime_mvc/application/views/scripts/form/login.phtml
+++ b/airtime_mvc/application/views/scripts/form/login.phtml
@@ -30,17 +30,15 @@
 
     <?php echo $this->element->getElement('csrf') ?>
     
-<!--    --><?php //if (Application_Model_Preference::GetEnableSystemEmail()): ?>
-    <dt id="reset-label" class="hidden">&nbsp;</dt>
-    <dd id="reset-element" class="text-right">
-      <a href="<?php echo $this->baseUrl('login/password-restore'); ?>" class="link reset"><?php echo _("Reset password") ?></a>
-    </dd>
-<!--    --><?php //endif; ?>
     <?php echo $this->element->getElement('captcha') ?>
     <dt id="submit-label">&nbsp;</dt>
     <dd id="submit-element">
       <?php echo $this->element->getElement('submit') ?>
     </dd>
-    
+
+    <dt id="reset-label" class="hidden">&nbsp;</dt>
+    <dd id="reset-element" class="text-right">
+      <a href="<?php echo $this->baseUrl('login/password-restore'); ?>" class="link reset"><?php echo _("Forgot your password?") ?></a>
+    </dd>
   </dl>
 </form>
diff --git a/airtime_mvc/application/views/scripts/login/password-restore.phtml b/airtime_mvc/application/views/scripts/login/password-restore.phtml
index e814ca6be..945ae73e4 100644
--- a/airtime_mvc/application/views/scripts/login/password-restore.phtml
+++ b/airtime_mvc/application/views/scripts/login/password-restore.phtml
@@ -1,10 +1,11 @@
 <div class="login_box">
   <div class="logobox">&nbsp;</div>
   <h2><?php echo _("Reset password") ?></h2>
-  
+
   <div id="login" class="login-content clearfix">
     <p class="light">
-        <?php echo _("Please enter your account e-mail address. You will receive a link to create a new password via e-mail.")?>
+        <?php echo _("Enter your account e-mail address or your username (or both, if you have multiple accounts
+                      using the same email address), and we'll send you a link to reset your password.")?>
     </p>
     <?php echo $this->form; ?>
   </div>