1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/Auth/ResetPasswordController.php

144 lines
4.4 KiB
PHP
Raw Normal View History

2018-10-04 19:10:43 +02:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +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-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
2018-10-04 19:10:43 +02:00
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
2021-12-09 11:50:29 +01:00
use App\Libraries\MultiDB;
2021-06-01 01:02:30 +02:00
use App\Models\Account;
2021-12-09 11:50:29 +01:00
use App\Models\Company;
2022-01-17 11:22:10 +01:00
use App\Utils\Ninja;
2018-10-04 19:10:43 +02:00
use Illuminate\Foundation\Auth\ResetsPasswords;
2020-10-28 11:10:49 +01:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
2018-10-04 19:10:43 +02:00
class ResetPasswordController 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
*/
protected $redirectTo = '/';
2018-10-04 19:10:43 +02:00
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
public function showResetForm(Request $request, $token = null)
{
2022-01-17 11:22:10 +01:00
$company = false;
if (Ninja::isHosted()) {
2022-01-17 11:22:10 +01:00
MultiDB::findAndSetDbByCompanyKey($request->session()->get('company_key'));
2023-08-04 08:40:44 +02:00
/** @var \App\Models\Company $company **/
2022-01-17 11:22:10 +01:00
$company = Company::where('company_key', $request->session()->get('company_key'))->first();
}
2021-12-10 03:10:02 +01:00
if ($company) {
2021-12-10 03:10:02 +01:00
$account = $company->account;
} else {
2021-12-10 03:10:02 +01:00
$account = Account::first();
}
2023-10-02 08:20:26 +02:00
return $this->render('auth.passwords.reset', ['root' => 'themes', 'token' => $token, 'account' => $account, 'email' => $request->email]);
}
/**
* Reset the given user's password.
*
2020-10-28 11:10:49 +01:00
* @param Request $request
* @return RedirectResponse|JsonResponse
* @throws \Illuminate\Validation\ValidationException
*/
public function reset(Request $request)
{
$request->validate($this->rules(), $this->validationErrorMessages());
// Here we will attempt to reset the user's password. If it is successful we
// will update the password on an actual user model and persist it to the
// database. Otherwise we will parse the error and return the response.
$response = $this->broker()->reset(
$this->credentials($request),
function ($user, $password) {
$this->resetPassword($user, $password);
}
);
// 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);
}
public function afterReset()
{
auth()->logout();
2023-10-02 08:20:26 +02:00
if(request()->has('react') || request()->hasHeader('X-React'))
return redirect(config('ninja.react_url').'/#/login');
return redirect('/');
}
/**
* Get the response for a successful password reset.
*
* @param \Illuminate\Http\Request $request
* @param string $response
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
*/
protected function sendResetResponse(Request $request, $response)
{
if ($request->wantsJson()) {
return new JsonResponse(['message' => trans($response)], 200);
}
2023-10-02 08:20:26 +02:00
if($request->hasHeader('X-REACT') || $request->has('react')){
2023-10-02 07:31:14 +02:00
return redirect(config('ninja.react_url').'/#/login');
}
2023-10-02 08:20:26 +02:00
else
return redirect('/#/login');
return redirect($this->redirectPath())
->with('status', trans($response));
}
2018-10-04 19:10:43 +02:00
}