Skip to content

Commit 992a9ff

Browse files
committed
Tweak password reminders, fixes #271
1 parent e527c5d commit 992a9ff

File tree

4 files changed

+97
-64
lines changed

4 files changed

+97
-64
lines changed

src/Jenssegers/Mongodb/Auth/DatabaseReminderRepository.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
<?php namespace Jenssegers\Mongodb\Auth;
22

3+
use DateTime;
4+
use MongoDate;
5+
36
class DatabaseReminderRepository extends \Illuminate\Auth\Reminders\DatabaseReminderRepository {
47

8+
/**
9+
* Build the record payload for the table.
10+
*
11+
* @param string $email
12+
* @param string $token
13+
* @return array
14+
*/
15+
protected function getPayload($email, $token)
16+
{
17+
return array('email' => $email, 'token' => $token, 'created_at' => new MongoDate);
18+
}
19+
520
/**
621
* Determine if the reminder has expired.
722
*
@@ -16,10 +31,21 @@ protected function reminderExpired($reminder)
1631
$reminder = (array) $reminder;
1732
}
1833

19-
// Convert the DateTime object that got saved to MongoDB
20-
if (is_array($reminder['created_at']))
34+
// Convert MongoDate to a date string.
35+
if ($reminder['created_at'] instanceof MongoDate)
36+
{
37+
$date = new DateTime();
38+
$date->setTimestamp($reminder['created_at']->sec);
39+
40+
$reminder['created_at'] = $date->format('Y-m-d H:i:s');
41+
}
42+
43+
// DEPRECATED: Convert DateTime to a date string.
44+
elseif (is_array($reminder['created_at']))
2145
{
22-
$reminder['created_at'] = $reminder['created_at']['date'] + $reminder['created_at']['timezone'];
46+
$date = DateTime::__set_state($reminder['created_at']);
47+
48+
$reminder['created_at'] = $date->format('Y-m-d H:i:s');
2349
}
2450

2551
return parent::reminderExpired($reminder);

tests/AuthTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
class AuthTest extends TestCase {
4+
5+
public function tearDown()
6+
{
7+
User::truncate();
8+
DB::collection('password_reminders')->truncate();
9+
}
10+
11+
public function testAuthAttempt()
12+
{
13+
$user = User::create(array(
14+
'name' => 'John Doe',
15+
'email' => '[email protected]',
16+
'password' => Hash::make('foobar')
17+
));
18+
19+
$this->assertTrue(Auth::attempt(array('email' => '[email protected]', 'password' => 'foobar'), true));
20+
$this->assertTrue(Auth::check());
21+
}
22+
23+
public function testRemind()
24+
{
25+
$mailer = Mockery::mock('Illuminate\Mail\Mailer');
26+
$this->app->instance('mailer', $mailer);
27+
28+
$user = User::create(array(
29+
'name' => 'John Doe',
30+
'email' => '[email protected]',
31+
'password' => Hash::make('foobar')
32+
));
33+
34+
$mailer->shouldReceive('send')->once();
35+
Password::remind(array('email' => '[email protected]'));
36+
37+
$this->assertEquals(1, DB::collection('password_reminders')->count());
38+
$reminder = DB::collection('password_reminders')->first();
39+
$this->assertEquals('[email protected]', $reminder['email']);
40+
$this->assertNotNull($reminder['token']);
41+
$this->assertInstanceOf('MongoDate', $reminder['created_at']);
42+
43+
$credentials = array(
44+
'email' => '[email protected]',
45+
'password' => 'foobar',
46+
'password_confirmation' => 'foobar',
47+
'token' => $reminder['token']
48+
);
49+
50+
$response = Password::reset($credentials, function($user, $password)
51+
{
52+
$user->password = Hash::make($password);
53+
$user->save();
54+
});
55+
56+
$this->assertEquals('reminders.reset', $response);
57+
$this->assertEquals(0, DB::collection('password_reminders')->count());
58+
}
59+
60+
}

tests/TestCase.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ class TestCase extends Orchestra\Testbench\TestCase {
99
*/
1010
protected function getPackageProviders()
1111
{
12-
return array('Jenssegers\Mongodb\MongodbServiceProvider');
12+
return array(
13+
'Jenssegers\Mongodb\MongodbServiceProvider',
14+
'Jenssegers\Mongodb\Auth\ReminderServiceProvider',
15+
);
1316
}
1417

1518
/**

tests/models/User.php

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
use Jenssegers\Mongodb\Model as Eloquent;
44

5+
use Illuminate\Auth\UserTrait;
56
use Illuminate\Auth\UserInterface;
7+
use Illuminate\Auth\Reminders\RemindableTrait;
68
use Illuminate\Auth\Reminders\RemindableInterface;
79

810
class User extends Eloquent implements UserInterface, RemindableInterface {
911

12+
use UserTrait, RemindableTrait;
13+
1014
protected $dates = array('birthday', 'entry.date');
1115
protected static $unguarded = true;
1216

@@ -60,66 +64,6 @@ public function father()
6064
return $this->embedsOne('User');
6165
}
6266

63-
/**
64-
* Get the unique identifier for the user.
65-
*
66-
* @return mixed
67-
*/
68-
public function getAuthIdentifier()
69-
{
70-
return $this->getKey();
71-
}
72-
73-
/**
74-
* Get the password for the user.
75-
*
76-
* @return string
77-
*/
78-
public function getAuthPassword()
79-
{
80-
return $this->password;
81-
}
82-
83-
/**
84-
* Get the e-mail address where password reminders are sent.
85-
*
86-
* @return string
87-
*/
88-
public function getReminderEmail()
89-
{
90-
return $this->email;
91-
}
92-
93-
/**
94-
* Get the token value for the "remember me" session.
95-
*
96-
* @return string
97-
*/
98-
public function getRememberToken()
99-
{
100-
return $this->rememberToken;
101-
}
102-
103-
/**
104-
* Set the token value for the "remember me" session.
105-
*
106-
* @param string $value
107-
* @return void
108-
*/
109-
public function setRememberToken($value)
110-
{
111-
$this->rememberToken = $value;
112-
}
113-
114-
/**
115-
* Get the column name for the "remember me" token.
116-
*
117-
* @return string
118-
*/
119-
public function getRememberTokenName() {
120-
return 'remember_token';
121-
}
122-
12367
protected function getDateFormat()
12468
{
12569
return 'l jS \of F Y h:i:s A';

0 commit comments

Comments
 (0)