Search in sources :

Example 1 with CheapInsecureRandomString

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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) CheapInsecureRandomString(won.protocol.util.CheapInsecureRandomString) CheapInsecureRandomString(won.protocol.util.CheapInsecureRandomString) UserAlreadyExistsException(won.owner.service.impl.UserAlreadyExistsException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ResponseEntity (org.springframework.http.ResponseEntity)1 Transactional (org.springframework.transaction.annotation.Transactional)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 UserAlreadyExistsException (won.owner.service.impl.UserAlreadyExistsException)1 CheapInsecureRandomString (won.protocol.util.CheapInsecureRandomString)1