feat(menu-guard): implement hasMenuGuard for route access control based on tenant menu permissions
This commit is contained in:
24
src/app/core/guards/menu.guard.ts
Normal file
24
src/app/core/guards/menu.guard.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { inject } from '@angular/core';
|
||||
import { CanActivateFn, Router } from '@angular/router';
|
||||
import { TenantService } from '../services/tenant.service';
|
||||
|
||||
export const hasMenuGuard = (menuCode: string): CanActivateFn => {
|
||||
return () => {
|
||||
const tenantService = inject(TenantService);
|
||||
const router = inject(Router);
|
||||
|
||||
const tenant = tenantService.tenant();
|
||||
|
||||
if (!tenant) {
|
||||
return router.createUrlTree(['/']);
|
||||
}
|
||||
|
||||
const hasMenu = tenant.menues?.some(menu => menu.code === menuCode) ?? false;
|
||||
|
||||
if (hasMenu) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return router.createUrlTree(['/']);
|
||||
};
|
||||
};
|
||||
@@ -4,6 +4,7 @@ import { SimpleLayoutComponent } from '../../core/layout/simple-layout/simple-la
|
||||
import { StoreLayoutComponent } from '../../core/layout/store-layout/store-layout.component';
|
||||
import { authGuard, guestOnlyGuard } from '../../core/services/auth/auth.guards';
|
||||
import { LoginPageComponent } from './pages/login-page/login-page.component';
|
||||
import { hasMenuGuard } from '../../core/guards/menu.guard';
|
||||
import { productDetailResolver } from './pages/product-detail-page/product-detail-page.resolver';
|
||||
import { RegisterPageComponent } from './pages/register-page/register-page.component';
|
||||
import { StoreHomePageComponent } from './pages/store-home-page/store-home-page.component';
|
||||
@@ -17,6 +18,7 @@ export const routes: Routes = [
|
||||
{
|
||||
path: '',
|
||||
component: StoreHomePageComponent,
|
||||
canActivate: [hasMenuGuard('index')],
|
||||
resolve: {
|
||||
productsData: storeHomeProductsResolver,
|
||||
},
|
||||
@@ -45,6 +47,7 @@ export const routes: Routes = [
|
||||
},
|
||||
{
|
||||
path: 'producto/:id',
|
||||
canActivate: [hasMenuGuard('product.detail')],
|
||||
resolve: {
|
||||
productDetailData: productDetailResolver,
|
||||
},
|
||||
@@ -55,7 +58,7 @@ export const routes: Routes = [
|
||||
},
|
||||
{
|
||||
path: 'checkout',
|
||||
canActivate: [authGuard],
|
||||
canActivate: [authGuard, hasMenuGuard('checkout')],
|
||||
loadComponent: () =>
|
||||
import('./pages/checkout-page/checkout-page.component').then(
|
||||
(m) => m.CheckoutPageComponent,
|
||||
@@ -82,6 +85,7 @@ export const routes: Routes = [
|
||||
children: [
|
||||
{
|
||||
path: 'datos-personales',
|
||||
canActivate: [hasMenuGuard('profile')],
|
||||
loadComponent: () =>
|
||||
import('./pages/account-page/pages/profile-page/profile-page').then(
|
||||
(m) => m.ProfilePage,
|
||||
@@ -89,6 +93,7 @@ export const routes: Routes = [
|
||||
},
|
||||
{
|
||||
path: 'compras',
|
||||
canActivate: [hasMenuGuard('purchases')],
|
||||
loadComponent: () =>
|
||||
import('./pages/account-page/pages/purchases-page/purchases-page').then(
|
||||
(m) => m.PurchasesPage,
|
||||
|
||||
Reference in New Issue
Block a user