use same saving strategie for local name details pref as for db name details pref
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / security / AuthenticationSourceProvider.java
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.hibernate.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 */
27 public class AuthenticationSourceProvider extends AbstractSourceProvider implements Observer {
28
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";
32
33 private final static String[] PROVIDED_SOURCE_NAMES = new String[] {
34 USER_ROLES_VARIABLE,
35 USER_NAME_VARIABLE,
36 USER_VARIABLE
37 };
38
39 private final static Map<String, Object> stateMap = new HashMap<String, Object>();
40
41 public AuthenticationSourceProvider() {
42 super();
43 initialize();
44 }
45
46 private void initialize() {
47 CdmStore.getLoginManager().addObserver(this);
48 }
49
50 @Override
51 public void dispose() {
52 CdmStore.getLoginManager().deleteObserver(this);
53 }
54
55 @Override
56 public Map getCurrentState() {
57
58 // SecurityContextHolder.getContext().
59 List<Role> roles = getCurrentAuthenticationsRoles();
60 List<String> rolesStr = new ArrayList<String>(roles.size());
61 for(Role r : roles){
62 rolesStr.add(r.getAuthority());
63 }
64
65 stateMap.put(USER_ROLES_VARIABLE, rolesStr);
66 stateMap.put(USER_VARIABLE, getCurrentAutheticationPrincipal());
67 stateMap.put(USER_NAME_VARIABLE, getCurrentAutheticationName());
68 return stateMap;
69 }
70
71 /**
72 * @return
73 */
74 private String getCurrentAutheticationName() {
75 Authentication authentication = CdmStore.getCurrentAuthentiation();
76 if (authentication != null) {
77 return authentication.getName();
78 }
79 return null;
80 }
81
82 /**
83 * @return most probably a Cdm User instance or null
84 */
85 private Object getCurrentAutheticationPrincipal() {
86 Authentication authentication = CdmStore.getCurrentAuthentiation();
87 if (authentication != null) {
88 return authentication.getPrincipal();
89 }
90 return null;
91 }
92
93
94 /*
95 * TODO refactor into User once Role is a model class
96 */
97 private List<Role> getCurrentAuthenticationsRoles() {
98
99 List<Role> roles = new ArrayList<Role>();
100 Authentication authentication = CdmStore.getCurrentAuthentiation();
101 if (authentication == null) {
102 return roles;
103 }
104
105 Collection<? extends GrantedAuthority> authorities = authentication
106 .getAuthorities();
107 if (authorities == null) {
108 return roles;
109 }
110
111 Role role = null;
112 for (GrantedAuthority ga : authorities) {
113 try {
114 // check if it is a valid role
115 role = Role.fromString(ga.getAuthority());
116 if (role != null) {
117 roles.add(role);
118 }
119 } catch (Exception e) {
120 /* IGNORE */
121 }
122 }
123 return roles;
124
125 }
126
127 @Override
128 public String[] getProvidedSourceNames() {
129 return PROVIDED_SOURCE_NAMES;
130 }
131
132 @Override
133 public void update(Observable o, Object arg) {
134 if(o instanceof LoginManager){
135 /*
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.
139 */
140 fireSourceChanged(ISources.WORKBENCH, getCurrentState());
141 }
142 }
143
144 }