View Javadoc

1   // ========================================================================
2   // Copyright (c) 2009-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.security;
15  
16  import java.security.Principal;
17  
18  import javax.security.auth.Subject;
19  import org.eclipse.jetty.server.UserIdentity;
20  
21  
22  /* ------------------------------------------------------------ */
23  /**
24   * The default implementation of UserIdentity.
25   *
26   */
27  public class DefaultUserIdentity implements UserIdentity
28  {
29      private final Subject _subject;
30      private final Principal _userPrincipal;
31      private final String[] _roles;
32      
33      public DefaultUserIdentity(Subject subject, Principal userPrincipal, String[] roles)
34      {
35          _subject=subject;
36          _userPrincipal=userPrincipal;
37          _roles=roles;
38      }
39  
40      public Subject getSubject()
41      {
42          return _subject;
43      }
44  
45      public Principal getUserPrincipal()
46      {
47          return _userPrincipal;
48      }
49  
50      public boolean isUserInRole(String role, Scope scope)
51      {
52          if (scope!=null && scope.getRoleRefMap()!=null)
53              role=scope.getRoleRefMap().get(role);
54  
55          for (String r :_roles)
56              if (r.equals(role))
57                  return true;
58          return false;
59      }
60  
61      public String toString()
62      {
63          return DefaultUserIdentity.class.getSimpleName()+"('"+_userPrincipal+"')";
64      }
65      
66  }