Skip to content

Commit bd9716b

Browse files
committed
REC-136: Efectores y cuil paciente
1 parent ca438d5 commit bd9716b

8 files changed

Lines changed: 504 additions & 465 deletions

File tree

src/controllers/auth.controller.ts

Lines changed: 355 additions & 355 deletions
Large diffs are not rendered by default.
Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
1-
import { Request, Response } from "express";
1+
import { Request, Response } from 'express';
22
import IUser from '../interfaces/user.interface';
3-
import User from "../models/user.model";
4-
class UsersController{
5-
public index = async (req: Request, res: Response): Promise<Response> => {
6-
try{
7-
const users: IUser[] | null = await User.find({},{ 'password':0, 'refreshToken':0, 'authenticationToken':0 }).populate('roles', 'role');
8-
return res.status(200).json(users);
9-
} catch (e){
10-
return res.status(500).json({mensaje:`${e}`});
11-
}
12-
};
3+
import User from '../models/user.model';
4+
class UsersController {
5+
public index = async (req: Request, res: Response): Promise<Response> => {
6+
try {
7+
const users: IUser[] | null = await User.find({},{ password:0, refreshToken:0, authenticationToken:0 }).populate('roles', 'role');
8+
return res.status(200).json(users);
9+
} catch (e) {
10+
return res.status(500).json({ mensaje:`${e}` });
11+
}
12+
};
1313

14-
public update = async (req: Request, res: Response): Promise<Response> => {
15-
try{
16-
if (req.body){
17-
const result = await User.findOneAndUpdate({_id: req.body._id}, req.body, { new: true, projection: {'password':0, 'refreshToken':0, 'authenticationToken':0}}).populate('roles', 'role');
18-
return res.status(200).json(result);
19-
}else{
20-
return res.status(400).json({mensaje: "Request body vacío"})
21-
}
22-
} catch (e){
23-
return res.status(500).json({mensaje: `${e}`});
24-
}
25-
}
14+
public show = async (req: Request, res: Response): Promise<Response> => {
15+
try {
16+
const user: IUser | null = await User.findById(req.params.id, { password:0, refreshToken:0, authenticationToken:0 }).populate('roles', 'role');
17+
if (user) {
18+
return res.status(200).json(user);
19+
} else {
20+
return res.status(404).json({ mensaje: 'Usuario no encontrado' });
21+
}
22+
} catch (e) {
23+
return res.status(500).json({ mensaje: `${e}` });
24+
}
25+
};
26+
27+
public update = async (req: Request, res: Response): Promise<Response> => {
28+
try {
29+
if (Object.keys(req.body).length !== 0 && req.body._id) {
30+
const result = await User.findOneAndUpdate({ _id: req.body._id }, req.body, { new: true, projection: { password:0, refreshToken:0, authenticationToken:0 } }).populate('roles', 'role');
31+
return res.status(200).json(result);
32+
} else {
33+
return res.status(400).json({ mensaje: 'Request body vacío' });
34+
}
35+
} catch (e) {
36+
return res.status(500).json({ mensaje: `${e}` });
37+
}
38+
};
2639
};
2740

28-
export default new UsersController;
41+
export default new UsersController;

src/interfaces/prescriptionAndes.interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export default interface IPrescriptionAndes extends Document {
147147
valor: Number;
148148
unidad: String;
149149
};
150+
cuil: String;
150151
};
151152
createdAt: Date;
152153
createdBy: {
@@ -164,4 +165,4 @@ export default interface IPrescriptionAndes extends Document {
164165
nombre: String;
165166
};
166167
};
167-
}
168+
}

src/interfaces/user.interface.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export default interface IUser extends Document{
1414
updatedAt?: Date;
1515
isActive: Boolean;
1616
lastLogin?: Date;
17+
efectores: [{
18+
_id: string;
19+
nombre: string;
20+
direccion: string;
21+
}];
1722
isValidPassword(thisUser: IUser, password: string): Promise<boolean>;
1823
idAndes?: string;
1924
}

src/models/prescriptionAndes.model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ const prescriptionAndesSchema = new Schema({
172172
edadReal: {
173173
valor: Number,
174174
unidad: String
175-
}
175+
},
176+
cuil: String
176177
},
177178
createdAt: Date,
178179
createdBy: {

src/models/user.model.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Schema, Model, model } from 'mongoose';
2+
import mongoose from 'mongoose';
23
import bcrypt from 'bcryptjs';
34
import IUser from '../interfaces/user.interface';
45

@@ -80,7 +81,17 @@ export const userSchema = new Schema({
8081
idAndes: {
8182
type: String,
8283
default: ''
83-
}
84+
},
85+
efectores: [
86+
{
87+
_id: {
88+
type: Schema.Types.ObjectId,
89+
default: () => new mongoose.Types.ObjectId()
90+
},
91+
nombre: String,
92+
direccion: String,
93+
}
94+
]
8495
});
8596

8697
// Model

src/routes/private.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ class PrivateRoutes {
104104

105105
// Users
106106
this.router.get('/users/index', hasPermissionIn('readAny', 'user'), usersController.index);
107-
this.router.post('/users/update', hasPermissionIn('updateAny', 'user'), usersController.update);
107+
this.router.get('/users/:id', hasPermissionIn('readAny', 'user'), usersController.show);
108+
this.router.patch('/users/update', hasPermissionIn('updateAny', 'user'), usersController.update);
108109

109110
// pharmacy
110111
// this.router.get(`/pharmacies/`, hasPermissionIn('readAny','patient'), pharmacyController.index);

src/utils/rbac_abac.ts

Lines changed: 89 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,89 +3,96 @@ import Role from '../models/role.model';
33

44
class AccessControlLoader {
55

6-
constructor(private accessControl: AccessControl = new AccessControl()){
7-
this.init();
8-
}
9-
10-
public getAccessControl = (): AccessControl => {
11-
return this.accessControl;
12-
}
13-
14-
public init = async (): Promise<void> =>{
15-
// const roles = await Role.find().populate({path: 'permissions', select: ['resource', 'action', 'attributes']} ).select('role');
16-
// await this.asyncForEach(roles, async (role: any) => {
17-
// this.accessControl.grant(role.role);
18-
// await this.asyncForEach(role.permissions, async (permission: any) => {
19-
// console.log('in permissions', permission);
20-
// });
21-
// });
22-
23-
let grantList = [
24-
// roles
25-
{ role: 'owner', resource: 'role', action: 'create:any', attributes: '*, !views' },
26-
{ role: 'owner', resource: 'role', action: 'read:any', attributes: '*' },
27-
{ role: 'owner', resource: 'role', action: 'update:any', attributes: '*, !views' },
28-
{ role: 'owner', resource: 'role', action: 'delete:any', attributes: '*' },
29-
30-
{ role: 'admin', resource: 'user', action: 'update:any', attributes: '*' },
31-
{ role: 'admin', resource: 'user', action: 'read:any', attributes: '*' },
32-
33-
{ role: 'auditor', resource: 'user', action: 'read:any', attributes: '*' },
34-
{ role: 'auditor', resource: 'user', action: 'update:any', attributes: '*' },
35-
36-
// prescriptions
37-
{ role: 'professional', resource: 'prescription', action: 'create:any', attributes: '*, !views' },
38-
{ role: 'professional', resource: 'prescription', action: 'read:own', attributes: '*' },
39-
{ role: 'professional', resource: 'prescription', action: 'read:any', attributes: '*' },
40-
{ role: 'professional', resource: 'prescription', action: 'update:own', attributes: '*' },
41-
{ role: 'professional', resource: 'prescription', action: 'delete:any', attributes: '*' },
42-
43-
{ role: 'pharmacist', resource: 'prescription', action: 'read:any', attributes: '*' },
44-
{ role: 'pharmacist', resource: 'prescription', action: 'update:any', attributes: '*, !views' },
45-
46-
{ role: 'owner', resource: 'prescription', action: 'delete:any', attributes: '*' },
47-
48-
{ role: 'auditor', resource: 'prescription', action: 'read:any', attributes: '*' },
49-
50-
// prescriptions public
51-
{ role: 'professional-public', resource: 'prescriptionPublic', action: 'create:any', attributes: '*, !views' },
52-
{ role: 'professional-public', resource: 'prescriptionPublic', action: 'read:own', attributes: '*' },
53-
{ role: 'professional-public', resource: 'prescriptionPublic', action: 'read:any', attributes: '*' },
54-
{ role: 'professional-public', resource: 'prescriptionPublic', action: 'update:own', attributes: '*' },
55-
{ role: 'professional-public', resource: 'prescriptionPublic', action: 'delete:any', attributes: '*' },
56-
57-
{ role: 'pharmacist-public', resource: 'prescriptionPublic', action: 'read:any', attributes: '*' },
58-
{ role: 'pharmacist-public', resource: 'prescriptionPublic', action: 'update:any', attributes: '*' },
59-
60-
{ role: 'owner', resource: 'prescriptionPublic', action: 'delete:any', attributes: '*' },
61-
62-
{ role: 'auditor', resource: 'prescriptionPublic', action: 'read:any', attributes: '*' },
63-
64-
// patients
65-
{ role: 'professional', resource: 'patient', action: 'create:any', attributes: '*, !views' },
66-
{ role: 'professional', resource: 'patient', action: 'read:own', attributes: '*' },
67-
{ role: 'pharmacist', resource: 'patient', action: 'read:any', attributes: '*' },
68-
{ role: 'admin', resource: 'patient', action: 'update:any', attributes: '*' },
69-
70-
{ role: 'owner', resource: 'patient', action: 'delete:any', attributes: '*' },
71-
72-
// supplies
73-
{ role: 'professional', resource: 'supplies', action: 'read:any', attributes: '*' },
74-
{ role: 'pharmacist', resource: 'supplies', action: 'read:any', attributes: '*' },
75-
{ role: 'admin', resource: 'supplies', action: 'create:any', attributes: '*' },
76-
{ role: 'admin', resource: 'supplies', action: 'update:any', attributes: '*' },
77-
{ role: 'andes', resource: 'andesPrescription', action: 'create:any', attributes: '*' }
78-
];
79-
this.accessControl.setGrants(grantList);
80-
console.log('grants initialized');
81-
}
82-
83-
84-
public asyncForEach = async (array: any[], callback: Function) => {
85-
for (let index = 0; index < array.length; index++) {
86-
await callback(array[index], index, array);
6+
constructor(private accessControl: AccessControl = new AccessControl()) {
7+
this.init();
878
}
88-
}
9+
10+
public getAccessControl = (): AccessControl => {
11+
return this.accessControl;
12+
};
13+
14+
public init = async (): Promise<void> => {
15+
// const roles = await Role.find().populate({path: 'permissions', select: ['resource', 'action', 'attributes']} ).select('role');
16+
// await this.asyncForEach(roles, async (role: any) => {
17+
// this.accessControl.grant(role.role);
18+
// await this.asyncForEach(role.permissions, async (permission: any) => {
19+
// console.log('in permissions', permission);
20+
// });
21+
// });
22+
23+
let grantList = [
24+
// roles
25+
{ role: 'owner', resource: 'role', action: 'create:any', attributes: '*, !views' },
26+
{ role: 'owner', resource: 'role', action: 'read:any', attributes: '*' },
27+
{ role: 'owner', resource: 'role', action: 'update:any', attributes: '*, !views' },
28+
{ role: 'owner', resource: 'role', action: 'delete:any', attributes: '*' },
29+
30+
// users
31+
{ role: 'admin', resource: 'user', action: 'update:any', attributes: '*' },
32+
{ role: 'admin', resource: 'user', action: 'read:any', attributes: '*' },
33+
34+
{ role: 'auditor', resource: 'user', action: 'read:any', attributes: '*' },
35+
{ role: 'auditor', resource: 'user', action: 'update:any', attributes: '*' },
36+
37+
{ role: 'professional', resource: 'user', action: 'update:any', attributes: '*' },
38+
{ role: 'professional', resource: 'user', action: 'read:any', attributes: '*' },
39+
40+
{ role: 'professional-public', resource: 'user', action: 'update:any', attributes: '*' },
41+
{ role: 'professional-public', resource: 'user', action: 'read:any', attributes: '*' },
42+
43+
// prescriptions
44+
{ role: 'professional', resource: 'prescription', action: 'create:any', attributes: '*, !views' },
45+
{ role: 'professional', resource: 'prescription', action: 'read:own', attributes: '*' },
46+
{ role: 'professional', resource: 'prescription', action: 'read:any', attributes: '*' },
47+
{ role: 'professional', resource: 'prescription', action: 'update:own', attributes: '*' },
48+
{ role: 'professional', resource: 'prescription', action: 'delete:any', attributes: '*' },
49+
50+
{ role: 'pharmacist', resource: 'prescription', action: 'read:any', attributes: '*' },
51+
{ role: 'pharmacist', resource: 'prescription', action: 'update:any', attributes: '*, !views' },
52+
53+
{ role: 'owner', resource: 'prescription', action: 'delete:any', attributes: '*' },
54+
55+
{ role: 'auditor', resource: 'prescription', action: 'read:any', attributes: '*' },
56+
57+
// prescriptions public
58+
{ role: 'professional-public', resource: 'prescriptionPublic', action: 'create:any', attributes: '*, !views' },
59+
{ role: 'professional-public', resource: 'prescriptionPublic', action: 'read:own', attributes: '*' },
60+
{ role: 'professional-public', resource: 'prescriptionPublic', action: 'read:any', attributes: '*' },
61+
{ role: 'professional-public', resource: 'prescriptionPublic', action: 'update:own', attributes: '*' },
62+
{ role: 'professional-public', resource: 'prescriptionPublic', action: 'delete:any', attributes: '*' },
63+
64+
{ role: 'pharmacist-public', resource: 'prescriptionPublic', action: 'read:any', attributes: '*' },
65+
{ role: 'pharmacist-public', resource: 'prescriptionPublic', action: 'update:any', attributes: '*' },
66+
67+
{ role: 'owner', resource: 'prescriptionPublic', action: 'delete:any', attributes: '*' },
68+
69+
{ role: 'auditor', resource: 'prescriptionPublic', action: 'read:any', attributes: '*' },
70+
71+
// patients
72+
{ role: 'professional', resource: 'patient', action: 'create:any', attributes: '*, !views' },
73+
{ role: 'professional', resource: 'patient', action: 'read:own', attributes: '*' },
74+
{ role: 'pharmacist', resource: 'patient', action: 'read:any', attributes: '*' },
75+
{ role: 'admin', resource: 'patient', action: 'update:any', attributes: '*' },
76+
77+
{ role: 'owner', resource: 'patient', action: 'delete:any', attributes: '*' },
78+
79+
// supplies
80+
{ role: 'professional', resource: 'supplies', action: 'read:any', attributes: '*' },
81+
{ role: 'pharmacist', resource: 'supplies', action: 'read:any', attributes: '*' },
82+
{ role: 'admin', resource: 'supplies', action: 'create:any', attributes: '*' },
83+
{ role: 'admin', resource: 'supplies', action: 'update:any', attributes: '*' },
84+
{ role: 'andes', resource: 'andesPrescription', action: 'create:any', attributes: '*' }
85+
];
86+
this.accessControl.setGrants(grantList);
87+
console.log('grants initialized');
88+
};
89+
90+
91+
public asyncForEach = async (array: any[], callback: Function) => {
92+
for (let index = 0; index < array.length; index++) {
93+
await callback(array[index], index, array);
94+
}
95+
};
8996

9097
}
9198

0 commit comments

Comments
 (0)