Commit 992a9ff7 authored by Jens Segers's avatar Jens Segers

Tweak password reminders, fixes #271

parent e527c5d6
<?php namespace Jenssegers\Mongodb\Auth;
use DateTime;
use MongoDate;
class DatabaseReminderRepository extends \Illuminate\Auth\Reminders\DatabaseReminderRepository {
/**
* Build the record payload for the table.
*
* @param string $email
* @param string $token
* @return array
*/
protected function getPayload($email, $token)
{
return array('email' => $email, 'token' => $token, 'created_at' => new MongoDate);
}
/**
* Determine if the reminder has expired.
*
......@@ -16,10 +31,21 @@ class DatabaseReminderRepository extends \Illuminate\Auth\Reminders\DatabaseRemi
$reminder = (array) $reminder;
}
// Convert the DateTime object that got saved to MongoDB
if (is_array($reminder['created_at']))
// Convert MongoDate to a date string.
if ($reminder['created_at'] instanceof MongoDate)
{
$date = new DateTime();
$date->setTimestamp($reminder['created_at']->sec);
$reminder['created_at'] = $date->format('Y-m-d H:i:s');
}
// DEPRECATED: Convert DateTime to a date string.
elseif (is_array($reminder['created_at']))
{
$reminder['created_at'] = $reminder['created_at']['date'] + $reminder['created_at']['timezone'];
$date = DateTime::__set_state($reminder['created_at']);
$reminder['created_at'] = $date->format('Y-m-d H:i:s');
}
return parent::reminderExpired($reminder);
......
<?php
class AuthTest extends TestCase {
public function tearDown()
{
User::truncate();
DB::collection('password_reminders')->truncate();
}
public function testAuthAttempt()
{
$user = User::create(array(
'name' => 'John Doe',
'email' => 'john@doe.com',
'password' => Hash::make('foobar')
));
$this->assertTrue(Auth::attempt(array('email' => 'john@doe.com', 'password' => 'foobar'), true));
$this->assertTrue(Auth::check());
}
public function testRemind()
{
$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'));
$this->assertEquals(1, DB::collection('password_reminders')->count());
$reminder = DB::collection('password_reminders')->first();
$this->assertEquals('john@doe.com', $reminder['email']);
$this->assertNotNull($reminder['token']);
$this->assertInstanceOf('MongoDate', $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->save();
});
$this->assertEquals('reminders.reset', $response);
$this->assertEquals(0, DB::collection('password_reminders')->count());
}
}
......@@ -9,7 +9,10 @@ class TestCase extends Orchestra\Testbench\TestCase {
*/
protected function getPackageProviders()
{
return array('Jenssegers\Mongodb\MongodbServiceProvider');
return array(
'Jenssegers\Mongodb\MongodbServiceProvider',
'Jenssegers\Mongodb\Auth\ReminderServiceProvider',
);
}
/**
......
......@@ -2,11 +2,15 @@
use Jenssegers\Mongodb\Model as Eloquent;
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $dates = array('birthday', 'entry.date');
protected static $unguarded = true;
......@@ -60,66 +64,6 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
return $this->embedsOne('User');
}
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return $this->rememberToken;
}
/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
$this->rememberToken = $value;
}
/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName() {
return 'remember_token';
}
protected function getDateFormat()
{
return 'l jS \of F Y h:i:s A';
......
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