1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.el;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import javax.faces.context.FacesContext;
26 import java.security.Principal;
27 import java.util.Collection;
28 import java.util.Map;
29 import java.util.Set;
30
31 public class UserWrapper {
32
33 private static final Logger LOG = LoggerFactory.getLogger(UserWrapper.class);
34
35 private Map roles;
36
37 public UserWrapper() {
38 roles = new RolesMap();
39 }
40
41 public Principal getPrincipal() {
42 FacesContext facesContext = FacesContext.getCurrentInstance();
43 Principal principal = facesContext.getExternalContext().getUserPrincipal();
44 if (LOG.isDebugEnabled()) {
45 LOG.debug("getPrincipal(): {}", principal);
46 }
47 return principal;
48 }
49
50 public Map getRoles() {
51 return roles;
52 }
53
54 private static class RolesMap implements Map {
55
56 public Object get(Object key) {
57 String role = (String) key;
58 FacesContext facesContext = FacesContext.getCurrentInstance();
59 boolean inRole = facesContext.getExternalContext().isUserInRole(role);
60 if (LOG.isDebugEnabled()) {
61 LOG.debug("is in role '{}': {}", key, inRole);
62 }
63 return Boolean.valueOf(inRole);
64 }
65
66 public int size() {
67 throw new UnsupportedOperationException();
68 }
69
70 public void clear() {
71 throw new UnsupportedOperationException();
72 }
73
74 public boolean isEmpty() {
75 throw new UnsupportedOperationException();
76 }
77
78 public boolean containsKey(Object key) {
79 throw new UnsupportedOperationException();
80 }
81
82 public boolean containsValue(Object value) {
83 throw new UnsupportedOperationException();
84 }
85
86 public Collection values() {
87 throw new UnsupportedOperationException();
88 }
89
90 public void putAll(Map t) {
91 throw new UnsupportedOperationException();
92 }
93
94 public Set entrySet() {
95 throw new UnsupportedOperationException();
96 }
97
98 public Set keySet() {
99 throw new UnsupportedOperationException();
100 }
101
102 public Object remove(Object key) {
103 throw new UnsupportedOperationException();
104 }
105
106 public Object put(Object key, Object value) {
107 throw new UnsupportedOperationException();
108 }
109 }
110 }