KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23 import org.acegisecurity.GrantedAuthority;
24
25 /**
26  * Class that exists purely to hold a "ternary" mapping of
27  * user <--> space <--> role and is also persisted
28  * the JTrac authorization (access control) scheme works as follows:
29  * if space is null, that means that this is a "global" JTrac role
30  * if space is not null, this role applies for the user to that
31  * space, and the getAuthority() method used by Acegi returns the
32  * role key appended with "_" + spacePrefixCode
33  */

34 public class UserSpaceRole implements GrantedAuthority, Serializable JavaDoc {
35     
36     private long id;
37     private User user;
38     private Space space;
39     private String JavaDoc roleKey;
40     
41     public UserSpaceRole() {
42         // zero arg constructor
43
}
44     
45     public UserSpaceRole(User user, Space space, String JavaDoc roleKey) {
46         this.user = user;
47         this.space = space;
48         this.roleKey = roleKey;
49     }
50     
51     public boolean isAbleToCreateNewItem() {
52         if (space == null) {
53             return false;
54         }
55         return user.getPermittedTransitions(space, State.NEW).size() > 0;
56     }
57     
58     //======== ACEGI GrantedAuthority implementation =============
59

60     public String JavaDoc getAuthority() {
61         if (space != null) {
62             return roleKey + "_" + space.getPrefixCode();
63         }
64         return roleKey;
65     }
66     
67     //=============================================================
68

69     public User getUser() {
70         return user;
71     }
72
73     public void setUser(User user) {
74         this.user = user;
75     }
76     
77     public String JavaDoc getRoleKey() {
78         return roleKey;
79     }
80
81     public void setRoleKey(String JavaDoc roleKey) {
82         this.roleKey = roleKey;
83     }
84     
85     public Space getSpace() {
86         return space;
87     }
88     
89     public void setSpace(Space space) {
90         this.space = space;
91     }
92
93     public long getId() {
94         return id;
95     }
96
97     public void setId(long id) {
98         this.id = id;
99     }
100     
101     @Override JavaDoc
102     public boolean equals(Object JavaDoc o) {
103         if (this == o) {
104             return true;
105         }
106         if (!(o instanceof UserSpaceRole)) {
107             return false;
108         }
109         final UserSpaceRole usr = (UserSpaceRole) o;
110         return (space == usr.getSpace() || space.equals(usr.getSpace())
111             && user.equals(usr.getUser())
112             && roleKey.equals(usr.getRoleKey()));
113     }
114     
115     @Override JavaDoc
116     public int hashCode() {
117         int hash = 7;
118         hash = hash * 31 + user.hashCode();
119         hash = hash * 31 + (space == null ? 0 : space.hashCode());
120         hash = hash * 31 + roleKey.hashCode();
121         return hash;
122     }
123     
124     @Override JavaDoc
125     public String JavaDoc toString() {
126         return getAuthority();
127     }
128     
129 }
130
Popular Tags