Skip to content

Commit

Permalink
Merge pull request #24 from udistrital/develop
Browse files Browse the repository at this point in the history
feat: Implementación de roles y permisos
  • Loading branch information
edwargl7 authored Dec 16, 2024
2 parents 53598ff + 2d4d551 commit 3f11058
Show file tree
Hide file tree
Showing 18 changed files with 198 additions and 628 deletions.
63 changes: 63 additions & 0 deletions src/_guards/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router';


@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate {

constructor(

) { }

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const menuInfo = localStorage.getItem('menu');
const menuPermisos = menuInfo ? JSON.parse(atob(menuInfo)) : null;
const fullUrl = window.location.href;
const url = new URL(fullUrl);
const path = url.pathname;

// Obtener parámetros de la ruta
const params = route.params;

if (menuPermisos != null) {
// Pasar tanto la URL como los parámetros a la función de verificación
if (checkUrlExists(menuPermisos, path, params)) {
return true;
}
}
return false;
}

canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.canActivate(route, state);
}
}

// Recorrer el menú para verificar si la URL y los parámetros existen
function checkUrlExists(menuItems: any, targetUrl: string, params: any) {
return menuItems.some((item: any) => {
// Verificar la URL y los parámetros
if (item.Url === targetUrl && checkParams(item.Params, params)) {
return true;
}
if (item.Opciones && item.Opciones.length > 0) {
return checkUrlExists(item.Opciones, targetUrl, params);
}
return false;
});
}

// Verificar los parámetros de la ruta
function checkParams(expectedParams: any, actualParams: any): boolean {
if (!expectedParams) {
return true; // No se requieren parámetros específicos
}
for (let key in expectedParams) {
if (expectedParams[key] !== actualParams[key]) {
return false; // Un parámetro no coincide
}
}
return true; // Todos los parámetros coinciden
}
6 changes: 2 additions & 4 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import { APP_BASE_HREF } from '@angular/common';
import { NgModule } from '@angular/core';
import { provideHttpClient, withFetch } from '@angular/common/http';
import { RouterModule, Routes } from '@angular/router';
import { AuthGuard } from 'src/_guards/auth.guard';

export const routes: Routes = [
{
path: "gestion-roles",
loadChildren: () => import ('./modules/gestion-roles/gestion-roles.module').then(m => m.GestionRolesModule),
},
{
path: "gestion-usuarios",
canActivate: [AuthGuard],
loadChildren: () => import ('./modules/gestion-usuarios/gestion-usuarios.module').then(m => m.GestionUsuariosModule),
},
];
Expand Down
44 changes: 0 additions & 44 deletions src/app/modules/gestion-roles/form-roles/form-roles.component.html

This file was deleted.

78 changes: 0 additions & 78 deletions src/app/modules/gestion-roles/form-roles/form-roles.component.scss

This file was deleted.

This file was deleted.

Loading

0 comments on commit 3f11058

Please sign in to comment.