Project

General

Profile

Download (3.28 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

    
13
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
15
import org.springframework.security.authentication.AnonymousAuthenticationToken;
16
import org.springframework.security.core.Authentication;
17
import org.springframework.security.core.context.SecurityContext;
18
import org.springframework.security.core.context.SecurityContextHolder;
19

    
20
import com.vaadin.navigator.View;
21
import com.vaadin.spring.access.ViewInstanceAccessControl;
22
import com.vaadin.ui.UI;
23

    
24
/**
25
 * @author a.kohlbecker
26
 * @since Apr 24, 2017
27
 *
28
 *
29
 * (FIMXE 1. consider renaming this class and its interface, since it is no longer annotation based!!!!)
30
 *
31
 * FIMXE 2. this class should implement ViewAccessControl. The view class and annotations can be accessed
32
 * via the application context before the view bean has been created. see #7967
33
 */
34
public class AnnotationBasedAccessControlBean implements ViewInstanceAccessControl, Serializable {
35

    
36
    private static final long serialVersionUID = -4232241572782673248L;
37

    
38
    private final static Logger logger = LogManager.getLogger();
39

    
40

    
41
    @Override
42
    public boolean isAccessGranted(UI ui, String beanName, View view) {
43

    
44
//        if(view.getClass().isAnnotationPresent(RequireAuthentication.class)){
45
//            return currentSecurityContext().getAuthentication().isAuthenticated();
46
//        }
47
        // no RequireAuthentication annotation => grant access
48

    
49
        Class<? extends View> viewClass = view.getClass();
50

    
51
        if(AccessRestrictedView.class.isAssignableFrom(viewClass)){
52
            AccessRestrictedView restricedView = (AccessRestrictedView)view;
53
            if(restricedView.allowAnonymousAccess()){
54
                if(logger.isTraceEnabled()){
55
                    logger.trace("anonymous access to " + viewClass.getName() + " allowed");
56
                }
57
                return true;
58
            } else {
59
                Authentication authentication = currentSecurityContext().getAuthentication();
60
                if(authentication != null && authentication.isAuthenticated() && !(authentication instanceof AnonymousAuthenticationToken)) {
61
                    if(logger.isTraceEnabled()){
62
                        logger.trace("allowing authenticated user " + authentication.getName() + " to access " + viewClass.getName() );
63
                    }
64
                    return true;
65
                }
66

    
67
                if(logger.isTraceEnabled()){
68
                    logger.trace("denying access to " + viewClass.getName());
69
                }
70
                restricedView.releaseResourcesOnAccessDenied();
71
                return false;
72
                // FIMXE implement further checks
73
                // TODO use the UserHelperBean?
74
            }
75
        }
76

    
77
        return true;
78
    }
79

    
80
    /**
81
     * @return
82
     *
83
     * FIXME is it ok to use the SecurityContextHolder or do we need to hold the context in the vaadin session?
84
     */
85
    private SecurityContext currentSecurityContext() {
86
        return SecurityContextHolder.getContext();
87
    }
88

    
89

    
90

    
91

    
92
}
(3-3/8)