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 1. consider renaming this class and its interface, since it is no longer annotation based!!!!
|
29
|
*
|
30
|
* FIMXE 2. this class should implement ViewAccessControl. The view class can be accessed via the application
|
31
|
* context before the view bean has been created. see #7967
|
32
|
*/
|
33
|
public class AnnotationBasedAccessControlBean implements ViewInstanceAccessControl, Serializable {
|
34
|
|
35
|
private static final long serialVersionUID = -4232241572782673248L;
|
36
|
|
37
|
private final static Logger logger = Logger.getLogger(AnnotationBasedAccessControlBean.class);
|
38
|
|
39
|
/**
|
40
|
* {@inheritDoc}
|
41
|
*/
|
42
|
@Override
|
43
|
public boolean isAccessGranted(UI ui, String beanName, View view) {
|
44
|
|
45
|
// if(view.getClass().isAnnotationPresent(RequireAuthentication.class)){
|
46
|
// return currentSecurityContext().getAuthentication().isAuthenticated();
|
47
|
// }
|
48
|
// no RequireAuthentication annotation => grant access
|
49
|
|
50
|
Class<? extends View> viewClass = view.getClass();
|
51
|
|
52
|
if(AccessRestrictedView.class.isAssignableFrom(viewClass)){
|
53
|
AccessRestrictedView restricedView = (AccessRestrictedView)view;
|
54
|
if(restricedView.allowAnonymousAccess()){
|
55
|
if(logger.isTraceEnabled()){
|
56
|
logger.trace("anonymous access to " + viewClass.getName() + " allowed");
|
57
|
}
|
58
|
return true;
|
59
|
} else {
|
60
|
Authentication authentication = currentSecurityContext().getAuthentication();
|
61
|
if(authentication != null && authentication.isAuthenticated() && !(authentication instanceof AnonymousAuthenticationToken)) {
|
62
|
if(logger.isTraceEnabled()){
|
63
|
logger.trace("allowing authenticated user " + authentication.getName() + " to access " + viewClass.getName() );
|
64
|
}
|
65
|
return true;
|
66
|
}
|
67
|
|
68
|
if(logger.isTraceEnabled()){
|
69
|
logger.trace("denying access to " + viewClass.getName());
|
70
|
}
|
71
|
restricedView.releaseResourcesOnAccessDenied();
|
72
|
return false;
|
73
|
// FIMXE implement further checks
|
74
|
// TODO use the UserHelperBean?
|
75
|
}
|
76
|
}
|
77
|
|
78
|
return true;
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* @return
|
83
|
*
|
84
|
* FIXME is it ok to use the SecurityContextHolder or do we need to hold the context in the vaadin session?
|
85
|
*/
|
86
|
private SecurityContext currentSecurityContext() {
|
87
|
return SecurityContextHolder.getContext();
|
88
|
}
|
89
|
|
90
|
|
91
|
|
92
|
|
93
|
}
|