use of won.protocol.util.CheapInsecureRandomString in project webofneeds by researchstudio-sat.
the class RestUserController method registerPrivateLinkAsUser.
/**
* registers user
*
* @param user registration data of a user
* @param errors
* @return ResponseEntity with Http Status Code
*/
@ResponseBody
@RequestMapping(value = "/private", method = RequestMethod.POST)
// TODO: move transactionality annotation into the service layer
@Transactional(propagation = Propagation.SUPPORTS)
public ResponseEntity registerPrivateLinkAsUser(@RequestBody UserPojo user, Errors errors) {
String privateLink = null;
try {
// TODO more secure random alphanum string
privateLink = (new CheapInsecureRandomString()).nextString(32);
user.setUsername(privateLink);
userRegisterValidator.validate(user, errors);
if (errors.hasErrors()) {
if (errors.getFieldErrorCount() > 0) {
// someone trying to go around js validation
return new ResponseEntity("\"" + errors.getAllErrors().get(0).getDefaultMessage() + "\"", HttpStatus.BAD_REQUEST);
} else {
// username is already in database
return new ResponseEntity("\"Cannot create user: name is already in use.\"", HttpStatus.CONFLICT);
}
} else {
registerUser(user.getUsername(), user.getPassword(), "ROLE_PRIVATE");
}
} catch (UserAlreadyExistsException e) {
// username is already in database
return new ResponseEntity("\"Cannot create user: name is already in use.\"", HttpStatus.CONFLICT);
}
return new ResponseEntity("\"" + privateLink + "\"", HttpStatus.CREATED);
}
Aggregations