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