-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser-management-controller.ts
More file actions
197 lines (162 loc) · 9.58 KB
/
user-management-controller.ts
File metadata and controls
197 lines (162 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import express, { NextFunction, Request } from "express";
import validationMiddleware from "../middleware/dto-validation-middleware";
import { RolesReqDto } from "../model/dto/roles-req-dto";
import { RegisterUserDto } from "../model/dto/register-user-dto";
import { BadRequest, Ok } from "../model/http/http-responses";
import userManagementServiceInstance from "../service/user-management-service";
import { IController } from "./interface/controller-interface";
import authorizationMiddleware from "../middleware/authorization-middleware";
import { Role } from "../constants/role-constants";
import { LoginDto } from "../model/dto/login-dto";
import HttpException from "../exceptions/http/http-base-exception";
import { Utility } from "../utility/utility";
import jwt_decode from 'jwt-decode';
import { ResetCredentialsDto } from "../model/dto/reset-credentials-dto";
class UserManagementController implements IController {
public path = '';
public router = express.Router();
constructor() {
this.intializeRoutes();
}
public intializeRoutes() {
this.router.post(`${this.path}/api/v1/register`, validationMiddleware(RegisterUserDto), this.registerUser);
this.router.post(`${this.path}/api/v1/permission`, authorizationMiddleware([Role.POC, Role.TDEI_ADMIN], true), validationMiddleware(RolesReqDto), this.updatePermissions);
this.router.put(`${this.path}/api/v1/permission/revoke`, authorizationMiddleware([Role.POC, Role.TDEI_ADMIN], true), validationMiddleware(RolesReqDto), this.revokePermissions);
this.router.get(`${this.path}/api/v1/roles`, authorizationMiddleware([Role.POC, Role.TDEI_ADMIN]), this.getRoles);
this.router.get(`${this.path}/api/v1/project-group-roles/:userId`, authorizationMiddleware([], false, true), this.projectGroupRoles);
this.router.post(`${this.path}/api/v1/authenticate`, validationMiddleware(LoginDto), this.login);
this.router.post(`${this.path}/api/v1/refresh-token`, this.refreshToken);
this.router.get(`${this.path}/api/v1/user-profile`, authorizationMiddleware([]), this.getUserProfile);
this.router.post(`${this.path}/api/v1/reset-credentials`, authorizationMiddleware([]), validationMiddleware(ResetCredentialsDto), this.resetCredentials);
this.router.get(`${this.path}/api/v1/users/download`, authorizationMiddleware([Role.TDEI_ADMIN]), this.downloadUsers);
}
public downloadUsers = async (request: Request, response: express.Response, next: NextFunction) => {
return userManagementServiceInstance.downloadUsers().then((users) => {
response.setHeader('Content-Type', 'text/csv');
response.setHeader('Content-Disposition', 'attachment; filename=tdei-users.csv');
response.status(200);
response.send(users);
}).catch((error: any) => {
let errorMessage = "Error fetching the tdei users";
Utility.handleError(response, next, error, errorMessage);
});
}
public resetCredentials = async (request: Request, response: express.Response, next: NextFunction) => {
try {
let reqBody = ResetCredentialsDto.from(request.body);
let authToken = Utility.extractToken(request);
var decoded: any = authToken != null ? jwt_decode(authToken) : undefined;
var isTdeiAdmin = false;
//Check if the user is TDEI Admin, "tdei-admin" is defined in Keycloak roles
if (decoded && decoded.realm_access.roles.includes("tdei-admin")) isTdeiAdmin = true;
//Check if the user is trying to reset the credentials of another user
if (!isTdeiAdmin && decoded && reqBody && decoded.preferred_username != reqBody.username) throw new HttpException(403, "Not authorized to reset the credentials");
let result = await userManagementServiceInstance.resetCredentials(reqBody);
return Ok(response, result);
}
catch (error) {
let errorMessage = "Error resetting the user credentials";
Utility.handleError(response, next, error, errorMessage);
}
}
public refreshToken = async (request: Request, response: express.Response, next: NextFunction) => {
if (request.headers.refresh_token == undefined || request.headers.refresh_token == "")
BadRequest(response);
let token = request.headers.refresh_token?.toString();
return userManagementServiceInstance.refreshToken(token ?? "").then((token) => {
Ok(response, token)
}).catch((error: Error) => {
let errorMessage = "Error refreshing the user token";
Utility.handleError(response, next, error, errorMessage);
});
}
public getUserProfile = async (request: Request, response: express.Response, next: NextFunction) => {
try {
let user_name = request.query.user_name;
if (user_name == undefined || user_name == null) throw new HttpException(400, "user_name query param missing");
return userManagementServiceInstance.getUserProfile(user_name as string).then((result) => {
Ok(response, result);
}).catch((error: any) => {
let errorMessage = "Error fetching the user profile";
Utility.handleError(response, next, error, errorMessage);
});
} catch (error) {
let errorMessage = "Error fetching the user profile";
Utility.handleError(response, next, error, errorMessage);
}
}
public projectGroupRoles = async (request: Request, response: express.Response, next: NextFunction) => {
try {
let authToken = Utility.extractToken(request);
var decoded: any = authToken != null ? jwt_decode(authToken) : undefined;
let userId = request.params.userId;
if (userId == undefined || userId == null) throw new HttpException(400, "UserId missing");
if (decoded && decoded.sub != userId) throw new HttpException(403, "Not authorized.");
let page_no = Number.parseInt(request.query.page_no?.toString() ?? "1");
let page_size = Number.parseInt(request.query.page_size?.toString() ?? "20");
//TODO:: Hot fix need to be removed later. Fixing default page size to 20 if page size is less than 20
if (page_size < 20) page_size = 20;
let searchText = request.query.searchText?.toString() ?? "";
return userManagementServiceInstance.getUserProjectGroupsWithRoles(userId.toString(), page_no, page_size, searchText).then((result) => {
Ok(response, result);
}).catch((error: any) => {
let errorMessage = "Error fetching the user project group & roles";
Utility.handleError(response, next, error, errorMessage);
});
} catch (error) {
let errorMessage = "Error fetching the user project group & roles";
Utility.handleError(response, next, error, errorMessage);
}
}
public login = async (request: Request, response: express.Response, next: NextFunction) => {
let loginBody = LoginDto.from(request.body);
return userManagementServiceInstance.login(loginBody).then((token) => {
Ok(response, token)
}).catch((error: any) => {
let errorMessage = "Error authenticating the user";
Utility.handleError(response, next, error, errorMessage);
});
}
public getRoles = async (request: Request, response: express.Response, next: NextFunction) => {
return userManagementServiceInstance.getRoles().then((roles) => {
Ok(response, { data: roles });
}).catch((error: any) => {
let errorMessage = "Error fetching the roles";
Utility.handleError(response, next, error, errorMessage);
});
}
public registerUser = async (request: Request, response: express.Response, next: NextFunction) => {
//Transform the body to DTO
let registerUserBody = new RegisterUserDto(request.body);
//Call service to register the user
return userManagementServiceInstance.registerUser(registerUserBody).catch((error: any) => {
let errorMessage = "Error registering the user";
Utility.handleError(response, next, error, errorMessage);
}).then((user) => {
Ok(response, { data: user });
});
}
public updatePermissions = async (request: Request, response: express.Response, next: NextFunction) => {
//Transform the body to DTO
let permissonObj = new RolesReqDto(request.body);
return userManagementServiceInstance.updatePermissions(permissonObj, request.userId)
.catch((error: any) => {
let errorMessage = "Error assigning the permissions to the user";
Utility.handleError(response, next, error, errorMessage);
}).then((flag) => {
Ok(response, { data: "Successful!" });
});
}
public revokePermissions = async (request: Request, response: express.Response, next: NextFunction) => {
//Transform the body to DTO
let permissonObj = new RolesReqDto(request.body);
return userManagementServiceInstance.revokeUserPermissions(permissonObj, request.userId).catch((error: any) => {
let errorMessage = 'Error revoking the permissions of the user';
Utility.handleError(response, next, error, errorMessage);
}).then((flag) => {
Ok(response, { data: "Successful!" });
});
}
}
const userManagementController = new UserManagementController();
export default userManagementController;