View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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      final FacesContext facesContext = FacesContext.getCurrentInstance();
43      final 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      @Override
57      public Object get(final Object key) {
58        final String role = (String) key;
59        final FacesContext facesContext = FacesContext.getCurrentInstance();
60        final boolean inRole = facesContext.getExternalContext().isUserInRole(role);
61        if (LOG.isDebugEnabled()) {
62          LOG.debug("is in role '{}': {}", key, inRole);
63        }
64        return Boolean.valueOf(inRole);
65      }
66  
67      @Override
68      public int size() {
69        throw new UnsupportedOperationException();
70      }
71  
72      @Override
73      public void clear() {
74        throw new UnsupportedOperationException();
75      }
76  
77      @Override
78      public boolean isEmpty() {
79        throw new UnsupportedOperationException();
80      }
81  
82      @Override
83      public boolean containsKey(final Object key) {
84        throw new UnsupportedOperationException();
85      }
86  
87      @Override
88      public boolean containsValue(final Object value) {
89        throw new UnsupportedOperationException();
90      }
91  
92      @Override
93      public Collection values() {
94        throw new UnsupportedOperationException();
95      }
96  
97      @Override
98      public void putAll(final Map t) {
99        throw new UnsupportedOperationException();
100     }
101 
102     @Override
103     public Set entrySet() {
104       throw new UnsupportedOperationException();
105     }
106 
107     @Override
108     public Set keySet() {
109       throw new UnsupportedOperationException();
110     }
111 
112     @Override
113     public Object remove(final Object key) {
114       throw new UnsupportedOperationException();
115     }
116 
117     @Override
118     public Object put(final Object key, final Object value) {
119       throw new UnsupportedOperationException();
120     }
121   }
122 }