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