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.io.Serializable;
022import java.security.Principal;
023
024/**
025 * Immutable Principal that represents a Group. GroupPrincipals are injected
026 * into a Subject's principal list at the time of authentication (login), and
027 * serve as proxies for Group objects for the purposes of making Java 2 security
028 * policy decisions. We add GroupPrincipals instead of the actual Groups because
029 * calling classes should never be able to obtain a mutable object (Group
030 * memberships can be changed by callers). Administrators who wish to grant
031 * privileges to specific wiki groups via the security policy file should always specify
032 * principals of type GroupPrincipal.
033 * @see org.apache.wiki.auth.authorize.Group
034 * @since 2.3.79
035 */
036public final class GroupPrincipal implements Principal, Serializable
037{
038    private static final long serialVersionUID = 123123123123L;
039    private final String m_name;
040
041    /**
042     * Constructs a new GroupPrincipal object with a supplied name.
043     *
044     * @param group the wiki group; cannot be <code>null</code>
045     */
046    public GroupPrincipal(final String group )
047    {
048        if ( group == null )
049        {
050            throw new IllegalArgumentException( "Group parameter cannot be null." );
051        }
052        m_name = group;
053    }
054    
055    /**
056     * Returns the name of the group principal.
057     * @return the name
058     * @see java.security.Principal#getName()
059     */
060    @Override
061    public String getName()
062    {
063        return m_name;
064    }
065
066    /**
067     * Two GroupPrincipals are equal if their names are equal.
068     * @param obj the object to compare
069     * @return the result of the equality test
070     * @see java.lang.Object#equals(java.lang.Object)
071     */
072    public boolean equals(final Object obj )
073    {
074        if ( !( obj instanceof GroupPrincipal ) )
075        {
076            return false;
077        }
078        final GroupPrincipal p = (GroupPrincipal)obj;
079        return p.m_name.equals( m_name );
080    }
081
082    /**
083     * Returns the hashcode for this object.
084     * @return the hash code
085     * @see java.lang.Object#hashCode()
086     */
087    public int hashCode()
088    {
089        return m_name.hashCode();
090    }
091
092    /**
093     * Returns a string representation of this object.
094     * @return the string
095     * @see java.lang.Object#toString()
096     */
097    public String toString()
098    {
099        return "[GroupPrincipal " + m_name + "]";
100    }
101
102}