Project

General

Profile

Download (3.06 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.permission;
10

    
11
import java.io.Serializable;
12
import java.util.EnumSet;
13
import java.util.UUID;
14

    
15
import org.apache.log4j.Logger;
16
import org.springframework.context.annotation.Profile;
17

    
18
import com.vaadin.server.FontAwesome;
19
import com.vaadin.server.VaadinSession;
20
import com.vaadin.spring.annotation.SpringComponent;
21
import com.vaadin.spring.annotation.UIScope;
22
import com.vaadin.ui.AbstractComponentContainer;
23
import com.vaadin.ui.Button;
24
import com.vaadin.ui.themes.ValoTheme;
25

    
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
28

    
29
/**
30
 * PermissionDebugUtils provide the following tools:
31
 * <ul>
32
 *   <li>{@link #addGainPerEntityPermissionButton(AbstractComponentContainer, Class, Integer, EnumSet)}:
33
 *   A button which gives a per entity authority to the current user.</li>
34
 * </ul>
35
 *
36
 *
37
 *
38
 * To enable the PermissionDebugUtils you need to activate the spring profile <code>debug</code>. You can add
39
 * <code>-Dspring.profiles.active=debug</code> to the command starting the jvm
40
 * or set this as an environment variable.
41
 *
42
 * @author a.kohlbecker
43
 * @since Oct 11, 2017
44
 *
45
 */
46
@SpringComponent
47
@UIScope
48
@Profile("debug")
49
public class PermissionDebugUtils implements Serializable {
50

    
51
    private static final long serialVersionUID = -210079304170235459L;
52

    
53
    private final static Logger logger = Logger.getLogger(PermissionDebugUtils.class);
54

    
55
    public static final String VADDIN_SESSION_KEY = "PERMISSION_DEBUG_UTILS";
56

    
57
    public static final String SYSTEM_PROP_KEY = "GainPerEntityPermissionButtons";
58

    
59

    
60
    public PermissionDebugUtils() {
61
        VaadinSession.getCurrent().setAttribute(VADDIN_SESSION_KEY, this);
62
    }
63

    
64
    public static PermissionDebugUtils fromSession() {
65
        return (PermissionDebugUtils)VaadinSession.getCurrent().getAttribute(VADDIN_SESSION_KEY);
66
     }
67

    
68
    public static Button addGainPerEntityPermissionButton(AbstractComponentContainer toContainer, Class<? extends CdmBase> cdmType,
69
            UUID entitiyUuid, EnumSet<CRUD> crud, String property){
70

    
71
        PermissionDebugUtils pu = PermissionDebugUtils.fromSession();
72
        if(pu != null){
73
            Button button = pu.gainPerEntityPermissionButton(cdmType, entitiyUuid, crud, property);
74
            if(button != null){
75
                toContainer.addComponent(button);
76
            }
77
            return button;
78
        }
79
        return null;
80
    }
81

    
82
    public Button gainPerEntityPermissionButton(Class<? extends CdmBase> cdmType, UUID entitiyUuid, EnumSet<CRUD> crud, String property){
83

    
84
       Button button = new Button(FontAwesome.BOLT);
85
       button.addClickListener(e -> UserHelper.fromSession().createAuthorityFor(UserHelper.fromSession().userName(), cdmType, entitiyUuid, crud, property));
86
       button.addStyleName(ValoTheme.BUTTON_DANGER);
87
       return button;
88

    
89
    }
90

    
91

    
92
}
(5-5/9)