Project

General

Profile

« Previous | Next » 

Revision 414b59c4

Added by Andreas Kohlbecker over 4 years ago

fix #7148 deleteting GrantedAuthorityImpl and dissociate them from User and Group

View differences:

src/main/java/eu/etaxonomy/cdm/persistence/hibernate/GrantedAuthorityRevokingRegistrationUpdateLister.java
14 14
import java.util.HashSet;
15 15
import java.util.List;
16 16
import java.util.Set;
17
import java.util.stream.Collectors;
17 18

  
18 19
import org.hibernate.FlushMode;
19 20
import org.hibernate.Query;
21
import org.hibernate.Session;
22
import org.hibernate.Transaction;
20 23
import org.hibernate.event.spi.EventSource;
21 24
import org.hibernate.event.spi.PostUpdateEvent;
22 25
import org.hibernate.event.spi.PostUpdateEventListener;
23 26
import org.hibernate.persister.entity.EntityPersister;
27
import org.springframework.security.core.GrantedAuthority;
24 28

  
25 29
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26 30
import eu.etaxonomy.cdm.model.agent.AgentBase;
27 31
import eu.etaxonomy.cdm.model.agent.Person;
28 32
import eu.etaxonomy.cdm.model.agent.Team;
29 33
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
34
import eu.etaxonomy.cdm.model.common.Group;
35
import eu.etaxonomy.cdm.model.common.User;
30 36
import eu.etaxonomy.cdm.model.name.Registration;
31 37
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
32 38
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
......
228 234
     */
229 235
    private void deleteAuthorities(EventSource session, Set<CdmAuthority> deleteCandidates) {
230 236

  
237
        if(deleteCandidates.isEmpty()){
238
            return;
239
        }
240

  
231 241
        Collection<String> authorityStrings = new ArrayList<String>(deleteCandidates.size());
232 242
        deleteCandidates.forEach( dc -> authorityStrings.add(dc.toString()));
233 243

  
244
        // -----------------------------------------------------------------------------------------
245
        // this needs to be executed in a separate session to avoid concurrent modification problems
246
        Session newSession = session.getSessionFactory().openSession();
247
        Transaction txState = newSession.beginTransaction();
248

  
249
        Query userQuery = newSession.createQuery("select u from User u join u.grantedAuthorities ga where ga.authority in (:authorities)");
250
        userQuery.setParameterList("authorities", authorityStrings);
251
        List<User> users = userQuery.list();
252
        for(User user : users){
253
            List<GrantedAuthority> deleteFromUser = user.getGrantedAuthorities().stream().filter(
254
                        ga -> authorityStrings.contains(ga.getAuthority())
255
                    )
256
                    .collect(Collectors.toList());
257
            user.getGrantedAuthorities().removeAll(deleteFromUser);
258
        }
259

  
260
        Query groupQuery = newSession.createQuery("select g from Group g join g.grantedAuthorities ga where ga.authority in (:authorities)");
261
        groupQuery.setParameterList("authorities", authorityStrings);
262
        List<Group> groups = groupQuery.list();
263
        for(Group group : groups){
264
            List<GrantedAuthority> deleteFromUser = group.getGrantedAuthorities().stream().filter(
265
                        ga -> authorityStrings.contains(ga.getAuthority())
266
                    )
267
                    .collect(Collectors.toList());
268
            group.getGrantedAuthorities().removeAll(deleteFromUser);
269
        }
270

  
271
        newSession.flush();
272
        txState.commit();
273
        newSession.close();
274
        // -----------------------------------------------------------------------------------------
275

  
234 276
        String hql = "delete from GrantedAuthorityImpl as ga where ga.authority in (:authorities)";
235
        Query query = session.createQuery(hql);
236
        query.setParameterList("authorities", authorityStrings);
237
        query.setFlushMode(FlushMode.MANUAL); // workaround for  HHH-11822 (https://hibernate.atlassian.net/browse/HHH-11822)
238
        query.executeUpdate();
277
        Query deleteQuery = session.createQuery(hql);
278
        deleteQuery.setParameterList("authorities", authorityStrings);
279
        deleteQuery.setFlushMode(FlushMode.MANUAL); // workaround for  HHH-11822 (https://hibernate.atlassian.net/browse/HHH-11822)
280
        deleteQuery.executeUpdate();
239 281

  
240 282
    }
241 283

  

Also available in: Unified diff