Project

General

Profile

Download (2.72 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.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
                return a.equals(RolesAndPermissions.ROLE_CURATION)
65
                        // doing faster regex check here instreas of using CdmAuthoritiy.fromString()
66
                        || a.getAuthority().matches("^Registration\\.\\[.*UPDATE");
67
            });
68
        }
69
        return false;
70
    }
71

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

    
81
    /**
82
     * @return
83
     */
84
    private static Authentication getAuthentication() {
85
        return currentSecurityContext().getAuthentication();
86
    }
87

    
88
}
(5-5/5)