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;
}
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);
}
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();
}
}
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");
}
}
}
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");
}
}
Aggregations