head	1.22;
access;
symbols
	TIGRIS_0_9_2_4:1.15.2.1
	TIGRIS_0_9_2_3:1.15.2.1
	HELM_PEER_PORT_BRANCH:1.20.0.2
	TURBINE_PEER:1.20
	TIGRIS_0_9_2_1:1.20
	TIGRIS_0_9_2:1.15.0.2
	TIGRIS_0_9_0:1.11.0.4
	TIGRIS_0_8_4:1.11.0.2
	NEWHELM:1.1.1.1
	TIGRIS_NOV_12_2000:1.1.1
	OLDHELM:1.1.0.2
	TIGRIS_NOV_11_2000:1.1;
locks; strict;
comment	@# @;


1.22
date	2001.02.21.03.23.56;	author edk;	state dead;
branches;
next	1.21;

1.21
date	2001.01.30.18.03.57;	author leonardr;	state Exp;
branches;
next	1.20;

1.20
date	2001.01.26.18.29.00;	author edk;	state Exp;
branches
	1.20.2.1;
next	1.19;

1.19
date	2001.01.26.05.24.05;	author edk;	state dead;
branches;
next	1.18;

1.18
date	2001.01.24.22.24.36;	author leonardr;	state Exp;
branches;
next	1.17;

1.17
date	2001.01.24.21.41.48;	author leonardr;	state Exp;
branches;
next	1.16;

1.16
date	2001.01.24.20.42.46;	author dlr;	state Exp;
branches;
next	1.15;

1.15
date	2001.01.12.01.19.22;	author leonardr;	state Exp;
branches
	1.15.2.1;
next	1.14;

1.14
date	2001.01.08.23.25.02;	author dlr;	state Exp;
branches;
next	1.13;

1.13
date	2001.01.05.21.42.38;	author leonardr;	state Exp;
branches;
next	1.12;

1.12
date	2001.01.05.00.13.13;	author kmaples;	state Exp;
branches;
next	1.11;

1.11
date	2000.12.01.22.14.50;	author elicia;	state Exp;
branches;
next	1.10;

1.10
date	2000.11.30.23.56.41;	author leonardr;	state Exp;
branches;
next	1.9;

1.9
date	2000.11.29.22.37.40;	author elicia;	state Exp;
branches;
next	1.8;

1.8
date	2000.11.29.01.21.28;	author elicia;	state Exp;
branches;
next	1.7;

1.7
date	2000.11.17.00.29.21;	author leonardr;	state Exp;
branches;
next	1.6;

1.6
date	2000.11.17.00.00.01;	author elicia;	state Exp;
branches;
next	1.5;

1.5
date	2000.11.15.22.48.39;	author leonardr;	state Exp;
branches;
next	1.4;

1.4
date	2000.11.14.16.56.31;	author leonardr;	state Exp;
branches;
next	1.3;

1.3
date	2000.11.13.19.18.48;	author leonardr;	state Exp;
branches;
next	1.2;

1.2
date	2000.11.12.22.57.31;	author dlr;	state Exp;
branches;
next	1.1;

1.1
date	2000.09.29.22.28.40;	author leonardr;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	2000.11.12.21.15.15;	author dlr;	state Exp;
branches;
next	;

1.15.2.1
date	2001.01.31.03.55.30;	author leonardr;	state Exp;
branches;
next	;

1.20.2.1
date	2001.01.28.01.02.17;	author dlr;	state dead;
branches;
next	;


desc
@@


1.22
log
@delete stuff from the head which is obsolete in the branch (more changes
are still needed for a clean compile)
@
text
@/* ================================================================
 * Copyright (c) 2000 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet (http://www.collab.net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */

package org.tigris.helm.database;

import java.sql.*;
import java.util.*;

import org.webmacro.util.*;
import org.webmacro.servlet.*;

import com.workingdogs.village.*;

import org.joist.database.*;
import org.joist.util.*;

import org.tigris.helm.DoesNotExist;
import org.tigris.helm.domain.User;
import org.tigris.helm.project.Project;
import org.tigris.helm.security.AccessControlList;
import org.tigris.helm.tool.*;

/**
 * Manages the User-Role-Project three-way join table.
 *
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 * @@version $Id: UserRoleProjectManager.java,v 1.21 2001/01/30 18:03:57 leonardr Exp $
 *
 * Copyright (c) 2000 CollabNet, Inc.
 */
public class UserRoleProjectManager extends TableManager
{
    /**
     * The name of the database table that this <code>TableManager</code> 
     * manages.
     */
    public static final String TABLE_NAME = "HELM_USER_ROLE_PROJECT";

    /**
     * A database key field.
     */
    public static final String USER_ID = "USER_ID";

    /**
     * A database key field.
     */
    public static final String ROLE_ID = "ROLE_ID";

    /**
     * A database key field.
     */
    public static final String PROJECT_ID = "PROJECT_ID";

    /**
     * User ID of the person who approved this role grant.
     */
    public static final String APPROVED_BY = "APPROVED_BY";

    /**
     * The logger instance for all instances of this class.
     */
    private static Log log = new Log ("urprMgr", "UserRoleProjectManager");

    /**
     * Whether we want debugging information.
     */
    private static final boolean debug = true;

    /**
     * Creates a new instance.
     *
     * @@param dataMgr The manager of this instance.
     */
    public UserRoleProjectManager (DataManager dataMgr)
    {
        super (dataMgr);
    }

    /**
     * Retrieves roles and projects for a user.
     *
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to obtain roles and projects for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getRolesProjectsForUser (Connection connection,
                                            String userID)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        return getUsersRolesProjects (connection, userID, null, null,
                                      Boolean.TRUE);
    }
    
    /**
     * Retrieves approved roles and projects for a list of users.
     *
     * @@param    connection    A JDBC database connection
     * @@param    users         A List of userIDs to obtain 
     *                        roles and projects for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getRolesProjectsForUsers (Connection connection,
                                             List users)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        TableDataSet urp = null;
        try
        {
            StringBuffer userIDs = new StringBuffer ();
            int numUsers = users.size ();
            for (int i = 0; i < numUsers; i++)
            {
                userIDs.append (users.get (i));
                if (i < numUsers - 1)
                {
                    userIDs.append (", ");
                }
            }
            urp = new TableDataSet (connection, TABLE_NAME);
            StringBuffer buf = new StringBuffer(USER_ID);
            buf.append(" in (").append(userIDs).append(')');
            buf.append(" and ").append(makeApprovedClause(connection, 
                                                          Boolean.TRUE));
            urp.where(buf.toString());
            urp.fetchRecords ();
        }
        catch (DataSetException e)
        {
            log.exception (e);
            if (debug)
                log.info ("Getting Roles and Projects: " + e.getMessage ());
            throw e;
        }
        catch (SQLException e)
        {
            log.exception (e);
            if (debug)
                log.info ("Getting Roles and Projects: " + e.getMessage ());
            throw e;
        }
        finally
        {
            registerToClose (urp);
        }
        return urp;
    }


    /**
     * Retrieves approved projects for a list of users.
     *
     * @@param    connection    A JDBC database connection
     * @@param    users         A List of userIDs to obtain 
     *                        roles and projects for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getProjectsForUsers (Connection connection,
                                        List users)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        String action = "Getting Projects for Users";
        QueryDataSet projects = null;

            StringBuffer userIDs = new StringBuffer ();
            int numUsers = users.size ();
            for (int i = 0; i < numUsers; i++)
            {
                userIDs.append (users.get (i));
                if (i < numUsers - 1)
                {
                    userIDs.append (", ");
                }
            }
        
        StringBuffer stmt =
            new StringBuffer
                ("select distinct HELM_PROJECT.PROJECT_ID from ");
	stmt.append("HELM_PROJECT join ").append(TABLE_NAME);
        stmt.append (" where ").append (USER_ID);
        stmt.append(" in (").append(userIDs).append(')');
        stmt.append (" and HELM_PROJECT.").append (PROJECT_ID);
        stmt.append ("=");
        stmt.append (TABLE_NAME).append (".").append (PROJECT_ID);
        stmt.append (" AND HELM_PROJECT.type=");
        stmt.append(escapeAndQuoteSQL(ProjectType.REGULAR));

        try
        {
            projects = new QueryDataSet (connection, stmt.toString ());
            return projects.fetchRecords ();
        }
        catch (DataSetException e)
        {
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
        }
        catch (SQLException e)
        {
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
        }
        finally
        {
            registerToClose (projects);
        }
    }

    /**
     * Retrieves projects that a user has a role in.     
     *
     * @@param    connection    A JDBC database connection
     * @@param   userID        The userID of the user in question.    
     * @@param   roleID        The roleID of the user.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getProjectsForUserRole (Connection connection,
                                           String userID, String roleID)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        return getUsersRolesProjects (connection, userID, roleID, null,
                                      Boolean.TRUE);
    }

    /**
     * Retrieves roles for a user and project.
     *
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to obtain roles and projects for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getRolesForUserProject (Connection connection,
                                           String userID, String projectID)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        return getUsersRolesProjects (connection, userID, null, projectID,
                                      Boolean.TRUE);
    }

    /**
     * Retrieves requested roles for a user and project.
     *
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to obtain roles and projects for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getRequestedRolesForUserProject (Connection connection,
                                                    String userID,
                                                    String projectID)
        throws DataSetException, SQLException, JoistDatabaseException
    {
        return getUsersRolesProjects (connection, userID, null, projectID,
                                      new Boolean (false));
    }

    /**
     * Retrieves requested roles for a project.
     *
     * @@param    connection    A JDBC database connection
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getRequestedRolesForProject (Connection connection,
                                                    String projectID)
        throws DataSetException, SQLException, JoistDatabaseException
    {
        return getUsersRolesProjects (connection, null, null, projectID,
                                      new Boolean (false));
    }
    /**
     * Retrieves roles for a user on a project.
     *
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to obtain roles for
     * @@param    projectID     The projectID to constrain the search to
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getRolesProjectsForUser (Connection connection,
                                            String userID,
                                            String projectID)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        return getUsersRolesProjects (connection, userID, projectID, null,
                                      Boolean.TRUE);
    }

    /**
     * Retrieves users and roles for a project.
     *
     * @@param    connection    A JDBC database connection
     * @@param    projectID     The projectID to get users and roles for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getUsersRolesForProject (Connection connection,
                                            String projectID)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        return getUsersRolesProjects (connection, null, null, projectID,
                                      Boolean.TRUE);
    }

    /**
     * Retrieves users with a role in a project.
     *
     * @@param    connection    A JDBC database connection
     * @@param    projectID     The projectID to get users and roles for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getUsersForRoleProject (Connection connection, 
                                           String roleID, String projectID)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        return getUsersRolesProjects (connection, null, roleID, projectID,
                                      Boolean.TRUE);
    }

    /**
     * Retrieves all users and roles for a project.
     *
     * @@param    connection    A JDBC database connection
     * @@param    projectID     The projectID to get users and roles for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getAllUsersRolesForProject (Connection connection)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        return getUsersRolesProjects (connection, null, null, null, Boolean.TRUE);
    }

    /**
     * Retrieves users, roles, and projects.
     *
     * <pre>
     *   Users     Roles   Projects
     *   leonardr  *       *          getRolesProjectsForUser
     *   leonardr  *       foo        getRolesForUserProject
     *   *         *       foo        getUsersRolesForProject
     *   *         *       *          getAllUsersRolesProjects
     * </pre>
     *
     * @@param    connection    A JDBC database connection
     * @@param    userID        A user ID constraint on the query.
     * @@param    roleID        A role ID constraint on the query.
     * @@param    projectID     A project ID constraint on the query
     * @@param    projectID     A project ID constraint on the query.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    private DataSet getUsersRolesProjects (Connection connection,
                                           String userID,
                                           String roleID,
                                           String projectID,
                                           Boolean approved)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        TableDataSet urp = null;
        try
        {
            urp = new TableDataSet (connection, TABLE_NAME);
            urp.where(buildWhereClause(connection, userID, roleID, projectID,
                                       approved));
            return urp.fetchRecords ();
        }
        catch (DataSetException e)
        {
            log.exception (e);
            if (debug) 
                log.info ("Getting Roles and Projects: " + e.getMessage ());
            throw e;
        }
        catch (SQLException e)
        {
            log.exception (e);
            if (debug) 
                log.info ("Getting Roles and Projects: " + e.getMessage ());
            throw e;
        }
        finally
        {
            registerToClose (urp);
        }
    }

    /**
     * Retrieves users who have any role on a project.
     *
     * @@param    connection    A JDBC database connection
     * @@param    projectID     The projectID to get users for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getUsersForProject (Connection connection,
                                       String projectID, Boolean approved)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        String action = "Getting Users for Project";
        String approvedClause = null;
        QueryDataSet users = null;

        StringBuffer stmt =
            new StringBuffer ("select distinct User.*  from User join ");
        stmt.append (TABLE_NAME).append (" where ").append (PROJECT_ID);
        stmt.append ("=").append (projectID).append (" and User.userID = ");
        stmt.append (TABLE_NAME).append (".").append (USER_ID);
        if (approved != null)
        {
            approvedClause = makeApprovedClause(connection, approved);
            stmt.append(" and ").append(approvedClause);
        }

        stmt.append (" order by name");
        try
        {
            users = new QueryDataSet (connection, stmt.toString ());
            return users.fetchRecords ();
        }
        catch (DataSetException e)
        {
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
        }
        catch (SQLException e)
        {
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
        }
        finally
        {
            registerToClose (users);
        }
    }

    /**
     * Retrieves projects which the given user has any approved role
     * on.  Only retrieves projects of type="regular"
     *
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to get projects for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception DataSetException 
     */
    public DataSet getProjectsForUser (Connection connection, String userID)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        return getProjectsForUser(connection, userID, true);
    }

    /**
     * Retrieves projects which the given user has any approved role
     * on.  Only retrieves projects of type="regular"
     *
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to get projects for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception DataSetException 
     */
    public DataSet getProjectsForUser (Connection connection,
                                       String userID, boolean regularOnly)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        String action = "Getting Projects for User";
        QueryDataSet users = null;
        
        StringBuffer stmt =
            new StringBuffer
                ("select distinct HELM_PROJECT.*  from HELM_PROJECT join ");
        stmt.append (TABLE_NAME).append (" where ").append (USER_ID);
        stmt.append ("=").append (userID);
        stmt.append (" and HELM_PROJECT.").append (PROJECT_ID);
        stmt.append ("=");
        stmt.append (TABLE_NAME).append (".").append (PROJECT_ID);
        if (regularOnly)
        {
            stmt.append (" AND HELM_PROJECT.type=");
            stmt.append(escapeAndQuoteSQL(ProjectType.REGULAR));
        }

        try
        {
            users = new QueryDataSet (connection, stmt.toString ());
            return users.fetchRecords ();
        }
        catch (DataSetException e)
        {
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
        }
        catch (SQLException e)
        {
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
        }
        finally
        {
            registerToClose (users);
        }
    }

    /**
     * Called by <code>User.deleteUser()</code>.
     */
    public void deleteRolesForUser (Connection connection, String userID) 
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
    {
        deleteRoles(connection, userID, null, null);
    }

    /**
     * Deletes all roles in a project.
     */
    public void deleteRolesForProject(Connection connection, String projectID) 
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
    {
        deleteRoles(connection, null, null, projectID);
    }

    /**
     * Revokes roles matching given criteria.
     */
    private void deleteRoles (Connection connection, String userID,
                              String roleID, String projectID) 
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
    {
        DataSet roles = getUsersRolesProjects
            (connection, userID, roleID, projectID, null);
        for (int i = 0; i < roles.size(); i++)
        {
            Record rec = roles.getRecord(i);
            String u = rec.getValue(USER_ID).toString();
            String r = rec.getValue(ROLE_ID).toString();
            String p = rec.getValue(PROJECT_ID).toString();
            revoke(connection, null, u, r, p);
        }
    }

    /**
     * Requests a role in a project for a user.
     *
     * @@param conn      The JDBC database connection to use.
     * @@param userID    The userID which will be recipient of the role
     * @@param roleID    The roleID to request.
     * @@param projectID The projectID representing the resource requested.
     * @@return          Whether the role was requested.
     *
     * @@exception DataSetException
     * @@exception JoistDatabaseException
     * @@exception SQLException
     */
    public boolean request (Connection connection, String userID, 
                            String roleID, String projectID)
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
    {
        boolean haveRoleInProject = false;
        TableDataSet urp = null;
        try
        {
            urp = new TableDataSet(connection, TABLE_NAME);
            urp.where (buildWhereClause (connection, userID, roleID, projectID,
                                         null));
            urp.fetchRecords (1);
            haveRoleInProject = urp.containsRecord(0);
        }
        finally
        {
            if (urp != null) urp.close();
        }
        if (!haveRoleInProject)
        {
            // The role will be unapproved.
            UserUserGroupManager usrGrpMgr = (UserUserGroupManager)
                getDataManager().getManager
                ("org.tigris.helm.database.UserUserGroupManager");            
            grant(connection, userID, roleID, projectID, 
                  usrGrpMgr.getNobodyUserID(connection));
        }
        return !haveRoleInProject;
    }

    /**
     * Grants the named role in the given project to the given user.
     *
     * @@param conn      The JDBC database connection to use.
     * @@param userID    The userID which will be recipient of the role
     * @@param roleName  Name of the role to grant.
     * @@param projectID The projectID representing the resource of the grant.
     *
     * @@exception DataSetException
     * @@exception JoistDatabaseException
     * @@exception SQLException
     */
    public void grantNamedRole (Connection connection, String userID, 
                                String roleName, String projectID,
                                String approvedBy)
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
    {
        RoleManager roleMgr = (RoleManager) 
            getDataManager().getManager("org.joist.database.RoleManager");
        String roleID = roleMgr.getRoleID(connection, roleName);
        if (debug) log.info("roleID=" + roleID);
        grant(connection, userID, roleID, projectID, approvedBy);
    }

    /**
     * Revokes the named role in the given project from the given user.
     *
     * @@param conn      The JDBC database connection to use.
     * @@param userID    The userID which will have the role revoked.
     * @@param roleName  Name of the role to revoke.
     *
     * @@exception DataSetException
     * @@exception JoistDatabaseException
     * @@exception SQLException
     */
    public void revokeNamedRole (Connection connection, String userID, 
                                 String roleName, String projectID)
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
    {
        RoleManager roleMgr = (RoleManager) 
            getDataManager().getManager("org.joist.database.RoleManager");
        String roleID = roleMgr.getRoleID(connection, roleName);
        revoke(connection, null, userID, roleID, projectID);
    }

    /**
     * Grants the given role in the given project to the given user.
     *
     * @@param    connection    The JDBC database connection to use.
     * @@param    userID        The userID which will be recipient of the role
     * @@param    roleID        The roleID to grant.
     * @@param    projectID     The projectID representing the resource of the 
     *                        grant.
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public void grant (Connection connection, String userID,
                       String roleID, String projectID, String approvedBy)
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
    {
        DataManager dMgr = getDataManager();
        UserUserGroupManager usrGrpMgr = (UserUserGroupManager)dMgr.getManager
            ("org.tigris.helm.database.UserUserGroupManager");

        TableDataSet urp = null;
        try
        {
            Project project = new Project(connection, dMgr, projectID);
            User user = User.getInstance(connection, dMgr, userID);
            String nobody = usrGrpMgr.getNobodyUserID(connection);
            if (approvedBy == null) 
            {
                approvedBy = nobody;
            }
            User approvedByUser = User.getInstance(connection, dMgr,
                                                   approvedBy);

            if (debug) log.info("Granting role " + roleID 
                                + " in project " + project.getName() 
                                + " to user " + user.getName()
                                + ", approved by  " + approvedByUser.getName());

            IssueTrackingAccountDescriptor before = null;
            if (approvedBy != nobody)
            {
                before = new IssueTrackingAccountDescriptor(project, user);
            }

            KeyDef kd = new KeyDef()
                .addAttrib(PROJECT_ID).addAttrib(USER_ID).addAttrib(ROLE_ID);
            urp = new TableDataSet (connection, TABLE_NAME, kd);
            urp.where (buildWhereClause (connection, userID, roleID, projectID,
                                         null));
            urp.fetchRecords(1);

            Record r;
            if (urp.containsRecord(0))
            {
                r = urp.getRecord(0);
            } else {
                r = urp.addRecord();
                r.setValue(USER_ID, userID);
                r.setValue(ROLE_ID, roleID);
                r.setValue(PROJECT_ID, projectID);
            }
            r.setValue(APPROVED_BY, approvedBy);
            r.save();

            if (approvedBy != nobody)
            {
                user.rebuildACL();
                    
                //TODO: Only do this if the role has a issue
                //tracker permission.
                IssueTrackingAccountDescriptor after = new
                    IssueTrackingAccountDescriptor(project, user);
                before.syncTo(connection, dMgr, after, approvedByUser);
            }
        }
        catch (DataSetException e)
        {
            log.exception (e);
            if (debug) log.warning("Error granting role: " + e.getMessage());
            throw e;
        }
        catch (SQLException e)
        {
            log.exception (e);
            if (debug) log.warning("Error granting role: " + e.getMessage());
            throw e;
        }
        finally
        {
            registerToClose(urp);
        }
    }

    /**
     * Revokes the given permission with the given resource from the given
     * role.
     *
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID from which to revoke the role
     * @@param    roleID        The roleID to revoke
     * @@param   projectID     The projectID from which to revoke
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public void revoke (Connection connection, String revokingUserID,
                        String userID, String roleID, String projectID) 
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
    {
        TableDataSet urp = null;
        try
        {
	    DataManager dMgr = getDataManager();
	    User revokingUser = null;
	    if (revokingUserID != null)
	    {
		revokingUser = User.getInstance(connection, dMgr,
                                                revokingUserID);
	    }
	    User user = User.getInstance(connection, dMgr, userID);	    
	    Project project = new Project(connection, dMgr, projectID);

            if (debug) log.info("Revoking role " + roleID 
                                + " in project " + project.getName() 
                                + " from user " + user.getName());
            if (revokingUser!=null)
            {
                log.info("Revoked by " + revokingUser.getName());
            }

	    IssueTrackingAccountDescriptor before = new
		IssueTrackingAccountDescriptor(project, user);

            KeyDef kd = new KeyDef().addAttrib (PROJECT_ID)
                .addAttrib(USER_ID).addAttrib(ROLE_ID);

            urp = new TableDataSet(connection, TABLE_NAME, kd);
            urp.where (buildWhereClause(connection, userID, roleID, 
                                         projectID, null));
            urp.fetchRecords ();

            for (int i = 0; i < urp.size (); i++)
            {
                urp.getRecord (i).markToBeDeleted ();
            }
            urp.save ();

            user.rebuildACL();

            //TODO: Only do this if the role had an issue tracker permission.

	    IssueTrackingAccountDescriptor after = new
		IssueTrackingAccountDescriptor(project, user);

	    before.syncTo(connection, dMgr, after, revokingUser);

        } catch (DataSetException e) {
            log.exception (e);
            String message = "Revoking role: " + e.getMessage ();
            if (debug) log.info (message);
            throw e;
        } catch (SQLException e) {
            log.exception (e);
            String message = "Revoking role: " + e.getMessage ();
            if (debug) log.info (message);
            throw e;
        } finally {
            registerToClose (urp);
        }
    }

    /**
     * Revoke all roles for a user across a project.
     *
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to revoke roles from
     * @@param    projectID     The projectID from which to revoke roles
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public void revoke (Connection connection, String userID, String projectID)
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
    {
        revoke (connection, null, userID, null, projectID);
    }

    /**
     * Revokes the given role unilaterally (across all projects).
     *
     * @@param    connection    A JDBC database connection
     * @@param    roleID  The roleID to revoke
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public void revoke (Connection connection, String roleID)
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
    {
        revoke (connection, null, roleID, null, null);
    }
    
    /**
     * Creates a SQL <code>where</code> clause.  
     */
    protected String buildWhereClause (Connection connection,
                                       String userID,
                                       String roleID,
                                       String projectID,
                                       Boolean approved)
        throws DataSetException, SQLException, JoistDatabaseException
    {
        StringBuffer whereClause = new StringBuffer ();
        boolean firstClause = true;

        if (userID != null)
        {
            whereClause.append (USER_ID).append ('=').append (userID);
            firstClause = false;
        }
        if (roleID != null)
        {
            if (!firstClause)
            {
                whereClause.append (" AND ");
            }
            whereClause.append (ROLE_ID).append ('=').append (roleID);
            firstClause = false;
        }
        if (projectID != null)
        {
            if (!firstClause)
            {
                whereClause.append (" AND ");
            }
            whereClause.append (PROJECT_ID).append ('=').append (projectID);
            firstClause = false;
        }

        if (approved != null)
        {            
            if (!firstClause)
            {
                whereClause.append (" AND ");
            }
            whereClause.append(makeApprovedClause(connection, approved));
        }
        return whereClause.toString ();
    }

    private String makeApprovedClause(Connection connection, Boolean approved)
        throws DataSetException, SQLException, JoistDatabaseException 
    {
        UserUserGroupManager usrGrpMgr = (UserUserGroupManager)
            getDataManager().getManager
            ("org.tigris.helm.database.UserUserGroupManager");

        StringBuffer clause = new StringBuffer();
        clause.append(APPROVED_BY);
        clause.append(approved.equals(Boolean.TRUE) ? "!=" : "=");
        clause.append(usrGrpMgr.getNobodyUserID(connection));
        return clause.toString();
    }

    /**
     * Updates a record in the table
     *
     *
     * @@param connection          The JDBC database connection to use.
     * @@param projectID           The database key of the project to update 
     *                            (or null, in which case a new project is 
     *                            created).
     * @@param   userID            The id of the user in the project
     * @@param   roleID            The id of the role the user has in the project
     * @@param   approvedBy       userID of person who approved user
     *
     * @@return  nothing
     *
     * @@exception SQLException
     * @@exception DataSetException 
     */
    public void updateField (Connection connection, String projectID,
                             String userID, String roleID,
                             String fieldName, String fieldValue)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        Record userRole = null;
        TableDataSet userRoles = null;

        try
        {
            KeyDef kd = new KeyDef ().addAttrib (PROJECT_ID);
            kd.addAttrib (USER_ID);
            kd.addAttrib (ROLE_ID);
            userRoles = new TableDataSet (connection, TABLE_NAME, kd);
            userRoles.where(buildWhereClause(connection, userID, roleID,
                                             projectID, null));

            // Do update.
            userRoles.fetchRecords ();
            if (userRoles.containsRecord (0))
            {
                userRole = userRoles.getRecord (0);
                userRole.setValue ("Approved_by", fieldValue);
                userRole.save ();
            }
            else
            {
                throw new DataSetException ("There is no record for PROJECT_ID=" +
                                            projectID + "and USER_ID=" + userID +
                                            "and ROLE_ID=" + roleID);
            }
        }
        catch (DataSetException e)
        {
            log.exception (e);
            String message = "Updating ProjectUserRole: " + e.getMessage ();
            if (debug) log.info (message);
            throw e;
        }
        catch (SQLException e)
        {
            log.exception (e);
            String message = "Updating ProjectUserRole: " + e.getMessage ();
            if (debug) log.info (message);
            throw e;
        }
        finally
        {
            registerToClose (userRoles);
        }
    }
}
@


1.21
log
@Enable the calling method to specify whether or not it wants to consider roles obtained in project groups.
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.20 2001/01/26 18:29:00 edk Exp $
@


1.20
log
@readding all the files as part of the process of returning to the way
things were as of tuesday
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.15 2001/01/12 01:19:22 leonardr Exp $
d513 16
d530 1
a530 1
                                       String userID)
d544 5
a548 2
        stmt.append (" AND HELM_PROJECT.type=");
        stmt.append(escapeAndQuoteSQL(ProjectType.REGULAR));
@


1.20.2.1
log
@Porting to the Turbine Peer system.  -dlr and leonardr
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.20 2001/01/26 18:29:00 edk Exp $
@


1.19
log
@revert this stuff to its state as of 2001/01/24 12:42:00 GMT -- with
the following exceptions, which have some different modifications so
they'll work with changes which have happened in joist:

ProjectNewsEdit.java
SkinManager.java
SkinStyleElementValueManager.java
StyleElementManager.java
StyleManager.java

it compiles.  i dunno if it works, though.
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.18 2001/01/24 22:24:36 leonardr Exp $
d120 1
a120 2
        super(dataMgr, new KeyDef().addAttrib(PROJECT_ID)
            .addAttrib(USER_ID).addAttrib(ROLE_ID));
d129 2
a130 2
     *
     * @@exception DBException
d134 1
a134 1
        throws DBException
d147 2
a148 2
     *
     * @@exception DBException
d152 1
a152 1
        throws DBException
d177 4
a180 1
            throw new DBException(e);
d184 4
a187 1
            throw new DBException(e);
d204 2
a205 2
     *
     * @@exception DBException
d209 1
a209 1
        throws DBException
d244 3
a246 1
            throw new DBException(e);
d250 3
a252 1
            throw new DBException(e);
d267 2
a268 2
     *
     * @@exception DBException
d272 1
a272 1
        throws DBException
d284 2
a285 2
     *
     * @@exception DBException
d289 1
a289 1
        throws DBException
d301 2
a302 2
     *
     * @@exception DBException
d307 1
a307 1
        throws DBException
d310 1
a310 1
                                      Boolean.FALSE);
d318 2
a319 2
     *
     * @@exception DBException
d323 1
a323 1
        throws DBException
d326 1
a326 1
                                      Boolean.FALSE);
d335 2
a336 2
     *
     * @@exception DBException
d341 1
a341 1
        throws DBException
d353 2
a354 2
     *
     * @@exception DBException
d358 1
a358 1
        throws DBException
d370 2
a371 2
     *
     * @@exception DBException
d375 1
a375 1
        throws DBException
d387 2
a388 2
     *
     * @@exception    DBException
d391 1
a391 1
        throws DBException
d393 1
a393 2
        return getUsersRolesProjects (connection, null, null, null,
                                      Boolean.TRUE);
d413 2
a414 2
     *
     * @@exception DBException
d421 1
a421 1
        throws DBException
d433 4
a436 1
            throw new DBException(e);
d440 4
a443 1
            throw new DBException(e);
d457 2
a458 2
     *
     * @@exception DBException
d462 1
a462 1
        throws DBException
d487 3
a489 1
            throw new DBException(e);
d493 3
a495 1
            throw new DBException(e);
d510 2
a511 2
     *
     * @@exception DBException
d515 1
a515 1
        throws DBException
d538 3
a540 1
            throw new DBException(e);
d544 3
a546 1
            throw new DBException(e);
d558 2
a559 1
        throws DoesNotExist, DBException
d568 2
a569 1
        throws DoesNotExist, DBException
d579 2
a580 1
        throws DoesNotExist, DBException
d584 1
a584 12
        try
        {
            for (int i = 0; i < roles.size(); i++)
            {
                Record rec = roles.getRecord(i);
                String u = rec.getValue(USER_ID).toString();
                String r = rec.getValue(ROLE_ID).toString();
                String p = rec.getValue(PROJECT_ID).toString();
                revoke(connection, null, u, r, p);
            }
        }
        catch (DataSetException e)
d586 5
a590 1
            throw new DBException(e);
d603 3
a605 1
     * @@exception DBException
d609 2
a610 1
        throws DoesNotExist, DBException
a621 4
        catch (DataSetException e)
        {
            throw new DBException(e);
        }
d646 3
a648 1
     * @@exception DBException
d653 2
a654 1
        throws DoesNotExist, DBException
d657 1
a657 1
            getDataManager().getManager("org.tigris.helm.database.RoleManager");
d670 3
a672 1
     * @@exception DBException
d676 2
a677 1
        throws DoesNotExist, DBException
d680 1
a680 1
            getDataManager().getManager("org.tigris.helm.database.RoleManager");
d693 2
a694 1
     * @@exception DBException
d698 2
a699 1
        throws DoesNotExist, DBException
d729 3
a731 1
            urp = new TableDataSet (connection, TABLE_NAME, keydef);
d762 3
a764 1
            throw new DBException(e);
d768 3
a770 1
            throw new DBException(e);
d786 2
a787 2
     *
     * @@exception DBException
d791 2
a792 1
        throws DoesNotExist, DBException
d818 4
a821 1
            urp = new TableDataSet(connection, TABLE_NAME, keydef);
d842 4
a845 1
            throw new DBException(e);
d847 4
a850 1
            throw new DBException(e);
d862 2
a863 2
     *
     * @@exception DBException
d866 2
a867 1
        throws DoesNotExist, DBException
d881 2
a882 1
        throws DoesNotExist, DBException
d895 1
a895 1
        throws DBException
d936 1
a936 1
        throws DBException
d969 1
a969 1
        throws DBException
d976 4
a979 1
            userRoles = new TableDataSet (connection, TABLE_NAME, keydef);
d993 3
a995 3
                throw new DBException ("There is no record for PROJECT_ID=" +
                                       projectID + "and USER_ID=" + userID +
                                       "and ROLE_ID=" + roleID);
d1000 4
a1003 1
            throw new DBException(e);
d1007 4
a1010 1
            throw new DBException(e);
@


1.18
log
@Moved TableManagers to new keydef constructor and keydef-less runSelect.
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.17 2001/01/24 21:41:48 leonardr Exp $
@


1.17
log
@Ported UserManager and RoleManager to Helm. Started moving other TableManagers toward a grab-connection-on-demand model. Massive breakage ensues. dlr and I are working on it.
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.16 2001/01/24 20:42:46 dlr Exp $
d120 2
a121 1
        super (dataMgr);
d704 1
a704 3
            KeyDef kd = new KeyDef()
                .addAttrib(PROJECT_ID).addAttrib(USER_ID).addAttrib(ROLE_ID);
            urp = new TableDataSet (connection, TABLE_NAME, kd);
d786 1
a786 4
            KeyDef kd = new KeyDef().addAttrib (PROJECT_ID)
                .addAttrib(USER_ID).addAttrib(ROLE_ID);

            urp = new TableDataSet(connection, TABLE_NAME, kd);
d933 1
a933 4
            KeyDef kd = new KeyDef ().addAttrib (PROJECT_ID);
            kd.addAttrib (USER_ID);
            kd.addAttrib (ROLE_ID);
            userRoles = new TableDataSet (connection, TABLE_NAME, kd);
@


1.16
log
@Making use of DBException.
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.15 2001/01/12 01:19:22 leonardr Exp $
d636 1
a636 1
            getDataManager().getManager("org.joist.database.RoleManager");
d656 1
a656 1
            getDataManager().getManager("org.joist.database.RoleManager");
@


1.15
log
@Fixed bizarre NPE in role revocation.
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.14 2001/01/08 23:25:02 dlr Exp $
d129 2
a130 2
     * @@exception    SQLException
     * @@exception    DataSetException
d134 1
a134 1
        throws SQLException, DataSetException, JoistDatabaseException
d147 2
a148 2
     * @@exception    SQLException
     * @@exception    DataSetException
d152 1
a152 1
        throws SQLException, DataSetException, JoistDatabaseException
d177 1
a177 4
            log.exception (e);
            if (debug)
                log.info ("Getting Roles and Projects: " + e.getMessage ());
            throw e;
d181 1
a181 4
            log.exception (e);
            if (debug)
                log.info ("Getting Roles and Projects: " + e.getMessage ());
            throw e;
d198 2
a199 2
     * @@exception    SQLException
     * @@exception    DataSetException
d203 1
a203 1
        throws SQLException, DataSetException, JoistDatabaseException
d238 1
a238 3
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
d242 1
a242 3
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
d257 2
a258 2
     * @@exception    SQLException
     * @@exception    DataSetException
d262 1
a262 1
        throws SQLException, DataSetException, JoistDatabaseException
d274 2
a275 2
     * @@exception    SQLException
     * @@exception    DataSetException
d279 1
a279 1
        throws SQLException, DataSetException, JoistDatabaseException
d291 2
a292 2
     * @@exception    SQLException
     * @@exception    DataSetException
d297 1
a297 1
        throws DataSetException, SQLException, JoistDatabaseException
d300 1
a300 1
                                      new Boolean (false));
d308 2
a309 2
     * @@exception    SQLException
     * @@exception    DataSetException
d313 1
a313 1
        throws DataSetException, SQLException, JoistDatabaseException
d316 1
a316 1
                                      new Boolean (false));
d325 2
a326 2
     * @@exception    SQLException
     * @@exception    DataSetException
d331 1
a331 1
        throws SQLException, DataSetException, JoistDatabaseException
d343 2
a344 2
     * @@exception    SQLException
     * @@exception    DataSetException
d348 1
a348 1
        throws SQLException, DataSetException, JoistDatabaseException
d360 2
a361 2
     * @@exception    SQLException
     * @@exception    DataSetException
d365 1
a365 1
        throws SQLException, DataSetException, JoistDatabaseException
d377 2
a378 2
     * @@exception    SQLException
     * @@exception    DataSetException
d381 1
a381 1
        throws SQLException, DataSetException, JoistDatabaseException
d383 2
a384 1
        return getUsersRolesProjects (connection, null, null, null, Boolean.TRUE);
d404 2
a405 2
     * @@exception    SQLException
     * @@exception    DataSetException
d412 1
a412 1
        throws SQLException, DataSetException, JoistDatabaseException
d424 1
a424 4
            log.exception (e);
            if (debug) 
                log.info ("Getting Roles and Projects: " + e.getMessage ());
            throw e;
d428 1
a428 4
            log.exception (e);
            if (debug) 
                log.info ("Getting Roles and Projects: " + e.getMessage ());
            throw e;
d442 2
a443 2
     * @@exception    SQLException
     * @@exception    DataSetException
d447 1
a447 1
        throws SQLException, DataSetException, JoistDatabaseException
d472 1
a472 3
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
d476 1
a476 3
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
d491 2
a492 2
     * @@exception    SQLException
     * @@exception DataSetException 
d496 1
a496 1
        throws SQLException, DataSetException, JoistDatabaseException
d519 1
a519 3
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
d523 1
a523 3
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
d535 1
a535 2
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
d544 1
a544 2
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
d554 1
a554 2
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
d558 1
a558 1
        for (int i = 0; i < roles.size(); i++)
d560 12
a571 5
            Record rec = roles.getRecord(i);
            String u = rec.getValue(USER_ID).toString();
            String r = rec.getValue(ROLE_ID).toString();
            String p = rec.getValue(PROJECT_ID).toString();
            revoke(connection, null, u, r, p);
d584 1
a584 3
     * @@exception DataSetException
     * @@exception JoistDatabaseException
     * @@exception SQLException
d588 1
a588 2
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
d600 4
d628 1
a628 3
     * @@exception DataSetException
     * @@exception JoistDatabaseException
     * @@exception SQLException
d633 1
a633 2
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
d649 1
a649 3
     * @@exception DataSetException
     * @@exception JoistDatabaseException
     * @@exception SQLException
d653 1
a653 2
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
d669 1
a669 2
     * @@exception    SQLException
     * @@exception    DataSetException
d673 1
a673 2
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
d736 1
a736 3
            log.exception (e);
            if (debug) log.warning("Error granting role: " + e.getMessage());
            throw e;
d740 1
a740 3
            log.exception (e);
            if (debug) log.warning("Error granting role: " + e.getMessage());
            throw e;
d756 2
a757 2
     * @@exception    SQLException
     * @@exception    DataSetException
d761 1
a761 2
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
d811 1
a811 4
            log.exception (e);
            String message = "Revoking role: " + e.getMessage ();
            if (debug) log.info (message);
            throw e;
d813 1
a813 4
            log.exception (e);
            String message = "Revoking role: " + e.getMessage ();
            if (debug) log.info (message);
            throw e;
d825 2
a826 2
     * @@exception    SQLException
     * @@exception    DataSetException
d829 1
a829 2
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
d843 1
a843 2
        throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
d856 1
a856 1
        throws DataSetException, SQLException, JoistDatabaseException
d897 1
a897 1
        throws DataSetException, SQLException, JoistDatabaseException 
d930 1
a930 1
        throws SQLException, DataSetException, JoistDatabaseException
d954 3
a956 3
                throw new DataSetException ("There is no record for PROJECT_ID=" +
                                            projectID + "and USER_ID=" + userID +
                                            "and ROLE_ID=" + roleID);
d961 1
a961 4
            log.exception (e);
            String message = "Updating ProjectUserRole: " + e.getMessage ();
            if (debug) log.info (message);
            throw e;
d965 1
a965 4
            log.exception (e);
            String message = "Updating ProjectUserRole: " + e.getMessage ();
            if (debug) log.info (message);
            throw e;
@


1.15.2.1
log
@Ported in changes that cause new users to be properly added to Issuezilla when user groups have IZ roles in project groups.
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.15 2001/01/12 01:19:22 leonardr Exp $
a502 17

    /**
     * Retrieves projects which the given user has any approved role
     * on.  Only retrieves projects of type="regular"
     *
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to get projects for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception DataSetException 
     */
    public DataSet getProjectsForUser (Connection connection, String userID)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        return getProjectsForUser(connection, userID, true);
    }

d514 1
a514 1
                                       String userID, boolean regularOnly)
d528 2
a529 5
        if (regularOnly)
        {
            stmt.append (" AND HELM_PROJECT.type=");
            stmt.append(escapeAndQuoteSQL(ProjectType.REGULAR));
        }
@


1.14
log
@Corrected a bunch of regressions to previous User instantion method.  Deprecated User's getUserID() method in favor of getID() for better conformance with BusinessObject interface and orthoginality with other business objects.  Added a guest user and interface.  Added some JavaDoc to User.
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.13 2001/01/05 21:42:38 leonardr Exp $
d809 5
a813 4
                                + " from user " + user.getName()
                                + ", revoked by  " 
                                + revokingUser==null ? "(unknown)" : 
                                revokingUser.getName());
@


1.13
log
@Big patch bringing the smart script runner online.
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.12 2001/01/05 00:13:13 kmaples Exp $
d709 1
a709 1
            User user = new User(connection, dMgr, userID);
d715 2
a716 1
            User approvedByUser = new User(connection, dMgr, approvedBy);
d801 2
a802 1
		revokingUser = new User(connection, dMgr, revokingUserID);
d804 1
a804 1
	    User user = new User(connection, dMgr, userID);	    
@


1.12
log
@Big patch implementing a pilot test of the smart 'determine which scripts to run' system.
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.11 2000/12/01 22:14:50 elicia Exp $
d701 4
a704 1
        if (debug) log.info("Attempting to grant role");
d708 20
a737 2
                if (debug) log.info("Using existing " + RoleManager.TABLE_NAME
                                    + " record");
d739 1
a739 5
            }
            else
            {
                if (debug) log.info("Adding new " + RoleManager.TABLE_NAME + 
                                    " record");
d748 1
a748 4
            UserUserGroupManager usrGrpMgr = (UserUserGroupManager)
                getDataManager().getManager
                ("org.tigris.helm.database.UserUserGroupManager");
            if (approvedBy != usrGrpMgr.getNobodyUserID(connection))
d750 1
a750 2
                AccessControlList.rebuildACL(connection, getDataManager(),
                                             userID);
d754 3
a756 2
                TigrisUpdate.reflectGrant(connection, getDataManager(), approvedBy, 
                                          userID, projectID);
a757 3
            if (debug) log.info("Granting role " + roleID + 
                                " in project " + projectID + 
                                ", approved by " + approvedBy);
d800 1
a800 2
		revokingUser = new User(connection, dMgr,
					revokingUserID);
d804 8
d829 1
a829 1
            AccessControlList.rebuildACL(connection, dMgr, userID);
@


1.11
log
@added revoking userID to revoke method call (it is needed by TigrisUpdate.reflectRevoke)
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.10 2000/11/30 23:56:41 leonardr Exp $
d785 13
a797 1
            KeyDef kd = new KeyDef ().addAttrib (PROJECT_ID)
d800 2
a801 2
            urp = new TableDataSet (connection, TABLE_NAME, kd);
            urp.where (buildWhereClause (connection, userID, roleID, 
d811 1
a811 1
            AccessControlList.rebuildACL(connection, getDataManager(), userID);
d814 6
a819 1
            TigrisUpdate.reflectRevoke(connection, getDataManager(), revokingUserID, userID, projectID);
@


1.10
log
@Mega patch which makes script data persistent and which provides a general framework for tasks to be run (which will be used by anzu eventually)
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.9 2000/11/29 22:37:40 elicia Exp $
d590 1
a590 1
            revoke(connection, u,r,p);
d682 1
a682 1
        revoke(connection, userID, roleID, projectID);
d777 2
a778 2
    public void revoke (Connection connection, String userID, String roleID,
                        String projectID) 
d802 1
a802 2
            TigrisUpdate.reflectRevoke(connection, getDataManager(), null,
                                       userID, projectID);
d831 1
a831 1
        revoke (connection, userID, null, projectID);
d846 1
a846 1
        revoke (connection, roleID, null, null);
@


1.9
log
@added getProjectsForUsers() method, to return projects associated with a list of users (to allow returning projects for all groups a user is associated with)
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.8 2000/11/29 01:21:28 elicia Exp $
d741 1
a741 1
                TigrisUpdate.reflectGrant(getDataManager(), connection, 
d802 2
a803 2
            TigrisUpdate.reflectRevoke(getDataManager(), connection, userID,
                                       projectID);
@


1.8
log
@added method to get all requested roles on a project
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.7 2000/11/17 00:29:21 leonardr Exp $
d196 64
d373 2
a374 1
    public DataSet getUsersForRoleProject (Connection connection, String roleID, String projectID)
@


1.7
log
@Made the TigrisUpdate stuff saner with the use of business objects.
@
text
@d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.6 2000/11/17 00:00:01 elicia Exp $
d232 1
a232 1
     * Retrieves roles for a user and project.
d249 15
@


1.6
log
@getting users for a project sorted alphabetically by name
@
text
@d60 3
d71 1
a71 1
 * @@version $Id: UserRoleProjectManager.java,v 1.5 2000/11/15 22:48:39 leonardr Exp $
d478 2
a479 1
        throws SQLException, DataSetException, JoistDatabaseException
d488 2
a489 1
        throws SQLException, DataSetException, JoistDatabaseException
d499 2
a500 1
        throws SQLException, DataSetException, JoistDatabaseException
d529 2
a530 1
        throws SQLException, DataSetException, JoistDatabaseException
d573 2
a574 1
        throws SQLException, DataSetException, JoistDatabaseException
d596 2
a597 1
        throws SQLException, DataSetException, JoistDatabaseException
d618 2
a619 1
        throws SQLException, DataSetException, JoistDatabaseException
d699 2
a700 1
        throws SQLException, DataSetException, JoistDatabaseException
d707 1
d720 1
d724 1
a724 3
        }
        catch (DataSetException e)
        {
d729 1
a729 3
        }
        catch (SQLException e)
        {
d734 1
a734 3
        }
        finally
        {
d749 2
a750 1
        throws SQLException, DataSetException, JoistDatabaseException
d764 2
a765 1
        throws SQLException, DataSetException, JoistDatabaseException
@


1.5
log
@Got rid of more superfluous UpdateException throwage.
@
text
@d68 1
a68 1
 * @@version $Id: UserRoleProjectManager.java,v 1.4 2000/11/14 16:56:31 leonardr Exp $
d396 1
@


1.4
log
@Fixed method call; added new method
@
text
@d68 1
a68 1
 * @@version $Id: UserRoleProjectManager.java,v 1.3 2000/11/13 19:18:48 leonardr Exp $
d474 1
a474 2
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
d483 1
a483 2
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
d493 1
a493 2
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
a518 1
     * @@exception UpdateException
d522 1
a522 2
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
a560 1
     * @@exception UpdateException
d565 1
a565 2
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
a583 1
     * @@exception UpdateException
d587 1
a587 2
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
d608 1
a608 2
        throws SQLException, DataSetException, UpdateException, 
               JoistDatabaseException
d688 1
a688 2
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
d741 1
a741 2
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
d755 1
a755 2
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
@


1.3
log
@Fixed project deletion and ProjectMembershipRequest
@
text
@d68 1
a68 1
 * @@version $Id: UserRoleProjectManager.java,v 1.2 2000/11/12 22:57:31 dlr Exp $
d278 1
a278 1
        return getUsersRolesProjects (connection, null, projectID, null,
d283 17
a299 1
     * Retrieves all users and roles.
@


1.2
log
@Resolved merge conflicts.
@
text
@d68 1
a68 1
 * @@version $Id: UserRoleProjectManager.java,v 1.1.1.1 2000/11/12 21:15:15 dlr Exp $
d457 24
a480 2
    public void deleteUser (Connection connection, String userID) 
        throws SQLException, DataSetException, JoistDatabaseException
d482 3
a484 2
        PreparedStatement stmt = null;
        try
d486 5
a490 11
            stmt = connection.prepareStatement
                (new StringBuffer("delete from ").append(TABLE_NAME)
                    .append(" where ")
                    .append(buildWhereClause(connection, userID, null, null,
                                             null))
                    .toString());
            stmt.executeUpdate();
        }
        finally
        {
            if (stmt != null) stmt.close();
a492 1

@


1.1
log
@Added this, hopefully the last of the new Tablemanagers.
@
text
@d49 1
a50 1
import java.sql.*;
d60 3
d68 1
a68 1
 * @@version $Id: $
a71 1

d83 1
a83 1
    public static final String USER_ID = "USER_ID";   
d88 1
a88 1
    public static final String ROLE_ID = "ROLE_ID";   
d93 6
a98 1
    public static final String PROJECT_ID = "PROJECT_ID";   
d103 1
a103 1
    private static Log log = new Log("urprMgr", "UserRoleProjectManager");
d108 1
a108 1
    private static boolean debug = true;
d115 1
a115 1
    public UserRoleProjectManager(DataManager dataMgr)
d117 1
a117 1
        super(dataMgr);
d123 5
a127 5
     * @@param	connection    A JDBC database connection
     * @@param	userID        The userID to obtain roles and projects for.
     * @@return	 a DataSet
     * @@exception	SQLException
     * @@exception	DataSetException
d129 1
a129 1
    public DataSet getRolesProjectsForUser (Connection connection, 
d131 1
a131 1
        throws SQLException, DataSetException
d133 111
a243 1
        return getUsersRolesProjects(connection, userID, null, null);
d249 6
a254 6
     * @@param	connection    A JDBC database connection
     * @@param	userID        The userID to obtain roles for
     * @@param	projectID     The projectID to constrain the search to
     * @@return	 a DataSet
     * @@exception	SQLException
     * @@exception	DataSetException
d256 1
a256 1
    public DataSet getRolesProjectsForUser (Connection connection, 
d259 1
a259 1
        throws SQLException, DataSetException
d261 2
a262 1
        return getUsersRolesProjects(connection, userID, projectID, null);
d268 5
a272 5
     * @@param	connection    A JDBC database connection
     * @@param	projectID     The projectID to get users and roles for.
     * @@return	 a DataSet
     * @@exception	SQLException
     * @@exception	DataSetException
d274 1
a274 1
    public DataSet getUsersRolesForProject (Connection connection, 
d276 1
a276 1
        throws SQLException, DataSetException
d278 2
a279 1
        return getUsersRolesProjects(connection, null, projectID, null);
d283 1
a283 1
     * Retrieves users and roles for a project.
d285 5
a289 5
     * @@param	connection    A JDBC database connection
     * @@param	projectID     The projectID to get users and roles for.
     * @@return	 a DataSet
     * @@exception	SQLException
     * @@exception	DataSetException
d292 1
a292 1
        throws SQLException, DataSetException
d294 1
a294 1
        return getUsersRolesProjects(connection, null, null, null);
d308 8
a315 7
     * @@param	connection    A JDBC database connection
     * @@param	userID        A user ID constraint on the query.
     * @@param	roleID        A role ID constraint on the query.
     * @@param	projectID     A project ID constraint on the query.
     * @@return	 a DataSet
     * @@exception	SQLException
     * @@exception	DataSetException
d317 1
a317 1
    private DataSet getUsersRolesProjects (Connection connection, 
d320 3
a322 2
                                           String projectID)
        throws SQLException, DataSetException
d327 108
a434 3
            urp = new TableDataSet(connection, TABLE_NAME);
            urp.where(buildWhereClause(userID, roleID, projectID));
            return urp.fetchRecords();
d438 2
a439 3
            log.exception(e);
            if (debug) log.debug("Getting Roles and Projects: " + 
                                 e.getMessage());
d444 2
a445 3
            log.exception(e);
            if (debug) log.debug("Getting Roles and Projects: " + 
                                 e.getMessage());
d450 60
a509 1
            if (urp != null) registerToClose(urp);
d511 59
d575 4
a578 4
     * @@param	connection    The JDBC database connection to use.
     * @@param	userID        The userID which will be recipient of the role
     * @@param	roleID        The roleID to grant.
     * @@param	projectID     The projectID representing the scope of the 
d580 2
a581 2
     * @@exception	SQLException
     * @@exception	DataSetException
d583 4
a586 3
    public void grant (Connection connection, String userID, 
                       String roleID, String projectID)
        throws SQLException, DataSetException
d588 1
d592 6
a597 2
            urp = new TableDataSet(connection, TABLE_NAME);
            urp.where(buildWhereClause(userID, roleID, projectID));
d599 2
a600 2
            urp.fetchRecords();
            if (!urp.containsRecord(0))
d602 9
a610 1
                Record r = urp.addRecord();
a613 1
                r.save();
d615 19
d637 2
a638 3
            log.exception(e);
            if (debug) log.debug("Granting role in group: " + 
                                 e.getMessage());
d643 2
a644 3
            log.exception(e);
            if (debug) log.debug("Granting role in group: " + 
                                 e.getMessage());
d649 1
a649 1
            if (urp != null) registerToClose(urp);
d654 2
a655 1
     * Revokes the given permission with the given scope from the given role.
d657 3
a659 3
     * @@param	connection    A JDBC database connection
     * @@param	userID        The userID from which to revoke the role
     * @@param	roleID        The roleID to revoke
d661 2
a662 2
     * @@exception	SQLException
     * @@exception	DataSetException
d664 4
a667 3
    public void revoke (Connection connection, String userID, String roleID, 
                        String projectID)
        throws SQLException, DataSetException
d672 6
a677 3
            urp = new TableDataSet(connection, TABLE_NAME);
            urp.where(buildWhereClause(roleID, roleID, projectID));
            urp.fetchRecords();
d679 1
a679 1
            for (int i = 0; i < urp.size(); i++)
d681 1
a681 1
                urp.getRecord(i).markToBeDeleted();
d683 6
a688 1
            urp.save();
d692 3
a694 3
            log.exception(e);
            String message = "Revoking role: " + e.getMessage();
            if (debug) log.debug(message);
d699 3
a701 3
            log.exception(e);
            String message = "Revoking role: " + e.getMessage();
            if (debug) log.debug(message);
d706 1
a706 1
            if (urp != null) registerToClose(urp);
d713 5
a717 5
     * @@param	connection    A JDBC database connection
     * @@param	userID        The userID to revoke roles from
     * @@param	projectID     The projectID from which to revoke roles
     * @@exception	SQLException
     * @@exception	DataSetException
d720 2
a721 1
        throws SQLException, DataSetException
d723 1
a723 1
        revoke(connection, userID, null, projectID);
d727 1
a727 1
     * Revokes the given permission unilaterally (across all roles).
d729 4
a732 4
     * @@param	connection    A JDBC database connection
     * @@param	roleID  The roleID to revoke
     * @@exception	SQLException
     * @@exception	DataSetException
d735 2
a736 1
	throws SQLException, DataSetException
d738 1
a738 1
        revoke(connection, roleID, null, null);
d742 1
a742 1
     * Creates a SQL <code>where</code> clause.
d744 6
a749 3
    protected static String buildWhereClause (String userID, 
                                              String roleID, 
                                              String projectID)
d751 2
a752 1
        StringBuffer whereClause = new StringBuffer();
d756 2
a757 1
            whereClause.append(USER_ID).append('=').append(userID);
d761 1
a761 1
            if (userID != null)
d763 1
a763 1
                whereClause.append(" AND ");
d765 2
a766 1
            whereClause.append(ROLE_ID).append('=').append(roleID);
d770 1
a770 1
            if (roleID != null)
d772 1
a772 1
                whereClause.append(" AND ");
d774 2
a775 1
            whereClause.append(PROJECT_ID).append('=').append(projectID);
d778 9
a786 1
        return whereClause.toString();
a787 1
}
d789 13
d803 24
d828 44
@


1.1.1.1
log
@Merging newhelm tree into helm, as per IZ issue #557.  Previous major version of helm is tagged TIGRIS_NOV_11_2000.
@
text
@d49 1
a50 1
import java.util.*;
a59 3
import org.tigris.helm.security.AccessControlList;
import org.tigris.helm.tool.*;

d65 1
a65 1
 * @@version $Id: UserRoleProjectManager.java,v 1.46 2000/11/11 04:45:03 dlr Exp $
d69 1
d81 1
a81 1
    public static final String USER_ID = "USER_ID";
d86 1
a86 1
    public static final String ROLE_ID = "ROLE_ID";
d91 1
a91 6
    public static final String PROJECT_ID = "PROJECT_ID";

    /**
     * User ID of the person who approved this role grant.
     */
    public static final String APPROVED_BY = "APPROVED_BY";
d96 1
a96 1
    private static Log log = new Log ("urprMgr", "UserRoleProjectManager");
d101 1
a101 1
    private static final boolean debug = true;
d108 1
a108 1
    public UserRoleProjectManager (DataManager dataMgr)
d110 1
a110 1
        super (dataMgr);
d116 5
a120 5
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to obtain roles and projects for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
d122 1
a122 1
    public DataSet getRolesProjectsForUser (Connection connection,
d124 1
a124 1
        throws SQLException, DataSetException, JoistDatabaseException
d126 1
a126 111
        return getUsersRolesProjects (connection, userID, null, null,
                                      Boolean.TRUE);
    }
    
    /**
     * Retrieves approved roles and projects for a list of users.
     *
     * @@param    connection    A JDBC database connection
     * @@param    users         A List of userIDs to obtain 
     *                        roles and projects for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getRolesProjectsForUsers (Connection connection,
                                             List users)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        TableDataSet urp = null;
        try
        {
            StringBuffer userIDs = new StringBuffer ();
            int numUsers = users.size ();
            for (int i = 0; i < numUsers; i++)
            {
                userIDs.append (users.get (i));
                if (i < numUsers - 1)
                {
                    userIDs.append (", ");
                }
            }
            urp = new TableDataSet (connection, TABLE_NAME);
            StringBuffer buf = new StringBuffer(USER_ID);
            buf.append(" in (").append(userIDs).append(')');
            buf.append(" and ").append(makeApprovedClause(connection, 
                                                          Boolean.TRUE));
            urp.where(buf.toString());
            urp.fetchRecords ();
        }
        catch (DataSetException e)
        {
            log.exception (e);
            if (debug)
                log.info ("Getting Roles and Projects: " + e.getMessage ());
            throw e;
        }
        catch (SQLException e)
        {
            log.exception (e);
            if (debug)
                log.info ("Getting Roles and Projects: " + e.getMessage ());
            throw e;
        }
        finally
        {
            registerToClose (urp);
        }
        return urp;
    }

    /**
     * Retrieves projects that a user has a role in.     
     *
     * @@param    connection    A JDBC database connection
     * @@param   userID        The userID of the user in question.    
     * @@param   roleID        The roleID of the user.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getProjectsForUserRole (Connection connection,
                                           String userID, String roleID)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        return getUsersRolesProjects (connection, userID, roleID, null,
                                      Boolean.TRUE);
    }

    /**
     * Retrieves roles for a user and project.
     *
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to obtain roles and projects for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getRolesForUserProject (Connection connection,
                                           String userID, String projectID)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        return getUsersRolesProjects (connection, userID, null, projectID,
                                      Boolean.TRUE);
    }

    /**
     * Retrieves roles for a user and project.
     *
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to obtain roles and projects for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getRequestedRolesForUserProject (Connection connection,
                                                    String userID,
                                                    String projectID)
        throws DataSetException, SQLException, JoistDatabaseException
    {
        return getUsersRolesProjects (connection, userID, null, projectID,
                                      new Boolean (false));
d132 6
a137 6
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to obtain roles for
     * @@param    projectID     The projectID to constrain the search to
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
d139 1
a139 1
    public DataSet getRolesProjectsForUser (Connection connection,
d142 1
a142 1
        throws SQLException, DataSetException, JoistDatabaseException
d144 1
a144 2
        return getUsersRolesProjects (connection, userID, projectID, null,
                                      Boolean.TRUE);
d150 5
a154 5
     * @@param    connection    A JDBC database connection
     * @@param    projectID     The projectID to get users and roles for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
d156 1
a156 1
    public DataSet getUsersRolesForProject (Connection connection,
d158 1
a158 1
        throws SQLException, DataSetException, JoistDatabaseException
d160 1
a160 2
        return getUsersRolesProjects (connection, null, projectID, null,
                                      Boolean.TRUE);
d164 1
a164 1
     * Retrieves all users and roles.
d166 5
a170 5
     * @@param    connection    A JDBC database connection
     * @@param    projectID     The projectID to get users and roles for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
d173 1
a173 1
        throws SQLException, DataSetException, JoistDatabaseException
d175 1
a175 1
        return getUsersRolesProjects (connection, null, null, null, Boolean.TRUE);
d189 7
a195 8
     * @@param    connection    A JDBC database connection
     * @@param    userID        A user ID constraint on the query.
     * @@param    roleID        A role ID constraint on the query.
     * @@param    projectID     A project ID constraint on the query
     * @@param    projectID     A project ID constraint on the query.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
d197 1
a197 1
    private DataSet getUsersRolesProjects (Connection connection,
d200 2
a201 3
                                           String projectID,
                                           Boolean approved)
        throws SQLException, DataSetException, JoistDatabaseException
d206 3
a208 57
            urp = new TableDataSet (connection, TABLE_NAME);
            urp.where(buildWhereClause(connection, userID, roleID, projectID,
                                       approved));
            return urp.fetchRecords ();
        }
        catch (DataSetException e)
        {
            log.exception (e);
            if (debug) 
                log.info ("Getting Roles and Projects: " + e.getMessage ());
            throw e;
        }
        catch (SQLException e)
        {
            log.exception (e);
            if (debug) 
                log.info ("Getting Roles and Projects: " + e.getMessage ());
            throw e;
        }
        finally
        {
            registerToClose (urp);
        }
    }

    /**
     * Retrieves users who have any role on a project.
     *
     * @@param    connection    A JDBC database connection
     * @@param    projectID     The projectID to get users for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception    DataSetException
     */
    public DataSet getUsersForProject (Connection connection,
                                       String projectID, Boolean approved)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        String action = "Getting Users for Project";
        String approvedClause = null;
        QueryDataSet users = null;

        StringBuffer stmt =
            new StringBuffer ("select distinct User.*  from User join ");
        stmt.append (TABLE_NAME).append (" where ").append (PROJECT_ID);
        stmt.append ("=").append (projectID).append (" and User.userID = ");
        stmt.append (TABLE_NAME).append (".").append (USER_ID);
        if (approved != null)
        {
            approvedClause = makeApprovedClause(connection, approved);
            stmt.append(" and ").append(approvedClause);
        }

        try
        {
            users = new QueryDataSet (connection, stmt.toString ());
            return users.fetchRecords ();
d212 3
a214 2
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
d219 3
a221 2
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
d226 1
a226 1
            registerToClose (users);
a230 169
     * Retrieves projects which the given user has any approved role
     * on.  Only retrieves projects of type="regular"
     *
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to get projects for.
     * @@return     a DataSet
     * @@exception    SQLException
     * @@exception DataSetException 
     */
    public DataSet getProjectsForUser (Connection connection,
                                       String userID)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        String action = "Getting Projects for User";
        QueryDataSet users = null;
        
        StringBuffer stmt =
            new StringBuffer
                ("select distinct HELM_PROJECT.*  from HELM_PROJECT join ");
        stmt.append (TABLE_NAME).append (" where ").append (USER_ID);
        stmt.append ("=").append (userID);
        stmt.append (" and HELM_PROJECT.").append (PROJECT_ID);
        stmt.append ("=");
        stmt.append (TABLE_NAME).append (".").append (PROJECT_ID);
        stmt.append (" AND HELM_PROJECT.type=");
        stmt.append(escapeAndQuoteSQL(ProjectType.REGULAR));

        try
        {
            users = new QueryDataSet (connection, stmt.toString ());
            return users.fetchRecords ();
        }
        catch (DataSetException e)
        {
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
        }
        catch (SQLException e)
        {
            log.exception (e);
            if (debug) log.info (action + e.getMessage ());
            throw e;
        }
        finally
        {
            registerToClose (users);
        }
    }

    /**
     * Called by <code>User.deleteUser()</code>.
     */
    public void deleteUser (Connection connection, String userID) 
        throws SQLException, DataSetException, JoistDatabaseException
    {
        PreparedStatement stmt = null;
        try
        {
            stmt = connection.prepareStatement
                (new StringBuffer("delete from ").append(TABLE_NAME)
                    .append(" where ")
                    .append(buildWhereClause(connection, userID, null, null,
                                             null))
                    .toString());
            stmt.executeUpdate();
        }
        finally
        {
            if (stmt != null) stmt.close();
        }
    }


    /**
     * Requests a role in a project for a user.
     *
     * @@param conn      The JDBC database connection to use.
     * @@param userID    The userID which will be recipient of the role
     * @@param roleID    The roleID to request.
     * @@param projectID The projectID representing the resource requested.
     * @@return          Whether the role was requested.
     *
     * @@exception DataSetException
     * @@exception JoistDatabaseException
     * @@exception SQLException
     * @@exception UpdateException
     */
    public boolean request (Connection connection, String userID, 
                            String roleID, String projectID)
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
    {
        boolean haveRoleInProject = false;
        TableDataSet urp = null;
        try
        {
            urp = new TableDataSet(connection, TABLE_NAME);
            urp.where (buildWhereClause (connection, userID, roleID, projectID,
                                         null));
            urp.fetchRecords (1);
            haveRoleInProject = urp.containsRecord(0);
        }
        finally
        {
            if (urp != null) urp.close();
        }
        if (!haveRoleInProject)
        {
            // The role will be unapproved.
            UserUserGroupManager usrGrpMgr = (UserUserGroupManager)
                getDataManager().getManager
                ("org.tigris.helm.database.UserUserGroupManager");            
            grant(connection, userID, roleID, projectID, 
                  usrGrpMgr.getNobodyUserID(connection));
        }
        return !haveRoleInProject;
    }

    /**
     * Grants the named role in the given project to the given user.
     *
     * @@param conn      The JDBC database connection to use.
     * @@param userID    The userID which will be recipient of the role
     * @@param roleName  Name of the role to grant.
     * @@param projectID The projectID representing the resource of the grant.
     *
     * @@exception DataSetException
     * @@exception JoistDatabaseException
     * @@exception SQLException
     * @@exception UpdateException
     */
    public void grantNamedRole (Connection connection, String userID, 
                                String roleName, String projectID,
                                String approvedBy)
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
    {
        RoleManager roleMgr = (RoleManager) 
            getDataManager().getManager("org.joist.database.RoleManager");
        String roleID = roleMgr.getRoleID(connection, roleName);
        if (debug) log.info("roleID=" + roleID);
        grant(connection, userID, roleID, projectID, approvedBy);
    }

    /**
     * Revokes the named role in the given project from the given user.
     *
     * @@param conn      The JDBC database connection to use.
     * @@param userID    The userID which will have the role revoked.
     * @@param roleName  Name of the role to revoke.
     *
     * @@exception DataSetException
     * @@exception JoistDatabaseException
     * @@exception SQLException
     * @@exception UpdateException
     */
    public void revokeNamedRole (Connection connection, String userID, 
                                 String roleName, String projectID)
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
    {
        RoleManager roleMgr = (RoleManager) 
            getDataManager().getManager("org.joist.database.RoleManager");
        String roleID = roleMgr.getRoleID(connection, roleName);
        revoke(connection, userID, roleID, projectID);
    }

    /**
d233 4
a236 4
     * @@param    connection    The JDBC database connection to use.
     * @@param    userID        The userID which will be recipient of the role
     * @@param    roleID        The roleID to grant.
     * @@param    projectID     The projectID representing the resource of the 
d238 2
a239 2
     * @@exception    SQLException
     * @@exception    DataSetException
d241 3
a243 4
    public void grant (Connection connection, String userID,
                       String roleID, String projectID, String approvedBy)
        throws SQLException, DataSetException, UpdateException, 
               JoistDatabaseException
a244 1
        if (debug) log.info("Attempting to grant role");
d248 2
a249 6
            KeyDef kd = new KeyDef()
                .addAttrib(PROJECT_ID).addAttrib(USER_ID).addAttrib(ROLE_ID);
            urp = new TableDataSet (connection, TABLE_NAME, kd);
            urp.where (buildWhereClause (connection, userID, roleID, projectID,
                                         null));
            urp.fetchRecords(1);
d251 2
a252 2
            Record r;
            if (urp.containsRecord(0))
d254 1
a254 9
                if (debug) log.info("Using existing " + RoleManager.TABLE_NAME
                                    + " record");
                r = urp.getRecord(0);
            }
            else
            {
                if (debug) log.info("Adding new " + RoleManager.TABLE_NAME + 
                                    " record");
                r = urp.addRecord();
d258 1
a259 19
            r.setValue(APPROVED_BY, approvedBy);
            r.save();

            UserUserGroupManager usrGrpMgr = (UserUserGroupManager)
                getDataManager().getManager
                ("org.tigris.helm.database.UserUserGroupManager");
            if (approvedBy != usrGrpMgr.getNobodyUserID(connection))
            {
                AccessControlList.rebuildACL(connection, getDataManager(),
                                             userID);
                    
                //TODO: Only do this if the role has a issue
                //tracker permission.
                TigrisUpdate.reflectGrant(getDataManager(), connection, 
                                          userID, projectID);
            }
            if (debug) log.info("Granting role " + roleID + 
                                " in project " + projectID + 
                                ", approved by " + approvedBy);
d263 3
a265 2
            log.exception (e);
            if (debug) log.warning("Error granting role: " + e.getMessage());
d270 3
a272 2
            log.exception (e);
            if (debug) log.warning("Error granting role: " + e.getMessage());
d277 1
a277 1
            registerToClose(urp);
d282 1
a282 2
     * Revokes the given permission with the given resource from the given
     * role.
d284 3
a286 3
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID from which to revoke the role
     * @@param    roleID        The roleID to revoke
d288 2
a289 2
     * @@exception    SQLException
     * @@exception    DataSetException
d291 3
a293 4
    public void revoke (Connection connection, String userID, String roleID,
                        String projectID) 
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
d298 3
a300 6
            KeyDef kd = new KeyDef ().addAttrib (PROJECT_ID)
                .addAttrib(USER_ID).addAttrib(ROLE_ID);
            urp = new TableDataSet (connection, TABLE_NAME, kd);
            urp.where (buildWhereClause (connection, userID, roleID, 
                                         projectID, null));
            urp.fetchRecords ();
d302 1
a302 1
            for (int i = 0; i < urp.size (); i++)
d304 1
a304 1
                urp.getRecord (i).markToBeDeleted ();
d306 1
a306 6
            urp.save ();

            AccessControlList.rebuildACL(connection, getDataManager(), userID);
            //TODO: Only do this if the role had an issue tracker permission.
            TigrisUpdate.reflectRevoke(getDataManager(), connection, userID,
                                       projectID);
d310 3
a312 3
            log.exception (e);
            String message = "Revoking role: " + e.getMessage ();
            if (debug) log.info (message);
d317 3
a319 3
            log.exception (e);
            String message = "Revoking role: " + e.getMessage ();
            if (debug) log.info (message);
d324 1
a324 1
            registerToClose (urp);
d331 5
a335 5
     * @@param    connection    A JDBC database connection
     * @@param    userID        The userID to revoke roles from
     * @@param    projectID     The projectID from which to revoke roles
     * @@exception    SQLException
     * @@exception    DataSetException
d338 1
a338 2
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
d340 1
a340 1
        revoke (connection, userID, null, projectID);
d344 1
a344 1
     * Revokes the given role unilaterally (across all projects).
d346 4
a349 4
     * @@param    connection    A JDBC database connection
     * @@param    roleID  The roleID to revoke
     * @@exception    SQLException
     * @@exception    DataSetException
d352 1
a352 2
        throws SQLException, DataSetException, JoistDatabaseException,
               UpdateException
d354 1
a354 1
        revoke (connection, roleID, null, null);
d358 1
a358 1
     * Creates a SQL <code>where</code> clause.  
d360 3
a362 6
    protected String buildWhereClause (Connection connection,
                                       String userID,
                                       String roleID,
                                       String projectID,
                                       Boolean approved)
        throws DataSetException, SQLException, JoistDatabaseException
d364 1
a364 2
        StringBuffer whereClause = new StringBuffer ();
        boolean firstClause = true;
d368 1
a368 2
            whereClause.append (USER_ID).append ('=').append (userID);
            firstClause = false;
d372 1
a372 1
            if (!firstClause)
d374 1
a374 1
                whereClause.append (" AND ");
d376 1
a376 2
            whereClause.append (ROLE_ID).append ('=').append (roleID);
            firstClause = false;
d380 1
a380 1
            if (!firstClause)
d382 1
a382 1
                whereClause.append (" AND ");
d384 1
a384 2
            whereClause.append (PROJECT_ID).append ('=').append (projectID);
            firstClause = false;
d387 1
a387 9
        if (approved != null)
        {            
            if (!firstClause)
            {
                whereClause.append (" AND ");
            }
            whereClause.append(makeApprovedClause(connection, approved));
        }
        return whereClause.toString ();
d389 1
a390 13
    private String makeApprovedClause(Connection connection, Boolean approved)
        throws DataSetException, SQLException, JoistDatabaseException 
    {
        UserUserGroupManager usrGrpMgr = (UserUserGroupManager)
            getDataManager().getManager
            ("org.tigris.helm.database.UserUserGroupManager");

        StringBuffer clause = new StringBuffer();
        clause.append(APPROVED_BY);
        clause.append(approved.equals(Boolean.TRUE) ? "!=" : "=");
        clause.append(usrGrpMgr.getNobodyUserID(connection));
        return clause.toString();
    }
a391 24
    /**
     * Updates a record in the table
     *
     *
     * @@param connection          The JDBC database connection to use.
     * @@param projectID           The database key of the project to update 
     *                            (or null, in which case a new project is 
     *                            created).
     * @@param   userID            The id of the user in the project
     * @@param   roleID            The id of the role the user has in the project
     * @@param   approvedBy       userID of person who approved user
     *
     * @@return  nothing
     *
     * @@exception SQLException
     * @@exception DataSetException 
     */
    public void updateField (Connection connection, String projectID,
                             String userID, String roleID,
                             String fieldName, String fieldValue)
        throws SQLException, DataSetException, JoistDatabaseException
    {
        Record userRole = null;
        TableDataSet userRoles = null;
a392 44
        try
        {
            KeyDef kd = new KeyDef ().addAttrib (PROJECT_ID);
            kd.addAttrib (USER_ID);
            kd.addAttrib (ROLE_ID);
            userRoles = new TableDataSet (connection, TABLE_NAME, kd);
            userRoles.where(buildWhereClause(connection, userID, roleID,
                                             projectID, null));

            // Do update.
            userRoles.fetchRecords ();
            if (userRoles.containsRecord (0))
            {
                userRole = userRoles.getRecord (0);
                userRole.setValue ("Approved_by", fieldValue);
                userRole.save ();
            }
            else
            {
                throw new DataSetException ("There is no record for PROJECT_ID=" +
                                            projectID + "and USER_ID=" + userID +
                                            "and ROLE_ID=" + roleID);
            }
        }
        catch (DataSetException e)
        {
            log.exception (e);
            String message = "Updating ProjectUserRole: " + e.getMessage ();
            if (debug) log.info (message);
            throw e;
        }
        catch (SQLException e)
        {
            log.exception (e);
            String message = "Updating ProjectUserRole: " + e.getMessage ();
            if (debug) log.info (message);
            throw e;
        }
        finally
        {
            registerToClose (userRoles);
        }
    }
}
@

