1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Http/Middleware/DatabaseLookup.php

74 lines
2.6 KiB
PHP
Raw Normal View History

2017-04-30 22:08:07 +02:00
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Closure;
2017-05-01 17:35:06 +02:00
use App\Models\LookupAccount;
2017-05-01 08:50:10 +02:00
use App\Models\LookupContact;
use App\Models\LookupInvitation;
2017-05-01 09:03:42 +02:00
use App\Models\LookupAccountToken;
2017-05-01 14:46:57 +02:00
use App\Models\LookupUser;
2017-05-10 09:23:50 +02:00
use Auth;
2017-11-15 20:09:43 +01:00
use Utils;
2017-04-30 22:08:07 +02:00
class DatabaseLookup
{
2017-05-01 08:50:10 +02:00
public function handle(Request $request, Closure $next, $guard = 'user')
2017-04-30 22:08:07 +02:00
{
2017-05-01 08:50:10 +02:00
if (! env('MULTI_DB_ENABLED')) {
return $next($request);
}
if ($guard == 'user') {
2017-05-10 10:15:58 +02:00
if ($code = $request->confirmation_code) {
LookupUser::setServerByField('confirmation_code', $code);
2017-05-10 11:41:31 +02:00
} elseif (session(SESSION_DB_SERVER)) {
2017-07-30 21:35:22 +02:00
if (Auth::viaRemember()) {
Auth::logout();
} else {
// do nothing
}
2017-05-10 11:41:31 +02:00
} elseif (! Auth::check() && $email = $request->email) {
2017-05-01 14:46:57 +02:00
LookupUser::setServerByField('email', $email);
2017-05-10 11:41:31 +02:00
} else {
Auth::logout();
2017-05-01 08:50:10 +02:00
}
2017-05-01 09:03:42 +02:00
} elseif ($guard == 'api') {
if ($token = $request->header('X-Ninja-Token')) {
LookupAccountToken::setServerByField('token', $token);
2017-05-01 21:00:21 +02:00
} elseif ($email = $request->email) {
LookupUser::setServerByField('email', $email);
2017-05-01 09:03:42 +02:00
}
2017-05-01 09:19:27 +02:00
} elseif ($guard == 'contact') {
2017-05-01 14:46:57 +02:00
if ($key = request()->invitation_key) {
LookupInvitation::setServerByField('invitation_key', $key);
2017-05-01 16:29:31 +02:00
} elseif ($key = request()->contact_key ?: session('contact_key')) {
2017-05-01 14:46:57 +02:00
LookupContact::setServerByField('contact_key', $key);
} elseif ($key = request()->account_key) {
LookupAccount::setServerByField('account_key', $key);
2017-11-15 20:09:43 +01:00
} else {
$subdomain = Utils::getSubdomain(\Request::server('HTTP_HOST'));
if ($subdomain != 'app') {
LookupAccount::setServerByField('subdomain', $subdomain);
}
2017-05-01 08:50:10 +02:00
}
2017-05-01 14:17:52 +02:00
} elseif ($guard == 'postmark') {
LookupInvitation::setServerByField('message_id', request()->MessageID);
2017-05-01 17:35:06 +02:00
} elseif ($guard == 'account') {
if ($key = request()->account_key) {
LookupAccount::setServerByField('account_key', $key);
2018-01-14 10:42:28 +01:00
} else {
$subdomain = Utils::getSubdomain(\Request::server('HTTP_HOST'));
if ($subdomain != 'app') {
LookupAccount::setServerByField('subdomain', $subdomain);
}
2017-05-01 17:35:06 +02:00
}
2017-05-01 17:25:18 +02:00
} elseif ($guard == 'license') {
config(['database.default' => DB_NINJA_1]);
2017-04-30 22:08:07 +02:00
}
return $next($request);
}
}