User: oconnor 
  Date: 00/10/04 07:56:45

  Added:       src/main/org/jboss/security DatabaseRealmMapping.java
  Log:
  First pass at database implementation of role mapping.
  
  Revision  Changes    Path
  1.1                  jboss/src/main/org/jboss/security/DatabaseRealmMapping.java
  
  Index: DatabaseRealmMapping.java
  ===================================================================
  /*
   * jBoss, the OpenSource EJB server
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
   
  package org.jboss.security;
  
  import java.io.File;
  import java.net.URL;
  import java.rmi.server.UnicastRemoteObject;
  import java.util.Set;
  import java.util.LinkedList;
  import java.util.Iterator;
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  
  import java.security.Principal;
  
  import javax.naming.InitialContext;
  import javax.naming.Context;
  import javax.naming.Reference;
  import javax.naming.Name;
  import javax.naming.spi.ObjectFactory;
  import javax.sql.DataSource;
  import javax.ejb.EJBException;
  
  import javax.management.MBeanServer;
  import javax.management.ObjectName;
  
  import org.jboss.logging.Log;
  import org.jboss.util.ServiceMBeanSupport;
  
  import org.jboss.system.RealmMapping;
  
  /**
   *      
   *   @see EJBSecurityManager
   *   @author Daniel O'Connor [EMAIL PROTECTED]
   */
  public class DatabaseRealmMapping implements RealmMapping
  {
  
        public boolean doesUserHaveRole( Principal principal, Set roleNames )
        {
      Connection con = null;
      try
      {
        InitialContext initial = new InitialContext();
        DataSource ds = (DataSource) initial.lookup( "SecurityDS" );
        con = ds.getConnection();
        PreparedStatement statement = con.prepareStatement(
          "select rolename from sec_roles where principal=? and setname=?");
        statement.setString(1, principal.getName());
        statement.setString(2, "basic");
        ResultSet rs = statement.executeQuery();
        boolean hasRole = false;
        while (rs.next() && !hasRole)
        {
          String roleName = rs.getString(1).trim();
          if (roleNames.contains(roleName))
            hasRole = true;
        }
        rs.close();
        statement.close();
        return hasRole;
      }
      catch (Exception e)
      {
        e.printStackTrace();
        throw new EJBException( e );
      }
      finally
      {
        try
        {
          if (con != null)
            con.close();
        }
        catch (Exception e)
        {
        }
      }
    }
  
        
  }
  
  
  
  

Reply via email to