Project

General

Profile

Download (4.6 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.service;
10

    
11
import java.util.EnumSet;
12

    
13
import org.springframework.beans.factory.annotation.Autowired;
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.spring.annotation.SpringComponent;
20
import com.vaadin.spring.annotation.UIScope;
21

    
22
import eu.etaxonomy.cdm.database.PermissionDeniedException;
23
import eu.etaxonomy.cdm.model.common.CdmBase;
24
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
25
import eu.etaxonomy.cdm.persistence.hibernate.permission.ICdmPermissionEvaluator;
26
import eu.etaxonomy.cdm.persistence.hibernate.permission.Role;
27
import eu.etaxonomy.cdm.vaadin.security.RolesAndPermissions;
28
import eu.etaxonomy.cdm.vaadin.security.VaadinUserHelper;
29

    
30
/**
31
 * @author a.kohlbecker
32
 * @since May 19, 2017
33
 *
34
 */
35
@SpringComponent
36
@UIScope
37
public class CdmUserHelper extends VaadinUserHelper {
38

    
39
    @Autowired
40
    private ICdmPermissionEvaluator permissionEvaluator;
41

    
42
    public CdmUserHelper(){
43
        super();
44
    }
45

    
46
    @Override
47
    public boolean userIsAutheticated() {
48
        Authentication authentication = getAuthentication();
49
        if(authentication != null){
50
            return authentication.isAuthenticated();
51
        }
52
        return false;
53
    }
54

    
55

    
56
    @Override
57
    public boolean userIsAnnonymous() {
58
        Authentication authentication = getAuthentication();
59
        return authentication != null
60
                && authentication.isAuthenticated()
61
                && authentication instanceof AnonymousAuthenticationToken;
62
    }
63

    
64
    @Override
65
    public String userName() {
66
        Authentication authentication = getAuthentication();
67
        if(authentication != null) {
68
            return authentication.getName();
69
        }
70
        return null;
71
    }
72

    
73
    @Override
74
    public boolean userIsAdmin() {
75
        Authentication authentication = getAuthentication();
76
        if(authentication != null) {
77
            return authentication.getAuthorities().stream().anyMatch(a -> {
78
                return a.getAuthority().equals(Role.ROLE_ADMIN.getAuthority());
79
            });
80
        }
81
        return false;
82
    }
83

    
84
    @Override
85
    public boolean userIsRegistrationCurator() {
86
        Authentication authentication = getAuthentication();
87
        if(authentication != null) {
88
            return authentication.getAuthorities().stream().anyMatch(a -> {
89
                return a.equals(RolesAndPermissions.ROLE_CURATION)
90
                        // doing faster regex check here instreas of using CdmAuthoritiy.fromString()
91
                        || a.getAuthority().matches("^Registration\\.\\[.*UPDATE");
92
            });
93
        }
94
        return false;
95
    }
96

    
97
    @Override
98
    public boolean userHasPermission(CdmBase entity, Object ... args){
99
        EnumSet<CRUD> crudSet = crudSetFromArgs(args);
100
        try {
101
        return permissionEvaluator.hasPermission(getAuthentication(), entity, crudSet);
102
        } catch (PermissionDeniedException e){
103
            //IGNORE
104
        }
105
        return false;
106
    }
107

    
108
    @Override
109
    public boolean userHasPermission(Class<? extends CdmBase> cdmType, Integer entitiyId, Object ... args){
110
        EnumSet<CRUD> crudSet = crudSetFromArgs(args);
111
        try {
112
        return permissionEvaluator.hasPermission(getAuthentication(), cdmType, entitiyId.toString(), crudSet);
113
        } catch (PermissionDeniedException e){
114
            //IGNORE
115
        }
116
        return false;
117
    }
118

    
119

    
120
    private EnumSet<CRUD> crudSetFromArgs(Object[] args) {
121
        EnumSet<CRUD> crudSet = EnumSet.noneOf(CRUD.class);
122
        for(int i = 0; i < args.length; i++){
123
            try {
124
                crudSet.add(CRUD.valueOf(args[i].toString()));
125
            } catch (Exception e){
126
                throw new IllegalArgumentException("could not add " + args[i], e);
127
            }
128
        }
129
        return crudSet;
130
    }
131

    
132

    
133
    /**
134
     * @return
135
     *
136
     * FIXME is it ok to use the SecurityContextHolder or do we need to hold the context in the vaadin session?
137
     */
138
    private SecurityContext currentSecurityContext() {
139
        return SecurityContextHolder.getContext();
140
    }
141

    
142
    /**
143
     * @return
144
     */
145
    private Authentication getAuthentication() {
146
        return currentSecurityContext().getAuthentication();
147
    }
148

    
149
}
(1-1/3)