2019-07-17 06:15:25 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-07-17 06:15:25 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-07-17 06:15:25 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-07-17 06:15:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2021-09-04 03:09:34 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2021-05-27 01:14:21 +02:00
|
|
|
use App\Models\Account;
|
2021-10-23 01:06:30 +02:00
|
|
|
use App\Models\ClientContact;
|
2021-12-09 11:50:29 +01:00
|
|
|
use App\Models\Company;
|
2021-10-23 01:06:30 +02:00
|
|
|
use Illuminate\Auth\Events\PasswordReset;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Contracts\View\Factory;
|
2019-07-17 06:15:25 +02:00
|
|
|
use Illuminate\Foundation\Auth\ResetsPasswords;
|
2019-07-18 01:45:18 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2021-10-23 01:06:30 +02:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2019-07-18 01:45:18 +02:00
|
|
|
use Illuminate\Support\Facades\Password;
|
2021-10-23 01:06:30 +02:00
|
|
|
use Illuminate\Support\Str;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\View\View;
|
2019-07-17 06:15:25 +02:00
|
|
|
|
|
|
|
class ContactResetPasswordController 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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Where to redirect users after resetting their password.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-07-18 01:45:18 +02:00
|
|
|
protected $redirectTo = '/client/dashboard';
|
2019-07-17 06:15:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2019-07-18 01:45:18 +02:00
|
|
|
$this->middleware('guest:contact');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the password reset view for the given token.
|
|
|
|
*
|
|
|
|
* If no token is present, display the link request form.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Request $request
|
2019-07-18 01:45:18 +02:00
|
|
|
* @param string|null $token
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Factory|View
|
2019-07-18 01:45:18 +02:00
|
|
|
*/
|
|
|
|
public function showResetForm(Request $request, $token = null)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($request->session()->has('company_key')) {
|
2021-12-09 11:50:29 +01:00
|
|
|
MultiDB::findAndSetDbByCompanyKey($request->session()->get('company_key'));
|
2023-08-04 08:40:44 +02:00
|
|
|
|
|
|
|
/** @var \App\Models\Company $company **/
|
2021-12-09 11:50:29 +01:00
|
|
|
$company = Company::where('company_key', $request->session()->get('company_key'))->first();
|
2021-12-09 06:34:23 +01:00
|
|
|
$db = $company->db;
|
|
|
|
$account = $company->account;
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-12-09 11:50:29 +01:00
|
|
|
$account_key = $request->session()->has('account_key') ? $request->session()->get('account_key') : false;
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($account_key) {
|
2021-12-09 11:50:29 +01:00
|
|
|
MultiDB::findAndSetDbByAccountKey($account_key);
|
2023-08-04 08:40:44 +02:00
|
|
|
/** @var \App\Models\Account $account **/
|
2021-12-09 11:50:29 +01:00
|
|
|
$account = Account::where('key', $account_key)->first();
|
|
|
|
$db = $account->companies->first()->db;
|
|
|
|
$company = $account->companies->first();
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2023-08-04 08:40:44 +02:00
|
|
|
/** @var \App\Models\Account $account **/
|
2021-12-09 11:50:29 +01:00
|
|
|
$account = Account::first();
|
|
|
|
$db = $account->companies->first()->db;
|
|
|
|
$company = $account->companies->first();
|
|
|
|
}
|
2021-12-09 06:34:23 +01:00
|
|
|
}
|
|
|
|
|
2020-03-23 18:10:42 +01:00
|
|
|
return $this->render('auth.passwords.reset')->with(
|
2021-10-07 13:23:45 +02:00
|
|
|
['token' => $token, 'email' => $request->email, 'account' => $account, 'db' => $db, 'company' => $company]
|
2019-07-18 01:45:18 +02:00
|
|
|
);
|
2019-07-17 06:15:25 +02:00
|
|
|
}
|
2019-07-17 06:52:54 +02:00
|
|
|
|
2021-07-29 04:19:56 +02:00
|
|
|
public function reset(Request $request)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($request->session()->has('company_key')) {
|
2021-12-09 11:50:29 +01:00
|
|
|
MultiDB::findAndSetDbByCompanyKey($request->session()->get('company_key'));
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
2021-07-29 04:19:56 +02:00
|
|
|
$request->validate($this->rules(), $this->validationErrorMessages());
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$user = ClientContact::where($request->only(['email', 'token']))->first();
|
2021-10-23 01:06:30 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $user) {
|
2021-10-23 01:06:30 +02:00
|
|
|
return $this->sendResetFailedResponse($request, PASSWORD::INVALID_USER);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-10-23 01:06:30 +02:00
|
|
|
|
|
|
|
$hashed_password = Hash::make($request->input('password'));
|
|
|
|
|
|
|
|
ClientContact::where('email', $user->email)->update([
|
|
|
|
'password' => $hashed_password,
|
2022-06-21 11:57:17 +02:00
|
|
|
'remember_token' => Str::random(60),
|
2021-10-23 01:06:30 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
event(new PasswordReset($user));
|
|
|
|
|
|
|
|
auth()->login($user, true);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 01:06:30 +02:00
|
|
|
$response = Password::PASSWORD_RESET;
|
2021-07-29 04:19:56 +02:00
|
|
|
|
|
|
|
// Added this because it collides the session between
|
|
|
|
// client & main portal giving unlimited redirects.
|
|
|
|
auth()->logout();
|
|
|
|
|
|
|
|
// If the password was successfully reset, we will redirect the user back to
|
|
|
|
// the application's home authenticated view. If there is an error we can
|
|
|
|
// redirect them back to where they came from with their error message.
|
|
|
|
return $response == Password::PASSWORD_RESET
|
|
|
|
? $this->sendResetResponse($request, $response)
|
|
|
|
: $this->sendResetFailedResponse($request, $response);
|
|
|
|
}
|
|
|
|
|
2019-07-17 06:52:54 +02:00
|
|
|
protected function guard()
|
|
|
|
{
|
|
|
|
return Auth::guard('contact');
|
|
|
|
}
|
2019-07-18 01:45:18 +02:00
|
|
|
|
2019-07-19 06:32:51 +02:00
|
|
|
public function broker()
|
2019-07-18 01:45:18 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
return Password::broker('contacts');
|
2019-07-18 01:45:18 +02:00
|
|
|
}
|
2019-07-17 06:15:25 +02:00
|
|
|
}
|