Spaces:
Running
Running
File size: 1,577 Bytes
6773345 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <?php
namespace App\Http\Middleware;
use App\Shared\Http\ApiResponse;
use Closure;
use Illuminate\Http\Request;
class RequirePortal
{
public function handle(Request $request, Closure $next, string $portal)
{
$user = $request->attributes->get('actor_user');
$roles = $request->attributes->get('actor_roles', []);
if ($user === null) {
return ApiResponse::error('UNAUTHORIZED', 'Authentication is required.', [], 401);
}
$portalStatus = strtoupper((string) ($user->portal_status ?? 'ACTIVE'));
if (in_array($portalStatus, ['SUSPENDED', 'DISABLED'], true)) {
return ApiResponse::error('PORTAL_ACCESS_DISABLED', 'This portal account is not active.', [], 403);
}
$userType = strtoupper((string) ($user->user_type ?? 'EXTERNAL'));
$portal = strtolower($portal);
if ($portal === 'admin') {
if ($userType === 'ADMIN' || in_array('SUPER_ADMIN', $roles, true)) {
return $next($request);
}
return ApiResponse::error('ADMIN_PORTAL_REQUIRED', 'An admin portal account is required.', [], 403);
}
if ($portal === 'client') {
if ($userType === 'CLIENT' && $portalStatus === 'ACTIVE') {
return $next($request);
}
return ApiResponse::error('CLIENT_PORTAL_REQUIRED', 'An active client portal account is required.', [], 403);
}
return ApiResponse::error('PORTAL_NOT_SUPPORTED', 'The requested portal is not supported.', [], 500);
}
}
|