Project

General

Profile

Download (3.75 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.security;
2

    
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.Observable;
9
import java.util.Observer;
10

    
11
import org.eclipse.ui.AbstractSourceProvider;
12
import org.eclipse.ui.ISources;
13
import org.springframework.security.core.Authentication;
14
import org.springframework.security.core.GrantedAuthority;
15

    
16
import eu.etaxonomy.cdm.persistence.permission.Role;
17
import eu.etaxonomy.taxeditor.store.CdmStore;
18
import eu.etaxonomy.taxeditor.store.LoginManager;
19

    
20
/**
21
 * Provides the Roles assigned to the currently authenticated principal as the
22
 * variable {@code eu.etaxonomy.taxeditor.security.userRoles}
23
 *
24
 * @author a.kohlbecker
25
 */
26
public class AuthenticationSourceProvider extends AbstractSourceProvider implements Observer {
27

    
28
	public final static String USER_ROLES_VARIABLE = "eu.etaxonomy.taxeditor.security.userRoles";
29
	public final static String USER_NAME_VARIABLE = "eu.etaxonomy.taxeditor.security.userName";
30
	public final static String USER_VARIABLE = "eu.etaxonomy.taxeditor.security.user";
31

    
32
	private final static String[] PROVIDED_SOURCE_NAMES = new String[] {
33
	    USER_ROLES_VARIABLE,
34
	    USER_NAME_VARIABLE,
35
	    USER_VARIABLE
36
	    };
37

    
38
	private final static Map<String, Object> stateMap = new HashMap<String, Object>();
39

    
40
	public AuthenticationSourceProvider() {
41
		super();
42
		initialize();
43
	}
44

    
45
	private void initialize() {
46
		CdmStore.getLoginManager().addObserver(this);
47
	}
48

    
49
	@Override
50
	public void dispose() {
51
		CdmStore.getLoginManager().deleteObserver(this);
52
	}
53

    
54
	@Override
55
	public Map getCurrentState() {
56

    
57
		// SecurityContextHolder.getContext().
58
		List<Role> roles = getCurrentAuthenticationsRoles();
59
		List<String> rolesStr = new ArrayList<String>(roles.size());
60
		for(Role r : roles){
61
			rolesStr.add(r.getAuthority());
62
		}
63

    
64
		stateMap.put(USER_ROLES_VARIABLE, rolesStr);
65
		stateMap.put(USER_VARIABLE, getCurrentAutheticationPrincipal());
66
		stateMap.put(USER_NAME_VARIABLE, getCurrentAutheticationName());
67
		return stateMap;
68
	}
69

    
70
    private String getCurrentAutheticationName() {
71
        Authentication authentication = CdmStore.getCurrentAuthentiation();
72
        if (authentication != null) {
73
            return authentication.getName();
74
        }
75
        return null;
76
    }
77

    
78
    /**
79
     * @return most probably a Cdm User instance or null
80
     */
81
    private Object getCurrentAutheticationPrincipal() {
82
        Authentication authentication = CdmStore.getCurrentAuthentiation();
83
        if (authentication != null) {
84
            return authentication.getPrincipal();
85
        }
86
        return null;
87
    }
88

    
89

    
90
    /*
91
	 * TODO refactor into User once Role is a model class
92
	 */
93
	private List<Role> getCurrentAuthenticationsRoles() {
94

    
95
		List<Role> roles = new ArrayList<>();
96
		Authentication authentication = CdmStore.getCurrentAuthentiation();
97
		if (authentication == null) {
98
			return roles;
99
		}
100

    
101
		Collection<? extends GrantedAuthority> authorities = authentication
102
				.getAuthorities();
103
		if (authorities == null) {
104
			return roles;
105
		}
106

    
107
		Role role = null;
108
		for (GrantedAuthority ga : authorities) {
109
			try {
110
				// check if it is a valid role
111
				role = Role.fromString(ga.getAuthority());
112
				if (role != null) {
113
					roles.add(role);
114
				}
115
			} catch (Exception e) {
116
				/* IGNORE */
117
			}
118
		}
119
		return roles;
120

    
121
	}
122

    
123
	@Override
124
    public String[] getProvidedSourceNames() {
125
		return PROVIDED_SOURCE_NAMES;
126
	}
127

    
128
	@Override
129
	public void update(Observable o, Object arg) {
130
		if(o instanceof LoginManager){
131
			/*
132
			 * This triggers an update of the variable state, and will update also
133
			 * all listeners to the evaluation service. So that every menu point,
134
			 * which is also expression controlled, gets updated too.
135
			 */
136
			fireSourceChanged(ISources.WORKBENCH, getCurrentState());
137
		}
138
	}
139

    
140
}
(1-1/3)