2020-09-23 03:45:07 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-09-23 03:45:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-09-23 03:45:07 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
2022-02-21 10:31:02 +01:00
|
|
|
use App\Http\ViewComposers\PortalComposer;
|
2020-09-23 03:45:07 +02:00
|
|
|
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();
|
2022-01-24 23:24:05 +01:00
|
|
|
$request->session()->invalidate();
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-09-23 03:45:07 +02:00
|
|
|
|
2022-11-23 02:26:52 +01:00
|
|
|
//magic links survive for 1 hour
|
2021-06-02 13:47:28 +02:00
|
|
|
if ($request->segment(2) && $request->segment(2) == 'magic_link' && $request->segment(3)) {
|
2021-06-03 01:50:31 +02:00
|
|
|
$payload = Cache::get($request->segment(3));
|
2021-09-13 00:42:15 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $payload) {
|
2021-09-13 00:42:15 +02:00
|
|
|
abort(403, 'Link expired.');
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-09-13 00:42:15 +02:00
|
|
|
|
2021-06-03 01:50:31 +02:00
|
|
|
$contact_email = $payload['email'];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
|
|
|
if ($client_contact = ClientContact::where('email', $contact_email)->where('company_id', $payload['company_id'])->first()) {
|
|
|
|
if (empty($client_contact->email)) {
|
|
|
|
$client_contact->email = Str::random(15).'@example.com';
|
|
|
|
}
|
|
|
|
$client_contact->save();
|
|
|
|
|
2021-12-07 22:45:24 +01:00
|
|
|
auth()->guard('contact')->loginUsingId($client_contact->id, true);
|
2021-06-02 13:47:28 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($request->query('redirect') && ! empty($request->query('redirect'))) {
|
2021-06-02 13:47:28 +02:00
|
|
|
return redirect()->to($request->query('redirect'));
|
|
|
|
}
|
|
|
|
|
2022-02-21 10:31:02 +01:00
|
|
|
return redirect($this->setRedirectPath());
|
2021-06-02 13:47:28 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif ($request->segment(3) && config('ninja.db.multi_db_enabled')) {
|
2021-09-01 06:02:57 +02:00
|
|
|
if (MultiDB::findAndSetDbByContactKey($request->segment(3))) {
|
2023-08-07 00:23:13 +02:00
|
|
|
if ($client_contact = ClientContact::query()->with('company')->where('contact_key', $request->segment(3))->first()) {
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($client_contact->company->settings->enable_client_portal_password) {
|
2022-11-23 02:26:52 +01:00
|
|
|
return redirect()->route('client.login', ['company_key' => $client_contact->company->company_key]);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-11-23 02:26:52 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (empty($client_contact->email)) {
|
|
|
|
$client_contact->email = Str::random(6).'@example.com';
|
|
|
|
}
|
|
|
|
$client_contact->save();
|
2021-09-01 06:02:57 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
auth()->guard('contact')->loginUsingId($client_contact->id, true);
|
2021-09-01 06:02:57 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($request->query('next')) {
|
|
|
|
return redirect()->to($request->query('next'));
|
|
|
|
}
|
2021-03-31 02:07:54 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return redirect($this->setRedirectPath());
|
|
|
|
}
|
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)) {
|
2022-11-23 02:26:52 +01:00
|
|
|
if ($client_contact = ClientContact::with('company')->where('contact_key', $request->segment(3))->first()) {
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($client_contact->company->settings->enable_client_portal_password) {
|
2022-11-23 02:26:52 +01:00
|
|
|
return redirect()->route('client.login', ['company_key' => $client_contact->company->company_key]);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-11-23 02:26:52 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (empty($client_contact->email)) {
|
|
|
|
$client_contact->email = Str::random(6).'@example.com';
|
|
|
|
$client_contact->save();
|
2021-08-10 10:18:05 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-12-07 22:45:24 +01:00
|
|
|
auth()->guard('contact')->loginUsingId($client_contact->id, true);
|
2021-08-10 10:18:05 +02:00
|
|
|
|
|
|
|
if ($request->query('next')) {
|
|
|
|
return redirect($request->query('next'));
|
|
|
|
}
|
|
|
|
|
2022-02-21 10:31:02 +01:00
|
|
|
return redirect($this->setRedirectPath());
|
2020-09-23 03:45:07 +02:00
|
|
|
}
|
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'))) {
|
2023-08-07 00:23:13 +02:00
|
|
|
if ($client = Client::query()->where('client_hash', $request->input('client_hash'))->first()) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$primary_contact = $client->primary_contact()->first();
|
2021-03-31 02:07:54 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (empty($primary_contact->email)) {
|
|
|
|
$primary_contact->email = Str::random(6).'@example.com';
|
|
|
|
}
|
|
|
|
$primary_contact->save();
|
2021-05-25 00:32:08 +02:00
|
|
|
|
2021-12-07 22:45:24 +01:00
|
|
|
auth()->guard('contact')->loginUsingId($primary_contact->id, true);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-02-21 10:31:02 +01:00
|
|
|
return redirect($this->setRedirectPath());
|
2021-03-31 02:07:54 +02:00
|
|
|
}
|
2020-09-30 00:42:55 +02:00
|
|
|
}
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ($request->has('client_hash')) {
|
2023-08-07 07:07:52 +02:00
|
|
|
if ($client = Client::query()->where('client_hash', $request->input('client_hash'))->first()) {
|
2021-05-25 00:32:08 +02:00
|
|
|
$primary_contact = $client->primary_contact()->first();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (empty($primary_contact->email)) {
|
|
|
|
$primary_contact->email = Str::random(6).'@example.com';
|
|
|
|
}
|
|
|
|
$primary_contact->save();
|
2021-05-25 00:32:08 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
auth()->guard('contact')->loginUsingId($primary_contact->id, true);
|
|
|
|
|
|
|
|
return redirect($this->setRedirectPath());
|
2021-03-31 02:07:54 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif ($request->segment(3)) {
|
2023-08-07 07:07:52 +02:00
|
|
|
if ($client_contact = ClientContact::query()->with('company')->where('contact_key', $request->segment(3))->first()) {
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($client_contact->company->settings->enable_client_portal_password) {
|
2022-11-23 02:26:52 +01:00
|
|
|
return redirect()->route('client.login', ['company_key' => $client_contact->company->company_key]);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-11-23 02:26:52 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (empty($client_contact->email)) {
|
|
|
|
$client_contact->email = Str::random(6).'@example.com';
|
|
|
|
$client_contact->save();
|
2022-04-05 05:54:10 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-04-05 05:54:10 +02:00
|
|
|
auth()->guard('contact')->loginUsingId($client_contact->id, true);
|
|
|
|
|
|
|
|
if ($request->query('next')) {
|
|
|
|
return redirect($request->query('next'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect($this->setRedirectPath());
|
|
|
|
}
|
2020-09-23 03:45:07 +02:00
|
|
|
}
|
2022-02-27 21:45:42 +01:00
|
|
|
//28-02-2022 middleware should not allow this to progress as we should have redirected by this stage.
|
2022-06-21 11:57:17 +02:00
|
|
|
abort(404, 'Unable to authenticate.');
|
2020-09-23 03:45:07 +02:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
2022-02-21 10:31:02 +01:00
|
|
|
|
|
|
|
private function setRedirectPath()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (auth()->guard('contact')->user()->company->enabled_modules & PortalComposer::MODULE_INVOICES) {
|
2022-02-21 10:31:02 +01:00
|
|
|
return '/client/invoices';
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif (auth()->guard('contact')->user()->company->enabled_modules & PortalComposer::MODULE_RECURRING_INVOICES) {
|
2022-02-21 10:31:02 +01:00
|
|
|
return '/client/recurring_invoices';
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif (auth()->guard('contact')->user()->company->enabled_modules & PortalComposer::MODULE_QUOTES) {
|
2022-02-21 10:31:02 +01:00
|
|
|
return '/client/quotes';
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif (auth()->guard('contact')->user()->company->enabled_modules & PortalComposer::MODULE_CREDITS) {
|
2022-02-21 10:31:02 +01:00
|
|
|
return '/client/credits';
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif (auth()->guard('contact')->user()->company->enabled_modules & PortalComposer::MODULE_TASKS) {
|
2022-02-21 10:31:02 +01:00
|
|
|
return '/client/tasks';
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif (auth()->guard('contact')->user()->company->enabled_modules & PortalComposer::MODULE_EXPENSES) {
|
2022-02-21 10:31:02 +01:00
|
|
|
return '/client/expenses';
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-02-21 10:31:02 +01:00
|
|
|
}
|
2020-09-23 03:45:07 +02:00
|
|
|
}
|