Skip to content

Commit cbbe666

Browse files
authored
feat(GDU):"Agregar una "fecha de vencimiento" para permisos por efector" (#2171)
1 parent 4e266ab commit cbbe666

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

auth/auth.class.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ export class Auth {
225225
pacienteRestringido: payload.pacienteRestringido
226226

227227
};
228+
229+
if (payload.organizacion.fechaVencimiento) {
230+
const now = new Date();
231+
const fechaVencimiento = new Date(payload.organizacion.fechaVencimiento);
232+
if (fechaVencimiento < now) {
233+
return next(403);
234+
}
235+
}
236+
228237
return next();
229238
} else {
230239
return next();

auth/auth.controller.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export function createPayload(user, authOrg, prof) {
6565
},
6666
profesional: prof && String(prof._id),
6767
permisos: [...user.permisosGlobales, ...authOrg.permisos],
68+
fechaVencimiento: authOrg.fechaVencimiento,
6869
feature: { ...(user.configuracion || {}) }
6970
};
7071
}
@@ -77,6 +78,7 @@ export async function findTokenData(username: number, organizacion: ObjectId) {
7778
const pProfesional = Profesional.findOne({ documento: String(username), habilitado: { $ne: false } }, { nombre: true, apellido: true });
7879
const [auth, prof]: [any, any] = await Promise.all([pAuth, pProfesional]);
7980
if (auth) {
81+
await checkAndInactivateExpired(auth);
8082
const authOrganizacion = auth.organizaciones.find(item => String(item._id) === String(organizacion));
8183
return {
8284
usuario: auth,
@@ -130,6 +132,7 @@ export async function findUser(username) {
130132
const pProfesional = Profesional.findOne({ documento: username, habilitado: { $ne: false } }, { matriculas: true, especialidad: true });
131133
const [auth, prof] = await Promise.all([pAuth, pProfesional]);
132134
if (auth) {
135+
await checkAndInactivateExpired(auth);
133136
return {
134137
user: auth,
135138
profesional: prof
@@ -138,6 +141,25 @@ export async function findUser(username) {
138141
return null;
139142
}
140143

144+
/**
145+
* Chequea las organizaciones del usuario e inactiva las que tienen fecha de vencimiento cumplida.
146+
* @param {any} user Instancia de AuthUsers
147+
*/
148+
export async function checkAndInactivateExpired(user) {
149+
let changed = false;
150+
const now = new Date();
151+
user.organizaciones.forEach(org => {
152+
if (org.activo && org.fechaVencimiento && org.fechaVencimiento < now) {
153+
org.activo = false;
154+
changed = true;
155+
}
156+
});
157+
if (changed) {
158+
user.audit(userScheduler);
159+
await user.save();
160+
}
161+
}
162+
141163
export async function updateUser(documento, nombre, apellido, password) {
142164
return await AuthUsers.findOneAndUpdate(
143165
{ usuario: documento },

auth/schemas/authUsers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface IAuthUsers {
2323
nombre: string;
2424
}[];
2525
lastLogin: Date;
26+
fechaVencimiento?: Date;
2627
}[];
2728
lastLogin: Date;
2829
tipo?: String;

auth/schemas/permisos-organizaciones.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export const PermisosOrganizacionesSchema = new Schema({
1111
_id: Types.ObjectId,
1212
nombre: String
1313
}],
14-
lastLogin: Date
14+
lastLogin: Date,
15+
fechaVencimiento: Date
1516
});
1617
PermisosOrganizacionesSchema.plugin(AuditPlugin);
1718

0 commit comments

Comments
 (0)