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.springframework.security.authentication.AnonymousAuthenticationToken;
|
12
|
import org.springframework.security.core.Authentication;
|
13
|
import org.springframework.security.core.context.SecurityContext;
|
14
|
import org.springframework.security.core.context.SecurityContextHolder;
|
15
|
|
16
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.Role;
|
17
|
|
18
|
/**
|
19
|
* @author a.kohlbecker
|
20
|
* @since May 19, 2017
|
21
|
*
|
22
|
*/
|
23
|
public class UserHelper {
|
24
|
|
25
|
|
26
|
public static boolean userIsAutheticated() {
|
27
|
Authentication authentication = getAuthentication();
|
28
|
if(authentication != null){
|
29
|
return authentication.isAuthenticated();
|
30
|
}
|
31
|
return false;
|
32
|
}
|
33
|
|
34
|
|
35
|
public static boolean userIsAnnonymous() {
|
36
|
Authentication authentication = getAuthentication();
|
37
|
return authentication != null
|
38
|
&& authentication.isAuthenticated()
|
39
|
&& authentication instanceof AnonymousAuthenticationToken;
|
40
|
}
|
41
|
|
42
|
public static String userName() {
|
43
|
Authentication authentication = getAuthentication();
|
44
|
if(authentication != null) {
|
45
|
return authentication.getName();
|
46
|
}
|
47
|
return null;
|
48
|
}
|
49
|
|
50
|
public static boolean userIsAdmin() {
|
51
|
Authentication authentication = getAuthentication();
|
52
|
if(authentication != null) {
|
53
|
return authentication.getAuthorities().stream().anyMatch(a -> {
|
54
|
return a.getAuthority().equals(Role.ROLE_ADMIN.getAuthority());
|
55
|
});
|
56
|
}
|
57
|
return false;
|
58
|
}
|
59
|
|
60
|
public static boolean userIsRegistrationCurator() {
|
61
|
Authentication authentication = getAuthentication();
|
62
|
if(authentication != null) {
|
63
|
return authentication.getAuthorities().stream().anyMatch(a -> {
|
64
|
// doing faster regex check here instreas of using CdmAuthoritiy.fromString()
|
65
|
return a.getAuthority().matches("^Registration\\.\\[.*UPDATE");
|
66
|
});
|
67
|
}
|
68
|
return false;
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* @return
|
73
|
*
|
74
|
* FIXME is it ok to use the SecurityContextHolder or do we need to hold the context in the vaadin session?
|
75
|
*/
|
76
|
private static SecurityContext currentSecurityContext() {
|
77
|
return SecurityContextHolder.getContext();
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* @return
|
82
|
*/
|
83
|
private static Authentication getAuthentication() {
|
84
|
return currentSecurityContext().getAuthentication();
|
85
|
}
|
86
|
|
87
|
}
|