Files
shopit-back/app/Http/Middleware/EnsureAdminAppTenant.php
ncoronel 44bf67bd12 feat(admin): add admin scope management for users and events
- Introduced AdminScope enum for tenant and event scopes.
- Updated User model to include admin_scope and event_id attributes.
- Enhanced AdminAppAccessService to validate user scopes.
- Modified AdminAppMeResource and UserResource to include event data.
- Implemented middleware to ensure valid tenant access.
- Created migration to add admin_scope and event_id to users table.
- Added tests for event admin functionality and scope validation.
2026-09-30 15:02:10 -03:00

37 lines
1.1 KiB
PHP

<?php
namespace App\Http\Middleware;
use App\Domains\Core\Auth\Services\AdminAppAccessService;
use App\Domains\Core\Authorization\Enums\RoleCode;
use Closure;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class EnsureAdminAppTenant
{
public function __construct(private readonly AdminAppAccessService $accessService) {}
/**
* Ensure the authenticated user is an AdminApp user bound to a tenant.
*/
public function handle(Request $request, Closure $next, string $access = 'tenant'): Response
{
$user = $request->user();
if (
! $user
|| $user->rol_codigo !== RoleCode::AdminApp->value
|| ! $user->tenant_codigo
|| ! $this->accessService->hasValidScope($user)
// Tenant operations remain unavailable until they implement event authorization.
|| ($access !== 'context' && ! $user->isTenantAdministrator())
) {
throw new AuthorizationException;
}
return $next($request);
}
}