diff --git a/auth/auth.class.ts b/auth/auth.class.ts index 08ec647eb..a3888ce46 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 3b6284c5f..592ab6b49 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 5fd54eb97..c72fea6ac 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 ef86ad44e..923923440 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);