From 901cd14bb01a4b779a1b089df052b038e0d6f8eb Mon Sep 17 00:00:00 2001 From: nicolasarana <90768149+nicolasarana@users.noreply.github.com> Date: Fri, 9 Jan 2026 12:43:51 -0300 Subject: [PATCH] feat(GDU):"Agregar una "fecha de vencimiento" para permisos por efector" --- auth/auth.class.ts | 9 +++++++++ auth/auth.controller.ts | 22 ++++++++++++++++++++++ auth/schemas/authUsers.ts | 1 + auth/schemas/permisos-organizaciones.ts | 3 ++- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/auth/auth.class.ts b/auth/auth.class.ts index 08ec647eb8..a3888ce468 100644 --- a/auth/auth.class.ts +++ b/auth/auth.class.ts @@ -225,6 +225,15 @@ export class Auth { pacienteRestringido: payload.pacienteRestringido }; + + if (payload.organizacion.fechaVencimiento) { + const now = new Date(); + const fechaVencimiento = new Date(payload.organizacion.fechaVencimiento); + if (fechaVencimiento < now) { + return next(403); + } + } + return next(); } else { return next(); diff --git a/auth/auth.controller.ts b/auth/auth.controller.ts index 3b6284c5f7..592ab6b49b 100644 --- a/auth/auth.controller.ts +++ b/auth/auth.controller.ts @@ -65,6 +65,7 @@ export function createPayload(user, authOrg, prof) { }, profesional: prof && String(prof._id), permisos: [...user.permisosGlobales, ...authOrg.permisos], + fechaVencimiento: authOrg.fechaVencimiento, feature: { ...(user.configuracion || {}) } }; } @@ -77,6 +78,7 @@ export async function findTokenData(username: number, organizacion: ObjectId) { const pProfesional = Profesional.findOne({ documento: String(username), habilitado: { $ne: false } }, { nombre: true, apellido: true }); const [auth, prof]: [any, any] = await Promise.all([pAuth, pProfesional]); if (auth) { + await checkAndInactivateExpired(auth); const authOrganizacion = auth.organizaciones.find(item => String(item._id) === String(organizacion)); return { usuario: auth, @@ -130,6 +132,7 @@ export async function findUser(username) { const pProfesional = Profesional.findOne({ documento: username, habilitado: { $ne: false } }, { matriculas: true, especialidad: true }); const [auth, prof] = await Promise.all([pAuth, pProfesional]); if (auth) { + await checkAndInactivateExpired(auth); return { user: auth, profesional: prof @@ -138,6 +141,25 @@ export async function findUser(username) { return null; } +/** + * Chequea las organizaciones del usuario e inactiva las que tienen fecha de vencimiento cumplida. + * @param {any} user Instancia de AuthUsers + */ +export async function checkAndInactivateExpired(user) { + let changed = false; + const now = new Date(); + user.organizaciones.forEach(org => { + if (org.activo && org.fechaVencimiento && org.fechaVencimiento < now) { + org.activo = false; + changed = true; + } + }); + if (changed) { + user.audit(userScheduler); + await user.save(); + } +} + export async function updateUser(documento, nombre, apellido, password) { return await AuthUsers.findOneAndUpdate( { usuario: documento }, diff --git a/auth/schemas/authUsers.ts b/auth/schemas/authUsers.ts index 5fd54eb973..c72fea6ac1 100644 --- a/auth/schemas/authUsers.ts +++ b/auth/schemas/authUsers.ts @@ -23,6 +23,7 @@ export interface IAuthUsers { nombre: string; }[]; lastLogin: Date; + fechaVencimiento?: Date; }[]; lastLogin: Date; tipo?: String; diff --git a/auth/schemas/permisos-organizaciones.ts b/auth/schemas/permisos-organizaciones.ts index ef86ad44e8..923923440c 100644 --- a/auth/schemas/permisos-organizaciones.ts +++ b/auth/schemas/permisos-organizaciones.ts @@ -10,7 +10,8 @@ export const PermisosOrganizacionesSchema = new Schema({ perfiles: [{ _id: Types.ObjectId, nombre: String - }] + }], + fechaVencimiento: Date }); PermisosOrganizacionesSchema.plugin(AuditPlugin);