001    package org.apache.turbine.om.security;
002    
003    
004    /*
005     * Licensed to the Apache Software Foundation (ASF) under one
006     * or more contributor license agreements.  See the NOTICE file
007     * distributed with this work for additional information
008     * regarding copyright ownership.  The ASF licenses this file
009     * to you under the Apache License, Version 2.0 (the
010     * "License"); you may not use this file except in compliance
011     * with the License.  You may obtain a copy of the License at
012     *
013     *   http://www.apache.org/licenses/LICENSE-2.0
014     *
015     * Unless required by applicable law or agreed to in writing,
016     * software distributed under the License is distributed on an
017     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018     * KIND, either express or implied.  See the License for the
019     * specific language governing permissions and limitations
020     * under the License.
021     */
022    
023    
024    import java.sql.Connection;
025    import java.util.Iterator;
026    
027    import org.apache.turbine.services.security.TurbineSecurity;
028    import org.apache.turbine.util.security.PermissionSet;
029    import org.apache.turbine.util.security.TurbineSecurityException;
030    
031    /**
032     * This class represents a role played by the User associated with the
033     * current Session.
034     *
035     * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
036     * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
037     * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
038     * @version $Id: TurbineRole.java 1078552 2011-03-06 19:58:46Z tv $
039     */
040    public class TurbineRole extends SecurityObject<Role> implements Role
041    {
042        /** Serial version */
043        private static final long serialVersionUID = -1354408789347969126L;
044    
045        /**
046         * Constructs a new Role
047         */
048        public TurbineRole()
049        {
050            super();
051        }
052    
053        /**
054         * Constructs a new Role with the sepcified name.
055         *
056         * @param name The name of the new object.
057         */
058        public TurbineRole(String name)
059        {
060            super(name);
061        }
062    
063        /** The permissions for this role. */
064        private PermissionSet permissionSet = null;
065    
066        /**
067         * Returns the set of Permissions associated with this Role.
068         *
069         * @return A PermissionSet.
070         * @exception Exception a generic exception.
071         */
072        public PermissionSet getPermissions()
073                throws Exception
074        {
075            return permissionSet;
076        }
077    
078        /**
079         * Sets the Permissions associated with this Role.
080         *
081         * @param permissionSet A PermissionSet.
082         */
083        public void setPermissions(PermissionSet permissionSet)
084        {
085            this.permissionSet = permissionSet;
086        }
087    
088        // These following methods are wrappers around TurbineSecurity
089    
090        /**
091         * Creates a new Role in the system.
092         *
093         * @param name The name of the new Role.
094         * @return An object representing the new Role.
095         * @throws TurbineSecurityException if the Role could not be created.
096         */
097        public Role create(String name)
098                throws TurbineSecurityException
099        {
100            //Role role = new Role(name);
101            Role role = new TurbineRole(name);
102            TurbineSecurity.addRole(role);
103            return role;
104        }
105    
106        /**
107         * Makes changes made to the Role attributes permanent.
108         *
109         * @throws TurbineSecurityException if there is a problem while
110         *  saving data.
111         */
112        public void save()
113                throws TurbineSecurityException
114        {
115            TurbineSecurity.saveRole(this);
116        }
117    
118        /**
119         * not implemented
120         *
121         * @param conn
122         * @throws Exception
123         */
124        public void save(Connection conn) throws Exception
125        {
126            throw new Exception("not implemented");
127        }
128    
129        /**
130         * not implemented
131         *
132         * @param dbname
133         * @throws Exception
134         */
135        public void save(String dbname) throws Exception
136        {
137            throw new Exception("not implemented");
138        }
139    
140        /**
141         * Removes a role from the system.
142         *
143         * @throws TurbineSecurityException if the Role could not be removed.
144         */
145        public void remove()
146                throws TurbineSecurityException
147        {
148            TurbineSecurity.removeRole(this);
149        }
150    
151        /**
152         * Renames the role.
153         *
154         * @param name The new Role name.
155         * @throws TurbineSecurityException if the Role could not be renamed.
156         */
157        public void rename(String name)
158                throws TurbineSecurityException
159        {
160            TurbineSecurity.renameRole(this, name);
161        }
162    
163        /**
164         * Grants a Permission to this Role.
165         *
166         * @param permission A Permission.
167         * @throws TurbineSecurityException if there is a problem while assigning
168         * the Permission.
169         */
170        public void grant(Permission permission)
171                throws TurbineSecurityException
172        {
173            TurbineSecurity.grant(this, permission);
174        }
175    
176        /**
177         * Grants Permissions from a PermissionSet to this Role.
178         *
179         * @param permissionSet A PermissionSet.
180         * @throws TurbineSecurityException if there is a problem while assigning
181         * the Permissions.
182         */
183        public void grant(PermissionSet permissionSet)
184                throws TurbineSecurityException
185        {
186            for (Iterator permissions = permissionSet.iterator(); permissions.hasNext();)
187            {
188                TurbineSecurity.grant(this, (Permission) permissions.next());
189            }
190        }
191    
192        /**
193         * Revokes a Permission from this Role.
194         *
195         * @param permission A Permission.
196         * @throws TurbineSecurityException if there is a problem while unassigning
197         * the Permission.
198         */
199        public void revoke(Permission permission)
200                throws TurbineSecurityException
201        {
202            TurbineSecurity.revoke(this, permission);
203        }
204    
205        /**
206         * Revokes Permissions from a PermissionSet from this Role.
207         *
208         * @param permissionSet A PermissionSet.
209         * @throws TurbineSecurityException if there is a problem while unassigning
210         * the Permissions.
211         */
212        public void revoke(PermissionSet permissionSet)
213                throws TurbineSecurityException
214        {
215            for (Iterator permissions = permissionSet.iterator(); permissions.hasNext();)
216            {
217                TurbineSecurity.revoke(this, (Permission) permissions.next());
218            }
219        }
220    }