Project

General

Profile

« Previous | Next » 

Revision 87cf034d

Added by Andreas Kohlbecker over 2 years ago

ref #6161 de-duplicating code for password encoding

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/UserService.java
134 134

  
135 135
            // check if old password is valid
136 136
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(), oldPassword));
137

  
138
            // make new password and set it
139
            Object salt = this.saltSource.getSalt(user);
140
            String password = passwordEncoder.encodePassword(newPassword, salt);
141
            user.setPassword(password);
142
            dao.update(user);
137
            encodeUserPassword(user, newPassword);
143 138

  
144 139
            // authenticate the user again with the new password
145 140
            UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
......
152 147
        }
153 148
    }
154 149

  
150
    /**
151
     * make new password salt, encode and set it for the passed user
152
     *
153
     * @param user
154
     *  The user to set the new password for.
155
     * @param newPassword
156
     *  the new password to be encoded and set for the <code>user</code>
157
     */
158
    @Override
159
    public void encodeUserPassword(User user, String newPassword) {
160
        Object salt = this.saltSource.getSalt(user);
161
        String password = passwordEncoder.encodePassword(newPassword, salt);
162
        user.setPassword(password);
163
        dao.update(user);
164
    }
165

  
155 166
    @Override
156 167
    @Transactional(readOnly=false)
157 168
    @PreAuthorize("#username == authentication.name or hasRole('ROLE_ADMIN') or hasRole('ROLE_USER_MANAGER')")
......
165 176
                throw new UsernameNotFoundException(username);
166 177
            }
167 178

  
168
            Object salt = this.saltSource.getSalt(user);
169

  
170
            String password = passwordEncoder.encodePassword(newPassword, salt);
171
            user.setPassword(password);
172

  
173
            dao.update(user);
179
            encodeUserPassword(user, newPassword);
174 180
            userCache.removeUserFromCache(user.getUsername());
175 181
        } catch(NonUniqueResultException nure) {
176 182
            throw new IncorrectResultSizeDataAccessException("More than one user found with name '" + username + "'", 1);
......
182 188
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER_MANAGER')")
183 189
    public void createUser(UserDetails user) {
184 190
    	Assert.isInstanceOf(User.class, user);
185

  
186
        String rawPassword = user.getPassword();
187
        Object salt = this.saltSource.getSalt(user);
188

  
189
        String password = passwordEncoder.encodePassword(rawPassword, salt);
190
        ((User)user).setPassword(password);
191

  
191
        encodeUserPassword((User)user, user.getPassword());
192 192
        dao.save((User)user);
193 193
    }
194 194

  

Also available in: Unified diff