001package org.apache.fulcrum.security.model.dynamic.entity.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Set;
023
024import org.apache.fulcrum.security.entity.Group;
025import org.apache.fulcrum.security.entity.Permission;
026import org.apache.fulcrum.security.entity.impl.SecurityEntityImpl;
027import org.apache.fulcrum.security.model.dynamic.entity.DynamicRole;
028import org.apache.fulcrum.security.util.GroupSet;
029import org.apache.fulcrum.security.util.PermissionSet;
030
031/**
032 * Represents the "simple" model where permissions are related to roles, roles
033 * are related to groups and groups are related to users, all in many to many
034 * relationships.
035 * 
036 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
037 * @version $Id: DynamicRole.java 437451 2006-08-27 20:20:44Z tv $
038 */
039public class DynamicRoleImpl extends SecurityEntityImpl implements DynamicRole
040{
041    private Set<? extends Permission> permissionSet = new PermissionSet();
042
043    private Set<? extends Group> groupSet = new GroupSet();
044
045    /**
046     * Get the permission that are part of this role
047     * 
048     * @return a set of permissions
049     */
050    public PermissionSet getPermissions()
051    {
052        if (permissionSet instanceof PermissionSet)
053        {
054            return (PermissionSet) permissionSet;
055        }
056        else
057        {
058            permissionSet = new PermissionSet(permissionSet);
059            return (PermissionSet) permissionSet;
060        }
061    }
062
063    /**
064     * Get the permission that are part of this role as Set
065     * 
066     * @return a set of permissions
067     */
068    @SuppressWarnings("unchecked")
069    public <T extends Permission> Set<T> getPermissionsAsSet()
070    {
071        return (Set<T>) permissionSet;
072    }
073
074    /**
075     * Set the permission that are part of this role
076     * 
077     * @param permissionSet
078     *            a set of permissions
079     */
080    public void setPermissions(PermissionSet permissionSet)
081    {
082        if (permissionSet != null)
083        {
084            this.permissionSet = permissionSet;
085        }
086        else
087        {
088            this.permissionSet = new PermissionSet();
089        }
090    }
091
092    /**
093     * Set the permission that are part of this role as Set
094     * 
095     * @param permissions
096     *            a set of permissions
097     */
098    public <T extends Permission> void setPermissionsAsSet(Set<T> permissions)
099    {
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}