From cf2d93f71a7a25363f80f17113dd01cb5ee4a35d Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Thu, 8 May 2014 21:38:32 +0100 Subject: [PATCH] Adding forgot password functionality. --- PHPCI/Controller/SessionController.php | 71 +++++++++++++ PHPCI/Helper/Email.php | 127 ++++++++++++++++++++++++ PHPCI/View/Session.phtml | 88 ++++++++++++++++ PHPCI/View/Session/forgotPassword.phtml | 33 ++++++ PHPCI/View/Session/login.phtml | 95 +----------------- PHPCI/View/Session/resetPassword.phtml | 27 +++++ 6 files changed, 351 insertions(+), 90 deletions(-) create mode 100644 PHPCI/Helper/Email.php create mode 100644 PHPCI/View/Session.phtml create mode 100644 PHPCI/View/Session/forgotPassword.phtml create mode 100644 PHPCI/View/Session/resetPassword.phtml diff --git a/PHPCI/Controller/SessionController.php b/PHPCI/Controller/SessionController.php index 15563b19..2c4712ab 100644 --- a/PHPCI/Controller/SessionController.php +++ b/PHPCI/Controller/SessionController.php @@ -10,6 +10,7 @@ namespace PHPCI\Controller; use b8; +use PHPCI\Helper\Email; /** * Session Controller - Handles user login / logout. @@ -88,4 +89,74 @@ class SessionController extends \PHPCI\Controller header('Location: ' . PHPCI_URL); die; } + + public function forgotPassword() + { + if ($this->request->getMethod() == 'POST') { + $email = $this->getParam('email', null); + $user = $this->userStore->getByEmail($email); + + if (empty($user)) { + $this->view->error = 'No user exists with that email address, please try again.'; + return $this->view->render(); + } + + $key = md5(date('Y-m-d') . $user->getHash()); + $url = PHPCI_URL; + $name = $user->getName(); + $id = $user->getId(); + + $message = <<setTo($user->getEmail(), $user->getName()); + $email->setSubject('Password reset'); + $email->setBody($message); + $email->send(); + + $this->view->emailed = true; + } + + return $this->view->render(); + } + + public function resetPassword($id, $key) + { + $user = $this->userStore->getById($id); + $userKey = md5(date('Y-m-d') . $user->getHash()); + + if (empty($user) || $key != $userKey) { + $this->view->error = 'Invalid password reset request.'; + return $this->view->render(); + } + + if ($this->request->getMethod() == 'POST') { + $hash = password_hash($this->getParam('password'), PASSWORD_DEFAULT); + $user->setHash($hash); + + $_SESSION['user'] = $this->userStore->save($user); + $_SESSION['user_id'] = $user->getId(); + + header('Location: ' . PHPCI_URL); + die; + } + + $this->view->id = $id; + $this->view->key = $key; + + return $this->view->render(); + } } diff --git a/PHPCI/Helper/Email.php b/PHPCI/Helper/Email.php new file mode 100644 index 00000000..32cbe4f4 --- /dev/null +++ b/PHPCI/Helper/Email.php @@ -0,0 +1,127 @@ +'; + + protected $to = array(); + protected $cc = array(); + protected $subject = 'Email from PHPCI'; + protected $body = ''; + protected $isHtml = false; + protected $config; + + public function __construct() + { + $this->config = Config::getInstance(); + } + + public function setTo($email, $name = null) + { + $this->to[$email] = $name; + + return $this; + } + + public function addCc($email, $name = null) + { + $this->cc[$email] = $name; + + return $this; + } + + public function setSubject($subject) + { + $this->subject = $subject; + + return $this; + } + + public function setBody($body) + { + $this->body = $body; + + return $this; + } + + public function setIsHtml($isHtml = false) + { + $this->isHtml = $isHtml; + + return $this; + } + + public function send() + { + $smtpServer = $this->config->get('phpci.email_settings.smtp_address'); + + if (empty($smtpServer)) { + return $this->sendViaMail(); + } else { + return $this->sendViaSwiftMailer(); + } + } + + protected function sendViaMail() + { + $headers = ''; + + if ($this->isHtml) { + $headers = 'Content-Type: text/html' . PHP_EOL; + } + + $headers .= 'From: ' . $this->getFrom() . PHP_EOL; + + $to = array(); + foreach ($this->to as $email => $name) { + $thisTo = $email; + + if (!is_null($name)) { + $thisTo = '"' . $name . '" <' . $thisTo . '>'; + } + + $to[] = $thisTo; + } + + $to = implode(', ', $to); + + return mail($to, $this->subject, $this->body, $headers); + } + + protected function sendViaSwiftMailer() + { + $factory = new MailerFactory($this->config->get('phpci')); + $mailer = $factory->getSwiftMailerFromConfig(); + + $message = \Swift_Message::newInstance($this->subject) + ->setFrom($this->getFrom()) + ->setTo($this->to) + ->setBody($this->body); + + if ($this->isHtml) { + $message->setContentType('text/html'); + } + + if (is_array($this->cc) && count($this->cc)) { + $message->setCc($this->cc); + } + + return $mailer->send($message); + } + + protected function getFrom() + { + $email = $this->config->get('phpci.email_settings.from_address', self::DEFAULT_FROM); + + if (empty($email)) { + $email = self::DEFAULT_FROM; + } + + return $email; + } +} \ No newline at end of file diff --git a/PHPCI/View/Session.phtml b/PHPCI/View/Session.phtml new file mode 100644 index 00000000..ed75d2b4 --- /dev/null +++ b/PHPCI/View/Session.phtml @@ -0,0 +1,88 @@ + + + + Log in to PHPCI + + + + + + + + + + +
+
+ +
+ +
+ + +
+
+ + \ No newline at end of file diff --git a/PHPCI/View/Session/forgotPassword.phtml b/PHPCI/View/Session/forgotPassword.phtml new file mode 100644 index 00000000..a9897bbd --- /dev/null +++ b/PHPCI/View/Session/forgotPassword.phtml @@ -0,0 +1,33 @@ + +

+ We've emailed you a link to reset your password. +

+ + +
+
+ Don't worry!
Just enter your email address below and we'll email you a link to reset your password. +
+ +
+
+ +
+ + +
+
+
+ + +
+ +
+ +
+
+
+
+ + + \ No newline at end of file diff --git a/PHPCI/View/Session/login.phtml b/PHPCI/View/Session/login.phtml index 2314a639..bc2f16e1 100644 --- a/PHPCI/View/Session/login.phtml +++ b/PHPCI/View/Session/login.phtml @@ -1,91 +1,6 @@ - - - - Log in to PHPCI + +

Incorrect email address or password

+ + - - - - - - - - - -
-
- -
- -

Incorrect email address or password

- - -
- - -
-
- - +Forgotten your password? \ No newline at end of file diff --git a/PHPCI/View/Session/resetPassword.phtml b/PHPCI/View/Session/resetPassword.phtml new file mode 100644 index 00000000..9544879e --- /dev/null +++ b/PHPCI/View/Session/resetPassword.phtml @@ -0,0 +1,27 @@ + + +
+
+ Please enter a new password +
+ +
+
+
+ + +
+ +
+ +
+
+
+
+ +
+ +
+ + +