001package org.apache.fulcrum.security.model.basic.entity.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Set;
023
024import org.apache.commons.lang3.builder.HashCodeBuilder;
025import org.apache.fulcrum.security.entity.Group;
026import org.apache.fulcrum.security.entity.User;
027import org.apache.fulcrum.security.entity.impl.SecurityEntityImpl;
028import org.apache.fulcrum.security.model.basic.entity.BasicUser;
029import org.apache.fulcrum.security.util.GroupSet;
030
031/**
032 * Represents the "basic" model where users can be part of multiple groups
033 * directly, with no roles or permissions.
034 *
035 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
036 * @version $Id: BasicUser.java 437451 2006-08-27 20:20:44Z tv $
037 */
038public class BasicUserImpl extends SecurityEntityImpl implements BasicUser
039{
040    /**
041     * Serial number
042     */
043    private static final long serialVersionUID = 7911631916604987203L;
044
045    /** The password */
046    private String password;
047
048    /** Set of related groups */
049    private Set<? extends Group> groupSet = new GroupSet();
050
051    /**
052     * Returns the user's password. This method should not be used by the
053     * application directly, because it's meaning depends upon the
054     * implementation of UserManager that manages this particular user object.
055     * Some implementations will use this attribute for storing a password
056     * encrypted in some way, other will not use it at all, when user entered
057     * password is presented to some external authority (like NT domain
058     * controller) to validate it. See also
059     * {@link org.apache.fulcrum.security.UserManager#authenticate(User,String)}
060     * .
061     *
062     * @return A String with the password for the user.
063     */
064    @Override
065    public String getPassword()
066    {
067        return password;
068    }
069
070    /**
071     * Set password. Application should not use this method directly, see
072     * {@link #getPassword()}. See also
073     * {@link org.apache.fulcrum.security.UserManager#changePassword(User,String,String)}
074     * .
075     *
076     * @param password
077     *            The new password.
078     */
079    @Override
080    public void setPassword(String password)
081    {
082        this.password = password;
083    }
084
085    /**
086     * Get the groups this user is part of
087     *
088     * @return a set of groups
089     */
090    @Override
091    public GroupSet getGroups()
092    {
093        if (groupSet instanceof GroupSet)
094        {
095            return (GroupSet) groupSet;
096        }
097        else
098        {
099            groupSet = new GroupSet(groupSet);
100            return (GroupSet) groupSet;
101        }
102    }
103
104    /**
105     * Set the groups this user is part of
106     *
107     * @param groups
108     *            the set of groups
109     */
110    @Override
111    public void setGroups(GroupSet groups)
112    {
113        if (groups != null)
114        {
115            this.groupSet = groups;
116        }
117        else
118        {
119            this.groupSet = new GroupSet();
120        }
121    }
122
123    /**
124     * Remove the group from the list of groups
125     *
126     * @param group
127     *            the group to remove
128     */
129    @Override
130    public void removeGroup(Group group)
131    {
132        getGroups().remove(group);
133    }
134
135    /**
136     * Add the group to the list of groups
137     *
138     * @param group
139     *            the group to add
140     */
141    @Override
142    public void addGroup(Group group)
143    {
144        getGroups().add(group);
145    }
146
147    /**
148     * Set the groups this user is part of as a Set
149     *
150     * @param groups
151     *            the set of groups
152     */
153    @Override
154    public <T extends Group> void setGroupsAsSet(Set<T> groups)
155    {
156        this.groupSet = groups;
157    }
158
159    /**
160     * Get the groups this user is part of as a Set
161     *
162     * @return a set of groups
163     */
164    @Override
165    @SuppressWarnings("unchecked")
166    public <T extends Group> Set<T> getGroupsAsSet()
167    {
168        return (Set<T>) groupSet;
169    }
170
171    /**
172     * Calculate a hash code for this object
173     *
174     * @see org.apache.fulcrum.security.entity.impl.SecurityEntityImpl#hashCode()
175     */
176    @Override
177    public int hashCode()
178    {
179        return new HashCodeBuilder(43, 19).append(getPassword()).appendSuper(super.hashCode()).toHashCode();
180    }
181}