2020-09-23 03:45:07 +02: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-09-23 03:45:07 +02:00
|
|
|
*
|
|
|
|
* @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 Auth;
|
2020-09-30 00:42:55 +02:00
|
|
|
use Closure;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Http\Request;
|
2021-03-31 02:07:54 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2021-05-25 00:32:08 +02:00
|
|
|
use Illuminate\Support\Str;
|
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-10-28 11:10:49 +01:00
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param Closure $next
|
2020-09-23 03:45:07 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
if (Auth::guard('contact')->check()) {
|
2020-10-04 12:24:55 +02:00
|
|
|
Auth::guard('contact')->logout();
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-09-23 03:45:07 +02:00
|
|
|
|
2020-09-23 05:56:56 +02:00
|
|
|
if ($request->segment(3) && config('ninja.db.multi_db_enabled')) {
|
|
|
|
if (MultiDB::findAndSetDbByContactKey($request->segment(3))) {
|
2021-03-31 02:07:54 +02:00
|
|
|
|
|
|
|
if($client_contact = ClientContact::where('contact_key', $request->segment(3))->first()){
|
2021-05-25 00:32:08 +02:00
|
|
|
if(empty($client_contact->email))
|
|
|
|
$client_contact->email = Str::random(6) . "@example.com"; $client_contact->save();
|
|
|
|
|
2021-05-14 05:32:37 +02:00
|
|
|
Auth::guard('contact')->login($client_contact, true);
|
2020-09-23 03:45:07 +02:00
|
|
|
return redirect()->to('client/dashboard');
|
2021-03-31 02:07:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 03:45:07 +02:00
|
|
|
}
|
2021-03-31 02:07:54 +02:00
|
|
|
} elseif ($request->segment(2) && $request->segment(2) == 'key_login' && $request->segment(3)) {
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($client_contact = ClientContact::where('contact_key', $request->segment(3))->first()) {
|
2021-05-25 00:32:08 +02:00
|
|
|
|
|
|
|
if(empty($client_contact->email))
|
|
|
|
$client_contact->email = Str::random(6) . "@example.com"; $client_contact->save();
|
|
|
|
|
2021-03-31 02:07:54 +02:00
|
|
|
auth()->guard('contact')->login($client_contact, true);
|
2020-09-23 03:45:07 +02:00
|
|
|
return redirect()->to('client/dashboard');
|
|
|
|
}
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ($request->has('client_hash') && config('ninja.db.multi_db_enabled')) {
|
2020-09-30 00:42:55 +02:00
|
|
|
if (MultiDB::findAndSetDbByClientHash($request->input('client_hash'))) {
|
2021-03-31 02:07:54 +02:00
|
|
|
|
|
|
|
if($client = Client::where('client_hash', $request->input('client_hash'))->first()){
|
2021-05-25 00:32:08 +02:00
|
|
|
|
|
|
|
$primary_contact = $client->primary_contact()->first();
|
|
|
|
|
|
|
|
if(empty($primary_contact->email))
|
|
|
|
$primary_contact->email = Str::random(6) . "@example.com"; $primary_contact->save();
|
|
|
|
|
|
|
|
auth()->guard('contact')->login($primary_contact, true);
|
2021-03-31 02:07:54 +02:00
|
|
|
return redirect()->to('client/dashboard');
|
|
|
|
}
|
2020-09-30 00:42:55 +02:00
|
|
|
}
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ($request->has('client_hash')) {
|
|
|
|
if ($client = Client::where('client_hash', $request->input('client_hash'))->first()) {
|
2021-05-25 00:32:08 +02:00
|
|
|
|
|
|
|
$primary_contact = $client->primary_contact()->first();
|
|
|
|
|
|
|
|
if(empty($primary_contact->email))
|
|
|
|
$primary_contact->email = Str::random(6) . "@example.com"; $primary_contact->save();
|
|
|
|
|
|
|
|
auth()->guard('contact')->login($primary_contact, true);
|
|
|
|
|
2020-09-30 00:42:55 +02:00
|
|
|
return redirect()->to('client/dashboard');
|
|
|
|
}
|
2021-03-31 02:07:54 +02:00
|
|
|
} elseif ($request->segment(2) && $request->segment(2) == 'magic_link' && $request->segment(3)) {
|
|
|
|
$contact_email = Cache::get($request->segment(3));
|
|
|
|
if($client_contact = ClientContact::where('email', $contact_email)->first()){
|
2021-05-25 00:32:08 +02:00
|
|
|
|
|
|
|
if(empty($client_contact->email))
|
|
|
|
$client_contact->email = Str::random(6) . "@example.com"; $client_contact->save();
|
|
|
|
|
|
|
|
auth()->guard('contact')->login($client_contact, true);
|
2021-03-31 18:10:44 +02:00
|
|
|
|
|
|
|
if ($request->query('redirect') && !empty($request->query('redirect'))) {
|
|
|
|
return redirect()->to($request->query('redirect'));
|
|
|
|
}
|
|
|
|
|
2021-03-31 02:07:54 +02:00
|
|
|
return redirect()->to('client/dashboard');
|
|
|
|
}
|
2020-09-23 03:45:07 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 02:07:54 +02:00
|
|
|
|
2020-09-23 03:45:07 +02:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|