1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-18 17:12:43 +01:00
invoiceninja/app/Http/Middleware/SetDb.php

48 lines
1.1 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Middleware;
use App\Libraries\MultiDB;
use Closure;
2020-10-28 11:10:49 +01:00
use Illuminate\Http\Request;
use stdClass;
class SetDb
{
/**
* Handle an incoming request.
*
2020-10-28 11:10:49 +01:00
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
2019-09-24 00:37:38 +02:00
$error = [
'message' => 'Invalid Token',
2020-10-28 11:10:49 +01:00
'errors' => new stdClass,
2019-09-24 00:37:38 +02:00
];
if ($request->header('X-API-TOKEN') && config('ninja.db.multi_db_enabled')) {
if (! MultiDB::findAndSetDb($request->header('X-API-TOKEN'))) {
2019-09-24 23:18:41 +02:00
return response()->json($error, 403);
}
} elseif (! config('ninja.db.multi_db_enabled')) {
return $next($request);
} else {
return response()->json($error, 403);
}
return $next($request);
}
}