1 package org.apache.fulcrum.security.model.basic.entity.impl;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.Set;
23
24 import org.apache.commons.lang3.builder.HashCodeBuilder;
25 import org.apache.fulcrum.security.entity.Group;
26 import org.apache.fulcrum.security.entity.User;
27 import org.apache.fulcrum.security.entity.impl.SecurityEntityImpl;
28 import org.apache.fulcrum.security.model.basic.entity.BasicUser;
29 import org.apache.fulcrum.security.util.GroupSet;
30
31 /**
32 * Represents the "basic" model where users can be part of multiple groups
33 * directly, with no roles or permissions.
34 *
35 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
36 * @version $Id: BasicUser.java 437451 2006-08-27 20:20:44Z tv $
37 */
38 public class BasicUserImpl extends SecurityEntityImpl implements BasicUser
39 {
40 /**
41 * Serial number
42 */
43 private static final long serialVersionUID = 7911631916604987203L;
44
45 /** The password */
46 private String password;
47
48 /** Set of related groups */
49 private Set<? extends Group> groupSet = new GroupSet();
50
51 /**
52 * Returns the user's password. This method should not be used by the
53 * application directly, because it's meaning depends upon the
54 * implementation of UserManager that manages this particular user object.
55 * Some implementations will use this attribute for storing a password
56 * encrypted in some way, other will not use it at all, when user entered
57 * password is presented to some external authority (like NT domain
58 * controller) to validate it. See also
59 * {@link org.apache.fulcrum.security.UserManager#authenticate(User,String)}
60 * .
61 *
62 * @return A String with the password for the user.
63 */
64 @Override
65 public String getPassword()
66 {
67 return password;
68 }
69
70 /**
71 * Set password. Application should not use this method directly, see
72 * {@link #getPassword()}. See also
73 * {@link org.apache.fulcrum.security.UserManager#changePassword(User,String,String)}
74 * .
75 *
76 * @param password
77 * The new password.
78 */
79 @Override
80 public void setPassword(String password)
81 {
82 this.password = password;
83 }
84
85 /**
86 * Get the groups this user is part of
87 *
88 * @return a set of groups
89 */
90 @Override
91 public GroupSet getGroups()
92 {
93 if (groupSet instanceof GroupSet)
94 {
95 return (GroupSet) groupSet;
96 }
97 else
98 {
99 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 }