2020-09-23 03:45:07 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
2020-09-30 00:42:55 +02:00
|
|
|
use App\Models\Client;
|
2020-09-23 03:45:07 +02:00
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\CompanyToken;
|
|
|
|
use Auth;
|
2020-09-30 00:42:55 +02:00
|
|
|
use Closure;
|
2020-09-23 03:45:07 +02:00
|
|
|
|
|
|
|
class ContactKeyLogin
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2020-09-30 00:42:55 +02:00
|
|
|
* Sets a contact LOGGED IN if an appropriate client_hash is provided as a query parameter
|
|
|
|
* OR
|
|
|
|
* If the contact_key is provided in the route
|
|
|
|
*
|
2020-09-23 03:45:07 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
|
|
|
|
2020-09-23 05:56:56 +02:00
|
|
|
if ($request->segment(3) && config('ninja.db.multi_db_enabled')) {
|
2020-09-23 03:45:07 +02:00
|
|
|
|
2020-09-23 05:56:56 +02:00
|
|
|
if (MultiDB::findAndSetDbByContactKey($request->segment(3))) {
|
2020-09-23 03:45:07 +02:00
|
|
|
|
2020-09-23 05:56:56 +02:00
|
|
|
$client_contact = ClientContact::where('contact_key', $request->segment(3))->first();
|
2020-09-23 03:45:07 +02:00
|
|
|
Auth::guard('contact')->login($client_contact, true);
|
|
|
|
return redirect()->to('client/dashboard');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if ($request->has('contact_key')) {
|
|
|
|
|
2020-09-23 05:56:56 +02:00
|
|
|
if($client_contact = ClientContact::where('contact_key', $request->segment(3))->first()){
|
2020-09-23 03:45:07 +02:00
|
|
|
Auth::guard('contact')->login($client_contact, true);
|
|
|
|
return redirect()->to('client/dashboard');
|
|
|
|
}
|
|
|
|
|
2020-09-30 00:42:55 +02:00
|
|
|
}
|
|
|
|
else if($request->has('client_hash') && config('ninja.db.multi_db_enabled')){
|
|
|
|
|
|
|
|
if (MultiDB::findAndSetDbByClientHash($request->input('client_hash'))) {
|
|
|
|
|
|
|
|
$client = Client::where('client_hash', $request->input('client_hash'))->first();
|
|
|
|
Auth::guard('contact')->login($client->primary_contact()->first(), true);
|
|
|
|
return redirect()->to('client/dashboard');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if($request->has('client_hash')){
|
|
|
|
|
|
|
|
if($client = Client::where('client_hash', $request->input('client_hash'))->first()){
|
|
|
|
Auth::guard('contact')->login($client->primary_contact()->first(), true);
|
|
|
|
return redirect()->to('client/dashboard');
|
|
|
|
}
|
|
|
|
|
2020-09-23 03:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|