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

54 lines
1.9 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-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-01 15:06:34 +02:00
if ($server = session(SESSION_DB_SERVER)) {
config(['database.default' => $server]);
} elseif ($email = $request->email) {
2017-05-01 14:46:57 +02:00
LookupUser::setServerByField('email', $email);
2017-05-01 16:29:31 +02:00
} elseif ($code = $request->confirmation_code) {
LookupUser::setServerByField('confirmation_code', $code);
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);
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);
}
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);
}
}