feat(authorization): filter tenant menus by user roles in bootstrap process

This commit is contained in:
2026-07-31 08:23:18 -03:00
parent a20b1713d3
commit a087a511e0
2 changed files with 46 additions and 1 deletions

View File

@@ -25,7 +25,10 @@ class BootstrapTenantController extends Controller
->where('dominio', $dominio)
->firstOrFail(),
[
'menues',
'menues' => fn ($query) => $query->whereHas(
'roles',
fn ($query) => $query->where('codigo', 'user')
),
'categories' => fn ($query) => $query->orderBy('nombre'),
]
)

View File

@@ -4,6 +4,7 @@ namespace Tests\Feature\Tenant;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Authorization\Models\Role;
use App\Domains\Catalog\Models\Category;
use App\Domains\Menu\Models\Menu;
use App\Domains\Tenant\Models\Tenant;
@@ -145,6 +146,10 @@ class BootstrapTenantControllerTest extends TestCase
public function test_it_returns_tenant_menus_as_a_clean_hierarchy(): void
{
$tenant = $this->createTenant();
$userRole = Role::query()->create([
'codigo' => 'user',
'nombre' => 'Usuario',
]);
$parent = Menu::query()->create([
'code' => 'help',
'label' => 'Ayuda',
@@ -181,6 +186,11 @@ class BootstrapTenantControllerTest extends TestCase
$staticChild->code => ['static_content' => $staticContent],
$dynamicChild->code,
]);
$userRole->menus()->sync([
$parent->code,
$staticChild->code,
$dynamicChild->code,
]);
$response = $this->getJson('/api/tenants/bootstrap/acme.com');
@@ -214,6 +224,38 @@ class BootstrapTenantControllerTest extends TestCase
$this->assertArrayNotHasKey('static_content', $menus[0]['submenues'][1]);
}
public function test_it_returns_only_menus_assigned_to_the_user_role(): void
{
$tenant = $this->createTenant();
$userRole = Role::query()->create([
'codigo' => 'user',
'nombre' => 'Usuario',
]);
$adminRole = Role::query()->create([
'codigo' => 'admin',
'nombre' => 'Administrador',
]);
$userMenu = Menu::query()->create([
'code' => 'account',
'label' => 'Mi cuenta',
'route' => '/mi-cuenta',
]);
$adminMenu = Menu::query()->create([
'code' => 'admin.catalog',
'label' => 'Catálogo',
'route' => '/admin/catalog',
]);
$tenant->menues()->sync([$userMenu->code, $adminMenu->code]);
$userRole->menus()->sync([$userMenu->code]);
$adminRole->menus()->sync([$adminMenu->code]);
$this->getJson('/api/tenants/bootstrap/acme.com')
->assertOk()
->assertJsonPath('data.menues.0.code', 'account')
->assertJsonMissing(['code' => 'admin.catalog']);
}
public function test_it_rejects_duplicate_domains_after_normalization_when_storing(): void
{
$base64Image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';