Project

General

Profile

Download (3.03 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.security;
10

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

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

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

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

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

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

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

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

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

    
58

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

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

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

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

    
81
    public Button gainPerEntityPermissionButton(Class<? extends CdmBase> cdmType, Integer entitiyId, EnumSet<CRUD> crud, String property){
82

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

    
88
    }
89

    
90

    
91
}
(4-4/8)