2020-12-03 15:33:18 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @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)
|
2020-12-03 15:33:18 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-12-03 15:33:18 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use Closure;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
class CheckClientExistence
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle(Request $request, Closure $next)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (session()->has('multiple_contacts')) {
|
2022-02-16 22:17:29 +01:00
|
|
|
return $next($request);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-02-16 22:17:29 +01:00
|
|
|
|
2024-06-15 08:18:33 +02:00
|
|
|
auth()->guard('contact')->user()->loadMissing(['company']); // @phpstan-ignore method.notFound
|
|
|
|
|
2020-12-03 15:33:18 +01:00
|
|
|
$multiple_contacts = ClientContact::query()
|
2024-06-15 08:18:33 +02:00
|
|
|
// ->with('client.gateway_tokens', 'company')
|
2022-02-16 03:24:10 +01:00
|
|
|
->where('email', auth()->guard('contact')->user()->email)
|
2020-12-03 15:33:18 +01:00
|
|
|
->whereNotNull('email')
|
2021-05-14 12:33:12 +02:00
|
|
|
->where('email', '<>', '')
|
2022-02-27 01:31:01 +01:00
|
|
|
->distinct('client_id')
|
2021-05-10 13:05:44 +02:00
|
|
|
->whereNotNull('company_id')
|
2020-12-03 15:33:18 +01:00
|
|
|
->whereHas('client', function ($query) {
|
2021-12-12 11:39:12 +01:00
|
|
|
return $query->where('is_deleted', false);
|
2020-12-03 15:33:18 +01:00
|
|
|
})
|
2022-06-21 11:57:17 +02:00
|
|
|
->whereHas('company', function ($query) {
|
2022-02-27 01:31:01 +01:00
|
|
|
return $query->where('companies.account_id', auth()->guard('contact')->user()->company->account_id);
|
2021-05-15 09:00:17 +02:00
|
|
|
})
|
2020-12-03 15:33:18 +01:00
|
|
|
->get();
|
|
|
|
|
2022-01-12 12:52:56 +01:00
|
|
|
/* This catches deleted clients who don't have access to the app. We automatically log them out here*/
|
2020-12-03 15:33:18 +01:00
|
|
|
if (count($multiple_contacts) == 0) {
|
|
|
|
Auth::logout();
|
|
|
|
|
2022-01-12 12:52:56 +01:00
|
|
|
return redirect()->route('client.login')->with('message', 'Login disabled');
|
2020-12-03 15:33:18 +01:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (count($multiple_contacts) == 1 && ! Auth::guard('contact')->check()) {
|
2021-12-07 22:45:24 +01:00
|
|
|
Auth::guard('contact')->loginUsingId($multiple_contacts[0]->id, true);
|
2024-06-15 08:18:33 +02:00
|
|
|
|
|
|
|
auth()->guard('contact')->user()->loadMissing(['client' => function ($query) {
|
|
|
|
$query->without('gateway_tokens', 'documents', 'contacts.company', 'contacts'); // Exclude 'grandchildren' relation of 'client'
|
|
|
|
}]);
|
|
|
|
|
2020-12-03 15:33:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
session()->put('multiple_contacts', $multiple_contacts);
|
|
|
|
|
2022-06-10 05:03:05 +02:00
|
|
|
session()->put('is_silent', request()->has('silent'));
|
|
|
|
|
2020-12-03 15:33:18 +01:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|