2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\ClientAuth;
|
2016-03-05 04:22:54 +01:00
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Models\Contact;
|
|
|
|
use App\Models\Invitation;
|
|
|
|
use Config;
|
2016-03-05 04:22:54 +01:00
|
|
|
use Illuminate\Foundation\Auth\ResetsPasswords;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Mail\Message;
|
|
|
|
use Illuminate\Support\Facades\Password;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
class PasswordController extends Controller
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Password Reset Controller
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| This controller is responsible for handling password reset requests
|
|
|
|
| and uses a simple trait to include this behavior. You're free to
|
|
|
|
| explore this trait and override any methods you wish to tweak.
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
use ResetsPasswords;
|
2016-03-05 04:22:54 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $redirectTo = '/client/dashboard';
|
2016-03-05 04:22:54 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* Create a new password controller instance.
|
|
|
|
*
|
|
|
|
* @internal param \Illuminate\Contracts\Auth\Guard $auth
|
|
|
|
* @internal param \Illuminate\Contracts\Auth\PasswordBroker $passwords
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('guest');
|
|
|
|
Config::set('auth.defaults.passwords', 'client');
|
|
|
|
}
|
2016-03-05 04:22:54 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function showLinkRequestForm()
|
|
|
|
{
|
2017-02-16 04:00:13 +01:00
|
|
|
$data = [
|
|
|
|
'clientauth' => true,
|
|
|
|
];
|
2016-05-24 23:02:28 +02:00
|
|
|
$contactKey = session('contact_key');
|
2017-02-16 04:26:55 +01:00
|
|
|
if (!$contactKey) {
|
2016-05-24 23:02:28 +02:00
|
|
|
return \Redirect::to('/client/sessionexpired');
|
2016-03-06 00:09:50 +01:00
|
|
|
}
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
return view('clientauth.password')->with($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-05 04:22:54 +01:00
|
|
|
* Send a reset link to the given user.
|
|
|
|
*
|
2017-01-30 20:40:43 +01:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2017-01-30 17:05:31 +01:00
|
|
|
*
|
2016-03-05 04:22:54 +01:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function sendResetLinkEmail(Request $request)
|
|
|
|
{
|
|
|
|
$broker = $this->getBroker();
|
|
|
|
|
2016-05-24 23:02:28 +02:00
|
|
|
$contactId = null;
|
|
|
|
$contactKey = session('contact_key');
|
2016-07-03 18:11:58 +02:00
|
|
|
if ($contactKey) {
|
2016-05-24 23:02:28 +02:00
|
|
|
$contact = Contact::where('contact_key', '=', $contactKey)->first();
|
2017-02-27 15:55:40 +01:00
|
|
|
if ($contact && ! $contact->is_deleted && $contact->email) {
|
2016-05-24 23:02:28 +02:00
|
|
|
$contactId = $contact->id;
|
2016-03-05 04:22:54 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
$response = Password::broker($broker)->sendResetLink(['id' => $contactId], function (Message $message) {
|
2016-03-05 04:22:54 +01:00
|
|
|
$message->subject($this->getEmailSubject());
|
|
|
|
});
|
|
|
|
|
|
|
|
switch ($response) {
|
|
|
|
case Password::RESET_LINK_SENT:
|
|
|
|
return $this->getSendResetLinkEmailSuccessResponse($response);
|
|
|
|
|
|
|
|
case Password::INVALID_USER:
|
|
|
|
default:
|
|
|
|
return $this->getSendResetLinkEmailFailureResponse($response);
|
|
|
|
}
|
|
|
|
}
|
2016-07-03 18:11:58 +02:00
|
|
|
|
2016-03-05 04:22:54 +01:00
|
|
|
/**
|
|
|
|
* Display the password reset view for the given token.
|
|
|
|
*
|
|
|
|
* If no token is present, display the link request form.
|
|
|
|
*
|
2017-01-30 20:40:43 +01:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param string|null $key
|
|
|
|
* @param string|null $token
|
|
|
|
*
|
2016-03-05 04:22:54 +01:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2016-05-24 23:02:28 +02:00
|
|
|
public function showResetForm(Request $request, $key = null, $token = null)
|
2016-03-05 04:22:54 +01:00
|
|
|
{
|
|
|
|
if (is_null($token)) {
|
|
|
|
return $this->getEmail();
|
|
|
|
}
|
2016-07-03 18:11:58 +02:00
|
|
|
|
2017-02-16 04:00:13 +01:00
|
|
|
$data = array(
|
|
|
|
'token' => $token,
|
|
|
|
'clientauth' => true,
|
|
|
|
);
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
if ($key) {
|
2016-05-24 23:02:28 +02:00
|
|
|
$contact = Contact::where('contact_key', '=', $key)->first();
|
2017-01-30 20:40:43 +01:00
|
|
|
if ($contact && ! $contact->is_deleted) {
|
2016-05-24 23:02:28 +02:00
|
|
|
$account = $contact->account;
|
|
|
|
$data['contact_key'] = $contact->contact_key;
|
|
|
|
} else {
|
|
|
|
// Maybe it's an invitation key
|
|
|
|
$invitation = Invitation::where('invitation_key', '=', $key)->first();
|
2017-01-30 20:40:43 +01:00
|
|
|
if ($invitation && ! $invitation->is_deleted) {
|
2016-05-24 23:02:28 +02:00
|
|
|
$account = $invitation->account;
|
|
|
|
$data['contact_key'] = $invitation->contact->contact_key;
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 23:50:21 +02:00
|
|
|
|
2017-02-16 04:26:55 +01:00
|
|
|
if ( empty($account)) {
|
2016-05-24 23:02:28 +02:00
|
|
|
return \Redirect::to('/client/sessionexpired');
|
2016-03-06 00:09:50 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-05 04:22:54 +01:00
|
|
|
|
2016-03-06 00:09:50 +01:00
|
|
|
return view('clientauth.reset')->with($data);
|
2016-03-05 04:22:54 +01:00
|
|
|
}
|
2016-07-03 18:11:58 +02:00
|
|
|
|
2016-03-05 04:22:54 +01:00
|
|
|
/**
|
|
|
|
* Display the password reset view for the given token.
|
|
|
|
*
|
|
|
|
* If no token is present, display the link request form.
|
|
|
|
*
|
2017-01-30 20:40:43 +01:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param string|null $key
|
|
|
|
* @param string|null $token
|
|
|
|
*
|
2016-03-05 04:22:54 +01:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2016-05-24 23:02:28 +02:00
|
|
|
public function getReset(Request $request, $key = null, $token = null)
|
2016-03-05 04:22:54 +01:00
|
|
|
{
|
2016-05-24 23:02:28 +02:00
|
|
|
return $this->showResetForm($request, $key, $token);
|
2016-03-05 04:22:54 +01:00
|
|
|
}
|
2016-07-03 18:11:58 +02:00
|
|
|
|
2016-03-05 04:22:54 +01:00
|
|
|
/**
|
|
|
|
* Reset the given user's password.
|
|
|
|
*
|
2017-01-30 20:40:43 +01:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*
|
2016-03-05 04:22:54 +01:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function reset(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, $this->getResetValidationRules());
|
|
|
|
|
|
|
|
$credentials = $request->only(
|
|
|
|
'password', 'password_confirmation', 'token'
|
|
|
|
);
|
2016-07-03 18:11:58 +02:00
|
|
|
|
2016-03-05 04:22:54 +01:00
|
|
|
$credentials['id'] = null;
|
2016-05-24 23:02:28 +02:00
|
|
|
|
|
|
|
$contactKey = session('contact_key');
|
2016-07-03 18:11:58 +02:00
|
|
|
if ($contactKey) {
|
2016-05-24 23:02:28 +02:00
|
|
|
$contact = Contact::where('contact_key', '=', $contactKey)->first();
|
2017-01-30 20:40:43 +01:00
|
|
|
if ($contact && ! $contact->is_deleted) {
|
2016-05-24 23:02:28 +02:00
|
|
|
$credentials['id'] = $contact->id;
|
2016-03-05 04:22:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
2016-07-03 18:11:58 +02:00
|
|
|
|
2016-03-05 04:22:54 +01:00
|
|
|
/**
|
|
|
|
* Get the password reset validation rules.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getResetValidationRules()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'token' => 'required',
|
|
|
|
'password' => 'required|confirmed|min:6',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|