001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.domain;
018:
019: import java.io.Serializable;
020: import org.acegisecurity.GrantedAuthority;
021:
022: /**
023: * Class that exists purely to hold a "ternary" mapping of
024: * user <--> space <--> role and is also persisted
025: * the JTrac authorization (access control) scheme works as follows:
026: * if space is null, that means that this is a "global" JTrac role
027: * if space is not null, this role applies for the user to that
028: * space, and the getAuthority() method used by Acegi returns the
029: * role key appended with "_" + spacePrefixCode
030: */
031: public class UserSpaceRole implements GrantedAuthority, Serializable {
032:
033: private long id;
034: private User user;
035: private Space space;
036: private String roleKey;
037:
038: public UserSpaceRole() {
039: // zero arg constructor
040: }
041:
042: public UserSpaceRole(User user, Space space, String roleKey) {
043: this .user = user;
044: this .space = space;
045: this .roleKey = roleKey;
046: }
047:
048: public boolean isAbleToCreateNewItem() {
049: if (space == null) {
050: return false;
051: }
052: return user.getPermittedTransitions(space, State.NEW).size() > 0;
053: }
054:
055: //======== ACEGI GrantedAuthority implementation =============
056:
057: public String getAuthority() {
058: if (space != null) {
059: return roleKey + "_" + space.getPrefixCode();
060: }
061: return roleKey;
062: }
063:
064: //=============================================================
065:
066: public User getUser() {
067: return user;
068: }
069:
070: public void setUser(User user) {
071: this .user = user;
072: }
073:
074: public String getRoleKey() {
075: return roleKey;
076: }
077:
078: public void setRoleKey(String roleKey) {
079: this .roleKey = roleKey;
080: }
081:
082: public Space getSpace() {
083: return space;
084: }
085:
086: public void setSpace(Space space) {
087: this .space = space;
088: }
089:
090: public long getId() {
091: return id;
092: }
093:
094: public void setId(long id) {
095: this .id = id;
096: }
097:
098: @Override
099: public boolean equals(Object o) {
100: if (this == o) {
101: return true;
102: }
103: if (!(o instanceof UserSpaceRole)) {
104: return false;
105: }
106: final UserSpaceRole usr = (UserSpaceRole) o;
107: return ((space == usr.getSpace() || space
108: .equals(usr.getSpace()))
109: && user.equals(usr.getUser()) && roleKey.equals(usr
110: .getRoleKey()));
111: }
112:
113: @Override
114: public int hashCode() {
115: int hash = 7;
116: hash = hash * 31 + user.hashCode();
117: hash = hash * 31 + (space == null ? 0 : space.hashCode());
118: hash = hash * 31 + roleKey.hashCode();
119: return hash;
120: }
121:
122: @Override
123: public String toString() {
124: return getAuthority();
125: }
126:
127: }
|