Search in sources :

Example 11 with UserAtom

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

the class RestUserController method getUserSettings.

@ResponseBody
@RequestMapping(value = "/settings", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
public List<UserSettingsPojo> getUserSettings(@RequestParam(name = "uri", required = false) String uri) {
    logger.debug("processing request to /settings");
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    // cannot use user object from context since hw doesn't know about created in
    // this session atom,
    // therefore, we have to retrieve the user object from the user repository
    User user = userService.getByUsername(username);
    List<UserSettingsPojo> userSettings = new ArrayList<UserSettingsPojo>();
    if (uri != null) {
        URI atomUri = null;
        try {
            atomUri = new URI(uri);
            for (UserAtom userAtom : user.getUserAtoms()) {
                if (userAtom.getUri().equals(atomUri)) {
                    userSettings.add(new UserSettingsPojo(user.getUsername(), user.getEmail(), userAtom.getUri(), userAtom.isMatches(), userAtom.isRequests(), userAtom.isConversations()));
                    break;
                }
            }
        } catch (URISyntaxException e) {
            // TODO error response
            logger.warn(uri + " atom uri problem", e);
        }
    } else {
        // No specific uri requested => all userSettings
        for (UserAtom userAtom : user.getUserAtoms()) {
            if (userAtom.getState() != AtomState.DELETED) {
                userSettings.add(new UserSettingsPojo(user.getUsername(), user.getEmail(), userAtom.getUri(), userAtom.isMatches(), userAtom.isRequests(), userAtom.isConversations()));
            }
        }
    }
    return userSettings;
}
Also used : User(won.owner.model.User) UserAtom(won.owner.model.UserAtom) 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 UserAtom

use of won.owner.model.UserAtom 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) {
    logger.debug("processing POST request to /settings");
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    // cannot use user object from context since hw doesn't know about created in
    // this session atom,
    // therefore, we have to retrieve the user object from the user repository
    User user = userService.getByUsername(username);
    if (!userSettingsPojo.getUsername().isEmpty() && !user.getUsername().equals(userSettingsPojo.getUsername())) {
        return generateStatusResponse(RestStatusResponse.USERNAME_MISMATCH);
    }
    if (!userSettingsPojo.getEmail().isEmpty() && user.getEmail() == null) {
        // TODO validate email server-side?
        // set email:
        user.setEmail(userSettingsPojo.getEmail());
        userService.save(user);
    } else if (!userSettingsPojo.getEmail().isEmpty() && !user.getEmail().equals(userSettingsPojo.getEmail())) {
        // TODO validate email server-side?
        // change email:
        user.setEmail(userSettingsPojo.getEmail());
        userService.save(user);
        logger.info("change email requested - email changed");
    }
    // retrieve UserAtom
    for (UserAtom userAtom : user.getUserAtoms()) {
        if (userAtom.getUri().equals(userSettingsPojo.getAtomUri())) {
            userAtom.setMatches(userSettingsPojo.isNotifyMatches());
            userAtom.setRequests(userSettingsPojo.isNotifyRequests());
            userAtom.setConversations(userSettingsPojo.isNotifyConversations());
            userAtomRepository.save(userAtom);
            break;
        }
    }
    return generateStatusResponse(RestStatusResponse.SETTINGS_CREATED);
}
Also used : User(won.owner.model.User) UserAtom(won.owner.model.UserAtom) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with UserAtom

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

the class UserAtomService method setAtomDeleted.

public void setAtomDeleted(URI atomURI) {
    // Get the atom from owner application db
    UserAtom userAtom = userAtomRepository.findByAtomUri(atomURI);
    userAtom.setState(AtomState.DELETED);
    userAtomRepository.save(userAtom);
}
Also used : UserAtom(won.owner.model.UserAtom)

Example 14 with UserAtom

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

the class UserAtomService method updateAtomUriState.

private void updateAtomUriState(final WonMessage wonMessage, AtomState newState) {
    URI atomUri = getOwnedAtomURI(wonMessage);
    UserAtom userAtom = userAtomRepository.findByAtomUri(atomUri);
    userAtom.setState(newState);
    // reload the user so we can save it
    // (the user object we get from getUserForSession is detached)
    userAtomRepository.save(userAtom);
}
Also used : UserAtom(won.owner.model.UserAtom) URI(java.net.URI)

Example 15 with UserAtom

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

the class OwnerPersistenceTest method createUserWithAtom.

private void createUserWithAtom(URI atomUri, String email) {
    UserAtom a = new UserAtom();
    a.setUri(atomUri);
    a = userAtomRepository.save(a);
    String password = "password";
    String role = "SOMEROLE";
    PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    User user = new User(email, passwordEncoder.encode(password), role);
    user.setEmail(email);
    // transfer only available when flag is set therefore we can just set
    user.setAcceptedTermsOfService(true);
    // this
    // to true (i think)
    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);
    user = userRepository.save(user);
    user.addUserAtom(a);
    user = userRepository.save(user);
}
Also used : UserAtom(won.owner.model.UserAtom) User(won.owner.model.User) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) KeystoreHolder(won.owner.model.KeystoreHolder) KeystorePasswordHolder(won.owner.model.KeystorePasswordHolder) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Aggregations

UserAtom (won.owner.model.UserAtom)15 URI (java.net.URI)9 User (won.owner.model.User)8 StringUtils (org.apache.commons.lang.StringUtils)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 Transactional (org.springframework.transaction.annotation.Transactional)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 MethodHandles (java.lang.invoke.MethodHandles)2 URISyntaxException (java.net.URISyntaxException)2 MessageDigest (java.security.MessageDigest)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 ObjectNode (org.codehaus.jackson.node.ObjectNode)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2