2019-11-16 04:12:29 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-11-16 04:12:29 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-11-16 04:12:29 +01: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;
|
2019-11-16 04:12:29 +01:00
|
|
|
|
|
|
|
class SetInviteDb
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Closure $next
|
2019-11-16 04:12:29 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
$error = [
|
2019-11-16 04:12:29 +01:00
|
|
|
'message' => 'Invalid URL',
|
2020-10-28 11:10:49 +01:00
|
|
|
'errors' => new stdClass,
|
2019-11-16 04:12:29 +01:00
|
|
|
];
|
2019-12-30 22:59:12 +01:00
|
|
|
/*
|
2019-11-16 04:12:29 +01:00
|
|
|
* Use the host name to set the active DB
|
|
|
|
**/
|
2020-03-06 14:41:15 +01:00
|
|
|
$entity = null;
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $request->route('entity')) {
|
2020-03-11 12:05:05 +01:00
|
|
|
$entity = $request->segment(2);
|
2020-03-21 06:37:30 +01:00
|
|
|
} else {
|
2020-03-06 14:41:15 +01:00
|
|
|
$entity = $request->route('entity');
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-06 14:41:15 +01:00
|
|
|
|
|
|
|
if ($request->getSchemeAndHttpHost() && config('ninja.db.multi_db_enabled') && ! MultiDB::findAndSetDbByInvitation($entity, $request->route('invitation_key'))) {
|
2019-12-30 22:59:12 +01:00
|
|
|
if (request()->json) {
|
2020-02-05 05:06:03 +01:00
|
|
|
return response()->json($error, 403);
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-11-16 04:12:29 +01:00
|
|
|
abort(404);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-11-16 04:12:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|