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      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 }