001package org.apache.fulcrum.security.hibernate.basic;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021import org.apache.fulcrum.security.entity.Group;
022import org.apache.fulcrum.security.entity.User;
023import org.apache.fulcrum.security.hibernate.PersistenceHelper;
024import org.apache.fulcrum.security.model.basic.BasicModelManager;
025import org.apache.fulcrum.security.model.basic.entity.BasicGroup;
026import org.apache.fulcrum.security.model.basic.entity.BasicUser;
027import org.apache.fulcrum.security.spi.AbstractManager;
028import org.apache.fulcrum.security.util.DataBackendException;
029import org.apache.fulcrum.security.util.UnknownEntityException;
030import org.hibernate.Session;
031import org.hibernate.Transaction;
032
033/**
034 * This implementation persists to a database via Hibernate.
035 * 
036 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
037 * @version $Id: HibernateModelManagerImpl.java 1374014 2012-08-16 19:47:27Z tv
038 *          $
039 */
040public class HibernateModelManagerImpl extends AbstractManager implements BasicModelManager
041{
042    private PersistenceHelper persistenceHelper;
043
044    /**
045     * Puts a user in a group.
046     * 
047     * This method is used when adding a user to a group
048     * 
049     * @param user
050     *            the User.
051     * @throws DataBackendException
052     *             if there was an error accessing the data backend.
053     * @throws UnknownEntityException
054     *             if the account is not present.
055     */
056    public synchronized void grant(User user, Group group) throws DataBackendException, UnknownEntityException
057    {
058        boolean groupExists = false;
059        boolean userExists = false;
060        Transaction transaction = null;
061
062        try
063        {
064            groupExists = getGroupManager().checkExists(group);
065            userExists = getUserManager().checkExists(user);
066            if (groupExists && userExists)
067            {
068                Session session = getPersistenceHelper().retrieveSession();
069                transaction = session.beginTransaction();
070                ((BasicUser) user).addGroup(group);
071                ((BasicGroup) group).addUser(user);
072                session.update(user);
073                session.update(group);
074                transaction.commit();
075                transaction = null;
076            }
077        }
078        catch (Exception e)
079        {
080            throw new DataBackendException("grant(Role,Permission) failed", e);
081        }
082        finally
083        {
084            if (transaction != null)
085            {
086                transaction.rollback();
087            }
088        }
089        if (!groupExists)
090        {
091            throw new UnknownEntityException("Unknown group '" + group.getName() + "'");
092        }
093        if (!userExists)
094        {
095            throw new UnknownEntityException("Unknown user '" + user.getName() + "'");
096        }
097    }
098
099    /**
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}