001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.el;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    
025    import javax.faces.context.FacesContext;
026    import java.security.Principal;
027    import java.util.Collection;
028    import java.util.Map;
029    import java.util.Set;
030    
031    public class UserWrapper {
032    
033      private static final Log LOG = LogFactory.getLog(UserWrapper.class);
034    
035      private Map roles;
036    
037      public UserWrapper() {
038        roles = new RolesMap();
039      }
040    
041      public Principal getPrincipal() {
042        FacesContext facesContext = FacesContext.getCurrentInstance();
043        Principal principal = facesContext.getExternalContext().getUserPrincipal();
044        if (LOG.isDebugEnabled()) {
045          LOG.debug("getPrincipal(): " + principal);
046        }
047        return principal;
048      }
049    
050      public Map getRoles() {
051        return roles;
052      }
053    
054      private static class RolesMap implements Map {
055    
056        public Object get(Object key) {
057          String role = (String) key;
058          FacesContext facesContext = FacesContext.getCurrentInstance();
059          boolean inRole = facesContext.getExternalContext().isUserInRole(role);
060          if (LOG.isDebugEnabled()) {
061            LOG.debug("is in role '" + key + "': " + inRole);
062          }
063          return Boolean.valueOf(inRole);
064        }
065    
066        public int size() {
067          throw new UnsupportedOperationException();
068        }
069    
070        public void clear() {
071          throw new UnsupportedOperationException();
072        }
073    
074        public boolean isEmpty() {
075          throw new UnsupportedOperationException();
076        }
077    
078        public boolean containsKey(Object key) {
079          throw new UnsupportedOperationException();
080        }
081    
082        public boolean containsValue(Object value) {
083          throw new UnsupportedOperationException();
084        }
085    
086        public Collection values() {
087          throw new UnsupportedOperationException();
088        }
089    
090        public void putAll(Map t) {
091          throw new UnsupportedOperationException();
092        }
093    
094        public Set entrySet() {
095          throw new UnsupportedOperationException();
096        }
097    
098        public Set keySet() {
099          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    }