Skip to content

Instantly share code, notes, and snippets.

@BioQwer
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BioQwer/c1991f33e1bfc1beb089 to your computer and use it in GitHub Desktop.
Save BioQwer/c1991f33e1bfc1beb089 to your computer and use it in GitHub Desktop.
public User getCurrentUser(Principal principal) {
System.out.println("principal.getName() = " + principal.getName());
System.out.println(" userService.getByLogin(principal.getName()) = " + userService.getByLogin(principal.getName()));
return userService.getByLogin(principal.getName());
}
/**
* Edit user method
* !! if Change user.login you must to re singIn on client !!
*/
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public Object editUser(Principal principal, @RequestBody User user, HttpServletResponse response) {
response.setStatus(400);
// Check user for situation when login or email is busy
User checker = userService.getByEmail(user.getEmail());
if ((checker != null) && (checker.getUserId() != user.getUserId())) {
return new Constraint("email", user.getEmail(), "Email " + user.getEmail() + " is busy");
} else {
checker = userService.getByLogin(user.getLogin());
if ((checker != null) && (checker.getUserId() != user.getUserId())) {
return new Constraint("login", user.getLogin(), "Login " + user.getLogin() + " is busy");
}
}
//try to edit user
try {
response.setStatus(200);
if (user.getUserId() == getCurrentUser(principal).getUserId())
return userService.editUser(user);
else
throw new BadRequestException();
} catch (ConstraintViolationException e) {
e.printStackTrace();
response.setStatus(400);
return getInCorrectValues(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment