View Javadoc
1   package org.apache.fulcrum.security.torque.basic;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  import java.sql.Connection;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.fulcrum.security.entity.Group;
25  import org.apache.fulcrum.security.entity.User;
26  import org.apache.fulcrum.security.model.basic.BasicModelManager;
27  import org.apache.fulcrum.security.model.basic.entity.BasicGroup;
28  import org.apache.fulcrum.security.model.basic.entity.BasicUser;
29  import org.apache.fulcrum.security.spi.AbstractManager;
30  import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
31  import org.apache.fulcrum.security.util.DataBackendException;
32  import org.apache.fulcrum.security.util.UnknownEntityException;
33  import org.apache.torque.TorqueException;
34  import org.apache.torque.util.Transaction;
35  /**
36   * This implementation persists to a database via Torque.
37   *
38   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
39   * @version $Id:$
40   */
41  public class TorqueBasicModelManagerImpl extends AbstractManager implements BasicModelManager
42  {
43      /**
44       * Puts a user in a group.
45       *
46       * This method is used when adding a user to a group
47       *
48       * @param user the User.
49       * @throws DataBackendException if there was an error accessing the data backend.
50       * @throws UnknownEntityException if the account is not present.
51       */
52      @Override
53  	public synchronized void grant(User user, Group group) throws DataBackendException, UnknownEntityException
54      {
55          boolean groupExists = getGroupManager().checkExists(group);
56          boolean userExists = getUserManager().checkExists(user);
57  
58          if (groupExists && userExists)
59          {
60              ((BasicUser) user).addGroup(group);
61              ((BasicGroup) group).addUser(user);
62  
63              Connection con = null;
64  
65              try
66              {
67              	con = Transaction.begin();
68  
69                  ((TorqueAbstractSecurityEntity)user).update(con);
70                  ((TorqueAbstractSecurityEntity)group).update(con);
71  
72                  Transaction.commit(con);
73                  con = null;
74              }
75              catch (TorqueException e)
76              {
77                  throw new DataBackendException("grant('" + user.getName() + user.getId() + "', '" + group.getName() + group.getId() + "') failed", e);
78              }
79              finally
80              {
81                  if (con != null)
82                  {
83                      Transaction.safeRollback(con);
84                  }
85              }
86  
87              return;
88          }
89  
90          if (!groupExists)
91          {
92              throw new UnknownEntityException("Unknown group '" + group.getName() + "'");
93          }
94  
95          if (!userExists)
96          {
97              throw new UnknownEntityException("Unknown user '" + user.getName() + "'");
98          }
99      }
100 
101     /**
102      * Removes a user in a group.
103      *
104      * This method is used when removing a user to a group
105      *
106      * @param user the User.
107      * @throws DataBackendException if there was an error accessing the data backend.
108      * @throws UnknownEntityException if the user or group is not present.
109      */
110     @Override
111 	public synchronized void revoke(User user, Group group) throws DataBackendException, UnknownEntityException
112     {
113         boolean groupExists = getGroupManager().checkExists(group);
114         boolean userExists = getUserManager().checkExists(user);
115 
116         if (groupExists && userExists)
117         {
118             ((BasicUser) user).removeGroup(group);
119             ((BasicGroup) group).removeUser(user);
120 
121             Connection con = null;
122 
123             try
124             {
125             	con = Transaction.begin();
126 
127                 ((TorqueAbstractSecurityEntity)user).update(con);
128                 ((TorqueAbstractSecurityEntity)group).update(con);
129 
130                 Transaction.commit(con);
131                 con = null;
132             }
133             catch (TorqueException e)
134             {
135                 throw new DataBackendException("grant('" + user.getName() + user.getId() + "', '" + group.getName() + group.getId() + "') failed", e);
136             }
137             finally
138             {
139                 if (con != null)
140                 {
141                     Transaction.safeRollback(con);
142                 }
143             }
144 
145             return;
146         }
147 
148         if (!groupExists)
149         {
150             throw new UnknownEntityException("Unknown group '" + group.getName() + "'");
151         }
152 
153         if (!userExists)
154         {
155             throw new UnknownEntityException("Unknown user '" + user.getName() + "'");
156         }
157     }
158 
159     /**
160      * Revokes all groups from a user
161      *
162      * This method is used when deleting an account.
163      *
164      * @param user the User.
165      * @throws DataBackendException if there was an error accessing the data backend.
166      * @throws UnknownEntityException if the account is not present.
167      */
168     @Override
169 	public synchronized void revokeAll(User user)
170         throws DataBackendException, UnknownEntityException
171     {
172         boolean userExists = getUserManager().checkExists(user);
173 
174         if (userExists)
175         {
176             BasicUser u = (BasicUser) user;
177 
178             // copy to avoid ConcurrentModificationException
179             List<Group> groups = new ArrayList<Group>(u.getGroups());
180 
181             for (Group group : groups)
182             {
183                 u.removeGroup(group);
184             }
185 
186             Connection con = null;
187 
188             try
189             {
190             	con = Transaction.begin();
191 
192                 ((TorqueAbstractSecurityEntity)user).update(con);
193 
194                 Transaction.commit(con);
195                 con = null;
196             }
197             catch (TorqueException e)
198             {
199                 throw new DataBackendException("revokeAll('" + user.getName() + user.getId() + "') failed", e);
200             }
201             finally
202             {
203                 if (con != null)
204                 {
205                     Transaction.safeRollback(con);
206                 }
207             }
208 
209             return;
210         }
211         else
212         {
213             throw new UnknownEntityException("Unknown user '" + user.getName() + "'");
214         }
215     }
216 }