001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.auth;
020
021import java.security.Principal;
022
023/**
024 * Immutable Principal that represents a Group. GroupPrincipals are injected
025 * into a Subject's principal list at the time of authentication (login), and
026 * serve as proxies for Group objects for the purposes of making Java 2 security
027 * policy decisions. We add GroupPrincipals instead of the actual Groups because
028 * calling classes should never be able to obtain a mutable object (Group
029 * memberships can be changed by callers). Administrators who wish to grant
030 * privileges to specific wiki groups via the security policy file should always specify
031 * principals of type GroupPrincipal.
032 * @see org.apache.wiki.auth.authorize.Group
033 * @since 2.3.79
034 */
035public final class GroupPrincipal implements Principal
036{
037    private final String m_name;
038
039    /**
040     * Constructs a new GroupPrincipal object with a supplied name.
041     *
042     * @param group the wiki group; cannot be <code>null</code>
043     */
044    public GroupPrincipal( String group )
045    {
046        if ( group == null )
047        {
048            throw new IllegalArgumentException( "Group parameter cannot be null." );
049        }
050        m_name = group;
051    }
052    
053    /**
054     * Returns the name of the group principal.
055     * @return the name
056     * @see java.security.Principal#getName()
057     */
058    public String getName()
059    {
060        return m_name;
061    }
062
063    /**
064     * Two GroupPrincipals are equal if their names are equal.
065     * @param obj the object to compare
066     * @return the result of the equality test
067     * @see java.lang.Object#equals(java.lang.Object)
068     */
069    public boolean equals( Object obj )
070    {
071        if ( !( obj instanceof GroupPrincipal ) )
072        {
073            return false;
074        }
075        GroupPrincipal p = (GroupPrincipal)obj;
076        return p.m_name.equals( m_name );
077    }
078
079    /**
080     * Returns the hashcode for this object.
081     * @return the hash code
082     * @see java.lang.Object#hashCode()
083     */
084    public int hashCode()
085    {
086        return m_name.hashCode();
087    }
088
089    /**
090     * Returns a string representation of this object.
091     * @return the string
092     * @see java.lang.Object#toString()
093     */
094    public String toString()
095    {
096        return "[GroupPrincipal " + m_name + "]";
097    }
098
099}