1 package org.apache.fulcrum.security.model.dynamic.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.fulcrum.security.entity.Group;
25 import org.apache.fulcrum.security.entity.Permission;
26 import org.apache.fulcrum.security.entity.impl.SecurityEntityImpl;
27 import org.apache.fulcrum.security.model.dynamic.entity.DynamicRole;
28 import org.apache.fulcrum.security.util.GroupSet;
29 import org.apache.fulcrum.security.util.PermissionSet;
30
31 /**
32 * Represents the "simple" model where permissions are related to roles, roles
33 * are related to groups and groups are related to users, all in many to many
34 * relationships.
35 *
36 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
37 * @version $Id: DynamicRole.java 437451 2006-08-27 20:20:44Z tv $
38 */
39 public class DynamicRoleImpl extends SecurityEntityImpl implements DynamicRole
40 {
41 private Set<? extends Permission> permissionSet = new PermissionSet();
42
43 private Set<? extends Group> groupSet = new GroupSet();
44
45 /**
46 * Get the permission that are part of this role
47 *
48 * @return a set of permissions
49 */
50 public PermissionSet getPermissions()
51 {
52 if (permissionSet instanceof PermissionSet)
53 {
54 return (PermissionSet) permissionSet;
55 }
56 else
57 {
58 permissionSet = new PermissionSet(permissionSet);
59 return (PermissionSet) permissionSet;
60 }
61 }
62
63 /**
64 * Get the permission that are part of this role as Set
65 *
66 * @return a set of permissions
67 */
68 @SuppressWarnings("unchecked")
69 public <T extends Permission> Set<T> getPermissionsAsSet()
70 {
71 return (Set<T>) permissionSet;
72 }
73
74 /**
75 * Set the permission that are part of this role
76 *
77 * @param permissionSet
78 * a set of permissions
79 */
80 public void setPermissions(PermissionSet permissionSet)
81 {
82 if (permissionSet != null)
83 {
84 this.permissionSet = permissionSet;
85 }
86 else
87 {
88 this.permissionSet = new PermissionSet();
89 }
90 }
91
92 /**
93 * Set the permission that are part of this role as Set
94 *
95 * @param permissions
96 * a set of permissions
97 */
98 public <T extends Permission> void setPermissionsAsSet(Set<T> permissions)
99 {
100 this.permissionSet = permissions;
101 }
102
103 /**
104 * This method should only be used by a RoleManager. Not directly.
105 *
106 * @param permission the Permission to add
107 */
108 public void addPermission(Permission permission)
109 {
110 getPermissions().add(permission);
111 }
112
113 /**
114 * This method should only be used by a RoleManager. Not directly.
115 *
116 * @param permission the Permission to remove
117 */
118 public void removePermission(Permission permission)
119 {
120 getPermissions().remove(permission);
121 }
122
123 /**
124 * Get the groups this role belongs to
125 *
126 * @return a set of groups
127 */
128 public GroupSet getGroups()
129 {
130 if (groupSet instanceof GroupSet)
131 {
132 return (GroupSet) groupSet;
133 }
134 else
135 {
136 groupSet = new GroupSet(groupSet);
137 return (GroupSet) groupSet;
138 }
139 }
140
141 /**
142 * Set the groups this role belongs to
143 *
144 * @param groupSet
145 * the set of groups
146 */
147 public void setGroups(GroupSet groupSet)
148 {
149 if (groupSet != null)
150 {
151 this.groupSet = groupSet;
152 }
153 else
154 {
155 this.groupSet = new GroupSet();
156 }
157 }
158
159 /**
160 * This method should only be used by a RoleManager. Not directly.
161 *
162 * @param group the Group to remove
163 */
164 public void removeGroup(Group group)
165 {
166 getGroups().remove(group);
167 }
168
169 /**
170 * This method should only be used by a RoleManager. Not directly.
171 *
172 * @param group the Group to add
173 */
174 public void addGroup(Group group)
175 {
176 getGroups().add(group);
177 }
178
179 /**
180 * Set the groups this role belongs to as a Set
181 *
182 * @param groups
183 * the set of groups
184 */
185 public <T extends Group> void setGroupsAsSet(Set<T> groups)
186 {
187 this.groupSet = groups;
188 }
189
190 /**
191 * Get the groups this role belongs to as a Set
192 *
193 * @return a set of groups
194 */
195 @SuppressWarnings("unchecked")
196 public <T extends Group> Set<T> getGroupsAsSet()
197 {
198 return (Set<T>) groupSet;
199 }
200 }