KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > domain > User


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package info.jtrac.domain;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.acegisecurity.GrantedAuthority;
29 import org.acegisecurity.userdetails.UserDetails;
30
31 /**
32  * Standard User entity with attributes such as name, password etc.
33  * The parent relationship is used for easy grouping of users and
34  * flexible inheritance of permission schemes. The user type
35  * determines if this is a normal user or a user group. Only
36  * user groups can have child references.
37  *
38  * We also tie in to the Acegi security framework and implement
39  * the Acegi UserDetails interface so that Acegi can take care
40  * of Authentication and Authorization
41  */

42 public class User implements UserDetails, Serializable JavaDoc {
43     
44     private long id;
45     private Integer JavaDoc type;
46     private User parent;
47     private String JavaDoc loginName;
48     private String JavaDoc name;
49     private String JavaDoc password;
50     private String JavaDoc email;
51     private Metadata metadata;
52     private String JavaDoc locale;
53     private boolean locked;
54     private Set JavaDoc<UserSpaceRole> userSpaceRoles = new HashSet JavaDoc<UserSpaceRole>();
55     private Map JavaDoc<Long JavaDoc, Boolean JavaDoc> spacesWhereAbleToCreateNewItem;
56     
57     //=============================================================
58

59     public void addSpaceWithRole(Space space, String JavaDoc roleKey) {
60         userSpaceRoles.add(new UserSpaceRole(this, space, roleKey));
61     }
62     
63     public void removeSpaceWithRole(Space space, String JavaDoc roleKey) {
64         userSpaceRoles.remove(new UserSpaceRole(this, space, roleKey));
65     }
66     
67     private List JavaDoc<String JavaDoc> getRoleKeys(Space space) {
68         List JavaDoc<String JavaDoc> roleKeys = new ArrayList JavaDoc<String JavaDoc>();
69         for(UserSpaceRole usr : userSpaceRoles) {
70             Space s = usr.getSpace();
71             if (s == space || ( s != null && s.equals(space))) {
72                 roleKeys.add(usr.getRoleKey());
73             }
74         }
75         return roleKeys;
76     }
77     
78     public Map JavaDoc<Integer JavaDoc, String JavaDoc> getPermittedTransitions(Space space, int status) {
79         return space.getMetadata().getPermittedTransitions(getRoleKeys(space), status);
80     }
81     
82     public List JavaDoc<Field> getEditableFieldList(Space space, int status) {
83         return space.getMetadata().getEditableFields(getRoleKeys(space), Collections.singletonList(status));
84     }
85     
86     public Set JavaDoc<Space> getSpaces() {
87         Set JavaDoc<Space> spaces = new HashSet JavaDoc<Space>(userSpaceRoles.size());
88         for (UserSpaceRole usr : userSpaceRoles) {
89             if (usr.getSpace() != null) {
90                 spaces.add(usr.getSpace());
91             }
92         }
93         return spaces;
94     }
95     
96     public int getSpaceCount() {
97         return getSpaces().size();
98     }
99     
100     public boolean isAdminForAllSpaces() {
101         return getRoleKeys(null).contains("ROLE_ADMIN");
102     }
103     
104     /**
105      * this returns 'valid' spaceRoles, where space not null and role not ROLE_ADMIN
106      * TODO multiple roles per space ?
107      */

108     public List JavaDoc<UserSpaceRole> getSpaceRoles() {
109         List JavaDoc<UserSpaceRole> list = new ArrayList JavaDoc<UserSpaceRole>(userSpaceRoles.size());
110         for(UserSpaceRole usr : userSpaceRoles) {
111             if(usr.getSpace() != null && usr.getRoleKey() != "ROLE_ADMIN") {
112                 list.add(usr);
113             }
114         }
115         return list;
116     }
117     
118     //============ ACEGI UserDetails implementation ===============
119

120     public boolean isAccountNonExpired() {
121         return true;
122     }
123     
124     public boolean isAccountNonLocked() {
125         return !isLocked();
126     }
127     
128     public GrantedAuthority[] getAuthorities() {
129         Collection JavaDoc<GrantedAuthority> authorities = new ArrayList JavaDoc<GrantedAuthority>();
130         // grant full access only if not a Guest
131
if (id > 0) {
132             authorities.add(new UserSpaceRole(this, null, "ROLE_USER"));
133         }
134         for (UserSpaceRole usr : userSpaceRoles) {
135             authorities.add(usr);
136         }
137         return authorities.toArray(new GrantedAuthority[authorities.size()]);
138     }
139     
140     public boolean isCredentialsNonExpired() {
141         return true;
142     }
143     
144     public boolean isEnabled() {
145         return true;
146     }
147     
148     public String JavaDoc getUsername() {
149         return getLoginName();
150     }
151     
152     public String JavaDoc getPassword() {
153         return password;
154     }
155     
156     //=============================================================
157

158     public Map JavaDoc<Long JavaDoc, Boolean JavaDoc> getSpacesWhereAbleToCreateNewItem() {
159         return spacesWhereAbleToCreateNewItem;
160     }
161
162     public void setSpacesWhereAbleToCreateNewItem(Map JavaDoc<Long JavaDoc, Boolean JavaDoc> spacesWhereAbleToCreateNewItem) {
163         this.spacesWhereAbleToCreateNewItem = spacesWhereAbleToCreateNewItem;
164     }
165     
166     public Set JavaDoc<UserSpaceRole> getUserSpaceRoles() {
167         return userSpaceRoles;
168     }
169
170     public void setUserSpaceRoles(Set JavaDoc<UserSpaceRole> userSpaceRoles) {
171         this.userSpaceRoles = userSpaceRoles;
172     }
173     
174     public User getParent() {
175         return parent;
176     }
177     
178     public void setParent(User parent) {
179         this.parent = parent;
180     }
181     
182     public String JavaDoc getName() {
183         return name;
184     }
185     
186     public void setName(String JavaDoc name) {
187         this.name = name;
188     }
189     
190     public void setPassword(String JavaDoc password) {
191         this.password = password;
192     }
193     
194     public String JavaDoc getEmail() {
195         return email;
196     }
197     
198     public void setEmail(String JavaDoc email) {
199         this.email = email;
200     }
201     
202     public String JavaDoc getLocale() {
203         return locale;
204     }
205     
206     public void setLocale(String JavaDoc locale) {
207         this.locale = locale;
208     }
209     
210     public boolean isLocked() {
211         return locked;
212     }
213     
214     public void setLocked(boolean locked) {
215         this.locked = locked;
216     }
217     
218     public Metadata getMetadata() {
219         return metadata;
220     }
221     
222     public void setMetadata(Metadata metadata) {
223         this.metadata = metadata;
224     }
225     
226     public long getId() {
227         return id;
228     }
229     
230     public void setId(long id) {
231         this.id = id;
232     }
233     
234     public Integer JavaDoc getType() {
235         return type;
236     }
237     
238     public void setType(Integer JavaDoc type) {
239         this.type = type;
240     }
241     
242     public String JavaDoc getLoginName() {
243         return loginName;
244     }
245     
246     public void setLoginName(String JavaDoc loginName) {
247         this.loginName = loginName;
248     }
249     
250     @Override JavaDoc
251     public String JavaDoc toString() {
252         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
253         sb.append("id [").append(id);
254         sb.append("]; loginName [").append(loginName);
255         sb.append("]");
256         return sb.toString();
257     }
258     
259     @Override JavaDoc
260     public boolean equals(Object JavaDoc o) {
261         if (this == o) {
262             return true;
263         }
264         if (!(o instanceof User)) {
265             return false;
266         }
267         final User u = (User) o;
268         return u.getLoginName().equals(loginName);
269     }
270     
271     @Override JavaDoc
272     public int hashCode() {
273         return loginName.hashCode();
274     }
275     
276 }
277
Popular Tags