use of gemma.gsec.authentication.UserDetailsImpl in project Gemma by PavlidisLab.
the class UserListControllerImpl method saveUser.
@Override
public void saveUser(UserValueObject user) {
String userName = user.getUserName();
gemma.gsec.model.User u = userManager.findByUserName(userName);
UserDetailsImpl userDetails;
boolean newUser = false;
if (u == null) {
userDetails = new UserDetailsImpl(passwordEncoder.encodePassword(user.getPassword(), user.getUserName()), user.getUserName(), false, null, user.getEmail(), userManager.generateSignupToken(user.getUserName()), new Date());
} else {
u.setEmail(user.getEmail());
u.setEnabled(user.isEnabled());
userDetails = new UserDetailsImpl(u);
}
if (newUser) {
userManager.createUser(userDetails);
} else {
userManager.updateUser(userDetails);
}
}
use of gemma.gsec.authentication.UserDetailsImpl in project Gemma by PavlidisLab.
the class SignupController method signup.
/*
* Used when a user signs themselves up.
*/
@RequestMapping(value = "/signup.html", method = RequestMethod.POST)
public void signup(HttpServletRequest request, HttpServletResponse response) throws Exception {
JSONUtil jsonUtil = new JSONUtil(request, response);
String jsonText = null;
String password = request.getParameter("password");
String cPass = request.getParameter("passwordConfirm");
if (reCaptcha.isPrivateKeySet()) {
if (!reCaptcha.validateRequest(request).isValid()) {
jsonText = "{success:false,message:'Captcha was not entered correctly.'}";
jsonUtil.writeToResponse(jsonText);
return;
}
} else {
log.warn("No recaptcha private key is configured, skipping validation");
}
if (password.length() < UserFormMultiActionController.MIN_PASSWORD_LENGTH || !password.equals(cPass)) {
jsonText = "{success:false,message:'Password was not valid or didn't match'}";
jsonUtil.writeToResponse(jsonText);
return;
}
String username = request.getParameter("username");
String encodedPassword = passwordEncoder.encodePassword(password, username);
String email = request.getParameter("email");
String cEmail = request.getParameter("emailConfirm");
/*
* Validate that it is a valid email....this regex adapted from extjs; a word possibly containing '-', '+' or
* '.', following by '@', followed by up to 5 chunks separated by '.', finally a 2-4 letter alphabetic suffix.
*/
if (!email.matches("^(\\w+)([-+.][\\w]+)*@(\\w[-\\w]*\\.){1,5}([A-Za-z]){2,4}$") || !email.equals(cEmail)) {
jsonText = "{success:false,message:'Email was not valid or didn't match'}";
jsonUtil.writeToResponse(jsonText);
return;
}
String key = userManager.generateSignupToken(username);
Date now = new Date();
UserDetailsImpl u = new UserDetailsImpl(encodedPassword, username, false, null, email, key, now);
try {
userManager.createUser(u);
sendSignupConfirmationEmail(request, u);
jsonText = "{success:true}";
} catch (Exception e) {
/*
* Most common cause: user exists already.
*/
log.error(e, e);
jsonText = jsonUtil.getJSONErrorMessage(e);
log.info(jsonText);
} finally {
jsonUtil.writeToResponse(jsonText);
}
}
use of gemma.gsec.authentication.UserDetailsImpl in project Gemma by PavlidisLab.
the class UserManagerImpl method validateSignupToken.
@Override
@Secured({ "IS_AUTHENTICATED_ANONYMOUSLY", "RUN_AS_ADMIN" })
public boolean validateSignupToken(String username, String key) {
UserDetailsImpl u = (UserDetailsImpl) this.loadUserByUsername(username);
if (u.isEnabled()) {
logger.warn("User is already enabled, skipping token validation");
return true;
}
String storedTok = u.getSignupToken();
Date storedDate = u.getSignupTokenDatestamp();
if (storedTok == null || storedDate == null) {
throw new IllegalArgumentException("User does not have a token");
}
Date oneWeekAgo = DateUtils.addWeeks(new Date(), -2);
if (!storedTok.equals(key) || storedDate.before(oneWeekAgo)) {
return false;
}
u.setEnabled(true);
this.updateUser(u);
return true;
}
use of gemma.gsec.authentication.UserDetailsImpl in project Gemma by PavlidisLab.
the class UserManagerImpl method loadUsersByUsername.
protected List<UserDetails> loadUsersByUsername(String username) {
List<UserDetails> result = new ArrayList<>();
User u = this.loadUser(username);
UserDetails ud = new UserDetailsImpl(u);
result.add(ud);
return result;
}
use of gemma.gsec.authentication.UserDetailsImpl in project Gemma by PavlidisLab.
the class UserManagerImpl method updateUser.
@Override
@Secured({ "IS_AUTHENTICATED_ANONYMOUSLY", "RUN_AS_ADMIN" })
@Transactional
public void updateUser(UserDetails user) {
String username = user.getUsername();
User u = userService.findByUserName(username);
if (u == null)
throw new IllegalArgumentException("No user could be loaded with name=" + user);
u.setPassword(user.getPassword());
u.setEnabled(user.isEnabled());
if (user instanceof UserDetailsImpl) {
u.setEmail(((UserDetailsImpl) user).getEmail());
}
userService.update(u);
userCache.removeUserFromCache(user.getUsername());
}
Aggregations