Project

General

Profile

Download (3.07 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.log4j.Logger;
14
import org.springframework.security.authentication.AnonymousAuthenticationToken;
15
import org.springframework.security.core.Authentication;
16
import org.springframework.security.core.context.SecurityContext;
17
import org.springframework.security.core.context.SecurityContextHolder;
18

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

    
23
/**
24
 * @author a.kohlbecker
25
 * @since Apr 24, 2017
26
 *
27
 *
28
 * FIMXE consider renaming this class and its interface, since it is no longer annotation based!!!!
29
 */
30
public class AnnotationBasedAccessControlBean implements ViewInstanceAccessControl, Serializable {
31

    
32
    private static final long serialVersionUID = -4232241572782673248L;
33

    
34
    private final static Logger logger = Logger.getLogger(AnnotationBasedAccessControlBean.class);
35

    
36
    /**
37
     * {@inheritDoc}
38
     */
39
    @Override
40
    public boolean isAccessGranted(UI ui, String beanName, View view) {
41

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

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

    
63
                if(logger.isTraceEnabled()){
64
                    logger.trace("denying access to " + view.getClass().getName());
65
                }
66
                restricedView.releaseResourcesOnAccessDenied();
67
                return false;
68
                // FIMXE implement further checks
69
                // TODO use the UserHelperBean?
70
            }
71
        }
72

    
73
        return true;
74
    }
75

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

    
85

    
86

    
87

    
88
}
(2-2/9)