View Javadoc
1   package org.apache.fulcrum.security.model.turbine;
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 org.apache.avalon.framework.configuration.Configurable;
23  import org.apache.avalon.framework.configuration.Configuration;
24  import org.apache.fulcrum.security.entity.Group;
25  import org.apache.fulcrum.security.entity.Permission;
26  import org.apache.fulcrum.security.entity.Role;
27  import org.apache.fulcrum.security.entity.User;
28  import org.apache.fulcrum.security.model.turbine.entity.TurbineGroup;
29  import org.apache.fulcrum.security.model.turbine.entity.TurbineRole;
30  import org.apache.fulcrum.security.model.turbine.entity.TurbineUser;
31  import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
32  import org.apache.fulcrum.security.spi.AbstractManager;
33  import org.apache.fulcrum.security.util.DataBackendException;
34  import org.apache.fulcrum.security.util.EntityExistsException;
35  import org.apache.fulcrum.security.util.UnknownEntityException;
36  
37  /**
38   * Holds shared functionality between different implementations of
39   * TurbineModelManager's.
40   * 
41   * @author <a href="mailto:epugh@upstate.com">Eric Pugh </a>
42   * @version $Id: AbstractDynamicModelManager.java,v 1.2 2004/07/07 18:18:09
43   *          epugh Exp $
44   */
45  public abstract class AbstractTurbineModelManager extends AbstractManager implements TurbineModelManager, Configurable
46  {
47  	
48      
49  	/**
50       * 
51       */
52      private static final long serialVersionUID = 1L;
53      
54      private String globalGroupName;
55      
56      //private boolean cascadeDelete;
57  	// ---------------- Avalon Lifecycle Methods ---------------------
58      /**
59       * Avalon component lifecycle method
60       */
61      @Override
62  	public void configure(Configuration conf)
63      {
64      	globalGroupName = conf.getAttribute(
65      			TurbineModelManager.GLOBAL_GROUP_ATTR_NAME,
66      			TurbineModelManager.GLOBAL_GROUP_NAME);
67      	//cascadeDelete = conf.getAttributeAsBoolean( TurbineModelManager.CASCADE_DELETE_ATTR_NAME, false );
68      }
69      
70      /**
71       * Provides a reference to the Group object that represents the <a
72       * href="#global">global group </a>.
73       * 
74       * @return A Group object that represents the global group.
75       */
76      @Override
77  	public Group getGlobalGroup() throws DataBackendException
78      {
79          Group g = null;
80          try
81          {
82              g = getGroupManager().getGroupByName(globalGroupName);
83          }
84          catch (UnknownEntityException uee)
85          {
86              g = getGroupManager().getGroupInstance(globalGroupName);
87              try
88              {
89                  getGroupManager().addGroup(g);
90              }
91              catch (EntityExistsException eee)
92              {
93                  throw new DataBackendException(eee.getMessage(), eee);
94              }
95  
96          }
97          return g;
98      }
99  
100     /**
101      * Revokes all permissions from a Role.
102      * 
103      * This method is used when deleting a Role.
104      * 
105      * @param role
106      *            the Role
107      * @throws DataBackendException
108      *             if there was an error accessing the data backend.
109      * @throws UnknownEntityException
110      *             if the Role is not present.
111      */
112     @Override
113 	public synchronized void revokeAll(Role role) throws DataBackendException, UnknownEntityException
114     {
115         revokeAll( role, false );
116     }
117     
118     /**
119      * Revokes by default all permissions from a Role and if flag is set
120      * all groups and users for this role
121      * 
122      * This method is used when deleting a Role.
123      * 
124      * @param role
125      *            the Role
126      * @param cascadeDelete
127      *             if <code>true </code> removes all groups and user for this role.
128      * @throws DataBackendException
129      *             if there was an error accessing the data backend.
130      * @throws UnknownEntityException
131      *             if the Role is not present.
132      */
133     @Override
134     public synchronized void revokeAll(Role role, boolean cascadeDelete) throws DataBackendException, UnknownEntityException
135     {
136         boolean roleExists = false;
137         roleExists = getRoleManager().checkExists(role);
138         if (roleExists)
139         {
140 
141             Object permissions[] = ((TurbineRole) role).getPermissions().toArray();
142             for (Object permission : permissions)
143             {
144                 revoke(role, (Permission) permission);
145             }
146             if (cascadeDelete) {
147                 Object userGroupRoles[] = ((TurbineRole) role).getUserGroupRoleSet().toArray();
148                 for (Object userGroupRole : userGroupRoles)
149                 {
150                     TurbineUserGroupRole/../../org/apache/fulcrum/security/model/turbine/entity/TurbineUserGroupRole.html#TurbineUserGroupRole">TurbineUserGroupRole ugr = (TurbineUserGroupRole) userGroupRole;
151                     revoke(ugr.getUser(), ugr.getGroup(), role);
152                 }
153             }
154         }
155         else
156         {
157             throw new UnknownEntityException("Unknown role '" + role.getName() + "'");
158         }
159 
160     }
161 
162     /**
163      * Revokes all roles and groups from a User.
164      * 
165      * This method is used when deleting a User.
166      * 
167      * @param user
168      *            the User
169      * @throws DataBackendException
170      *             if there was an error accessing the data backend.
171      * @throws UnknownEntityException
172      *             if the Role is not present.
173      */
174     @Override
175 	public synchronized void revokeAll(User user) throws DataBackendException, UnknownEntityException
176     {
177         boolean userExists = false;
178         userExists = getUserManager().checkExists(user);
179         if (userExists)
180         {
181 
182             Object userGroupRoles[] = ((TurbineUser) user).getUserGroupRoleSet().toArray();
183             for (Object userGroupRole : userGroupRoles)
184             {
185                 TurbineUserGroupRole/../../org/apache/fulcrum/security/model/turbine/entity/TurbineUserGroupRole.html#TurbineUserGroupRole">TurbineUserGroupRole ugr = (TurbineUserGroupRole) userGroupRole;
186                 revoke(user, ugr.getGroup(), ugr.getRole());
187             }
188         }
189         else
190         {
191             throw new UnknownEntityException("Unknown user '" + user.getName() + "'");
192         }
193     }
194     
195     /**
196      * Revokes all roles and users from a Group.
197      * 
198      * This method is used when deleting a User.
199      * 
200      * @param group
201      *            the Group
202      * @throws DataBackendException
203      *             if there was an error accessing the data backend.
204      * @throws UnknownEntityException
205      *             if the Group is not present.
206      */
207     @Override
208     public synchronized void revokeAll(Group group) throws DataBackendException, UnknownEntityException
209     {
210         boolean groupExists = false;
211         groupExists = getGroupManager().checkExists(group);
212         if (groupExists)
213         {
214 
215             Object userGroupRoles[] = ((TurbineGroup) group).getUserGroupRoleSet().toArray();
216             for (Object userGroupRole : userGroupRoles)
217             {
218                 TurbineUserGroupRole/../../org/apache/fulcrum/security/model/turbine/entity/TurbineUserGroupRole.html#TurbineUserGroupRole">TurbineUserGroupRole ugr = (TurbineUserGroupRole) userGroupRole;
219                 revoke(ugr.getUser(), group, ugr.getRole());
220             }
221         }
222         else
223         {
224             throw new UnknownEntityException("Unknown group '" + group.getName() + "'");
225         }
226     }
227 
228 	@Override
229 	public String getGlobalGroupName() {
230 		return globalGroupName;
231 	}
232 }