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
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. 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-07-08 23:57:31 +02:00
|
|
|
use App\Http\Requests\ClientPortal\Contact\ContactPasswordResetRequest;
|
2021-02-15 00:39:40 +01: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;
|
|
|
|
use App\Models\Company;
|
2021-09-04 03:27:18 +02:00
|
|
|
use App\Utils\Ninja;
|
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\SendsPasswordResetEmails;
|
2021-02-15 00:39:40 +01:00
|
|
|
use Illuminate\Http\Request;
|
2019-07-18 01:45:18 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
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 ContactForgotPasswordController extends Controller
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Password Reset Controller
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| This controller is responsible for handling password reset emails and
|
|
|
|
| includes a trait which assists in sending these notifications from
|
|
|
|
| your application to your users. Feel free to explore this trait.
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
use SendsPasswordResetEmails;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2019-07-18 01:45:18 +02:00
|
|
|
$this->middleware('guest:contact');
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Show the reset email form.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Factory|View
|
2020-09-06 11:38:10 +02:00
|
|
|
*/
|
2021-05-27 01:14:21 +02:00
|
|
|
public function showLinkRequestForm(Request $request)
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2021-12-09 06:34:23 +01:00
|
|
|
$account = false;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
|
|
|
if (Ninja::isHosted() && $request->session()->has('company_key')) {
|
2021-12-09 11:50:29 +01:00
|
|
|
MultiDB::findAndSetDbByCompanyKey($request->session()->get('company_key'));
|
2023-08-03 06:30:14 +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();
|
|
|
|
$account = $company->account;
|
2021-12-09 06:34:23 +01:00
|
|
|
}
|
2021-05-27 01:14:21 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $account) {
|
2023-08-03 06:30:14 +02:00
|
|
|
|
|
|
|
/** @var \App\Models\Account $account **/
|
2021-12-01 11:29:48 +01:00
|
|
|
$account = Account::first();
|
2021-12-09 06:34:23 +01:00
|
|
|
$company = $account->companies->first();
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-03-23 18:10:42 +01:00
|
|
|
return $this->render('auth.passwords.request', [
|
2019-07-18 01:45:18 +02:00
|
|
|
'title' => 'Client Password Reset',
|
2020-09-06 11:38:10 +02:00
|
|
|
'passwordEmailRoute' => 'client.password.email',
|
2021-09-04 03:27:18 +02:00
|
|
|
'account' => $account,
|
2022-06-21 11:57:17 +02:00
|
|
|
'company' => $company,
|
2023-10-03 15:59:20 +02:00
|
|
|
'is_react' => false,
|
2019-07-18 01:45:18 +02:00
|
|
|
]);
|
2019-07-17 06:15:25 +02:00
|
|
|
}
|
2019-07-17 06:52:54 +02:00
|
|
|
|
|
|
|
protected function guard()
|
|
|
|
{
|
|
|
|
return Auth::guard('contact');
|
|
|
|
}
|
2019-07-18 01:45:18 +02:00
|
|
|
|
|
|
|
public function broker()
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
return Password::broker('contacts');
|
2019-07-18 01:45:18 +02:00
|
|
|
}
|
2021-02-15 00:39:40 +01:00
|
|
|
|
2021-07-08 23:57:31 +02:00
|
|
|
public function sendResetLinkEmail(ContactPasswordResetRequest $request)
|
2021-02-15 00:39:40 +01:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted() && $request->has('company_key')) {
|
2021-12-10 04:00:22 +01:00
|
|
|
MultiDB::findAndSetDbByCompanyKey($request->input('company_key'));
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
2021-10-23 01:06:30 +02:00
|
|
|
$this->validateEmail($request);
|
2021-09-04 03:27:18 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted() && $company = Company::where('company_key', $request->input('company_key'))->first()) {
|
2023-08-04 08:40:44 +02:00
|
|
|
/** @var \App\Models\Company $company **/
|
|
|
|
|
|
|
|
/** @var \App\Models\ClientContact $contact **/
|
2022-01-18 01:24:40 +01:00
|
|
|
$contact = ClientContact::where(['email' => $request->input('email'), 'company_id' => $company->id])
|
|
|
|
->whereHas('client', function ($query) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$query->where('is_deleted', 0);
|
|
|
|
})->first();
|
|
|
|
} else {
|
2022-01-18 01:24:40 +01:00
|
|
|
$contact = ClientContact::where(['email' => $request->input('email')])
|
|
|
|
->whereHas('client', function ($query) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$query->where('is_deleted', 0);
|
|
|
|
})->first();
|
2021-12-12 11:39:12 +01:00
|
|
|
}
|
2021-02-15 00:39:40 +01:00
|
|
|
|
2024-06-17 02:14:04 +02:00
|
|
|
// $response = false;
|
2021-10-23 01:06:30 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($contact) {
|
2021-10-23 01:06:30 +02:00
|
|
|
/* Update all instances of the client */
|
|
|
|
$token = Str::random(60);
|
|
|
|
ClientContact::where('email', $contact->email)->update(['token' => $token]);
|
|
|
|
$contact->sendPasswordResetNotification($token);
|
|
|
|
$response = Password::RESET_LINK_SENT;
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-10-23 01:06:30 +02:00
|
|
|
return $this->sendResetLinkFailedResponse($request, Password::INVALID_USER);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-02-15 00:39:40 +01:00
|
|
|
|
|
|
|
if ($request->ajax()) {
|
2024-06-17 02:14:04 +02:00
|
|
|
if ($response == Password::RESET_THROTTLED) { // @phpstan-ignore-line
|
2021-05-13 14:41:32 +02:00
|
|
|
return response()->json(['message' => ctrans('passwords.throttled'), 'status' => false], 429);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-05-13 14:41:32 +02:00
|
|
|
|
2024-06-17 02:14:04 +02:00
|
|
|
return $response == Password::RESET_LINK_SENT // @phpstan-ignore-line
|
2021-02-15 00:39:40 +01:00
|
|
|
? response()->json(['message' => 'Reset link sent to your email.', 'status' => true], 201)
|
|
|
|
: response()->json(['message' => 'Email not found', 'status' => false], 401);
|
|
|
|
}
|
|
|
|
|
2024-06-17 02:14:04 +02:00
|
|
|
return $response == Password::RESET_LINK_SENT // @phpstan-ignore-line
|
2021-02-15 00:39:40 +01:00
|
|
|
? $this->sendResetLinkResponse($request, $response)
|
|
|
|
: $this->sendResetLinkFailedResponse($request, $response);
|
|
|
|
}
|
2019-07-17 06:15:25 +02:00
|
|
|
}
|