1 package org.usermanagement.controller;
5 import javax.servlet.http.HttpServletRequest;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.http.HttpStatus;
14 import org.springframework.http.MediaType;
15 import org.springframework.web.bind.annotation.PathVariable;
16 import org.springframework.web.bind.annotation.RequestBody;
17 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.RequestMethod;
19 import org.springframework.web.bind.annotation.ResponseStatus;
20 import org.springframework.web.bind.annotation.RestController;
31 @RequestMapping(
path =
"/user", produces = MediaType.APPLICATION_JSON_VALUE)
34 private static final Logger LOGGER = LoggerFactory.getLogger(
UserController.class);
51 @ResponseStatus(HttpStatus.OK)
52 @RequestMapping(
value =
"/role/{role_id}", method = RequestMethod.GET)
55 LOGGER.info(
"[getUsersByRoleId] (roleId: " + roleId +
")");
56 return roleService.getUserByRoleId(roleId);
65 @ResponseStatus(HttpStatus.OK)
66 @RequestMapping(method = RequestMethod.POST)
69 LOGGER.info(
"[createUser] (username: " + userInfo.getUsername() +
", name: " + userInfo.getName() +
")");
70 return userService.createUser(userInfo);
80 @ResponseStatus(HttpStatus.OK)
81 @RequestMapping(
value =
"/{user_id}", method = RequestMethod.PUT)
84 LOGGER.info(
"[updateUser] (id: " + userId +
")");
86 return userService.updateUser(userInfo, userId);
94 @ResponseStatus(HttpStatus.OK)
95 @RequestMapping(method = RequestMethod.GET)
97 LOGGER.info(
"[getUsers]");
98 return userService.getAllUsers();
107 @ResponseStatus(HttpStatus.OK)
108 @RequestMapping(
value =
"/{user_id}", method = RequestMethod.GET)
109 public
UserInfo getUserById(@PathVariable("user_id") final Long userId) {
110 LOGGER.info(
"[getUserById] (id: " + userId +
")");
111 UserInfo userInfo = userService.getUserById(userId);
121 @ResponseStatus(HttpStatus.NO_CONTENT)
122 @RequestMapping(
value =
"/{user_id}", method = RequestMethod.DELETE)
125 LOGGER.info(
"[deleteUserById] (id: " + userId +
")");
126 userService.deleteUserById(userId);
136 @ResponseStatus(HttpStatus.OK)
137 @RequestMapping(
value =
"/role/{role_id}", method = RequestMethod.PUT)
140 LOGGER.info(
"[assignUsersByRoleId] (roleId: " + roleId +
")");
141 return userService.assignUserByRoleId(roleId, role);
152 @ResponseStatus(HttpStatus.OK)
153 @RequestMapping(
value =
"/changePassword/{user_id}", method = RequestMethod.PUT)
154 public
Message changePassword(@RequestBody final
UserInfo userInfo, @PathVariable("user_id") final Long userId) {
155 LOGGER.info(
"[changePassword] (userId: " + userId +
")");
156 userService.changePassword(userInfo, userId);
157 return new Message(
"Password has been changed successfully.");
166 @ResponseStatus(HttpStatus.OK)
167 @RequestMapping(
value =
"/resetpassword/{id}", method = RequestMethod.GET)
170 LOGGER.info(
"[resetPassword] (userId: " + userId +
")");
171 userService.resetPassword(userId,
false);
172 return new Message(
"Password has been sent to your EmailId");
181 @ResponseStatus(HttpStatus.OK)
182 @RequestMapping(
value =
"/admin/resetpassword/{id}", method = RequestMethod.GET)
185 LOGGER.info(
"[resetPasswordByAdmin] (userId: " + userId +
")");
186 return userService.resetPassword(userId,
true);
195 @ResponseStatus(HttpStatus.OK)
196 @RequestMapping(
value =
"/reset2fa/{user_id}", method = RequestMethod.PUT)
199 LOGGER.info(
"[resetTwofa] (userId: " + userId +
")");
200 userService.reset2fa(userId);
201 return new Message(
"2FA has been reset for the user.");
204 @ResponseStatus(HttpStatus.OK)
205 @RequestMapping(
value =
"/settings", method = RequestMethod.GET)
206 public String getUserSettings() {
207 LOGGER.info(
"[getUserSettings] (userId: " + serverContext.getRequestContext().getUserId() +
")");
208 return userService.getUserSettings(serverContext.getRequestContext().getUserId());
211 @ResponseStatus(HttpStatus.OK)
212 @RequestMapping(
value =
"/settings", method = RequestMethod.PATCH)
213 public String saveOrUpdateSettings(@RequestBody final String
data) {
216 userInfo.
setUserId(serverContext.getRequestContext().getUserId());
217 LOGGER.info(
"[saveOrUpdateSettings] (userId: " + userInfo.
getUserId() +
")");
218 userService.saveOrUpdateSettings(userInfo);
222 @ResponseStatus(HttpStatus.OK)
223 @RequestMapping(
value =
"/validateotp", method = RequestMethod.POST)
224 public
boolean validateOtp(@RequestBody final
UserInfo userInfo,
225 final HttpServletRequest request) {
227 "[validateOTP] (userId: " + serverContext.getRequestContext().getUserId() +
")");
228 return userService.validateOTP(serverContext.getRequestContext().getUserId(),
UserInfo updateUser(@RequestBody final UserInfo userInfo, @PathVariable("user_id") final Long userId)
Message resetTwofa(@PathVariable("user_id") final Long userId)
static final String UM_USER_EDIT
static final String UM_USER_RESET2FA
Role getUsersByRoleId(@PathVariable("role_id") final Long roleId)
UserInfo createUser(@RequestBody final UserInfo userInfo)
static final String UM_USER_DELETE
Object resetPassword(@PathVariable("id") final Long userId)
static final String UM_ASSIGN_ROLE_TO_USERS
void setData(String data)
static final String UM_ROLE_VIEW_USERS
void deleteUserById(@PathVariable("user_id") final Long userId)
Object resetPasswordByAdmin(@PathVariable("id") final Long userId)
static final String UM_USER_RESET_ADMIN
static final String UM_USER_ADD
static final String UM_USER_RESET
Role assignUsersByRoleId(@PathVariable("role_id") final Long roleId, @RequestBody final Role role)
void setUserId(final Long userId)