feat: multi-tenant architecture and tenant isolation changes
This commit is contained in:
40
app/Http/Middleware/CheckModuleAccess.php
Normal file
40
app/Http/Middleware/CheckModuleAccess.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\TenantModule;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class CheckModuleAccess
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request for a tenant route.
|
||||
*
|
||||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
||||
* @param string $moduleKey
|
||||
*/
|
||||
public function handle(Request $request, Closure $next, string $moduleKey): Response
|
||||
{
|
||||
// If tenancy is initialized and tenant_modules exists
|
||||
if (function_exists('tenant') && tenant()) {
|
||||
$isModuleEnabled = TenantModule::where('module_key', $moduleKey)
|
||||
->where('enabled', true)
|
||||
->exists();
|
||||
|
||||
if (!$isModuleEnabled) {
|
||||
if ($request->wantsJson() || $request->is('api/*')) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => "The '{$moduleKey}' module is not enabled for your company subscription.",
|
||||
], 403);
|
||||
}
|
||||
|
||||
abort(403, "The '{$moduleKey}' module is not enabled for your company subscription.");
|
||||
}
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user