Permalink
Browse files

Merge branch 'reminders'

  • Loading branch information...
taylorotwell committed Nov 15, 2013
2 parents 90c82aa + 50c8f8c commit f86d5ea61f6adc2004b8ed259a62cc8008d08fd0
@@ -0,0 +1,63 @@
<?php namespace Illuminate\Auth\Console;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
class RemindersControllerCommand extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'auth:reminders-controller';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a stub password reminder controller';
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* Create a new reminder table command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @return void
*/
public function __construct(Filesystem $files)
{
parent::__construct();
$this->files = $files;
}
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$destination = $this->laravel['path'].'/controllers/RemindersController.php';
if ( ! $this->files->exists($destination))
{
$this->files->copy(__DIR__.'/stubs/controller.stub', $destination);
$this->info('Password reminders controller created successfully!');
}
else
{
$this->error('Password reminders controller already exists!');
}
}
}
@@ -0,0 +1,73 @@
<?php
class RemindersController extends Controller {
/**
* Display the password reminder view.
*
* @return Response
*/
public function getRemind()
{
return View::make('password.remind');
}
/**
* Handle a POST request to remind a user of their password.
*
* @return Response
*/
public function postRemind()
{
switch (Password::remind(Input::only('email')))
{
case Password::INVALID_USER:
return Redirect::back()->with('error', 'User not found.');
case Password::REMINDER_SENT:
return Redirect::back()->with('status', 'Password reminder sent!');
}
}
/**
* Display the password reset view for the given token.
*
* @param string $token
* @return Response
*/
public function getReset($token)
{
return View::make('password.reset')->with('token', $token);
}
/**
* Handle a POST request to reset a user's password.
*
* @return Response
*/
public function postReset()
{
$credentials = Input::only(
'email', 'password', 'password_confirmation', 'token'
);
$response = Password::reset($credentials, function($user, $password)
{
$user->password = Hash::make($password);
$user->save();
});
switch ($response)
{
case Password::INVALID_PASSWORD:
case Password::INVALID_TOKEN:
case Password::INVALID_USER:
return Redirect::back()->with('error', Lang::get($response));
case Password::PASSWORD_RESET:
return Redirect::to('/');
}
}
}
@@ -2,11 +2,45 @@
use Closure;
use Illuminate\Mail\Mailer;
use Illuminate\Routing\Redirector;
use Illuminate\Auth\UserProviderInterface;
class PasswordBroker {
/**
* Constant representing a successfully sent reminder.
*
* @var int
*/
const REMINDER_SENT = 'reminder.sent';
/**
* Constant representing a successfully reset password.
*
* @var int
*/
const PASSWORD_RESET = 'reminder.reset';
/**
* Constant representing the user not found response.
*
* @var int
*/
const INVALID_USER = 'reminder.user';
/**
* Constant representing an invalid password.
*
* @var int
*/
const INVALID_PASSWORD = 'reminder.password';
/**
* Constant representing an invalid token.
*
* @var int
*/
const INVALID_TOKEN = 'reminder.token';
/**
* The password reminder repository.
*
@@ -21,13 +55,6 @@ class PasswordBroker {
*/
protected $users;
/**
* The redirector instance.
*
* @var \Illuminate\Routing\Redirector
*/
protected $redirector;
/**
* The mailer instance.
*
@@ -42,25 +69,29 @@ class PasswordBroker {
*/
protected $reminderView;
/**
* The custom password validator callback.
*
* @var \Closure
*/
protected $passwordValidator;
/**
* Create a new password broker instance.
*
* @param \Illuminate\Auth\Reminders\ReminderRepositoryInterface $reminders
* @param \Illuminate\Auth\UserProviderInterface $users
* @param \Illuminate\Routing\Redirector $redirect
* @param \Illuminate\Mail\Mailer $mailer
* @param string $reminderView
* @return void
*/
public function __construct(ReminderRepositoryInterface $reminders,
UserProviderInterface $users,
Redirector $redirect,
Mailer $mailer,
$reminderView)
{
$this->users = $users;
$this->mailer = $mailer;
$this->redirect = $redirect;
$this->reminders = $reminders;
$this->reminderView = $reminderView;
}
@@ -81,7 +112,7 @@ public function remind(array $credentials, Closure $callback = null)
if (is_null($user))
{
return $this->makeErrorRedirect('user');
return self::INVALID_USER;
}
// Once we have the reminder token, we are ready to send a message out to the
@@ -91,7 +122,7 @@ public function remind(array $credentials, Closure $callback = null)
$this->sendReminder($user, $token, $callback);
return $this->redirect->refresh()->with('success', true);
return self::REMINDER_SENT;
}
/**
@@ -136,16 +167,16 @@ public function reset(array $credentials, Closure $callback)
return $user;
}
$pass = $this->getPassword();
$pass = $credentials['password'];
// Once we have called this callback, we will remove this token row from the
// table and return the response from this callback so the user gets sent
// to the destination given by the developers from the callback return.
$response = call_user_func($callback, $user, $pass);
call_user_func($callback, $user, $pass);
$this->reminders->delete($this->getToken());
$this->reminders->delete($credentials['token']);
return $response;
return self::PASSWORD_RESET;
}
/**
@@ -158,47 +189,62 @@ protected function validateReset(array $credentials)
{
if (is_null($user = $this->getUser($credentials)))
{
return $this->makeErrorRedirect('user');
return self::INVALID_USER;
}
if ( ! $this->validNewPasswords())
if ( ! $this->validNewPasswords($credentials))
{
return $this->makeErrorRedirect('password');
return self::INVALID_PASSWORD;
}
if ( ! $this->reminders->exists($user, $this->getToken()))
if ( ! $this->reminders->exists($user, $credentials['token']))
{
return $this->makeErrorRedirect('token');
return self::INVALID_TOKEN;
}
return $user;
}
/**
* Set a custom password validator.
*
* @param \Closure $callback
* @return void
*/
public function validator(Closure $callback)
{
$this->passwordValidator = $callback;
}
/**
* Determine if the passwords match for the request.
*
* @param array $credentials
* @return bool
*/
protected function validNewPasswords()
protected function validNewPasswords(array $credentials)
{
$password = $this->getPassword();
$confirm = $this->getConfirmedPassword();
return $password && strlen($password) >= 6 && $password == $confirm;
if (isset($this->passwordValidator))
{
return call_user_func($this->passwordValidator, $credentials);
}
else
{
return $this->validatePasswordWithDefaults($credentials);
}
}
/**
* Make an error redirect response.
* Determine if the passwords are valid for the request.
*
* @param string $reason
* @return \Illuminate\Http\RedirectResponse
* @param array $credentials
* @return bool
*/
protected function makeErrorRedirect($reason = '')
protected function validatePasswordWithDefaults(array $credentials)
{
if ($reason != '') $reason = 'reminders.'.$reason;
list($password, $confirm) = array($credentials['password'], $credentials['password_confirmation']);
return $this->redirect->refresh()->with('error', true)->with('reason', $reason);
return $password && strlen($password) >= 6 && $password == $confirm;
}
/**
@@ -219,46 +265,6 @@ public function getUser(array $credentials)
return $user;
}
/**
* Get the current request object.
*
* @return \Illuminate\Http\Request
*/
protected function getRequest()
{
return $this->redirect->getUrlGenerator()->getRequest();
}
/**
* Get the reset token for the current request.
*
* @return string
*/
protected function getToken()
{
return $this->getRequest()->input('token');
}
/**
* Get the password for the current request.
*
* @return string
*/
protected function getPassword()
{
return $this->getRequest()->input('password');
}
/**
* Get the confirmed password.
*
* @return string
*/
protected function getConfirmedPassword()
{
return $this->getRequest()->input('password_confirmation');
}
/**
* Get the password reminder repository implementation.
*
Oops, something went wrong.

0 comments on commit f86d5ea

Please sign in to comment.