View Javadoc

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