Project

General

Profile

« Previous | Next » 

Revision 55ecdf3a

Added by Andreas Kohlbecker over 6 years ago

ref #6867 generic failsave mechanism to grant per entity permission in CdmPopupEditors

View differences:

src/main/java/eu/etaxonomy/vaadin/mvp/AbstractCdmEditorPresenter.java
8 8
*/
9 9
package eu.etaxonomy.vaadin.mvp;
10 10

  
11
import java.util.EnumSet;
12

  
11 13
import org.apache.log4j.Logger;
14
import org.hibernate.HibernateException;
12 15
import org.springframework.context.event.EventListener;
13 16

  
14 17
import eu.etaxonomy.cdm.api.service.IService;
15 18
import eu.etaxonomy.cdm.model.common.CdmBase;
19
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
20
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
16 21
import eu.etaxonomy.cdm.service.CdmStore;
17 22
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
23
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
18 24
import eu.etaxonomy.vaadin.mvp.event.EditorPreSaveEvent;
19 25
import eu.etaxonomy.vaadin.mvp.event.EditorSaveEvent;
20 26

  
......
31 37

  
32 38
    private static final Logger logger = Logger.getLogger(AbstractCdmEditorPresenter.class);
33 39

  
40
    /**
41
     * if not null, this CRUD set is to be used to create a CdmAuthoritiy for the base entitiy which will be
42
     * granted to the current use as long this grant is not assigned yet.
43
     */
44
    protected EnumSet<CRUD> crud = null;
45

  
34 46

  
35 47
    public AbstractCdmEditorPresenter() {
36 48
        super();
......
39 51

  
40 52
    CdmStore<DTO, IService<DTO>> store ;
41 53

  
54
    protected CdmAuthority newAuthorityCreated;
55

  
42 56
    protected CdmStore<DTO, IService<DTO>> getStore() {
43 57
        if(store == null){
44 58
            store = new CdmStore<>(getRepo(), getService());
......
46 60
        return store;
47 61
    }
48 62

  
63
    @Override
64
    protected DTO loadBeanById(Object identifier) {
65

  
66
        if(identifier != null) {
67
            Integer integerID = (Integer)identifier;
68
            // CdmAuthority is needed before the bean is loaded into the session.
69
            // otherwise adding the authority to the user would cause a flush
70
            guaranteePerEntityCRUDPermissions(integerID);
71
            return loadCdmEntityById(integerID);
72
        } else {
73
            DTO cdmEntitiy = loadCdmEntityById(null);
74
            if(cdmEntitiy != null){
75
                guaranteePerEntityCRUDPermissions(cdmEntitiy);
76
            }
77
            return cdmEntitiy;
78
        }
79

  
80
    }
81

  
82

  
83
    /**
84
     * @param identifier
85
     * @return
86
     */
87
    protected abstract DTO loadCdmEntityById(Integer identifier);
88

  
89
    /**
90
     * Grant per entity CdmAuthority to the current user <b>for the bean which is not yet loaded</b>
91
     * into the editor. The <code>CRUD</code> to be granted are stored in the <code>crud</code> field.
92
     */
93
    protected abstract void guaranteePerEntityCRUDPermissions(Integer identifier);
94

  
95
    /**
96
     * Grant per entity CdmAuthority to the current user for the bean which is loaded
97
     * into the editor. The <code>CRUD</code> to be granted are stored in the <code>crud</code> field.
98
     */
99
     protected abstract void guaranteePerEntityCRUDPermissions(DTO bean);
49 100

  
50 101
    /**
51 102
     * @return
......
72 123
            return;
73 124
        }
74 125

  
126

  
75 127
        // the bean is now updated with the changes made by the user
76 128
        DTO bean = (DTO) saveEvent.getBean();
77
        bean = handleTransientProperties(bean);
78
        EntityChangeEvent changeEvent = getStore().saveBean(bean);
79 129

  
80
        if(changeEvent != null){
81
            eventBus.publishEvent(changeEvent);
130
        bean = handleTransientProperties(bean);
131
        try {
132
            EntityChangeEvent changeEvent = getStore().saveBean(bean);
133

  
134
            if(changeEvent != null){
135
                eventBus.publishEvent(changeEvent);
136
            }
137
        } catch (HibernateException e){
138
            if(newAuthorityCreated != null){
139
                UserHelper.fromSession().removeAuthorityForCurrentUser(newAuthorityCreated);
140
            }
82 141
        }
83 142
    }
84 143

  
......
135 194

  
136 195
    }
137 196

  
197
    /**
198
     * @param crud
199
     */
200
    public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
201
        this.crud = crud;
202

  
203
    }
204

  
205

  
138 206
}

Also available in: Unified diff