Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions auth/auth.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 22 additions & 0 deletions auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {}) }
};
}
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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 },
Expand Down
1 change: 1 addition & 0 deletions auth/schemas/authUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface IAuthUsers {
nombre: string;
}[];
lastLogin: Date;
fechaVencimiento?: Date;
}[];
lastLogin: Date;
tipo?: String;
Expand Down
3 changes: 2 additions & 1 deletion auth/schemas/permisos-organizaciones.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const PermisosOrganizacionesSchema = new Schema({
perfiles: [{
_id: Types.ObjectId,
nombre: String
}]
}],
fechaVencimiento: Date
});
PermisosOrganizacionesSchema.plugin(AuditPlugin);