head	1.14;
access;
symbols
	TIGRIS_0_9_2_4:1.9
	TIGRIS_0_9_2_3:1.9
	HELM_PEER_PORT_BRANCH:1.13.0.2
	TURBINE_PEER:1.13
	TIGRIS_0_9_2_1:1.13
	TIGRIS_0_9_2:1.9.0.8
	TIGRIS_0_9_0:1.9.0.6
	TIGRIS_0_8_4:1.9.0.4
	TIGRIS_NOV_12_2000:1.9
	OLDHELM:1.9.0.2
	TIGRIS_NOV_11_2000:1.9;
locks; strict;
comment	@# @;


1.14
date	2001.02.21.03.23.56;	author edk;	state dead;
branches;
next	1.13;

1.13
date	2001.01.26.18.29.00;	author edk;	state Exp;
branches
	1.13.2.1;
next	1.12;

1.12
date	2001.01.26.05.24.05;	author edk;	state dead;
branches;
next	1.11;

1.11
date	2001.01.24.20.42.46;	author dlr;	state Exp;
branches;
next	1.10;

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

1.9
date	2000.09.29.21.19.07;	author leonardr;	state Exp;
branches;
next	1.8;

1.8
date	2000.09.29.20.53.03;	author dlr;	state Exp;
branches;
next	1.7;

1.7
date	2000.09.29.20.50.55;	author dlr;	state Exp;
branches;
next	1.6;

1.6
date	2000.09.29.20.50.04;	author dlr;	state Exp;
branches;
next	1.5;

1.5
date	2000.09.29.20.10.12;	author dlr;	state Exp;
branches;
next	1.4;

1.4
date	2000.09.29.20.04.30;	author dlr;	state Exp;
branches;
next	1.3;

1.3
date	2000.09.29.19.55.46;	author dlr;	state Exp;
branches;
next	1.2;

1.2
date	2000.09.29.19.28.30;	author leonardr;	state Exp;
branches;
next	1.1;

1.1
date	2000.09.29.01.42.45;	author dlr;	state Exp;
branches;
next	;

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


desc
@@


1.14
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.util.*;
import java.sql.*;

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

import com.workingdogs.village.*;

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

/**
 * Manages the Role-Permission-Scope three-way join table.
 *
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 * @@version $Id: RolePermissionScopeManager.java,v 1.13 2001/01/26 18:29:00 edk Exp $
 *
 * Copyright (c) 2000-2001 CollabNet, Inc.
 */

public class RolePermissionScopeManager extends TableManager
{
    /**
     * The name of the database table that this <code>TableManager</code> 
     * manages.
     */
    public static final String TABLE_NAME = "HELM_ROLE_PERMISSION_SCOPE";

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

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

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

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

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

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

    /**
     * Retrieves permissions and scopes for a role.
     *
     * @@param connection A JDBC database connection
     * @@param roleID     The roleID to obtain permissions and scopes for.
     * @@return           The requested permissions.
     *
     * @@exception DBException
     */
    public DataSet getPermissionsScopesForRole (Connection connection, 
                                                String roleID)
	throws DBException
    {
        TableDataSet rps = null;
        try
        {
            rps = new TableDataSet(connection, TABLE_NAME);
            rps.where(buildWhereClause(roleID, null, null));
            return rps.fetchRecords();
        }
        catch (DataSetException e)
        {
            throw new DBException(e);
        }
        catch (SQLException e)
        {
            throw new DBException(e);
        }
        finally
        {
            registerToClose(rps);
        }
    }

    /**
     * Retrieves roles which have the given permission (regardless of scope).
     *
     * @@param connection   A JDBC database connection.
     * @@param permissionID The permissionID to obtain roles for
     * @@param scopeID      The scopeID, if any, associated with the permission
     * @@return	           The requested roles.
     *
     * @@exception DBException
     */
    public DataSet getRolesForPermissionScope (Connection connection, 
                                               String permissionID,
                                               String scopeID)
	throws SQLException, DataSetException
    {
        TableDataSet rps = null;
        try
        {
            rps = new TableDataSet(connection, TABLE_NAME);
            rps.where(buildWhereClause(null, permissionID, null));
            return rps.fetchRecords();
        }
        catch (DataSetException e)
        {
            log.exception(e);
            if (debug) log.debug("Getting Roles for Permission: " + 
                                 e.getMessage());
            throw e;
        }
        catch (SQLException e)
        {
            log.exception(e);
            if (debug) log.debug("Getting Roles for Permission: " + 
                                 e.getMessage());
            throw e;
        }
        finally
        {
            if (rps != null) registerToClose(rps);
        }
    }

    /**
     * Retrieves roles which have the given permission (regardless of scope).
     *
     * @@param	connection    A JDBC database connection
     * @@param	permissionID  The permissionID to obtain roles for.
     * @@return	a DataSet
     *
     * @@exception DBException
     */
    public DataSet getRolesForPermission (Connection connection, 
                                          String permissionID)
        throws SQLException, DataSetException
    {
        return getRolesForPermissionScope(connection, permissionID, null);
    }

    /**
     * Grants the given permission in the given scope to the given role.
     *
     * @@param	connection    The JDBC database connection to use.
     * @@param	roleID        The roleID which will be granted the permission.
     * @@param	permissionID  The permissionID to grant.
     * @@param	scopeID       The scopeID representing the scope of the 
     *                        permission.
     *
     * @@exception DBException
     */
    public void grant (Connection connection, String roleID, 
                       String permissionID, String scopeID)
	throws SQLException, DataSetException
    {
        TableDataSet rps = null;
        try
        {
            rps = new TableDataSet(connection, TABLE_NAME);
            rps.where(buildWhereClause(roleID, permissionID, scopeID));

            rps.fetchRecords();
            if (!rps.containsRecord(0))
            {
                Record r = rps.addRecord();
                r.setValue(ROLE_ID, roleID);
                r.setValue(PERMISSION_ID, permissionID);
                r.setValue(SCOPE_ID, scopeID);
                r.save();
            }
        }
        catch (DataSetException e)
        {
            log.exception(e);
            if (debug) log.debug("Getting permission to role: " + 
                                 e.getMessage());
            throw e;
        }
        catch (SQLException e)
        {
            log.exception(e);
            if (debug) log.debug("Granting permission to role: " + 
                                 e.getMessage());
            throw e;
        }
        finally
        {
            if (rps != null) registerToClose(rps);
        }
    }

    /**
     * Revokes the given permission with the given scope from the given role.
     *
     * @@param	connection    A JDBC database connection
     * @@param	roleID        The roleID from which to revoke the permission
     * @@param	permissionID  The permissionID to revoke
     * @@param   scopeID       The scopeID of the permission to revoke
     *
     * @@exception DBException
     */
    public void revoke (Connection connection, String roleID, 
                        String permissionID, String scopeID)
	throws SQLException, DataSetException
    {
        TableDataSet rps = null;
        try
        {
            rps = new TableDataSet(connection, TABLE_NAME);
            rps.where(buildWhereClause(roleID, permissionID, scopeID));
            rps.fetchRecords();
            for (int i = 0; i < rps.size(); i++)
            {
                rps.getRecord(i).markToBeDeleted();
            }
            rps.save();
        }
        catch (DataSetException e)
        {
            log.exception(e);
            String message = "Revoking permission: " + e.getMessage();
            if (debug) log.debug(message);
            throw e;
        }
        catch (SQLException e)
        {
            log.exception(e);
            String message = "Revoking permission: " + e.getMessage();
            if (debug) log.debug(message);
            throw e;
        }
        finally
        {
            registerToClose(rps);
        }
    }

    /**
     * Revokes the given permission across the given role.
     *
     * @@param	connection    A JDBC database connection
     * @@param	permissionID  The permissionID to revoke
     * @@param	roleID        The roleID to revoke
     *
     * @@exception DBException
     */
    public void revoke (Connection connection, String roleID, 
                        String permissionID)
	throws SQLException, DataSetException
    {
        revoke(connection, permissionID, null, roleID);
    }

    /**
     * Revokes the given permission unilaterally (across all roles).
     *
     * @@param	connection    A JDBC database connection
     * @@param	permissionID  The permissionID to revoke
     *
     * @@exception DBException
     */
    public void revoke (Connection connection, String permissionID)
	throws SQLException, DataSetException
    {
        revoke(connection, permissionID, null, null);
    }
    
    /**
     * Creates a SQL <code>where</code> clause.
     */
    protected static String buildWhereClause (String roleID, 
                                              String permissionID, 
                                              String scopeID)
    {
        StringBuffer whereClause = new StringBuffer();

        if (roleID != null)
        {
            whereClause.append(ROLE_ID).append('=').append(roleID);
        }
        if (permissionID != null)
        {
            if (roleID != null)
            {
                whereClause.append(" AND ");
            }
            whereClause.append(PERMISSION_ID).append('=').append(permissionID);
        }
        if (scopeID != null)
        {
            if (roleID != null || permissionID != null)
            {
                whereClause.append(" AND ");
            }
            whereClause.append(SCOPE_ID).append('=').append(scopeID);
        }

        return whereClause.toString();
    }
}
@


1.13
log
@readding all the files as part of the process of returning to the way
things were as of tuesday
@
text
@d65 1
a65 1
 * @@version $Id: RolePermissionScopeManager.java,v 1.10 2001/01/24 01:21:00 leonardr Exp $
@


1.13.2.1
log
@Porting to the Turbine Peer system.  -dlr and leonardr
@
text
@d65 1
a65 1
 * @@version $Id: RolePermissionScopeManager.java,v 1.13 2001/01/26 18:29:00 edk Exp $
@


1.12
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
@d65 1
a65 1
 * @@version $Id: RolePermissionScopeManager.java,v 1.11 2001/01/24 20:42:46 dlr Exp $
d160 1
a160 1
	throws DBException
d171 4
a174 1
            throw new DBException(e);
d178 4
a181 1
            throw new DBException(e);
d200 1
a200 1
        throws DBException
d218 1
a218 1
	throws DBException
d238 4
a241 1
            throw new DBException(e);
d245 4
a248 1
            throw new DBException(e);
d252 1
a252 1
            registerToClose(rps);
d268 1
a268 1
	throws DBException
d284 4
a287 1
            throw new DBException(e);
d291 4
a294 1
            throw new DBException(e);
d313 1
a313 1
	throws DBException
d327 1
a327 1
	throws DBException
@


1.11
log
@Making use of DBException.
@
text
@d65 1
a65 1
 * @@version $Id: RolePermissionScopeManager.java,v 1.10 2001/01/24 01:21:00 leonardr Exp $
@


1.10
log
@Switching to DBException.
@
text
@d65 1
a65 1
 * @@version $Id: RolePermissionScopeManager.java,v 1.9 2000/09/29 21:19:07 leonardr Exp $
d160 1
a160 1
	throws SQLException, DataSetException
d171 1
a171 4
            log.exception(e);
            if (debug) log.debug("Getting Roles for Permission: " + 
                                 e.getMessage());
            throw e;
d175 1
a175 4
            log.exception(e);
            if (debug) log.debug("Getting Roles for Permission: " + 
                                 e.getMessage());
            throw e;
d194 1
a194 1
        throws SQLException, DataSetException
d212 1
a212 1
	throws SQLException, DataSetException
d232 1
a232 4
            log.exception(e);
            if (debug) log.debug("Getting permission to role: " + 
                                 e.getMessage());
            throw e;
d236 1
a236 4
            log.exception(e);
            if (debug) log.debug("Granting permission to role: " + 
                                 e.getMessage());
            throw e;
d240 1
a240 1
            if (rps != null) registerToClose(rps);
d256 1
a256 1
	throws SQLException, DataSetException
d272 1
a272 4
            log.exception(e);
            String message = "Revoking permission: " + e.getMessage();
            if (debug) log.debug(message);
            throw e;
d276 1
a276 4
            log.exception(e);
            String message = "Revoking permission: " + e.getMessage();
            if (debug) log.debug(message);
            throw e;
d295 1
a295 1
	throws SQLException, DataSetException
d309 1
a309 1
	throws SQLException, DataSetException
@


1.9
log
@Corrected table name
@
text
@d65 1
a65 1
 * @@version $Id: RolePermissionScopeManager.java,v 1.8 2000/09/29 20:53:03 dlr Exp $
d67 1
a67 1
 * Copyright (c) 2000 CollabNet, Inc.
d116 5
a120 5
     * @@param	connection    A JDBC database connection
     * @@param	roleID        The roleID to obtain permissions and scopes for.
     * @@return	 a String
     * @@exception	SQLException
     * @@exception	DataSetException
d124 1
a124 1
	throws SQLException, DataSetException
d135 1
a135 4
            log.exception(e);
            if (debug) log.debug("Getting Permissions and Scopes: " + 
                                 e.getMessage());
            throw e;
d139 1
a139 4
            log.exception(e);
            if (debug) log.debug("Getting Permissions and Scopes: " + 
                                 e.getMessage());
            throw e;
d143 1
a143 1
            if (rps != null) registerToClose(rps);
d150 6
a155 6
     * @@param	connection    A JDBC database connection
     * @@param	permissionID  The permissionID to obtain roles for
     * @@param	scopeID       The scopeID, if any, associated with the permission
     * @@return	a DataSet
     * @@exception	SQLException
     * @@exception	DataSetException
d195 2
a196 2
     * @@exception	SQLException
     * @@exception	DataSetException
d213 2
a214 2
     * @@exception	SQLException
     * @@exception	DataSetException
d263 2
a264 2
     * @@exception	SQLException
     * @@exception	DataSetException
d298 1
a298 1
            if (rps != null) registerToClose(rps);
d308 2
a309 2
     * @@exception	SQLException
     * @@exception	DataSetException
d323 2
a324 2
     * @@exception	SQLException
     * @@exception	DataSetException
@


1.8
log
@Corrected SQL generator.
@
text
@d65 1
a65 1
 * @@version $Id: RolePermissionScopeManager.java,v 1.7 2000/09/29 20:50:55 dlr Exp $
d76 1
a76 1
    public static final String TABLE_NAME = "HELM_SCOPE";
d157 2
a158 1
     * @@param	permissionID  The permissionID to obtain roles for.
d164 2
a165 1
                                               String permissionID)
d193 16
@


1.7
log
@Return a String.
@
text
@d65 1
a65 1
 * @@version $Id: RolePermissionScopeManager.java,v 1.6 2000/09/29 20:50:04 dlr Exp $
d343 1
a343 1
            if (permissionID != null)
a352 3



@


1.6
log
@- Added SQL where clause builder method.
- Ordering of arguments should always be Role, Permission, Scope, as
  per the name of the class.
- Move local var r to reduce its scope.
- Formatted for 80 columns.
@
text
@d65 1
a65 1
 * @@version $Id: RolePermissionScopeManager.java,v 1.5 2000/09/29 20:10:12 dlr Exp $
d350 1
a350 1
        return whereClause;
@


1.5
log
@latest review changes
@
text
@d65 1
a65 1
 * @@version $Id: RolePermissionScopeManager.java,v 1.4 2000/09/29 20:04:30 dlr Exp $
d130 1
a130 1
            rps.where(ROLE_ID + '=' + roleID);
d170 1
a170 1
            rps.where(PERMISSION_ID + '=' + permissionID);
d196 5
a200 4
     * @@param	connection    A JDBC database connection
     * @@param	roleID        The roleID which will be granted the permission
     * @@param	permissionID  The permissionID to grant
     * @@param	scopeID       The scopeID representing the scope of the permission
d204 2
a205 1
    public void grant (Connection connection, String permissionID, String roleID, String scopeID)
d208 1
a208 2
        TableDataSet rps=null;
        Record r = null;
d212 2
a213 2
            rps.where(ROLE_ID + "=" + roleID + " AND " + PERMISSION_ID + "=" + permissionID
                      + " AND " + SCOPE_ID + "=" + scopeID);
d217 1
a217 1
                r = rps.addRecord();
d227 2
a228 2
            String message = "Getting permission to role: " + e.getMessage();
            if (debug) log.debug(message);
d234 2
a235 2
            String message = "Granting permission to role: " + e.getMessage();
            if (debug) log.debug(message);
d254 2
a255 1
    public void revoke (Connection connection, String permissionID, String scopeID, String roleID)
d258 1
a258 2
        TableDataSet rps=null;
        Record r = null;
d262 1
a262 4
            StringBuffer query = new StringBuffer(PERMISSION_ID + "=" + permissionID);
            if (scopeID != null) query.append(" AND " + SCOPE_ID + "=" + scopeID);
            if (roleID != null) query.append(" AND " + ROLE_ID + "=" + roleID);
            rps.where(query.toString());
d299 2
a300 1
    public void revoke (Connection connection, String permissionID, String roleID)
d320 32
d353 3
@


1.4
log
@Started cleanup.
@
text
@d65 1
a65 1
 * @@version $Id: RolePermissionScopeManager.java,v 1.3 2000/09/29 19:55:46 dlr Exp $
d162 2
a163 1
    public DataSet getRolesForPermissionScope (Connection connection, String permissionID)
d166 1
a166 1
        TableDataSet rps=null;
d170 1
a170 1
            rps.where(PERMISSION_ID + " = " + permissionID);
d176 2
a177 2
            String message = "Getting Roles for Permission: " + e.getMessage();
            if (debug) log.debug(message);
d183 2
a184 2
            String message = "Getting Roles for Permission: " + e.getMessage();
            if (debug) log.debug(message);
@


1.3
log
@Collab.Net --> CollabNet in copyright.
@
text
@d65 1
a65 1
 * @@version $Id: RolePermissionScopeManager.java,v 1.2 2000/09/29 19:28:30 leonardr Exp $
d72 24
d97 4
a102 5
    static final String rpsTable = "HELM_SCOPE";
    static final String roleIDField = "ROLE_ID";   
    static final String permissionIDField = "PERMISSION_ID";   
    static final String scopeIDField = "SCOPE_ID";

d104 1
a104 1
     * Instantiates a new instance.
d106 1
a106 1
     * @@param	dataMgr  A variable of type DataManager
d122 2
a123 1
    public DataSet getPermissionsScopesForRole (Connection connection, String roleID)
d126 1
a126 1
        TableDataSet rps=null;
d129 2
a130 2
            rps = new TableDataSet(connection, rpsTable);
            rps.where(roleIDField + " = " + roleID);
d136 2
a137 2
            String message = "Getting Permissions and Scopes: " + e.getMessage();
            if (debug) log.debug(message);
d143 2
a144 2
            String message = "Getting Permissions and Scopes: " + e.getMessage();
            if (debug) log.debug(message);
d168 2
a169 2
            rps = new TableDataSet(connection, rpsTable);
            rps.where(permissionIDField + " = " + permissionID);
d209 3
a211 3
            rps = new TableDataSet(connection, rpsTable);
            rps.where(roleIDField + "=" + roleID + " AND " + permissionIDField + "=" + permissionID
                      + " AND " + scopeIDField + "=" + scopeID);
d216 3
a218 3
                r.setValue(roleIDField, roleID);
                r.setValue(permissionIDField, permissionID);
                r.setValue(scopeIDField, scopeID);
d259 4
a262 4
            rps = new TableDataSet(connection, rpsTable);
            StringBuffer query = new StringBuffer(permissionIDField + "=" + permissionID);
            if (scopeID != null) query.append(" AND " + scopeIDField + "=" + scopeID);
            if (roleID != null) query.append(" AND " + roleIDField + "=" + roleID);
@


1.2
log
@Wrote initial functionality.
@
text
@d65 1
a65 1
 * @@version $Id: RolePermissionScopeManager.java,v 1.1 2000/09/29 01:42:45 dlr Exp $
d67 1
a67 1
 * Copyright (c) 2000 Collab.Net, Inc.
@


1.1
log
@Table manager stubs for the new Helm permission system.
@
text
@d2 1
a2 1
 * Copyright (c) 2000 Collab.Net.  All rights reserved.
d17 1
a17 1
 * software developed by Collab.Net (http://www.Collab.Net/)."
d27 1
a27 1
 * permission of Collab.Net.
d32 1
a32 1
 * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
d44 1
a44 1
 * individuals on behalf of Collab.Net.
d61 1
a61 1
 * Manages one or more database table.
d64 2
a65 1
 * @@version $Id: $
d72 1
a72 1
    private static Log log = new Log("rpsmMgr", "RolePermissionScopeManager");
d75 5
d89 207
@

