Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate dependency on email address as a reliable handle for user #523

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Confide/Confide.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,12 @@ public function isThrottled($identity)
*
* @return string $token
*/
public function forgotPassword($email)
public function forgotPassword($emailorusername)
{
$user = $this->repo->getUserByEmail($email);
$user = $this->repo->getUserByEmailOrUsername($emailorusername);

if ($user) {
if ($user)
{
return $this->passService->requestChangePassword($user);
}

Expand Down Expand Up @@ -266,6 +267,13 @@ public function destroyForgotPasswordToken($token)
*/
public function userByResetPasswordToken($token)
{
$id = $this->passService->getUserIdentityByToken($token);

if($id)
{
return $this->repo->getUserByIdentity($id);
}

$email = $this->passService->getEmailByToken($token);

if ($email) {
Expand Down
43 changes: 15 additions & 28 deletions src/Confide/EloquentPasswordService.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace Zizaco\Confide;

use Illuminate\Auth\Reminders\RemindableInterface;

use DateTime;
/**
* A service that abstracts all user password management related methods.
*
Expand Down Expand Up @@ -38,13 +38,13 @@ public function __construct($app = null)
*/
public function requestChangePassword(RemindableInterface $user)
{
$email = $user->getReminderEmail();
$token = $this->generateToken();

$values = array(
'email'=> $email,
'user_id' => $user->getAuthIdentifier(),
'email'=> $user->getReminderEmail(),
'token'=> $token,
'created_at'=> new \DateTime
'created_at'=> new DateTime
);

$table = $this->getTable();
Expand All @@ -67,22 +67,24 @@ public function requestChangePassword(RemindableInterface $user)
*
* @return string Email.
*/
public function getEmailByToken($token)
public function getUserIdentityByToken($token)
{
$connection = $this->getConnection();
$table = $this->getTable();

$email = $this->app['db']
$id = $this->app['db']
->connection($connection)
->table($table)
->select('email')
->select('user_id')
->where('token', '=', $token)
->where('created_at', '>=', $this->getOldestValidDate())
->first();

$email = $this->unwrapEmail($email);

return $email;
if($id)
{
$id = ['id' => $id->user_id];
}
return $id;
}

/**
Expand Down Expand Up @@ -139,23 +141,6 @@ protected function generateToken()
return md5(uniqid(mt_rand(), true));
}

/**
* Extracts the email of the given object or array.
*
* @param mixed $email An object, array or email string.
*
* @return string The email address.
*/
protected function unwrapEmail($email)
{
if ($email && is_object($email)) {
$email = $email->email;
} elseif ($email && is_array($email)) {
$email = $email['email'];
}

return $email;
}

/**
* Sends an email containing the reset password link with the
Expand All @@ -168,6 +153,8 @@ protected function unwrapEmail($email)
*/
protected function sendEmail($user, $token)
{
$email = $user->getReminderEmail();

$config = $this->app['config'];
$lang = $this->app['translator'];

Expand All @@ -177,7 +164,7 @@ protected function sendEmail($user, $token)
compact('user', 'token'),
function ($message) use ($user, $token, $lang) {
$message
->to($user->email, $user->username)
->to($email, $user->username)
->subject($lang->get('confide::confide.email.password_reset.subject'));
}
);
Expand Down
15 changes: 10 additions & 5 deletions src/Confide/EloquentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,17 @@ public function getUserByEmail($email)
*/
public function getUserByEmailOrUsername($emailOrUsername)
{
$identity = [
'email' => $emailOrUsername,
'username' => $emailOrUsername
];
$user = $this->getUserByUsername($emailOrUsername);
if(!$user)
{
$user = $this->getUserByEmail($emailOrUsername);
}
return $user;
}

return $this->getUserByIdentity($identity);
public function getUserByUsername($username)
{
return $this->getUserByIdentity(['username' => $username]);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Confide/PasswordServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ interface PasswordServiceInterface
public function requestChangePassword(RemindableInterface $user);

/**
* Returns the email associated with the given reset password token.
* Returns the user id associated with the given reset password token.
*
* @param string $token
*
* @return string Email.
* @return mixed id.
*/
public function getEmailByToken($token);
public function getUserIdentityByToken($token);

/**
* Delete the record of the given token from database.
Expand Down
5 changes: 5 additions & 0 deletions src/commands/ControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function fire()
$model = $this->app['config']->get('auth.model');
$restful = $this->option('restful');
$includeUsername = $this->option('username');
$includeEmail = $this->option('email');
if(!$includeUsername && !$includeEmail)
{
$includeUsername = true;
}

$viewVars = compact(
'class',
Expand Down
10 changes: 10 additions & 0 deletions src/commands/MigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public function fire()
// Prepare variables
$table = lcfirst($this->option('table'));
$includeUsername = $this->option('username');
$includeEmail = $this->option('email');
if(!$includeUsername && !$includeEmail)
{
$includeUsername = true;
}

$viewVars = compact(
'table',
Expand All @@ -65,6 +70,11 @@ public function fire()
"An 'username' column will be included in the table."
);
}
if ($includeEmail) {
$this->comment(
"An 'email' column will be included in the table."
);
}
$this->line('');

if ($this->confirm("Proceed with the migration creation? [Yes|no]")) {
Expand Down
4 changes: 4 additions & 0 deletions src/views/generators/migration.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public function up()
@if ($includeUsername)
$table->string('username')->unique();
@endif
@if ($includeEmail)
$table->string('email')->unique();
@endif
$table->string('password');
$table->string('confirmation_code');
$table->string('remember_token')->nullable();
Expand All @@ -25,9 +27,11 @@ public function up()

// Creates password reminders table
Schema::create('password_reminders', function ($table) {
$table->integer('user_id');
$table->string('email');
$table->string('token');
$table->timestamp('created_at');
$table->primary('token');
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/views/generators/repository.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UserRepository
/**
* Signup a new account with the given parameters
*
* @param array $input Array containing {{ ($includeUsername) ? "'username', " : '' }}'email' and 'password'.
* @param array $input Array containing {{ ($includeUsername) ? "'username', " : '' }} {{ ($includeEmail) ? "'email', " : '' }} and 'password'.
*
* @return {{ $nonNamespacedName }} {{ $nonNamespacedName }} object that may or may not be saved successfully. Check the id to make sure.
*/
Expand All @@ -30,7 +30,9 @@ public function signup($input)
@if ($includeUsername)
$user->username = array_get($input, 'username');
@endif
@if ($includeEmail)
$user->email = array_get($input, 'email');
@endif
$user->password = array_get($input, 'password');

// The password confirmation will be removed from model
Expand Down