JCVSPermManager.java :  » Source-Control » jcvsweb » com » ice » jcvsweb » manager » Java Open Source

Java Open Source » Source Control » jcvsweb 
jcvsweb » com » ice » jcvsweb » manager » JCVSPermManager.java

package com.ice.jcvsweb.manager;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.ServletContext;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.ice.jcvsweb.bean.JCVSConfiguration;
import com.ice.jcvsweb.bean.JCVSProject;
import com.ice.jcvsweb.bean.JCVSUser;
import com.ice.jcvsweb.bean.JCVSUserPerm;
import com.ice.jcvsweb.helper.ContextHelper;
import com.ice.jcvsweb.helper.XMLHelper;


/**
 * This class Manages our JCVSPermissions.
 *
 * @author Tim Endres, <a href="mailto:time@jcvs.org">time@jcvs.org</a>
 * @see com.ice.cvsc
 */

public
class    JCVSPermManager
  {
  public static final String      ATTR_NAME = "jcvsPermManager";
  private static final String      KEY_SEPARATOR = "|";


  private HashMap        userPermMap = null;
  private JCVSUserPerm    defUserPerm = null;
  private ServletContext    context = null;
  private JCVSConfiguration  config = null;


  public
  JCVSPermManager( ServletContext ctx, JCVSConfiguration config )
    throws IOException
    {
    this.context = ctx;
    this.config = config;
    this.userPermMap = new HashMap( 256 );
    }

  private String
  getUserPermKey( String user, String project )
    {
    return ( user + KEY_SEPARATOR + project );
    }

  public File
  getPermConfDir()
    {
    return new File
      ( ContextHelper.getRealAbsolutePath
        ( this.context, this.config.getConfigDir() ), "permission" );
    }

  public File
  getUserPermFile()
    {
    return new File( this.getPermConfDir(), "user.xml" );
    }

  public JCVSUserPerm
  getUserPermission( String user, String project )
    {
    String key = this.getUserPermKey( user, project );

    JCVSUserPerm result =
      (JCVSUserPerm) this.userPermMap.get( key );

    if ( result == null )
      {
      result = this.defUserPerm;
      }

    return result;
    }

  public boolean
  getUserCanEdit( JCVSUser user, JCVSProject project )
    {
    return ( user.getIsAdmin()
      || project.getIsOwner( user.getName() )
      );
    }

  public boolean
  getUserCanView( JCVSUser user, JCVSProject project )
    {
    boolean result = false;

    if ( user != null
        && ( user.getIsAdmin()
          || project.getIsOwner( user.getName() ) ) )
      {
      result = true;
      }
    else if ( user != null && project.getDef().getViewingRestricted() )
      {
      JCVSUserPerm perm = this.getUserPermission
        ( user.getName(), project.getDef().getKey() );

      if ( perm != null )
        {
        result = perm.getViewPerm();
        }
      }
    else
      {
      result = ! this.config.getViewingRestricted();
      }

    return result;
    }

  public int
  getProjectGrantCount( String projectKey )
    {
    int count = 0;
    Iterator iter = this.userPermMap.keySet().iterator();
    for ( ; iter.hasNext() ; )
      {
      String key = (String) iter.next();
      int idx = key.indexOf( KEY_SEPARATOR );
      if ( idx != -1 )
        {
        String userName = key.substring( 0, idx );
        String pKey = key.substring( idx + 1 );
        if ( pKey != null && pKey.equals( projectKey ) )
          {
          ++count;
          }
        }
      }

    return count;
    }

  public String[]
  getProjectGrantees( String projectKey )
    {
    ArrayList users = new ArrayList( 32 );
    Iterator iter = this.userPermMap.keySet().iterator();
    for ( ; iter.hasNext() ; )
      {
      String key = (String) iter.next();
      int idx = key.indexOf( KEY_SEPARATOR );
      if ( idx != -1 )
        {
        String userName = key.substring( 0, idx );
        String pKey = key.substring( idx + 1 );
        if ( pKey != null && pKey.equals( projectKey ) )
          {
          users.add( userName );
          }
        }
      }

    String[] result = new String[ users.size() ];
    users.toArray( result );
    return result;
    }

  public JCVSUserPerm
  createUserPermission( String name, String projectKey )
    throws IOException
    {
    JCVSUserPerm perm = new JCVSUserPerm();

    perm.setUserName( name );
    perm.setProjectKey( projectKey );
    perm.setViewPerm( false );
    perm.setAdminPerm( false );

    String key = this.getUserPermKey
      ( perm.getUserName(), perm.getProjectKey() );

    this.userPermMap.put( key, perm );

    return perm;
    }

  public JCVSUserPerm
  deleteUserPermission( String name, String projectKey )
    throws IOException
    {
    String key = this.getUserPermKey( name, projectKey );

    JCVSUserPerm perm = (JCVSUserPerm) this.userPermMap.remove( key );

    return perm;
    }

  private void
  loadUserPermissions( File permFile )
    throws IOException
    {
    try {
      Document xmlDoc = XMLHelper.readXMLConfiguration( permFile );

      if ( xmlDoc != null )
        {
        NodeList nodes =
          XMLHelper.selectNodeList
            ( xmlDoc.getDocumentElement(),
              "/jcvs-permissions/jcvs-user-perm" );

        for ( int pi = 0 ; pi < nodes.getLength() ; ++pi )
          {
          JCVSUserPerm perm = new JCVSUserPerm();
          perm.loadConfiguration( nodes.item( pi ) );

          String key = this.getUserPermKey
            ( perm.getUserName(), perm.getProjectKey() );

          this.userPermMap.put( key, perm );
          }
        }
      }
    catch ( SAXException ex )
      {
      throw new IOException
        ( "loading user permissions from '" + permFile
          + "', " + ex.getMessage() );
      }
    }

  public void
  saveUserPermissions()
    throws IOException
    {
    FileWriter fW = null;
    PrintWriter pW = null;
    try {
      fW = new FileWriter( this.getUserPermFile() );
      pW = new PrintWriter( fW );

      pW.println( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
      pW.println( "<jcvs-permissions>" );
      pW.println( "" );

      Iterator permIter = this.userPermMap.values().iterator();
      for ( ; permIter.hasNext() ; )
        {
        JCVSUserPerm perm = (JCVSUserPerm) permIter.next();
        perm.saveConfiguration( pW, "\t" );
        }

      pW.println( "" );
      pW.println( "</jcvs-permissions>" );
      }
    finally
      {
      if ( pW != null ) pW.close();
      }
    }

  public void
  ensureInitialState()
    {
    JCVSUserPerm perm = new JCVSUserPerm();
    this.defUserPerm = perm;
    perm.setUserName( "_DEFAULT_" );
    perm.setProjectKey( "_DEFAULT_" );
    perm.setViewPerm( false );
    perm.setAdminPerm( false );

    File dirF = this.getPermConfDir();
    if ( ! dirF.exists() )
      {
      if ( ! dirF.mkdirs() )
        {
        this.context.log(
          "FATAL Could not establish directory '"
          + dirF.getPath() + "'" );
        }
      }

    this.ensureUserPermissions();

    File userPermF = this.getUserPermFile();
    try {
      this.loadUserPermissions( userPermF );
      }
    catch ( IOException ex )
      {
      this.context.log(
        "FATAL failed loading user permissions '"
        + userPermF.getPath() + "'", ex );
      }
    }

  private void
  ensureUserPermissions()
    {
    File f = this.getUserPermFile();
    if ( ! f.exists() )
      {
      try { this.saveUserPermissions(); }
      catch ( IOException ex )
        {
        this.context.log
          ( "FATAL ensuring permissions", ex );
        }
      }
    }

  }


java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.