View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.util.Set;
22  import java.util.concurrent.CopyOnWriteArraySet;
23  
24  /**
25   * RoleInfo
26   * 
27   * Badly named class that holds the role and user data constraint info for a
28   * path/http method combination, extracted and combined from security
29   * constraints.
30   * 
31   * @version $Rev: 4793 $ $Date: 2009-03-19 00:00:01 +0100 (Thu, 19 Mar 2009) $
32   */
33  public class RoleInfo
34  {
35      private boolean _isAnyAuth;
36      private boolean _isAnyRole;
37      private boolean _checked;
38      private boolean _forbidden;
39      private UserDataConstraint _userDataConstraint;
40  
41      /**
42       * List of permitted roles
43       */
44      private final Set<String> _roles = new CopyOnWriteArraySet<String>();
45  
46      public RoleInfo()
47      {    
48      }
49      
50      public boolean isChecked()
51      {
52          return _checked;
53      }
54  
55      public void setChecked(boolean checked)
56      {
57          this._checked = checked;
58          if (!checked)
59          {
60              _forbidden=false;
61              _roles.clear();
62              _isAnyRole=false;
63              _isAnyAuth=false;
64          }
65      }
66  
67      public boolean isForbidden()
68      {
69          return _forbidden;
70      }
71  
72      public void setForbidden(boolean forbidden)
73      {
74          this._forbidden = forbidden;
75          if (forbidden)
76          {
77              _checked = true;
78              _userDataConstraint = null;
79              _isAnyRole=false;
80              _isAnyAuth=false;
81              _roles.clear();
82          }
83      }
84  
85      public boolean isAnyRole()
86      {
87          return _isAnyRole;
88      }
89  
90      public void setAnyRole(boolean anyRole)
91      {
92          this._isAnyRole=anyRole;
93          if (anyRole)
94              _checked = true;
95      }
96      
97      public boolean isAnyAuth ()
98      {
99          return _isAnyAuth;
100     }
101     
102     public void setAnyAuth(boolean anyAuth)
103     {
104         this._isAnyAuth=anyAuth;
105         if (anyAuth)
106             _checked = true;
107     }
108 
109     public UserDataConstraint getUserDataConstraint()
110     {
111         return _userDataConstraint;
112     }
113 
114     public void setUserDataConstraint(UserDataConstraint userDataConstraint)
115     {
116         if (userDataConstraint == null) throw new NullPointerException("Null UserDataConstraint");
117         if (this._userDataConstraint == null)
118         {
119            
120             this._userDataConstraint = userDataConstraint;
121         }
122         else
123         {
124             this._userDataConstraint = this._userDataConstraint.combine(userDataConstraint);
125         }
126     }
127 
128     public Set<String> getRoles()
129     {
130         return _roles;
131     }
132     
133     public void addRole(String role)
134     {
135         _roles.add(role);
136     }
137 
138     public void combine(RoleInfo other)
139     {
140         if (other._forbidden)
141             setForbidden(true);
142         else if (!other._checked) // TODO is this the right way around???
143             setChecked(true);
144         else if (other._isAnyRole)
145             setAnyRole(true);
146         else if (other._isAnyAuth)
147             setAnyAuth(true);
148         else if (!_isAnyRole)
149         {
150             for (String r : other._roles)
151                 _roles.add(r);
152         }
153         
154         setUserDataConstraint(other._userDataConstraint);
155     }
156     
157     @Override
158     public String toString()
159     {
160         return "{RoleInfo"+(_forbidden?",F":"")+(_checked?",C":"")+(_isAnyRole?",*":_roles)+(_userDataConstraint!=null?","+_userDataConstraint:"")+"}";
161     }
162 }