Open Kilda Java Documentation
UserController.java
Go to the documentation of this file.
1 package org.usermanagement.controller;
2 
3 import java.util.List;
4 
5 import javax.servlet.http.HttpServletRequest;
6 
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;
26 
30 @RestController
31 @RequestMapping(path = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
32 public class UserController {
33 
34  private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
35 
36  @Autowired
37  private UserService userService;
38 
39  @Autowired
40  private RoleService roleService;
41 
42  @Autowired
43  private ServerContext serverContext;
44 
51  @ResponseStatus(HttpStatus.OK)
52  @RequestMapping(value = "/role/{role_id}", method = RequestMethod.GET)
54  public Role getUsersByRoleId(@PathVariable("role_id") final Long roleId) {
55  LOGGER.info("[getUsersByRoleId] (roleId: " + roleId + ")");
56  return roleService.getUserByRoleId(roleId);
57  }
58 
65  @ResponseStatus(HttpStatus.OK)
66  @RequestMapping(method = RequestMethod.POST)
68  public UserInfo createUser(@RequestBody final UserInfo userInfo) {
69  LOGGER.info("[createUser] (username: " + userInfo.getUsername() + ", name: " + userInfo.getName() + ")");
70  return userService.createUser(userInfo);
71  }
72 
80  @ResponseStatus(HttpStatus.OK)
81  @RequestMapping(value = "/{user_id}", method = RequestMethod.PUT)
83  public UserInfo updateUser(@RequestBody final UserInfo userInfo, @PathVariable("user_id") final Long userId) {
84  LOGGER.info("[updateUser] (id: " + userId + ")");
85  userInfo.setUserId(userId);
86  return userService.updateUser(userInfo, userId);
87  }
88 
94  @ResponseStatus(HttpStatus.OK)
95  @RequestMapping(method = RequestMethod.GET)
96  public List<UserInfo> getUsers() {
97  LOGGER.info("[getUsers]");
98  return userService.getAllUsers();
99  }
100 
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);
112  return userInfo;
113  }
114 
115 
121  @ResponseStatus(HttpStatus.NO_CONTENT)
122  @RequestMapping(value = "/{user_id}", method = RequestMethod.DELETE)
124  public void deleteUserById(@PathVariable("user_id") final Long userId) {
125  LOGGER.info("[deleteUserById] (id: " + userId + ")");
126  userService.deleteUserById(userId);
127  }
128 
136  @ResponseStatus(HttpStatus.OK)
137  @RequestMapping(value = "/role/{role_id}", method = RequestMethod.PUT)
139  public Role assignUsersByRoleId(@PathVariable("role_id") final Long roleId, @RequestBody final Role role) {
140  LOGGER.info("[assignUsersByRoleId] (roleId: " + roleId + ")");
141  return userService.assignUserByRoleId(roleId, role);
142  }
143 
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.");
158  }
159 
166  @ResponseStatus(HttpStatus.OK)
167  @RequestMapping(value = "/resetpassword/{id}", method = RequestMethod.GET)
169  public Object resetPassword(@PathVariable("id") final Long userId) {
170  LOGGER.info("[resetPassword] (userId: " + userId + ")");
171  userService.resetPassword(userId, false);
172  return new Message("Password has been sent to your EmailId");
173  }
174 
181  @ResponseStatus(HttpStatus.OK)
182  @RequestMapping(value = "/admin/resetpassword/{id}", method = RequestMethod.GET)
184  public Object resetPasswordByAdmin(@PathVariable("id") final Long userId) {
185  LOGGER.info("[resetPasswordByAdmin] (userId: " + userId + ")");
186  return userService.resetPassword(userId, true);
187  }
188 
195  @ResponseStatus(HttpStatus.OK)
196  @RequestMapping(value = "/reset2fa/{user_id}", method = RequestMethod.PUT)
198  public Message resetTwofa(@PathVariable("user_id") final Long userId) {
199  LOGGER.info("[resetTwofa] (userId: " + userId + ")");
200  userService.reset2fa(userId);
201  return new Message("2FA has been reset for the user.");
202  }
203 
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());
209  }
210 
211  @ResponseStatus(HttpStatus.OK)
212  @RequestMapping(value = "/settings", method = RequestMethod.PATCH)
213  public String saveOrUpdateSettings(@RequestBody final String data) {
214  UserInfo userInfo = new UserInfo();
215  userInfo.setData(data);
216  userInfo.setUserId(serverContext.getRequestContext().getUserId());
217  LOGGER.info("[saveOrUpdateSettings] (userId: " + userInfo.getUserId() + ")");
218  userService.saveOrUpdateSettings(userInfo);
219  return data;
220  }
221 
222  @ResponseStatus(HttpStatus.OK)
223  @RequestMapping(value = "/validateotp", method = RequestMethod.POST)
224  public boolean validateOtp(@RequestBody final UserInfo userInfo,
225  final HttpServletRequest request) {
226  LOGGER.info(
227  "[validateOTP] (userId: " + serverContext.getRequestContext().getUserId() + ")");
228  return userService.validateOTP(serverContext.getRequestContext().getUserId(),
229  userInfo.getCode());
230  }
231 }
UserInfo updateUser(@RequestBody final UserInfo userInfo, @PathVariable("user_id") final Long userId)
Message resetTwofa(@PathVariable("user_id") final Long userId)
value
Definition: nodes.py:62
Role getUsersByRoleId(@PathVariable("role_id") final Long roleId)
UserInfo createUser(@RequestBody final UserInfo userInfo)
Object resetPassword(@PathVariable("id") final Long userId)
void deleteUserById(@PathVariable("user_id") final Long userId)
Object resetPasswordByAdmin(@PathVariable("id") final Long userId)
Role assignUsersByRoleId(@PathVariable("role_id") final Long roleId, @RequestBody final Role role)
void setUserId(final Long userId)
Definition: UserInfo.java:79