Page MenuHomeDevCentral

No OneTemporary

diff --git a/app/Console/Commands/AccountReset.php b/app/Console/Commands/AccountReset.php
index 8852e1f..a75013e 100644
--- a/app/Console/Commands/AccountReset.php
+++ b/app/Console/Commands/AccountReset.php
@@ -1,129 +1,131 @@
<?php
namespace AuthGrove\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Contracts\Auth\PasswordBroker;
+use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Password;
+
use AuthGrove\Console\Services\AccountHelpers;
class AccountReset extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'account:reset {user} {--subject=} {--format=human}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send a mail with a reset password link for an account';
/**
* The user e-mail
*
* @var string
*/
private $email;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Gets e-mail subject from command line option or default l10n
*
* @return string the e-mail subject
*/
public function getEmailSubject () {
$subject = $this->option('subject');
if ($subject === null) {
return trans('emails.reset-password-subject');
}
return $subject;
}
/**
* Sends a reset passsword e-mail
*
* @return bool true if a mail has been sent, false if the user is invalid
*/
public function sendResetMail () {
//Information we need for this mail
$subject = $this->getEmailSubject();
$credentials = [
'email' => $this->email
];
//Tries to send the mail
- $response = Password::sendResetLink($credentials, function($m) use ($subject)
+ $response = Password::sendResetLink($credentials, function(Message $m) use ($subject)
{
$m->subject($subject);
});
//Handles password broker response, returning true on success
switch ($response) {
case PasswordBroker::RESET_LINK_SENT:
return true;
case PasswordBroker::INVALID_USER:
return false;
default:
throw new \Exception("Unhandled password broker response: " . $response);
}
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//Gets the information
$user = AccountHelpers::findUser($this->argument('user'));
if ($user === null) {
$this->error("User not found.");
return;
}
$this->email = $user->getInformation()['email'];
//Operation
$success = $this->sendResetMail();
if (!$success) {
$this->error("The user has been found, but the password broker considers this user is invalid.");
return;
}
//Regular output
$format = $this->option('format');
switch ($format) {
case "human":
$this->info("A reset link mail has been sent to $this->email.");
break;
case "json":
echo json_encode([
"result" => "ok",
"email" => $this->email
]);
echo PHP_EOL;
break;
default:
$this->error("Unknown format: $format");
}
}
}
diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php
index ae0343e..2f44abb 100644
--- a/app/Http/Middleware/Authenticate.php
+++ b/app/Http/Middleware/Authenticate.php
@@ -1,26 +1,28 @@
<?php namespace AuthGrove\Http\Middleware;
use Closure;
+
+use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class Authenticate {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
- public function handle($request, Closure $next, $guard = null) {
+ public function handle(Request $request, Closure $next, $guard = null) {
if (Auth::guard($guard)->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('auth/login');
}
}
return $next($request);
}
}
diff --git a/app/Services/ResetsPasswords.php b/app/Services/ResetsPasswords.php
index c3f696a..d84ca05 100644
--- a/app/Services/ResetsPasswords.php
+++ b/app/Services/ResetsPasswords.php
@@ -1,308 +1,309 @@
<?php
namespace AuthGrove\Services;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;
+use Illuminate\Contracts\Auth\CanResetPassword;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;
use Illuminate\Foundation\Auth\RedirectsUsers;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
trait ResetsPasswords {
use RedirectsUsers;
///
/// GET and POST routes
///
/**
* Display the password recover view.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function getRecover(Request $request) {
return view('auth.recover');
}
/**
* Send a reset link to the given user.
*
* @param Request $request
* @return Response
*/
public function postRecover(Request $request) {
return $this->sendResetLinkEmail($request);
}
/**
* Display the password reset view for the given token.
*
* @param string $token
* @return Response
*/
public function getReset($token = null)
{
if (is_null($token))
{
throw new NotFoundHttpException;
}
return view('auth.reset')->with('token', $token);
}
/**
* Reset the given user's password.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postReset(Request $request) {
return $this->reset($request);
}
/**
* Get the response for after a successful password reset.
*
* @param string $response
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function getResetSuccessResponse($response) {
return redirect($this->redirectPath())
->with('status', trans($response));
}
/**
* Get the response for after a failing password reset.
*
* @param Request $request
* @param string $response
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function getResetFailureResponse(Request $request, $response) {
return redirect()->back()
->withInput($request->only('email'))
->withErrors(['email' => trans($response)]);
}
///
/// Controller helper utilities
///
/**
* Get the name of the guest middleware.
*
* @return string
*/
protected function guestMiddleware() {
$guard = $this->getGuard();
return $guard ? 'guest:' . $guard : 'guest';
}
/**
* Get the broker to be used during password reset.
*
* @return string|null
*/
public function getBroker() {
return property_exists($this, 'broker') ? $this->broker : null;
}
/**
* Get the guard to be used during password reset.
*
* @return string|null
*/
protected function getGuard() {
return property_exists($this, 'guard') ? $this->guard : null;
}
///
/// Reset password e-mail helper methods
///
/**
* Send a reset link to the given user.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function sendResetLinkEmail(Request $request) {
$this->validateSendResetLinkEmail($request);
$broker = $this->getBroker();
$response = Password::broker($broker)->sendResetLink(
$this->getSendResetLinkEmailCredentials($request),
$this->resetEmailBuilder()
);
switch ($response) {
case Password::RESET_LINK_SENT:
return $this->getSendResetLinkEmailSuccessResponse($response);
case Password::INVALID_USER:
default:
return $this->getSendResetLinkEmailFailureResponse($response);
}
}
/**
* Validate the request of sending reset link.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function validateSendResetLinkEmail(Request $request) {
$this->validate($request, ['email' => 'required|email']);
}
/**
* Get the needed credentials for sending the reset link.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function getSendResetLinkEmailCredentials(Request $request) {
return $request->only('email');
}
/**
* Get the Closure which is used to build the password reset email message.
*
* @return \Closure
*/
protected function resetEmailBuilder() {
return function (Message $message) {
$message->subject($this->getEmailSubject());
};
}
/**
* Get the e-mail subject line to be used for the reset link email.
*
* @return string
*/
protected function getEmailSubject()
{
return property_exists($this, 'subject') ? $this->subject : trans('emails.reset-password-subject');
}
/**
* Get the response for after the reset link has been successfully sent.
*
* @param string $response
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function getSendResetLinkEmailSuccessResponse($response) {
return redirect()->back()->with('status', trans($response));
}
/**
* Get the response for after the reset link could not be sent.
*
* @param string $response
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function getSendResetLinkEmailFailureResponse($response) {
return redirect()->back()->withErrors(['email' => trans($response)]);
}
///
/// Helper methods to handle password reset
///
/**
* Reset the given user's password.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function reset(Request $request) {
$this->validate(
$request,
$this->getResetValidationRules(),
$this->getResetValidationMessages(),
$this->getResetValidationCustomAttributes()
);
$credentials = $this->getResetCredentials($request);
$broker = $this->getBroker();
$response = Password::broker($broker)->reset($credentials, function ($user, $password) {
$this->resetPassword($user, $password);
});
switch ($response) {
case Password::PASSWORD_RESET:
return $this->getResetSuccessResponse($response);
default:
return $this->getResetFailureResponse($request, $response);
}
}
/**
* Get the password reset validation rules.
*
* @return array
*/
protected function getResetValidationRules() {
return [
'token' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed|min:8',
];
}
/**
* Get the password reset validation messages.
*
* @return array
*/
protected function getResetValidationMessages() {
return [];
}
/**
* Get the password reset validation custom attributes.
*
* @return array
*/
protected function getResetValidationCustomAttributes() {
return [];
}
/**
* Get the password reset credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function getResetCredentials(Request $request) {
return $request->only(
'email', 'password', 'password_confirmation', 'token'
);
}
/**
* Reset the given user's password.
*
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
* @param string $password
* @return void
*/
- protected function resetPassword($user, $password) {
+ protected function resetPassword(CanResetPassword $user, $password) {
$user->forceFill([
'password' => bcrypt($password),
'remember_token' => Str::random(60),
])->save();
Auth::guard($this->getGuard())->login($user);
}
}

File Metadata

Mime Type
text/x-diff
Expires
Mon, Nov 25, 01:57 (18 h, 2 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2259352
Default Alt Text
(13 KB)

Event Timeline