Project

General

Profile

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

    
17
import com.vaadin.navigator.View;
18
import com.vaadin.spring.access.ViewInstanceAccessControl;
19
import com.vaadin.ui.UI;
20

    
21
/**
22
 * @author a.kohlbecker
23
 * @since Apr 24, 2017
24
 *
25
 */
26
public class AnnotationBasedAccessControlBean implements ViewInstanceAccessControl {
27

    
28

    
29
    private final static Logger logger = Logger.getLogger(AnnotationBasedAccessControlBean.class);
30

    
31
    /**
32
     * {@inheritDoc}
33
     */
34
    @Override
35
    public boolean isAccessGranted(UI ui, String beanName, View view) {
36

    
37
//        if(view.getClass().isAnnotationPresent(RequireAuthentication.class)){
38
//            return currentSecurityContext().getAuthentication().isAuthenticated();
39
//        }
40
        // no RequireAuthentication annotation => grant access
41

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

    
58
                if(logger.isTraceEnabled()){
59
                    logger.trace("denying access to " + view.getClass().getName());
60
                }
61
                return false;
62
                // FIMXE implement further checks
63
            }
64
        }
65

    
66
        return true;
67
    }
68

    
69
    /**
70
     * @return
71
     *
72
     * FIXME is it ok to use the SecurityContextHolder or do we need to hold the context in the vaadin session?
73
     */
74
    private SecurityContext currentSecurityContext() {
75
        return SecurityContextHolder.getContext();
76
    }
77

    
78

    
79

    
80

    
81
}
(2-2/3)