Project

General

Profile

« Previous | Next » 

Revision 9da690b7

Added by Andreas Kohlbecker over 6 years ago

ref #7197 assuring the previous authentication is restored after runAsAutheticator.runAsAuthentication in CdmUserHelper

View differences:

src/main/java/eu/etaxonomy/cdm/service/CdmUserHelper.java
207 207
        UserDetails userDetails = repo.getUserService().loadUserByUsername(username);
208 208
        boolean newAuthorityAdded = false;
209 209
        CdmAuthority authority = null;
210
        User user = (User)userDetails;
210 211
        if(userDetails != null){
211
            runAsAutheticator.runAsAuthentication(Role.ROLE_USER_MANAGER);
212
            User user = (User)userDetails;
213
            authority = new CdmAuthority(cdmEntity, property, crud);
214
            try {
215
                GrantedAuthorityImpl grantedAuthority = repo.getGrantedAuthorityService().findAuthorityString(authority.toString());
216
                if(grantedAuthority == null){
217
                    grantedAuthority = authority.asNewGrantedAuthority();
212
            try{
213
                runAsAutheticator.runAsAuthentication(Role.ROLE_USER_MANAGER);
214
                authority = new CdmAuthority(cdmEntity, property, crud);
215
                try {
216
                    GrantedAuthorityImpl grantedAuthority = repo.getGrantedAuthorityService().findAuthorityString(authority.toString());
217
                    if(grantedAuthority == null){
218
                        grantedAuthority = authority.asNewGrantedAuthority();
219
                    }
220
                    newAuthorityAdded = user.getGrantedAuthorities().add(grantedAuthority);
221
                } catch (CdmAuthorityParsingException e) {
222
                    throw new RuntimeException(e);
218 223
                }
219
                newAuthorityAdded = user.getGrantedAuthorities().add(grantedAuthority);
220
            } catch (CdmAuthorityParsingException e) {
221
                throw new RuntimeException(e);
224
                repo.getSession().flush();
225
            } finally {
226
                // in any case restore the previous authentication
227
                runAsAutheticator.restoreAuthentication();
222 228
            }
223
            repo.getSession().flush();
224
            runAsAutheticator.restoreAuthentication();
225 229
            logger.debug("new authority for " + username + ": " + authority.toString());
226 230
            Authentication authentication = new PreAuthenticatedAuthenticationToken(user, user.getPassword(), user.getAuthorities());
227 231
            SecurityContextHolder.getContext().setAuthentication(authentication);

Also available in: Unified diff