1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.businessobjects.security;
16
17 import org.figure8.join.core.EntityObject;
18 /**
19 * This is an entity representing a Permission : a binding between
20 * a security <code>Role</code> and a <code>User</code>. Permission
21 * can be relative to a specified resource (that should be another
22 * <code>EntityObject</code>)
23 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
24 * @version $Revision: 1.1 $
25 *
26 * @hibernate.class table="join_permissions"
27 * @hibernate.cache usage="read-write"
28 *
29 * @hibernate.query name="join.permission_findByUserId" query="from Permission permission where permission.userId = :id"
30 */
31 public class Permission extends EntityObject{
32
33
34
35 /** The security role asscoiated with this permission */
36 private Role role;
37 /** The user associated with this permission */
38 private String userId;
39 /** The identifier of resource associated with this permission */
40 private String resourceId;
41
42
43
44
45 /**
46 * Creates a new instance of Permission
47 */
48 public Permission(){
49 }
50
51 /**
52 * Creates a new instance of Permission
53 * @param role The security role associated to this permission
54 * @param userId The unique identifier of user associated with this permission
55 */
56 public Permission(Role role, String userId){
57 this.role = role;
58 this.userId = userId;
59 this.resourceId = null;
60 }
61
62 /**
63 * Creates a new instance of Permission
64 * @param role The security role associated to this permission
65 * @param userId The unique identifier of user associated with this permission
66 * @param resourceId The idenitfier of resource entity to whow applies this permission
67 */
68 public Permission(Role role, String userId, String resourceId){
69 this.role = role;
70 this.userId = userId;
71 this.resourceId = resourceId;
72 }
73
74
75
76
77 /**
78 * @hibernate.many-to-one column="s_role_fk" not-null="true"
79 * @return The security role associated to this permission
80 */
81 public Role getRole(){
82 return role;
83 }
84 /** @param role Role associated to this permission */
85 public void setRole(Role role){
86 this.role = role;
87 }
88
89 /**
90 * @hibernate.property column="s_userid" not-null="true" length="20"
91 * @return The unique identifier of the user which has this permission
92 */
93 public String getUserId(){
94 return userId;
95 }
96 /** @param userId Unique identifier of the user whom belongs this permission */
97 public void setUserId(String userId){
98 this.userId = userId;
99 }
100
101 /**
102 * @hibernate.property column="s_resourceid"
103 * @return The identifier of the resource to whom this permission applies
104 */
105 public String getResourceId(){
106 return resourceId;
107 }
108 /** @param resourceId The identifier of the resource to whom this permission applies */
109 public void setResourceId(String resourceId){
110 this.resourceId = resourceId;
111 }
112
113 /** @return true if this Permission doesn't apply to a specific resource */
114 public boolean isGlobalPermission(){
115 return !isResourcePermission();
116 }
117 /** @return true if this Permission applies to a specific resource */
118 public boolean isResourcePermission(){
119 return (getResourceId() != null);
120 }
121
122
123
124
125 /** @return Get the string representation of this permission */
126 public String toString(){
127 StringBuffer buffer = new StringBuffer("Permission with id: " + getId());
128 buffer.append("\n").append("for user: ").append(getUserId());
129 buffer.append("\n").append("and role: ").append(getRole().getName());
130 if (isResourcePermission())
131 buffer.append("\n").append("on resource: ").append(getResourceId());
132 return buffer.toString();
133 }
134 }