head	1.6;
access;
symbols
	TIGRIS_NOV_12_2000:1.5
	OLDHELM:1.5.0.2
	TIGRIS_NOV_11_2000:1.5
	TIGRIS_SEP_13_2000:1.4.0.2
	TIGRIS_BASELINE:1.4;
locks; strict;
comment	@# @;


1.6
date	2000.11.12.23.12.53;	author dlr;	state dead;
branches;
next	1.5;

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

1.4
date	2000.09.20.03.13.16;	author brian;	state Exp;
branches;
next	1.3;

1.3
date	2000.04.18.00.10.34;	author jrobbins;	state Exp;
branches;
next	1.2;

1.2
date	2000.02.03.04.21.32;	author jrobbins;	state Exp;
branches;
next	1.1;

1.1
date	2000.01.30.01.59.38;	author jrobbins;	state Exp;
branches;
next	;


desc
@@


1.6
log
@Does not exist in newhelm.
@
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 Collab.Net (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 Collab.Net.
 * 
 * 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 COLLAB.NET 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 Collab.Net.
 */


package org.tigris.helm.database;

import javax.servlet.*;

import java.util.*;
import com.sun.java.util.collections.*;
import java.sql.*;
import org.tigris.helm.tool.UpdateException;
import org.tigris.helm.tool.TigrisUpdate;

public class UserPrivs {
    /**
     * An array of the names of the privileges
     */
    private static String[] optList = {
	//"users_priv",
	//"grant_priv",
	//"projects_priv",
	//"config_priv",
	"checkout_priv",
	"commit_priv"
    };
    protected List initPrivList() {
	return Arrays.asList(optList);
    }


  private static HashMap optTable;
  static {
    // Initialize the list of privileges. Eventually this should probably
    // be serialized. For now this will do.
    optTable = new HashMap(optList.length);
    //optTable.put("users_priv", "add, edit, remove users");
    //optTable.put("grant_priv", "grant domain privileges to other users");
    //optTable.put("projects_priv", "add, edit, remove projects");
    //optTable.put("config_priv", "configure domain properties");
    optTable.put("checkout_priv", "read all CVS modules in the domain");
    optTable.put("commit_priv", "change all CVS modules in the domain");
  };
  protected Map initPrivTable() {
    return optTable;
  }

  public static class UPEntry {
    private String name;
    private boolean value;
	
    UPEntry(String optName, boolean optValue) {
	name = optName;
	value = optValue;
    }

    /**
     * Get the internal name of this option. This generally corresponds
     * to the database column this name is found under
     */
    public String getName() { return name; }
    /**
     * Returns a short description of this option, usually about
     * a sentence in length.
     */
    public String getText() { return (String)optTable.get(name); }
    /**
     * Obtain the default value of this option.  All values default to false.
     *
     * @@return true if and only if the option exists and is valid
     */
    public boolean getValue() { return value; }
  } /* end class UPEntry */

  /**
   * Returns a List of UserPrivs.UPEntry entries for the
   * default values for a mailing list for the boolean options.
   */
  public static List getDefaultValues() {
    UPEntry[] opts = new UPEntry[optList.length];
    for (int i = 0; i < optList.length; i++)
	    opts[i] = new UPEntry(optList[i], false);
    return Arrays.asList(opts);
  }

  /**
   * A list of the names of the privileges. They should correspond
   * to the columns of the privilege table, and should map to the keys
   * of the privTable table below.
   */
  private List privList = initPrivList();

  /**
   * The current privileges. The keys are the names of the privs.
   * The values are instances of class Boolean (true for Y)
   */
  private HashMap privValues;

  
  /**
   * A map with the (db) names of the privileges as keys and
   * a short description of them as values.
   */
  private Map privTable = initPrivTable();

  /**
   * Does a row for this set of privileges exist in the table?
   */
  public boolean rowExists;

  /**
   * The table used to find the current privileges in.
   */
  private String tableName;
  private String username;
  

  public static class UserDoesNotExist extends Exception {
    private UserDoesNotExist(String user) {
	    super("User " + user + " does not exist");
    }
    private UserDoesNotExist() {
	    super("User does not exist");
    }
  } /* end class UserDoesNotExist */ 

  /**
   * Create a new UserPrivs object based on the given mailing list. 
   *
   * @@param project the project the mailing list belongs to
   * @@param list the name of the mailing list
   *
   * @@exception DoesNotExist if the mailing list does not exist
   * @@exception SQLException if there is a database error
   */
  public UserPrivs(Connection connection, String username)
    throws SQLException, UserDoesNotExist {
    
    this.username = username;
    tableName = "domain_privs";
    
    StringBuffer cols = new StringBuffer();
    for (Iterator i = privList.iterator(); i.hasNext();) {
	    cols.append((String)i.next());
	    if (i.hasNext()) cols.append(',');
    }
    
    Statement s = connection.createStatement();
    ResultSet r = s.executeQuery("SELECT " + cols + " FROM " + tableName
                                 + " WHERE username='" + username + "'");
    
    rowExists = r.next();
    
    // Fill up the privValues table with either the results from the
    // database, or default values if no row exists
    privValues = new HashMap(privList.size());
    for (ListIterator i = privList.listIterator(); i.hasNext();) {
	    int idx = i.nextIndex();
	    String name = (String)i.next();
	    privValues.put(name, new Boolean(rowExists
                                       ? r.getString(idx+1).equals("Y")
                                       : false));
    }
    
  }



    /**
     * Returns a List of UPEntry objects that represent the
     * domain privileges for this object
     *
     * @@see UserPrivs.UPEntry
     */
    public List getPrivList() {
	return Arrays.asList(getPrivArray());
    }

    public UPEntry[] getPrivArray() {
	UPEntry[] privs = new UPEntry[privList.size()];
	for (ListIterator i = privList.listIterator(); i.hasNext();) {
	    int idx = i.nextIndex();
	    String optName = (String) i.next();
	    boolean optValue = Boolean.TRUE.equals(privValues.get(optName));
	    privs[idx] = new UPEntry(optName, optValue);
	}
	return privs;
    }

  /**
   * Returns a List of Strings corresponding to the names of the
   * privileges
   */
  public List getPrivNames() {
    return Collections.unmodifiableList(privList);
  }

    
  /**
   * Changes a privilege. This method presumes the privilege is a
   * valid name; if not, an SQL error may occur.
   *
   * @@param privilege the name of the privilege to change
   * @@param value true if and only if the given privilege should exist
   */
  public void setPrivilege(Connection connection,
			   String privilege, boolean value)
    throws SQLException {
    Statement s = connection.createStatement();
    
    // only bother changing if we're making a change
    if (value == getPrivilege(privilege))
	    return;
    
    if (rowExists) {
	    s.executeUpdate("UPDATE " + tableName + " SET " + privilege
			    + " = '" + (value ? 'Y' : 'N') +
			    "' WHERE username='" + username +"'");
    } else {
      
	    // Note that both strings now end with a comma. We use this fact
	    // below.
      
	    s.executeUpdate("INSERT INTO " + tableName + " (username,"
                      + privilege + ") VALUES ('" + username + "',"
                      + (value ? "'Y'" : "'N'") + ')');
	    rowExists = true;
    }
    
    privValues.put(privilege, new Boolean(value));
  }




  

  /**
   * Returns the value of the given privilege
   *
   * @@param privilege the name of the privilege to query
   * @@return true if and only if the privilege exists and is valid
   */
  public boolean getPrivilege(String privilege) {
    try {
      return ((Boolean)privValues.get(privilege)).booleanValue();
    } catch (NullPointerException e) {
      return false;
    }
  }
  
  /**
   * Returns a short (sentence-long) description of the abilities
   * granted by the given privilege
   *
   * @@param privilege the name of the privilege to describe
   * @@return a description, or null if the named privilege does not exist
   */
  public String getDescription(String privilege) {
    return (String)privTable.get(privilege);
  }
  
} /* end class UserPrivs */
@


1.5
log
@Collab.Net --> CollabNet in copyright.
@
text
@@


1.4
log
@
Obtained from:
Submitted by:
Reviewed by:

Moved from org.helm to org.tigris.helm
Moved from org/helm to org/tigris/helm

perl -pi.bak -e, how I love thee

note: this update was done POST-CVS-REPOSITORY surgury.  In order to
get this right, you may need to remove the directories

$CVSROOT/helm/java/org/helm
$CVSROOT/helm/data/htdoc/look/www/org/helm
$CVSROOT/helm/templates/org/helm

before doing the "CVS update" call, since those directories don't
exist anymore and CVS will try to create lockfiles for you in them,
giving you an error.
@
text
@d2 1
a2 1
 * Copyright (c) 2000 Collab.Net.  All rights reserved.
@


1.3
log
@switched from collections2.jar to collections.jar
@
text
@d48 1
a48 1
package org.helm.database;
d55 2
a56 2
import org.helm.tool.UpdateException;
import org.helm.tool.TigrisUpdate;
@


1.2
log
@Added tigris.org license
PR:
Obtained from:
Submitted by:
Reviewed by:
@
text
@d53 1
@


1.1
log
@initial checking
@
text
@d1 46
a46 1
// Copyright (c) 2000 Collab.Net, Inc. All Rights Reserved.
@

