001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2004 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.auth.authorize;
021:
022: import java.security.Principal;
023:
024: /**
025: * A lightweight, immutable Principal that represents a built-in wiki role such
026: * as Anonymous, Asserted and Authenticated. It can also represent dynamic roles
027: * used by an external {@link com.ecyrd.jspwiki.auth.Authorizer}, such as a web
028: * container.
029: * @author Andrew Jaquith
030: * @since 2.3
031: */
032: public final class Role implements Principal {
033:
034: /** All users, regardless of authentication status */
035: public static final Role ALL = new Role("All");
036:
037: /** If the user hasn't supplied a name */
038: public static final Role ANONYMOUS = new Role("Anonymous");
039:
040: /** If the user has supplied a cookie with a username */
041: public static final Role ASSERTED = new Role("Asserted");
042:
043: /** If the user has authenticated with the Container or UserDatabase */
044: public static final Role AUTHENTICATED = new Role("Authenticated");
045:
046: private final String m_name;
047:
048: /**
049: * Constructs a new Role with a given name.
050: * @param name the name of the Role
051: */
052: public Role(String name) {
053: m_name = name;
054: }
055:
056: /**
057: * Returns <code>true</code> if a supplied Role is a built-in Role:
058: * {@link #ALL}, {@link #ANONYMOUS}, {@link #ASSERTED},
059: * or {@link #AUTHENTICATED}.
060: * @param role the role to check
061: * @return the result of the check
062: */
063: public static final boolean isBuiltInRole(Role role) {
064: return role.equals(ALL) || role.equals(ANONYMOUS)
065: || role.equals(ASSERTED) || role.equals(AUTHENTICATED);
066:
067: }
068:
069: /**
070: * Returns <code>true</code> if the supplied name is identical to the name
071: * of a built-in Role; that is, the value returned by <code>getName()</code>
072: * for built-in Roles {@link #ALL}, {@link #ANONYMOUS},
073: * {@link #ASSERTED}, or {@link #AUTHENTICATED}.
074: * @param name the name to be tested
075: * @return <code>true</code> if the name is reserved; <code>false</code>
076: * if not
077: */
078: public static final boolean isReservedName(String name) {
079: return name.equals(ALL.m_name) || name.equals(ANONYMOUS.m_name)
080: || name.equals(ASSERTED.m_name)
081: || name.equals(AUTHENTICATED.m_name);
082: }
083:
084: /**
085: * Returns a unique hashcode for the Role.
086: * @return the hashcode
087: */
088: public final int hashCode() {
089: return m_name.hashCode();
090: }
091:
092: /**
093: * Two Role objects are considered equal if their names are identical.
094: * @param obj the object to test
095: * @return <code>true</code> if both objects are of type Role and have identical names
096: * @see java.lang.Object#equals(java.lang.Object)
097: */
098: public final boolean equals(Object obj) {
099: if (obj == null || !(obj instanceof Role))
100: return false;
101: return m_name.equals(((Role) obj).getName());
102: }
103:
104: /**
105: * Returns the name of the Principal.
106: * @return the name of the Role
107: */
108: public final String getName() {
109: return m_name;
110: }
111:
112: /**
113: * Returns a String representation of the role
114: * @return the string representation of the role
115: * @see java.lang.Object#toString()
116: */
117: public final String toString() {
118: return "[" + this .getClass().getName() + ": " + m_name + "]";
119: }
120:
121: }
|