1 package eu
.etaxonomy
.taxeditor
.security
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Collection
;
5 import java
.util
.HashMap
;
8 import java
.util
.Observable
;
9 import java
.util
.Observer
;
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
;
16 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.Role
;
17 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
18 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
21 * Provides the Roles assigned to the currently authenticated principal as the
22 * variable {@code eu.etaxonomy.taxeditor.security.userRoles}
24 * @author a.kohlbecker
27 public class AuthenticationSourceProvider
extends AbstractSourceProvider
implements Observer
{
29 public final static String USER_ROLES_VARIABLE
= "eu.etaxonomy.taxeditor.security.userRoles";
30 public final static String USER_NAME_VARIABLE
= "eu.etaxonomy.taxeditor.security.userName";
31 public final static String USER_VARIABLE
= "eu.etaxonomy.taxeditor.security.user";
33 private final static String
[] PROVIDED_SOURCE_NAMES
= new String
[] {
39 private final static Map
<String
, Object
> stateMap
= new HashMap
<String
, Object
>();
41 public AuthenticationSourceProvider() {
46 private void initialize() {
47 CdmStore
.getLoginManager().addObserver(this);
51 public void dispose() {
52 CdmStore
.getLoginManager().deleteObserver(this);
56 public Map
getCurrentState() {
58 // SecurityContextHolder.getContext().
59 List
<Role
> roles
= getCurrentAuthenticationsRoles();
60 List
<String
> rolesStr
= new ArrayList
<String
>(roles
.size());
62 rolesStr
.add(r
.getAuthority());
65 stateMap
.put(USER_ROLES_VARIABLE
, rolesStr
);
66 stateMap
.put(USER_VARIABLE
, getCurrentAutheticationPrincipal());
67 stateMap
.put(USER_NAME_VARIABLE
, getCurrentAutheticationName());
74 private String
getCurrentAutheticationName() {
75 Authentication authentication
= CdmStore
.getCurrentAuthentiation();
76 if (authentication
!= null) {
77 return authentication
.getName();
83 * @return most probably a Cdm User instance or null
85 private Object
getCurrentAutheticationPrincipal() {
86 Authentication authentication
= CdmStore
.getCurrentAuthentiation();
87 if (authentication
!= null) {
88 return authentication
.getPrincipal();
95 * TODO refactor into User once Role is a model class
97 private List
<Role
> getCurrentAuthenticationsRoles() {
99 List
<Role
> roles
= new ArrayList
<Role
>();
100 Authentication authentication
= CdmStore
.getCurrentAuthentiation();
101 if (authentication
== null) {
105 Collection
<?
extends GrantedAuthority
> authorities
= authentication
107 if (authorities
== null) {
112 for (GrantedAuthority ga
: authorities
) {
114 // check if it is a valid role
115 role
= Role
.fromString(ga
.getAuthority());
119 } catch (Exception e
) {
128 public String
[] getProvidedSourceNames() {
129 return PROVIDED_SOURCE_NAMES
;
133 public void update(Observable o
, Object arg
) {
134 if(o
instanceof LoginManager
){
136 * This triggers an update of the variable state, and will update also
137 * all listeners to the evaluation service. So that every menu point,
138 * which is also expression controlled, gets updated too.
140 fireSourceChanged(ISources
.WORKBENCH
, getCurrentState());