2020-12-03 15:33:18 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. 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)
|
|
|
|
{
|
2021-12-12 11:39:12 +01:00
|
|
|
|
2020-12-03 15:33:18 +01:00
|
|
|
$multiple_contacts = ClientContact::query()
|
2021-12-12 11:39:12 +01:00
|
|
|
->with('client.gateway_tokens','company')
|
2020-12-03 15:33:18 +01:00
|
|
|
->where('email', auth('contact')->user()->email)
|
|
|
|
->whereNotNull('email')
|
2021-05-14 12:33:12 +02:00
|
|
|
->where('email', '<>', '')
|
2022-01-13 05:10:43 +01:00
|
|
|
// ->whereNull('deleted_at')
|
2020-12-03 15:33:18 +01:00
|
|
|
->distinct('company_id')
|
2021-05-11 03:55:47 +02:00
|
|
|
->distinct('email')
|
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
|
|
|
})
|
2021-12-12 11:39:12 +01:00
|
|
|
->whereHas('company', function ($query){
|
2021-05-15 09:00:17 +02:00
|
|
|
return $query->where('account_id', auth('contact')->user()->client->company->account->id);
|
|
|
|
})
|
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
|
|
|
}
|
|
|
|
|
2021-12-20 01:20:22 +01: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);
|
2020-12-03 15:33:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
session()->put('multiple_contacts', $multiple_contacts);
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|