1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Http/Controllers/Auth/ContactForgotPasswordController.php

146 lines
4.6 KiB
PHP
Raw Normal View History

2019-07-17 06:15:25 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2019-07-17 06:15:25 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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;
use App\Http\Requests\ClientPortal\Contact\ContactPasswordResetRequest;
use App\Libraries\MultiDB;
use App\Models\Account;
use App\Models\ClientContact;
use App\Models\Company;
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;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;
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()
{
$this->middleware('guest:contact');
}
/**
* Show the reset email form.
*
2020-10-28 11:10:49 +01:00
* @return Factory|View
*/
public function showLinkRequestForm(Request $request)
{
2021-12-09 11:50:29 +01:00
2021-12-09 06:34:23 +01:00
$account = false;
2021-12-09 11:50:29 +01:00
if(Ninja::isHosted() && $request->session()->has('company_key'))
2021-12-09 06:34:23 +01:00
{
2021-12-09 11:50:29 +01:00
MultiDB::findAndSetDbByCompanyKey($request->session()->get('company_key'));
$company = Company::where('company_key', $request->session()->get('company_key'))->first();
$account = $company->account;
2021-12-09 06:34:23 +01:00
}
2021-12-09 06:34:23 +01:00
if(!$account){
2021-12-01 11:29:48 +01:00
$account = Account::first();
2021-12-09 06:34:23 +01:00
$company = $account->companies->first();
}
2021-12-01 11:29:48 +01:00
2020-03-23 18:10:42 +01:00
return $this->render('auth.passwords.request', [
'title' => 'Client Password Reset',
'passwordEmailRoute' => 'client.password.email',
'account' => $account,
'company' => $company
]);
2019-07-17 06:15:25 +02:00
}
2019-07-17 06:52:54 +02:00
protected function guard()
{
return Auth::guard('contact');
}
public function broker()
{
return Password::broker('contacts');
}
public function sendResetLinkEmail(ContactPasswordResetRequest $request)
{
2021-12-10 04:00:22 +01:00
if(Ninja::isHosted() && $request->has('company_key'))
MultiDB::findAndSetDbByCompanyKey($request->input('company_key'));
$this->validateEmail($request);
2021-12-12 11:39:12 +01:00
if(Ninja::isHosted() && $company = Company::where('company_key', $request->input('company_key'))->first())
{
$contact = ClientContact::where(['email' => $request->input('email'), 'company_id' => $company->id])
->whereHas('client', function ($query) {
$query->where('is_deleted',0);
})->first();
}
else {
$contact = ClientContact::where(['email' => $request->input('email')])
->whereHas('client', function ($query) {
$query->where('is_deleted',0);
})->first();
2021-12-12 11:39:12 +01:00
}
$response = false;
if($contact){
/* 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;
2021-12-10 05:39:07 +01:00
}
else
return $this->sendResetLinkFailedResponse($request, Password::INVALID_USER);
if ($request->ajax()) {
if($response == Password::RESET_THROTTLED)
return response()->json(['message' => ctrans('passwords.throttled'), 'status' => false], 429);
return $response == Password::RESET_LINK_SENT
? response()->json(['message' => 'Reset link sent to your email.', 'status' => true], 201)
: response()->json(['message' => 'Email not found', 'status' => false], 401);
}
return $response == Password::RESET_LINK_SENT
? $this->sendResetLinkResponse($request, $response)
: $this->sendResetLinkFailedResponse($request, $response);
}
2019-07-17 06:15:25 +02:00
}