View Javadoc
1   package org.apache.fulcrum.security.hibernate.basic;
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  import org.apache.fulcrum.security.entity.Group;
22  import org.apache.fulcrum.security.entity.User;
23  import org.apache.fulcrum.security.hibernate.PersistenceHelper;
24  import org.apache.fulcrum.security.model.basic.BasicModelManager;
25  import org.apache.fulcrum.security.model.basic.entity.BasicGroup;
26  import org.apache.fulcrum.security.model.basic.entity.BasicUser;
27  import org.apache.fulcrum.security.spi.AbstractManager;
28  import org.apache.fulcrum.security.util.DataBackendException;
29  import org.apache.fulcrum.security.util.UnknownEntityException;
30  import org.hibernate.Session;
31  import org.hibernate.Transaction;
32  
33  /**
34   * This implementation persists to a database via Hibernate.
35   * 
36   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
37   * @version $Id: HibernateModelManagerImpl.java 1374014 2012-08-16 19:47:27Z tv
38   *          $
39   */
40  public class HibernateModelManagerImpl extends AbstractManager implements BasicModelManager
41  {
42      private PersistenceHelper persistenceHelper;
43  
44      /**
45       * Puts a user in a group.
46       * 
47       * This method is used when adding a user to a group
48       * 
49       * @param user
50       *            the User.
51       * @throws DataBackendException
52       *             if there was an error accessing the data backend.
53       * @throws UnknownEntityException
54       *             if the account is not present.
55       */
56      public synchronized void grant(User user, Group group) throws DataBackendException, UnknownEntityException
57      {
58          boolean groupExists = false;
59          boolean userExists = false;
60          Transaction transaction = null;
61  
62          try
63          {
64              groupExists = getGroupManager().checkExists(group);
65              userExists = getUserManager().checkExists(user);
66              if (groupExists && userExists)
67              {
68                  Session session = getPersistenceHelper().retrieveSession();
69                  transaction = session.beginTransaction();
70                  ((BasicUser) user).addGroup(group);
71                  ((BasicGroup) group).addUser(user);
72                  session.update(user);
73                  session.update(group);
74                  transaction.commit();
75                  transaction = null;
76              }
77          }
78          catch (Exception e)
79          {
80              throw new DataBackendException("grant(Role,Permission) failed", e);
81          }
82          finally
83          {
84              if (transaction != null)
85              {
86                  transaction.rollback();
87              }
88          }
89          if (!groupExists)
90          {
91              throw new UnknownEntityException("Unknown group '" + group.getName() + "'");
92          }
93          if (!userExists)
94          {
95              throw new UnknownEntityException("Unknown user '" + user.getName() + "'");
96          }
97      }
98  
99      /**
100      * Removes a user in a group.
101      * 
102      * This method is used when removing a user to a group
103      * 
104      * @param user
105      *            the User.
106      * @throws DataBackendException
107      *             if there was an error accessing the data backend.
108      * @throws UnknownEntityException
109      *             if the user or group is not present.
110      */
111     public synchronized void revoke(User user, Group group) throws DataBackendException, UnknownEntityException
112     {
113         boolean groupExists = false;
114         boolean userExists = false;
115         Transaction transaction = null;
116 
117         try
118         {
119             groupExists = getGroupManager().checkExists(group);
120             userExists = getUserManager().checkExists(user);
121             if (groupExists && userExists)
122             {
123                 Session session = getPersistenceHelper().retrieveSession();
124                 transaction = session.beginTransaction();
125                 ((BasicUser) user).removeGroup(group);
126                 ((BasicGroup) group).removeUser(user);
127                 session.update(user);
128                 session.update(group);
129                 transaction.commit();
130                 transaction = null;
131             }
132         }
133         catch (Exception e)
134         {
135             throw new DataBackendException("grant(Role,Permission) failed", e);
136         }
137         finally
138         {
139             if (transaction != null)
140             {
141                 transaction.rollback();
142             }
143         }
144         if (!groupExists)
145         {
146             throw new UnknownEntityException("Unknown group '" + group.getName() + "'");
147         }
148         if (!userExists)
149         {
150             throw new UnknownEntityException("Unknown user '" + user.getName() + "'");
151         }
152     }
153 
154     /**
155      * Revokes all groups from a user
156      * 
157      * This method is used when deleting an account.
158      * 
159      * @param user
160      *            the User.
161      * @throws DataBackendException
162      *             if there was an error accessing the data backend.
163      * @throws UnknownEntityException
164      *             if the account is not present.
165      */
166     public synchronized void revokeAll(User user) throws DataBackendException, UnknownEntityException
167     {
168         boolean userExists = false;
169         userExists = getUserManager().checkExists(user);
170         if (userExists)
171         {
172             Object groups[] = ((BasicUser) user).getGroups().toArray();
173 
174             for (Object group2 : groups)
175             {
176                 Group group = (Group) group2;
177                 revoke(user, group);
178             }
179 
180             return;
181         }
182         else
183         {
184             throw new UnknownEntityException("Unknown user '" + user.getName() + "'");
185         }
186     }
187 
188     /**
189      * @return Returns the persistenceHelper.
190      */
191     public PersistenceHelper getPersistenceHelper()
192     {
193         if (persistenceHelper == null)
194         {
195             persistenceHelper = (PersistenceHelper) resolve(PersistenceHelper.ROLE);
196         }
197         return persistenceHelper;
198     }
199 
200 }