View Javadoc
1   package org.apache.fulcrum.security.model.dynamic;
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.fulcrum.security.entity.Group;
23  import org.apache.fulcrum.security.entity.Permission;
24  import org.apache.fulcrum.security.entity.Role;
25  import org.apache.fulcrum.security.entity.User;
26  import org.apache.fulcrum.security.model.dynamic.entity.DynamicGroup;
27  import org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission;
28  import org.apache.fulcrum.security.model.dynamic.entity.DynamicRole;
29  import org.apache.fulcrum.security.model.dynamic.entity.DynamicUser;
30  import org.apache.fulcrum.security.spi.AbstractManager;
31  import org.apache.fulcrum.security.util.DataBackendException;
32  import org.apache.fulcrum.security.util.UnknownEntityException;
33  
34  /**
35   * Holds shared functionality between different implementations of
36   * DyanamicModelManager's.
37   * 
38   * @author <a href="mailto:epugh@upstate.com">Eric Pugh </a>
39   * @author <a href="mailto:ben@gidley.co.uk">Ben Gidley</a>
40   * @version $Id: AbstractDynamicModelManager.java,v 1.2 2004/07/07 18:18:09
41   *          epugh Exp $
42   */
43  public abstract class AbstractDynamicModelManager extends AbstractManager implements DynamicModelManager
44  {
45      /**
46       * Revokes all roles from a permission
47       * 
48       * This method is used when deleting a permission.
49       * 
50       * @param permission
51       *            the permission.
52       * @throws DataBackendException
53       *             if there was an error accessing the data backend.
54       * @throws UnknownEntityException
55       *             if the account is not present.
56       */
57      public synchronized void revokeAll(Permission permission) throws DataBackendException, UnknownEntityException
58      {
59          boolean permissionExists = false;
60          permissionExists = getPermissionManager().checkExists(permission);
61          if (permissionExists)
62          {
63              Object roles[] = ((DynamicPermission) permission).getRoles().toArray();
64              for (Object role2 : roles)
65              {
66                  revoke((Role) role2, permission);
67              }
68  
69              return;
70          }
71          else
72          {
73              throw new UnknownEntityException("Unknown permission '" + permission.getName() + "'");
74          }
75      }
76  
77      /**
78       * Revokes all users and roles from a group
79       * 
80       * This method is used when deleting a group.
81       * 
82       * @param group
83       *            the Group.
84       * @throws DataBackendException
85       *             if there was an error accessing the data backend.
86       * @throws UnknownEntityException
87       *             if the account is not present.
88       */
89      public synchronized void revokeAll(Group group) throws DataBackendException, UnknownEntityException
90      {
91          boolean groupExists = false;
92          groupExists = getGroupManager().checkExists(group);
93          if (groupExists)
94          {
95              Object users[] = ((DynamicGroup) group).getUsers().toArray();
96              for (Object user : users)
97              {
98                  revoke((User) user, group);
99              }
100 
101             Object roles[] = ((DynamicGroup) group).getRoles().toArray();
102             for (Object role2 : roles)
103             {
104                 revoke(group, (Role) role2);
105             }
106 
107             return;
108         }
109         else
110         {
111             throw new UnknownEntityException("Unknown group '" + group.getName() + "'");
112         }
113     }
114 
115     /**
116      * Revokes all groups from a user
117      * 
118      * This method is used when deleting an account.
119      * 
120      * @param user
121      *            the User.
122      * @throws DataBackendException
123      *             if there was an error accessing the data backend.
124      * @throws UnknownEntityException
125      *             if the account is not present.
126      */
127     public synchronized void revokeAll(User user) throws DataBackendException, UnknownEntityException
128     {
129         boolean userExists = false;
130         userExists = getUserManager().checkExists(user);
131         if (userExists)
132         {
133             Object groups[] = ((DynamicUser) user).getGroups().toArray();
134             for (Object group : groups)
135             {
136                 revoke(user, (Group) group);
137             }
138 
139             return;
140         }
141         else
142         {
143             throw new UnknownEntityException("Unknown user '" + user.getName() + "'");
144         }
145     }
146 
147     /**
148      * Revokes all permissions and groups from a Role.
149      * 
150      * This method is used when deleting a Role.
151      * 
152      * @param role
153      *            the Role
154      * @throws DataBackendException
155      *             if there was an error accessing the data backend.
156      * @throws UnknownEntityException
157      *             if the Role is not present.
158      */
159     public synchronized void revokeAll(Role role) throws DataBackendException, UnknownEntityException
160     {
161         boolean roleExists = false;
162         roleExists = getRoleManager().checkExists(role);
163         if (roleExists)
164         {
165             Object groups[] = ((DynamicRole) role).getGroups().toArray();
166             for (Object group : groups)
167             {
168                 revoke((Group) group, role);
169             }
170 
171             Object permissions[] = ((DynamicRole) role).getPermissions().toArray();
172             for (Object permission : permissions)
173             {
174                 revoke(role, (Permission) permission);
175             }
176         }
177         else
178         {
179             throw new UnknownEntityException("Unknown role '" + role.getName() + "'");
180         }
181 
182     }
183 
184     /**
185      * It is expected the real implementation will overide this and save either
186      * side of the function. It is not abstract as a in memory implementation
187      * would not need to do anything.
188      */
189     public void addDelegate(User"../../../../../../org/apache/fulcrum/security/entity/User.html#User">User delegator, User delegatee) throws DataBackendException, UnknownEntityException
190     {
191         DynamicUser../org/apache/fulcrum/security/model/dynamic/entity/DynamicUser.html#DynamicUser">DynamicUser dynamicDelegator = (DynamicUser) delegator;
192         DynamicUser../org/apache/fulcrum/security/model/dynamic/entity/DynamicUser.html#DynamicUser">DynamicUser dynamicDelegatee = (DynamicUser) delegatee;
193 
194         // check it hasn't already been done
195         // It is NOT an error to call this twice
196         if (!(dynamicDelegator.getDelegatees().contains(delegatee) || dynamicDelegatee.getDelegators().contains(delegator)))
197         {
198             dynamicDelegator.getDelegatees().add(dynamicDelegatee);
199             dynamicDelegatee.getDelegators().add(dynamicDelegator);
200         }
201     }
202 
203     /**
204      * Implementors should overide this to save and call super if they want the
205      * base class to do the work
206      */
207     public void removeDelegate(User"../../../../../../org/apache/fulcrum/security/entity/User.html#User">User delegator, User delegatee) throws DataBackendException, UnknownEntityException
208     {
209         DynamicUser../org/apache/fulcrum/security/model/dynamic/entity/DynamicUser.html#DynamicUser">DynamicUser dynamicDelegator = (DynamicUser) delegator;
210         DynamicUser../org/apache/fulcrum/security/model/dynamic/entity/DynamicUser.html#DynamicUser">DynamicUser dynamicDelegatee = (DynamicUser) delegatee;
211 
212         if (dynamicDelegator.getDelegatees().contains(dynamicDelegatee))
213         {
214             dynamicDelegator.getDelegatees().remove(dynamicDelegatee);
215             dynamicDelegatee.getDelegators().remove(dynamicDelegator);
216         }
217         else
218         {
219             throw new UnknownEntityException("Tried to remove a delegate that does not exist");
220         }
221 
222     }
223 }