Search in sources :

Example 11 with UserDetailsImpl

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);
    }
}
Also used : UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) Date(java.util.Date)

Example 12 with UserDetailsImpl

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);
    }
}
Also used : UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) JSONUtil(gemma.gsec.util.JSONUtil) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with UserDetailsImpl

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;
}
Also used : UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) Secured(org.springframework.security.access.annotation.Secured)

Example 14 with UserDetailsImpl

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;
}
Also used : UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) UserDetails(org.springframework.security.core.userdetails.UserDetails) User(gemma.gsec.model.User)

Example 15 with UserDetailsImpl

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());
}
Also used : UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) User(gemma.gsec.model.User) Secured(org.springframework.security.access.annotation.Secured) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UserDetailsImpl (gemma.gsec.authentication.UserDetailsImpl)15 Date (java.util.Date)7 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)5 User (gemma.gsec.model.User)4 Before (org.junit.Before)4 Secured (org.springframework.security.access.annotation.Secured)3 JSONUtil (gemma.gsec.util.JSONUtil)2 Test (org.junit.Test)2 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 Transactional (org.springframework.transaction.annotation.Transactional)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 BaseSpringContextTest (ubic.gemma.core.testing.BaseSpringContextTest)2 UserExistsException (gemma.gsec.authentication.UserExistsException)1 UserGroup (gemma.gsec.model.UserGroup)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 ExecutorService (java.util.concurrent.ExecutorService)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 ExecutingTask (ubic.gemma.core.job.executor.common.ExecutingTask)1