Commit 698e166b authored by duxet's avatar duxet

Fix auth and password resetting

parent 6d7aa1dc
......@@ -3,7 +3,7 @@
use DateTime;
use MongoDate;
class DatabaseReminderRepository extends \Illuminate\Auth\Reminders\DatabaseReminderRepository {
class DatabaseTokenRepository extends \Illuminate\Auth\Passwords\DatabaseTokenRepository {
/**
* Build the record payload for the table.
......@@ -14,36 +14,28 @@ class DatabaseReminderRepository extends \Illuminate\Auth\Reminders\DatabaseRemi
*/
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
*/
protected function reminderExpired($reminder)
protected function tokenExpired($token)
{
// Convert MongoDate to a date string.
if ($reminder['created_at'] instanceof MongoDate)
if ($token['created_at'] instanceof MongoDate)
{
$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).
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);
return parent::tokenExpired($token);
}
}
<?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
*/
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();
// The database reminder repository is an implementation of the reminder repo
// The database token repository is an implementation of the token repository
// 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.
$table = $app['config']['auth.reminder.table'];
$table = $app['config']['auth.password.table'];
$key = $app['config']['app.key'];
$expire = $app['config']->get('auth.reminder.expire', 60);
$expire = $app['config']->get('auth.password.expire', 60);
return new DbRepository($connection, $table, $key, $expire);
});
}
......
<?php
use Illuminate\Auth\Passwords\PasswordBroker;
class AuthTest extends TestCase {
public function tearDown()
......@@ -23,7 +25,10 @@ class AuthTest extends TestCase {
public function testRemind()
{
$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(
'name' => 'John Doe',
......@@ -32,10 +37,10 @@ class AuthTest extends TestCase {
));
$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());
$reminder = DB::collection('password_reminders')->first();
$this->assertEquals(1, DB::collection('password_resets')->count());
$reminder = DB::collection('password_resets')->first();
$this->assertEquals('john@doe.com', $reminder['email']);
$this->assertNotNull($reminder['token']);
$this->assertInstanceOf('MongoDate', $reminder['created_at']);
......@@ -47,49 +52,14 @@ class AuthTest extends TestCase {
'token' => $reminder['token']
);
$response = Password::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)
$response = $broker->reset($credentials, function($user, $password)
{
$user->password = Hash::make($password);
$user->password = bcrypt($password);
$user->save();
});
$this->assertEquals('reminders.reset', $response);
$this->assertEquals(0, DB::collection('password_reminders')->count());
$this->assertEquals('passwords.reset', $response);
$this->assertEquals(0, DB::collection('password_resets')->count());
}
}
......@@ -11,7 +11,7 @@ class TestCase extends Orchestra\Testbench\TestCase {
{
return array(
'Jenssegers\Mongodb\MongodbServiceProvider',
'Jenssegers\Mongodb\Auth\ReminderServiceProvider',
'Jenssegers\Mongodb\Auth\PasswordResetServiceProvider',
);
}
......@@ -36,7 +36,7 @@ class TestCase extends Orchestra\Testbench\TestCase {
$app['config']->set('database.connections.mysql', $config['connections']['mysql']);
$app['config']->set('database.connections.mongodb', $config['connections']['mongodb']);
// overwrite cache configuration
$app['config']->set('auth.model', 'User');
$app['config']->set('cache.driver', 'array');
}
......
......@@ -2,14 +2,14 @@
use Jenssegers\Mongodb\Model as Eloquent;
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
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 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