View Javadoc
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.Role;
25  import org.apache.fulcrum.security.entity.impl.SecurityEntityImpl;
26  import org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission;
27  import org.apache.fulcrum.security.util.RoleSet;
28  
29  /**
30   * Represents the "simple" model where permissions are related to roles, roles
31   * are related to groups and groups are related to users, all in many to many
32   * relationships.
33   * 
34   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
35   * @version $Id: DynamicPermission.java 223059 2004-07-07 16:49:09Z epugh $
36   */
37  public class DynamicPermissionImpl extends SecurityEntityImpl implements DynamicPermission
38  {
39      private Set<? extends Role> roleSet = new RoleSet();
40  
41      /**
42       * Get the roles that this permission belongs to
43       * 
44       * @return a set of roles
45       */
46      public RoleSet getRoles()
47      {
48          if (roleSet instanceof RoleSet)
49          {
50              return (RoleSet) roleSet;
51          }
52          else
53          {
54              roleSet = new RoleSet(roleSet);
55              return (RoleSet) roleSet;
56          }
57      }
58  
59      /**
60       * Set the roles that this permission belongs to
61       * 
62       * @param roleSet
63       *            a set of roles
64       */
65      public void setRoles(RoleSet roleSet)
66      {
67          if (roleSet != null)
68          {
69              this.roleSet = roleSet;
70          }
71          else
72          {
73              this.roleSet = new RoleSet();
74          }
75      }
76  
77      /**
78       * Add a role to this permission
79       * 
80       * @param role
81       *            the role to add
82       */
83      public void addRole(Role role)
84      {
85          getRoles().add(role);
86      }
87  
88      /**
89       * Remove a role from this permission
90       * 
91       * @param role
92       *            the role to remove
93       */
94      public void removeRole(Role role)
95      {
96          getRoles().remove(role);
97      }
98  
99      /**
100      * Set the roles that this permission belongs to as Set
101      * 
102      * @param roles
103      *            a set of roles
104      */
105     public <T extends Role> void setRolesAsSet(Set<T> roles)
106     {
107         this.roleSet = roles;
108     }
109 
110     /**
111      * Get the roles that this permission belongs to as Set
112      * 
113      * @return a set of roles
114      */
115     @SuppressWarnings("unchecked")
116     public <T extends Role> Set<T> getRolesAsSet()
117     {
118         return (Set<T>) roleSet;
119     }
120 }