001package org.apache.fulcrum.security.model.turbine.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.Role;
025import org.apache.fulcrum.security.entity.impl.SecurityEntityImpl;
026import org.apache.fulcrum.security.model.turbine.entity.TurbinePermission;
027import org.apache.fulcrum.security.util.RoleSet;
028
029/**
030 * Represents the "turbine" model where permissions are in a many to many
031 * relationship to roles, roles are related to groups are related to users, all
032 * in many to many relationships.
033 * 
034 * @author <a href="mailto:epugh@upstate.com">Eric Pugh </a>
035 * @version $Id: TurbinePermission.java 223081 2004-10-07 15:11:58Z epugh $
036 */
037public class TurbinePermissionImpl extends SecurityEntityImpl implements TurbinePermission
038{
039    private Set<? extends Role> roleSet = new RoleSet();
040
041    /**
042     * Get the roles that this permission belongs to
043     * 
044     * @return a set of roles
045     */
046    public RoleSet getRoles()
047    {
048        if (roleSet instanceof RoleSet)
049        {
050            return (RoleSet) roleSet;
051        }
052        else
053        {
054            roleSet = new RoleSet(roleSet);
055            return (RoleSet) roleSet;
056        }
057    }
058
059    /**
060     * Set the roles that this permission belongs to
061     * 
062     * @param roleSet
063     *            a set of roles
064     */
065    public void setRoles(RoleSet roleSet)
066    {
067        if (roleSet != null)
068        {
069            this.roleSet = roleSet;
070        }
071        else
072        {
073            this.roleSet = new RoleSet();
074        }
075    }
076
077    /**
078     * Add a role to this permission
079     * 
080     * @param role
081     *            the role to add
082     */
083    public void addRole(Role role)
084    {
085        getRoles().add(role);
086    }
087
088    /**
089     * Remove a role from this permission
090     * 
091     * @param role
092     *            the role to remove
093     */
094    public void removeRole(Role role)
095    {
096        getRoles().remove(role);
097    }
098
099    /**
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}