Explorar el Código

Added support for resetting passwords.

Alan Lam hace 8 años
padre
commit
da3a92610a
Se han modificado 6 ficheros con 233 adiciones y 1 borrados
  1. 85 0
      controllers/reset_password.php
  2. 77 0
      emails/password_reset.html
  3. 25 1
      engine/user.php
  4. 1 0
      routes.php
  5. 1 0
      views/log_in.php
  6. 44 0
      views/reset_password.php

+ 85 - 0
controllers/reset_password.php

@@ -0,0 +1,85 @@
+<?php
+
+$page_title = "Reset Password";
+
+Core::addBreadcrumb("/reset-password", $page_title);
+
+if(!isset($token)) $token = false;
+
+if($token) {
+	$user = User::queryOne([
+		"reset_token" => $token
+	]);
+
+	if(!$user) throw new Exception("Invalid reset token.");
+
+	$submit = http::value("submit");
+	if($submit) {
+		try {
+			Core::validateCSRFToken($submit);
+
+			$form_exception = new FormException;
+
+			$password = http::value("password");
+			$password_confirm = http::value("password_confirm");
+
+			if($password != $password_confirm) $form_exception->addError("Passwords do not match.");
+			if(!User::validatePassword($password)) $form_exception->addError("Password must be at least 6 characters in length and contain at least one lowercase, uppercase, number and special character.");
+
+			if($form_exception->hasErrors()) throw $form_exception;
+
+			$user->password = password_hash($password, PASSWORD_DEFAULT, ["cost" => 14]);
+
+			$success = $user->commit();
+
+			Core::addAlert("success", "Password reset successfully. You can now log in.");
+			HTTP::redirect("/log-in");
+		}
+		catch(FormException $e) {
+			$e->generateAlerts();
+		}
+		catch(Exception $e) {
+			Core::addAlert("danger", $e->getMessage());
+		}
+
+	}
+}
+else {
+	$submit = http::value("submit");
+	if($submit) {
+		try {
+			Core::validateCSRFToken($submit);
+
+			$form_exception = new FormException;
+
+			$email = http::value("email", "email");
+			if(!Form::checkRecaptcha()) $form_exception->addError("reCAPTCHA check failed.");
+			if(empty($email)) $form_exception->addError("Invalid email address.");
+			if($form_exception->hasErrors()) throw $form_exception;
+
+			$user = User::queryOne(["email" => db::escape($email)]);
+			if($user) {
+				$user->sendPasswordResetEmail();
+			}
+			else {
+				sleep(2);
+			}
+
+			Core::addAlert("success", "A password reset email has been sent to the address provided if an account exists. Follow the instructions in the email to reset your password.");
+			HTTP::redirect("/log-in");
+		}
+		catch(FormException $e) {
+			$e->generateAlerts();
+		}
+		catch(Exception $e) {
+			Core::addAlert("danger", $e->getMessage());
+		}
+
+	}
+}
+
+View::includeOnce("header", compact("page_title"));
+View::include("reset_password", compact("token"));
+View::includeOnce("footer");
+
+?>

+ 77 - 0
emails/password_reset.html

@@ -0,0 +1,77 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+	<head>
+		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+		<title>Check Policy</title>
+		<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
+	</head>
+	<body>
+		<table align="center" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse;">
+			<tr>
+				<td>
+					<table border="0" cellpadding="0" cellspacing="0" width="600" style="border: 1px solid #cccccc;">
+						<tr>
+							<td align="center" style="padding: 40px 0 40px 0; border-bottom: 1px solid #cccccc;">
+								<a href="">
+									<img src="../public/img/logo.png" width="386" height="80" style="display: block;" alt="Check Policy logo" />
+								</a>
+							</td>
+						</tr>
+						<tr>
+							<td style="padding: 40px 30px 40px 30px;">
+								<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
+											<b>Hi {name},</b>
+										</td>
+									</tr>
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif; font-size: 24px;">
+											<b>A reset password request has been received.</b>
+										</td>
+									</tr>
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
+											To reset your password, please tap on the following link:
+										</td>
+									</tr>
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
+											<a href="{link}">{link}</a>
+										</td>
+									</tr>
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
+											If you <strong>did not</strong> request for your password to be reset, please ignore this email.
+										</td>
+									</tr>
+									<tr>
+										<td style="font-family: Arial, sans-serif;">
+											Check Policy Team
+										</td>
+									</tr>
+								</table>
+							</td>
+						</tr>
+						<tr>
+							<td bgcolor="#4CAF50" style="padding: 40px 30px 40px 30px;">
+								<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
+									<tr>
+										<td style="font-family: Arial, sans-serif;">
+											<b><font color="#ffffff">&copy; Check Policy 2018</font></b>
+										</td>
+										<td align="right" style="font-family: Arial, sans-serif;">
+											<a href="https://www.facebook.com/checkpolicy" style="color: #ffffff;">
+												<font color="#ffffff">Facebook</font>
+											</a>
+										</td>
+									</tr>
+								</table>
+							</td>
+						</tr>
+					</table>
+				</td>
+			</tr>
+		</table>
+	</body>
+</html>

+ 25 - 1
engine/user.php

@@ -4,7 +4,7 @@ class User extends Model {
 
 	protected static $table = "user";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["email", "old_id", "password", "activated", "level", "score", "title", "first_name", "last_name", "gender", "date_of_birth", "phone", "verify_token", "preferred_payment_method", "bank_name", "bank_sort_code", "bank_account_number", "paypal_email", "dts_registered", "dts_last_seen", "dts_verified", "verifier_id", "verifier_notes"];
+	protected static $fillable_fields = ["email", "old_id", "password", "activated", "level", "score", "title", "first_name", "last_name", "gender", "date_of_birth", "phone", "verify_token", "reset_token", "preferred_payment_method", "bank_name", "bank_sort_code", "bank_account_number", "paypal_email", "dts_registered", "dts_last_seen", "dts_verified", "verifier_id", "verifier_notes"];
 
 	protected static $field_map = [
 	];
@@ -97,6 +97,30 @@ class User extends Model {
 
 	}
 
+	public function sendPasswordResetEmail() {
+
+		$this->reset_token = bin2hex(random_bytes(16));
+		$this->commit();
+
+		$mail = new Email();
+
+		$mail->loadHtmlTemplate("password_reset");
+		$mail->setValues([
+			"name" => $this->first_name,
+			"link" => PROTOCOL . "://" . DOMAIN . "/reset-password/" . $this->reset_token
+		]);
+
+		$mail->setFrom("noreply@check-policy.co.uk", "Check Policy");
+		$mail->addAddress($this->email, $this->getDisplayName());
+
+		$mail->setSubject("Password reset instructions");
+
+		$mail->send();
+
+		return true;
+
+	}
+
 	public function canAdmin() {
 		return $this->level >= User::LEVEL_ADMIN;
 	}

+ 1 - 0
routes.php

@@ -20,6 +20,7 @@ $routes = [
 	"/^register$/" => ["controller" => "register"],
 	"/^log-in$/" => ["controller" => "log_in"],
 	"/^reset-password$/" => ["controller" => "reset_password"],
+	"/^reset-password\/([a-f0-9]+)$/" => ["controller" => "reset_password", "params" => ["token"]],
 	"/^log-out$/" => ["controller" => "log_out"],
 	"/^activate$/" => ["controller" => "activate"],
 	"/^privacy-policy$/" => ["controller" => "privacy_policy"],

+ 1 - 0
views/log_in.php

@@ -21,6 +21,7 @@
 				</div>
 
 				<button class="btn btn-primary" type="submit" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>">Log In</button>
+				<a class="btn btn-secondary" href="/reset-password">Forgot Password</a>
 			</div>
 		</div>
 

+ 44 - 0
views/reset_password.php

@@ -0,0 +1,44 @@
+<div class="container mt-3 py-4">
+	<h2>Reset Password</h2>
+
+
+	<form method="post">
+
+		<div class="row">
+			<div class="col-lg-8 offset-lg-2">
+			<?php
+			if($token) {
+				?>
+				<div class="row mb-1">
+					<label class="col-sm-4 col-form-label"  for="password">Password</label>
+					<div class="col-sm">
+						<input class="form-control" type="password" name="password" id="password" placeholder="Enter password" required />
+						<small class="text-muted form-text">At least 6 characters. Must contain at least one lowercase, uppercase, number and special character.</small>
+					</div>
+				</div>
+				<div class="row mb-1">
+					<label class="col-sm-4 col-form-label"  for="password-confirm">Confirm Password</label>
+					<div class="col-sm">
+						<input class="form-control" type="password" name="password_confirm" id="password-confirm" placeholder="Confirm password" required />
+					</div>
+				</div>
+				<?php
+			}
+			else {
+				?>
+				<div class="row mb-1">
+					<label class="col-sm-4 col-form-label"  for="email">Email Address</label>
+					<div class="col-sm">
+						<input class="form-control" type="email" name="email" id="email" placeholder="Enter email address" required />
+					</div>
+				</div>
+				<div class="g-recaptcha my-3" data-sitekey="<?=GOOGLE_RECAPTCHA_SITE_KEY?>"></div>
+				<?php
+			}
+			?>
+
+				<button class="btn btn-primary" type="submit" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>">Submit</button>
+			</div>
+		</div>
+	</form>
+</div>