mirror of
https://github.com/flarum/core.git
synced 2025-10-24 21:26:11 +02:00
Very rough implementation of forgot password
This commit is contained in:
11
src/Core/Commands/RequestPasswordResetCommand.php
Normal file
11
src/Core/Commands/RequestPasswordResetCommand.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php namespace Flarum\Core\Commands;
|
||||
|
||||
class RequestPasswordResetCommand
|
||||
{
|
||||
public $email;
|
||||
|
||||
public function __construct($email)
|
||||
{
|
||||
$this->email = $email;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php namespace Flarum\Core\Handlers\Commands;
|
||||
|
||||
use Flarum\Core\Commands\RequestPasswordResetCommand;
|
||||
use Flarum\Core\Models\ResetToken;
|
||||
use Flarum\Core\Repositories\UserRepositoryInterface;
|
||||
use Illuminate\Contracts\Mail\Mailer;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
class RequestPasswordResetCommandHandler
|
||||
{
|
||||
/**
|
||||
* @var UserRepositoryInterface
|
||||
*/
|
||||
protected $users;
|
||||
|
||||
/**
|
||||
* The mailer instance.
|
||||
*
|
||||
* @var \Illuminate\Contracts\Mail\Mailer
|
||||
*/
|
||||
protected $mailer;
|
||||
|
||||
public function __construct(UserRepositoryInterface $users, Mailer $mailer)
|
||||
{
|
||||
$this->users = $users;
|
||||
$this->mailer = $mailer;
|
||||
}
|
||||
|
||||
public function handle(RequestPasswordResetCommand $command)
|
||||
{
|
||||
$user = $this->users->findByEmail($command->email);
|
||||
|
||||
if (! $user) {
|
||||
throw new ModelNotFoundException;
|
||||
}
|
||||
|
||||
$token = ResetToken::generate($user->id);
|
||||
$token->save();
|
||||
|
||||
$data = [
|
||||
'username' => $user->username,
|
||||
'url' => route('flarum.forum.resetPassword', ['token' => $token->id])
|
||||
];
|
||||
|
||||
$this->mailer->send(['text' => 'flarum::emails.reset'], $data, function ($message) use ($user) {
|
||||
$message->to($user->email);
|
||||
$message->subject('Reset Your Password');
|
||||
});
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
44
src/Core/Models/ResetToken.php
Normal file
44
src/Core/Models/ResetToken.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php namespace Flarum\Core\Models;
|
||||
|
||||
class ResetToken extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'reset_tokens';
|
||||
|
||||
/**
|
||||
* Use a custom primary key for this model.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
public $incrementing = false;
|
||||
|
||||
/**
|
||||
* Generate a reset token for the specified user.
|
||||
*
|
||||
* @param int $userId
|
||||
* @return static
|
||||
*/
|
||||
public static function generate($userId)
|
||||
{
|
||||
$token = new static;
|
||||
|
||||
$token->id = str_random(40);
|
||||
$token->user_id = $userId;
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the relationship with the owner of this reset token.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Models\User');
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,17 @@ class EloquentUserRepository implements UserRepositoryInterface
|
||||
return User::where($field, $identification)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a user by email.
|
||||
*
|
||||
* @param string $email
|
||||
* @return \Flarum\Core\Models\User|null
|
||||
*/
|
||||
public function findByEmail($email)
|
||||
{
|
||||
return User::where('email', $email)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID of a user with the given username.
|
||||
*
|
||||
|
||||
@@ -31,6 +31,14 @@ interface UserRepositoryInterface
|
||||
*/
|
||||
public function findByIdentification($identification);
|
||||
|
||||
/**
|
||||
* Find a user by email.
|
||||
*
|
||||
* @param string $email
|
||||
* @return \Flarum\Core\Models\User|null
|
||||
*/
|
||||
public function findByEmail($email);
|
||||
|
||||
/**
|
||||
* Get the ID of a user with the given username.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user