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 { StoreLayoutComponent } from '../../core/layout/store-layout/store-layout.component';
|
||||||
import { authGuard, guestOnlyGuard } from '../../core/services/auth/auth.guards';
|
import { authGuard, guestOnlyGuard } from '../../core/services/auth/auth.guards';
|
||||||
import { LoginPageComponent } from './pages/login-page/login-page.component';
|
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 { productDetailResolver } from './pages/product-detail-page/product-detail-page.resolver';
|
||||||
import { RegisterPageComponent } from './pages/register-page/register-page.component';
|
import { RegisterPageComponent } from './pages/register-page/register-page.component';
|
||||||
import { StoreHomePageComponent } from './pages/store-home-page/store-home-page.component';
|
import { StoreHomePageComponent } from './pages/store-home-page/store-home-page.component';
|
||||||
@@ -17,6 +18,7 @@ export const routes: Routes = [
|
|||||||
{
|
{
|
||||||
path: '',
|
path: '',
|
||||||
component: StoreHomePageComponent,
|
component: StoreHomePageComponent,
|
||||||
|
canActivate: [hasMenuGuard('index')],
|
||||||
resolve: {
|
resolve: {
|
||||||
productsData: storeHomeProductsResolver,
|
productsData: storeHomeProductsResolver,
|
||||||
},
|
},
|
||||||
@@ -45,6 +47,7 @@ export const routes: Routes = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'producto/:id',
|
path: 'producto/:id',
|
||||||
|
canActivate: [hasMenuGuard('product.detail')],
|
||||||
resolve: {
|
resolve: {
|
||||||
productDetailData: productDetailResolver,
|
productDetailData: productDetailResolver,
|
||||||
},
|
},
|
||||||
@@ -55,7 +58,7 @@ export const routes: Routes = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'checkout',
|
path: 'checkout',
|
||||||
canActivate: [authGuard],
|
canActivate: [authGuard, hasMenuGuard('checkout')],
|
||||||
loadComponent: () =>
|
loadComponent: () =>
|
||||||
import('./pages/checkout-page/checkout-page.component').then(
|
import('./pages/checkout-page/checkout-page.component').then(
|
||||||
(m) => m.CheckoutPageComponent,
|
(m) => m.CheckoutPageComponent,
|
||||||
@@ -82,6 +85,7 @@ export const routes: Routes = [
|
|||||||
children: [
|
children: [
|
||||||
{
|
{
|
||||||
path: 'datos-personales',
|
path: 'datos-personales',
|
||||||
|
canActivate: [hasMenuGuard('profile')],
|
||||||
loadComponent: () =>
|
loadComponent: () =>
|
||||||
import('./pages/account-page/pages/profile-page/profile-page').then(
|
import('./pages/account-page/pages/profile-page/profile-page').then(
|
||||||
(m) => m.ProfilePage,
|
(m) => m.ProfilePage,
|
||||||
@@ -89,6 +93,7 @@ export const routes: Routes = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'compras',
|
path: 'compras',
|
||||||
|
canActivate: [hasMenuGuard('purchases')],
|
||||||
loadComponent: () =>
|
loadComponent: () =>
|
||||||
import('./pages/account-page/pages/purchases-page/purchases-page').then(
|
import('./pages/account-page/pages/purchases-page/purchases-page').then(
|
||||||
(m) => m.PurchasesPage,
|
(m) => m.PurchasesPage,
|
||||||
|
|||||||
Reference in New Issue
Block a user