2019-11-16 04:12:29 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. 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;
|
|
|
|
|
|
|
|
class SetInviteDb
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @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-08-12 00:59:28 +02: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-03-21 06:37:30 +01: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);
|
|
|
|
}
|
|
|
|
}
|