Commit 698e166b authored by duxet's avatar duxet

Fix auth and password resetting

parent 6d7aa1dc
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
use DateTime; use DateTime;
use MongoDate; use MongoDate;
class DatabaseReminderRepository extends \Illuminate\Auth\Reminders\DatabaseReminderRepository { class DatabaseTokenRepository extends \Illuminate\Auth\Passwords\DatabaseTokenRepository {
/** /**
* Build the record payload for the table. * Build the record payload for the table.
...@@ -14,36 +14,28 @@ class DatabaseReminderRepository extends \Illuminate\Auth\Reminders\DatabaseRemi ...@@ -14,36 +14,28 @@ class DatabaseReminderRepository extends \Illuminate\Auth\Reminders\DatabaseRemi
*/ */
protected function getPayload($email, $token) protected function getPayload($email, $token)
{ {
return array('email' => $email, 'token' => $token, 'created_at' => new MongoDate); return ['email' => $email, 'token' => $token, 'created_at' => new MongoDate];
} }
/** /**
* Determine if the reminder has expired. * Determine if the token has expired.
* *
* @param object $reminder * @param array $token
* @return bool * @return bool
*/ */
protected function reminderExpired($reminder) protected function tokenExpired($token)
{ {
// Convert MongoDate to a date string. // Convert MongoDate to a date string.
if ($reminder['created_at'] instanceof MongoDate) if ($token['created_at'] instanceof MongoDate)
{ {
$date = new DateTime; $date = new DateTime;
$date->setTimestamp($reminder['created_at']->sec); $date->setTimestamp($token['created_at']->sec);
$reminder['created_at'] = $date->format('Y-m-d H:i:s'); $token['created_at'] = $date->format('Y-m-d H:i:s');
} }
// Convert DateTime to a date string (backwards compatibility). return parent::tokenExpired($token);
elseif (is_array($reminder['created_at']))
{
$date = DateTime::__set_state($reminder['created_at']);
$reminder['created_at'] = $date->format('Y-m-d H:i:s');
}
return parent::reminderExpired($reminder);
} }
} }
<?php namespace Jenssegers\Mongodb\Auth; <?php namespace Jenssegers\Mongodb\Auth;
use Jenssegers\Mongodb\Auth\DatabaseReminderRepository as DbRepository; use Jenssegers\Mongodb\Auth\DatabaseTokenRepository as DbRepository;
class ReminderServiceProvider extends \Illuminate\Auth\Reminders\ReminderServiceProvider { class PasswordResetServiceProvider extends \Illuminate\Auth\Passwords\PasswordResetServiceProvider {
/** /**
* Register the reminder repository implementation. * Register the token repository implementation.
* *
* @return void * @return void
*/ */
protected function registerReminderRepository() protected function registerTokenRepository()
{ {
$this->app->bindShared('auth.reminder.repository', function($app) $this->app->singleton('auth.password.tokens', function($app)
{ {
$connection = $app['db']->connection(); $connection = $app['db']->connection();
// The database token repository is an implementation of the token repository
// The database reminder repository is an implementation of the reminder repo
// interface, and is responsible for the actual storing of auth tokens and // interface, and is responsible for the actual storing of auth tokens and
// their e-mail addresses. We will inject this table and hash key to it. // their e-mail addresses. We will inject this table and hash key to it.
$table = $app['config']['auth.reminder.table']; $table = $app['config']['auth.password.table'];
$key = $app['config']['app.key']; $key = $app['config']['app.key'];
$expire = $app['config']->get('auth.password.expire', 60);
$expire = $app['config']->get('auth.reminder.expire', 60);
return new DbRepository($connection, $table, $key, $expire); return new DbRepository($connection, $table, $key, $expire);
}); });
} }
......
<?php <?php
use Illuminate\Auth\Passwords\PasswordBroker;
class AuthTest extends TestCase { class AuthTest extends TestCase {
public function tearDown() public function tearDown()
...@@ -23,7 +25,10 @@ class AuthTest extends TestCase { ...@@ -23,7 +25,10 @@ class AuthTest extends TestCase {
public function testRemind() public function testRemind()
{ {
$mailer = Mockery::mock('Illuminate\Mail\Mailer'); $mailer = Mockery::mock('Illuminate\Mail\Mailer');
$this->app->instance('mailer', $mailer); $tokens = $this->app->make('auth.password.tokens');
$users = $this->app['auth']->driver()->getProvider();
$broker = new PasswordBroker($tokens, $users, $mailer, '');
$user = User::create(array( $user = User::create(array(
'name' => 'John Doe', 'name' => 'John Doe',
...@@ -32,10 +37,10 @@ class AuthTest extends TestCase { ...@@ -32,10 +37,10 @@ class AuthTest extends TestCase {
)); ));
$mailer->shouldReceive('send')->once(); $mailer->shouldReceive('send')->once();
Password::remind(array('email' => 'john@doe.com')); $broker->sendResetLink(array('email' => 'john@doe.com'));
$this->assertEquals(1, DB::collection('password_reminders')->count()); $this->assertEquals(1, DB::collection('password_resets')->count());
$reminder = DB::collection('password_reminders')->first(); $reminder = DB::collection('password_resets')->first();
$this->assertEquals('john@doe.com', $reminder['email']); $this->assertEquals('john@doe.com', $reminder['email']);
$this->assertNotNull($reminder['token']); $this->assertNotNull($reminder['token']);
$this->assertInstanceOf('MongoDate', $reminder['created_at']); $this->assertInstanceOf('MongoDate', $reminder['created_at']);
...@@ -47,49 +52,14 @@ class AuthTest extends TestCase { ...@@ -47,49 +52,14 @@ class AuthTest extends TestCase {
'token' => $reminder['token'] 'token' => $reminder['token']
); );
$response = Password::reset($credentials, function($user, $password) $response = $broker->reset($credentials, function($user, $password)
{
$user->password = Hash::make($password);
$user->save();
});
$this->assertEquals('reminders.reset', $response);
$this->assertEquals(0, DB::collection('password_reminders')->count());
}
public function testDeprecatedRemind()
{
$mailer = Mockery::mock('Illuminate\Mail\Mailer');
$this->app->instance('mailer', $mailer);
$user = User::create(array(
'name' => 'John Doe',
'email' => 'john@doe.com',
'password' => Hash::make('foobar')
));
$mailer->shouldReceive('send')->once();
Password::remind(array('email' => 'john@doe.com'));
DB::collection('password_reminders')->update(array('created_at' => new DateTime));
$reminder = DB::collection('password_reminders')->first();
$this->assertTrue(is_array($reminder['created_at']));
$credentials = array(
'email' => 'john@doe.com',
'password' => 'foobar',
'password_confirmation' => 'foobar',
'token' => $reminder['token']
);
$response = Password::reset($credentials, function($user, $password)
{ {
$user->password = Hash::make($password); $user->password = bcrypt($password);
$user->save(); $user->save();
}); });
$this->assertEquals('reminders.reset', $response); $this->assertEquals('passwords.reset', $response);
$this->assertEquals(0, DB::collection('password_reminders')->count()); $this->assertEquals(0, DB::collection('password_resets')->count());
} }
} }
...@@ -11,7 +11,7 @@ class TestCase extends Orchestra\Testbench\TestCase { ...@@ -11,7 +11,7 @@ class TestCase extends Orchestra\Testbench\TestCase {
{ {
return array( return array(
'Jenssegers\Mongodb\MongodbServiceProvider', 'Jenssegers\Mongodb\MongodbServiceProvider',
'Jenssegers\Mongodb\Auth\ReminderServiceProvider', 'Jenssegers\Mongodb\Auth\PasswordResetServiceProvider',
); );
} }
...@@ -36,7 +36,7 @@ class TestCase extends Orchestra\Testbench\TestCase { ...@@ -36,7 +36,7 @@ class TestCase extends Orchestra\Testbench\TestCase {
$app['config']->set('database.connections.mysql', $config['connections']['mysql']); $app['config']->set('database.connections.mysql', $config['connections']['mysql']);
$app['config']->set('database.connections.mongodb', $config['connections']['mongodb']); $app['config']->set('database.connections.mongodb', $config['connections']['mongodb']);
// overwrite cache configuration $app['config']->set('auth.model', 'User');
$app['config']->set('cache.driver', 'array'); $app['config']->set('cache.driver', 'array');
} }
......
...@@ -2,14 +2,14 @@ ...@@ -2,14 +2,14 @@
use Jenssegers\Mongodb\Model as Eloquent; use Jenssegers\Mongodb\Model as Eloquent;
use Illuminate\Auth\UserTrait; use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\UserInterface; use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Auth\Reminders\RemindableTrait; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Auth\Reminders\RemindableInterface; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Eloquent implements UserInterface, RemindableInterface { class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract {
use UserTrait, RemindableTrait; use Authenticatable, CanResetPassword;
protected $dates = array('birthday', 'entry.date'); protected $dates = array('birthday', 'entry.date');
protected static $unguarded = true; protected static $unguarded = true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment