Search in sources :

Example 11 with User

use of won.owner.model.User in project webofneeds by researchstudio-sat.

the class RestUserController method getUserSettings.

@ResponseBody
@RequestMapping(value = "/settings", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
public // TODO: move transactionality annotation into the service layer
UserSettingsPojo getUserSettings(@RequestParam("uri") String uri) {
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    // cannot use user object from context since hw doesn't know about created in this session need,
    // therefore, we have to retrieve the user object from the user repository
    User user = userRepository.findByUsername(username);
    UserSettingsPojo userSettingsPojo = new UserSettingsPojo(user.getUsername(), user.getEmail());
    URI needUri = null;
    try {
        needUri = new URI(uri);
        userSettingsPojo.setNeedUri(uri);
        for (UserNeed userNeed : user.getUserNeeds()) {
            if (userNeed.getUri().equals(needUri)) {
                userSettingsPojo.setNotify(userNeed.isMatches(), userNeed.isRequests(), userNeed.isConversations());
                // userSettingsPojo.setEmail(user.getEmail());
                break;
            }
        }
    } catch (URISyntaxException e) {
        // TODO error response
        logger.warn(uri + " need uri problem", e);
    }
    return userSettingsPojo;
}
Also used : User(won.owner.model.User) UserNeed(won.owner.model.UserNeed) CheapInsecureRandomString(won.protocol.util.CheapInsecureRandomString) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) UserSettingsPojo(won.owner.pojo.UserSettingsPojo) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with User

use of won.owner.model.User in project webofneeds by researchstudio-sat.

the class RestUserController method setUserSettings.

@ResponseBody
@RequestMapping(value = "/settings", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
// TODO: move transactionality annotation into the service layer
@Transactional(propagation = Propagation.SUPPORTS)
public ResponseEntity setUserSettings(@RequestBody UserSettingsPojo userSettingsPojo) {
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    // cannot use user object from context since hw doesn't know about created in this session need,
    // therefore, we have to retrieve the user object from the user repository
    User user = userRepository.findByUsername(username);
    if (!user.getUsername().equals(userSettingsPojo.getUsername())) {
        logger.warn("user name wrong");
        return new ResponseEntity("\"user name problem\"", HttpStatus.BAD_REQUEST);
    }
    if (user.getEmail() == null) {
        // TODO validate email server-side?
        // set email:
        user.setEmail(userSettingsPojo.getEmail());
        userRepository.save(user);
    } else if (!user.getEmail().equals(userSettingsPojo.getEmail())) {
        // TODO validate email server-side?
        // change email:
        user.setEmail(userSettingsPojo.getEmail());
        userRepository.save(user);
        logger.info("change email requested - email changed");
    }
    // retrieve UserNeed
    URI needUri = null;
    try {
        needUri = new URI(userSettingsPojo.getNeedUri());
        for (UserNeed userNeed : user.getUserNeeds()) {
            if (userNeed.getUri().equals(needUri)) {
                userNeed.setMatches(userSettingsPojo.isNotifyMatches());
                userNeed.setRequests(userSettingsPojo.isNotifyRequests());
                userNeed.setConversations(userSettingsPojo.isNotifyConversations());
                userNeedRepository.save(userNeed);
                break;
            }
        }
    } catch (URISyntaxException e) {
        logger.warn(userSettingsPojo.getNeedUri() + " need uri problem.", e);
        return new ResponseEntity("\"" + userSettingsPojo.getNeedUri() + " need uri problem.\"", HttpStatus.BAD_REQUEST);
    }
    return new ResponseEntity("\"Settings created\"", HttpStatus.CREATED);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) User(won.owner.model.User) UserNeed(won.owner.model.UserNeed) CheapInsecureRandomString(won.protocol.util.CheapInsecureRandomString) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with User

use of won.owner.model.User in project webofneeds by researchstudio-sat.

the class RestUserController method registerUser.

/**
 * Registers the specified user with password and an opional role.
 * Assumes values have already been checked for syntactic validity.
 * @param email
 * @param password
 * @param role
 * @throws UserAlreadyExistsException
 */
private void registerUser(String email, String password, String role) throws UserAlreadyExistsException {
    User user = userRepository.findByUsername(email);
    if (user != null) {
        throw new UserAlreadyExistsException();
    }
    try {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        user = new User(email, passwordEncoder.encode(password), role);
        user.setEmail(email);
        KeystorePasswordHolder keystorePassword = new KeystorePasswordHolder();
        // generate a password for the keystore and save it in the database, encrypted with a symmetric key
        // derived from the user's password
        keystorePassword.setPassword(KeystorePasswordUtils.generatePassword(KeystorePasswordUtils.KEYSTORE_PASSWORD_BYTES), password);
        // keystorePassword = keystorePasswordRepository.save(keystorePassword);
        // generate the keystore for the user
        KeystoreHolder keystoreHolder = new KeystoreHolder();
        try {
            // create the keystore if it doesnt exist yet
            keystoreHolder.getKeystore(keystorePassword.getPassword(password));
        } catch (Exception e) {
            throw new IllegalStateException("could not create keystore for user " + email);
        }
        // keystoreHolder = keystoreHolderRepository.save(keystoreHolder);
        user.setKeystorePasswordHolder(keystorePassword);
        user.setKeystoreHolder(keystoreHolder);
        userRepository.save(user);
    } catch (DataIntegrityViolationException e) {
        // username is already in database
        throw new UserAlreadyExistsException();
    }
}
Also used : User(won.owner.model.User) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) KeystoreHolder(won.owner.model.KeystoreHolder) UserAlreadyExistsException(won.owner.service.impl.UserAlreadyExistsException) KeystorePasswordHolder(won.owner.model.KeystorePasswordHolder) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) URISyntaxException(java.net.URISyntaxException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) UserAlreadyExistsException(won.owner.service.impl.UserAlreadyExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 14 with User

use of won.owner.model.User in project webofneeds by researchstudio-sat.

the class UserRegisterValidator method validate.

@Override
public void validate(final Object target, final Errors errors) {
    UserPojo user = (UserPojo) target;
    validator.validate(target, errors);
    if (user.getPassword().length() < 6) {
        errors.rejectValue("password", "passwordTooShort", "Password needs to be at least 6 Characters long");
    }
    if (errors.getFieldError("username") != null) {
        User userInDb = (User) wonUserDetailService.loadUserByUsername(user.getUsername());
        if (userInDb != null) {
            errors.reject("userIsAlreadyInDb", "Username already exists, please choose a different one");
        }
    }
}
Also used : User(won.owner.model.User) UserPojo(won.owner.pojo.UserPojo)

Example 15 with User

use of won.owner.model.User in project webofneeds by researchstudio-sat.

the class WonWebSocketHandler method afterConnectionEstablished.

@Override
public void afterConnectionEstablished(final WebSocketSession session) throws Exception {
    super.afterConnectionEstablished(session);
    // remember which user or (if not logged in) which needUri the session is bound
    // to
    User user = getUserForSession(session);
    if (user != null) {
        logger.debug("connection established, binding session to user {}", user.getId());
        this.webSocketSessionService.addMapping(user, session);
    } else {
        logger.debug("connection established, but no user found in session to bind to");
    }
}
Also used : User(won.owner.model.User)

Aggregations

User (won.owner.model.User)18 URI (java.net.URI)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)7 Transactional (org.springframework.transaction.annotation.Transactional)6 UserNeed (won.owner.model.UserNeed)5 URISyntaxException (java.net.URISyntaxException)4 Draft (won.owner.model.Draft)3 KeystoreHolder (won.owner.model.KeystoreHolder)3 KeystorePasswordHolder (won.owner.model.KeystorePasswordHolder)3 CheapInsecureRandomString (won.protocol.util.CheapInsecureRandomString)3 KeyStore (java.security.KeyStore)2 ArrayList (java.util.ArrayList)2 ResponseEntity (org.springframework.http.ResponseEntity)2 CreateDraftPojo (won.owner.pojo.CreateDraftPojo)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Transactional (javax.transaction.Transactional)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1