portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tay...@apache.org
Subject cvs commit: jakarta-jetspeed/src/java/org/apache/jetspeed/services/security/turbine TurbineAuthentication.java TurbineUserManagement.java
Date Wed, 05 Jun 2002 01:21:53 GMT
taylor      2002/06/04 18:21:53

  Modified:    src/java/org/apache/jetspeed/om/security Tag: security_14
                        BaseJetspeedUser.java JetspeedUser.java
                        JetspeedUserFactory.java
               src/java/org/apache/jetspeed/om/security/turbine Tag:
                        security_14 TurbineUserPeer.java
               src/java/org/apache/jetspeed/services Tag: security_14
                        JetspeedSecurity.java JetspeedUserManagement.java
               src/java/org/apache/jetspeed/services/security Tag:
                        security_14 TestPortalAuthentication.java
                        TestUserManagement.java
               src/java/org/apache/jetspeed/services/security/turbine Tag:
                        security_14 TurbineAuthentication.java
                        TurbineUserManagement.java
  Log:
  - completed initial implementation of JetspeedUserManagement
  - unit tests for UserManagement (getUser, getUsers, saveUser, addUser, removeUser)
  - next step is to integrate into Jetspeed actions (login,logout,access-control)
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.3.2.3   +34 -2     jakarta-jetspeed/src/java/org/apache/jetspeed/om/security/BaseJetspeedUser.java
  
  Index: BaseJetspeedUser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/om/security/BaseJetspeedUser.java,v
  retrieving revision 1.3.2.2
  retrieving revision 1.3.2.3
  diff -u -r1.3.2.2 -r1.3.2.3
  --- BaseJetspeedUser.java	4 Jun 2002 06:34:53 -0000	1.3.2.2
  +++ BaseJetspeedUser.java	5 Jun 2002 01:21:52 -0000	1.3.2.3
  @@ -79,9 +79,9 @@
    * for custom attributes). 
    *
    * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
  - * @version $Id: BaseJetspeedUser.java,v 1.3.2.2 2002/06/04 06:34:53 taylor Exp $
  + * @version $Id: BaseJetspeedUser.java,v 1.3.2.3 2002/06/05 01:21:52 taylor Exp $
    */
  -public class BaseJetspeedUser extends SecurityObject implements JetspeedUser
  +public class BaseJetspeedUser /*extends SecurityObject*/ implements JetspeedUser
   {
       /** The date on which the user account was created. */
       private Date createDate = null;
  @@ -94,6 +94,10 @@
       /** This is data that will not survive a servlet engine restart. */
       private Hashtable tempStorage = null;
   
  +    protected String name = "";
  +
  +    protected boolean isNew = true;
  +
       /**
        * Constructor.
        * Create a new User and set the createDate.
  @@ -105,6 +109,7 @@
           permStorage = new Hashtable(10);
           setHasLoggedIn(Boolean.FALSE);
           setDisabled(false);
  +        isNew = true;
       }
   
   
  @@ -130,6 +135,14 @@
           return tmp;
       }
   
  +    public void setUserId(String id)
  +    {
  +        if (getUserId() == null)
  +        {
  +            setPerm(JetspeedUser.USER_ID, id);
  +        }
  +    }
  +
       /**
        * Gets the access counter for a user during a session.
        *
  @@ -736,4 +749,23 @@
           setPerm(JetspeedUser.DISABLED, (disabled) ? "T" : "F");
       }
   
  +    public String getName()
  +    {
  +        return name;
  +    }
  +
  +    public void setName(String name)
  +    {
  +        this.name = name;
  +    }
  +
  +    public boolean isNew()
  +    {
  +        return isNew;
  +    }
  +
  +    void setNew(boolean isNew)
  +    {
  +        this.isNew = isNew;
  +    }
   }
  
  
  
  1.1.2.3   +3 -1      jakarta-jetspeed/src/java/org/apache/jetspeed/om/security/JetspeedUser.java
  
  Index: JetspeedUser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/om/security/JetspeedUser.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- JetspeedUser.java	4 Jun 2002 06:34:53 -0000	1.1.2.2
  +++ JetspeedUser.java	5 Jun 2002 01:21:52 -0000	1.1.2.3
  @@ -60,7 +60,7 @@
    * A Jetspeed extension of the Turbine User interface.
    *
    * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
  - * @version $Id: JetspeedUser.java,v 1.1.2.2 2002/06/04 06:34:53 taylor Exp $
  + * @version $Id: JetspeedUser.java,v 1.1.2.3 2002/06/05 01:21:52 taylor Exp $
    */
   public interface JetspeedUser extends User
   {
  @@ -84,4 +84,6 @@
         */
       public String getUserId();
   
  +
  +    public boolean isNew();
   }
  
  
  
  1.1.2.2   +10 -0     jakarta-jetspeed/src/java/org/apache/jetspeed/om/security/Attic/JetspeedUserFactory.java
  
  Index: JetspeedUserFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/om/security/Attic/JetspeedUserFactory.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- JetspeedUserFactory.java	30 May 2002 07:04:10 -0000	1.1.2.1
  +++ JetspeedUserFactory.java	5 Jun 2002 01:21:52 -0000	1.1.2.2
  @@ -88,6 +88,12 @@
       public static JetspeedUser getInstance()
           throws UserException
       {
  +        return getInstance(true);
  +    }
  +
  +    public static JetspeedUser getInstance(boolean isNew)
  +        throws UserException
  +    {
           JetspeedUser user = null;
   
           if (null == userClassName)
  @@ -109,6 +115,10 @@
           try
           {
               user = (JetspeedUser)userClass.newInstance();
  +            if (user instanceof BaseJetspeedUser)
  +            {
  +                ((BaseJetspeedUser)user).setNew(isNew);
  +            }            
           }
           catch(Exception e)
           {
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.3   +38 -1     jakarta-jetspeed/src/java/org/apache/jetspeed/om/security/turbine/Attic/TurbineUserPeer.java
  
  Index: TurbineUserPeer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/om/security/turbine/Attic/TurbineUserPeer.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- TurbineUserPeer.java	4 Jun 2002 06:34:53 -0000	1.1.2.2
  +++ TurbineUserPeer.java	5 Jun 2002 01:21:52 -0000	1.1.2.3
  @@ -147,7 +147,7 @@
               //populateObject(row, offset, obj);
               //obj.setModified(false);
               //obj.setNew(false);
  -            JetspeedUser user = JetspeedUserFactory.getInstance();
  +            JetspeedUser user = JetspeedUserFactory.getInstance(false);
               populateUserObject(row, offset, user);
               return user;
           }
  @@ -178,10 +178,16 @@
           int objectDataPosition = columnNames.length;
           for( int i=0; i<columnNames.length; i++ )
           {
  +            if (columnNames[i].equals(USER_ID))
  +                idPosition = i+1;
  +
               if (columnNames[i].equals(TURBINE_OBJECTDATA))
                   objectDataPosition = i+1;
           }
   
  +        //((org.apache.turbine.om.Persistent)user).setPrimaryKey( 
  +        //    new org.apache.turbine.om.NumberKey(row.getValue(idPosition).asBigDecimal())
);
  +
           // Restore the Permanent Storage Hashtable.  First the
           // Hashtable is restored, then any explicit table columns
           // which should be included in the Hashtable are added.
  @@ -239,6 +245,37 @@
               }
           }
           user.setPermStorage( tempHash );
  +    }
  +
  +    /**
  +     * Builds a criteria object based upon an User object
  +     */
  +    public static Criteria buildCriteria(JetspeedUser user)
  +    {
  +        Hashtable permData = (Hashtable) user.getPermStorage().clone();
  +        Criteria criteria = new Criteria();
  +        if ( !user.isNew() )
  +        {
  +            System.out.println("$$$ User IS NOT NEW");
  +            criteria.add(USER_ID, new NumberKey(user.getUserId()));
  +        }
  +
  +        for (int i=1; i < columnNames.length; i++ )
  +        {
  +            if ( permData.containsKey(columnNames[i]) )
  +            {
  +                String key = new StringBuffer(TABLE_NAME)
  +                    .append('.').append(columnNames[i]).toString();
  +
  +                System.out.println("setting column = " + key);
  +
  +                criteria.add( key,
  +                              permData.remove(TurbineUserPeer.columnNames[i]) );
  +            }
  +        }
  +        String dataKey = new StringBuffer(TABLE_NAME).append('.').append(TURBINE_OBJECTDATA).toString();
  +        criteria.add( dataKey, permData );
  +        return criteria;
       }
   
   }
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.10.2.5  +37 -3     jakarta-jetspeed/src/java/org/apache/jetspeed/services/JetspeedSecurity.java
  
  Index: JetspeedSecurity.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/JetspeedSecurity.java,v
  retrieving revision 1.10.2.4
  retrieving revision 1.10.2.5
  diff -u -r1.10.2.4 -r1.10.2.5
  --- JetspeedSecurity.java	2 Jun 2002 13:09:21 -0000	1.10.2.4
  +++ JetspeedSecurity.java	5 Jun 2002 01:21:53 -0000	1.10.2.5
  @@ -73,6 +73,8 @@
   import org.apache.jetspeed.om.security.JetspeedUser;
   import org.apache.jetspeed.services.JetspeedPortalAccessController;
   import org.apache.jetspeed.services.security.PortalResource;
  +import org.apache.jetspeed.services.JetspeedUserManagement;
  +import org.apache.jetspeed.services.JetspeedAuthentication;
   import org.apache.turbine.util.Log;
   
   /**
  @@ -81,7 +83,7 @@
    * 
    * @see org.apache.jetspeed.services.security.JetspeedSecurityService
    * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
  - * @version $Id: JetspeedSecurity.java,v 1.10.2.4 2002/06/02 13:09:21 paulsp Exp $
  + * @version $Id: JetspeedSecurity.java,v 1.10.2.5 2002/06/05 01:21:53 taylor Exp $
    */
   
   abstract public class JetspeedSecurity extends TurbineSecurity
  @@ -221,12 +223,19 @@
   
       /**
        * @see JetspeedSecurityService#getAuthenticatedUser
  -     */
      public static User getAuthenticatedUser(String username, String password)
           throws DataBackendException, UnknownEntityException, PasswordMismatchException
       {
  -       return ((JetspeedSecurityService)getService()).getAuthenticatedUser(username, password);
  +       try
  +       {
  +           JetspeedUser user = JetspeedAuthentication.login(username, password);
  +       }
  +       catch(JetspeedSecurityException e)
  +       {
  +           throw new 
  +       }
       }
  +*/
   
       /**
        * @see JetspeedSecurityService#convertUserName
  @@ -265,4 +274,29 @@
          return ((JetspeedSecurityService)getService()).getAutoLogonDisable();
       }
   
  +/*
  +    public static User getUser( String username ) 
  +        throws DataBackendException, UnknownEntityException
  +    {
  +        return JetspeedUserManagement.getUser(username);
  +    }
  +
  +    public static User[] getUsers( Criteria criteria )
  +        throws DataBackendException
  +    {
  +        return JetspeedUserManagement.getUsers(criteria);
  +    }
  +
  +    public static User getAnonymousUser()
  +        throws UnknownEntityException
  +    {
  +        return JetspeedUserManagement.getAnonymousUser();
  +    }
  +
  +    public static void saveUser( User user )
  +        throws UnknownEntityException, DataBackendException
  +    {
  +        JetspeedUserManagement.saveUser(user);
  +    }
  +  */
   }
  
  
  
  1.1.2.2   +6 -6      jakarta-jetspeed/src/java/org/apache/jetspeed/services/Attic/JetspeedUserManagement.java
  
  Index: JetspeedUserManagement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/Attic/JetspeedUserManagement.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- JetspeedUserManagement.java	30 May 2002 07:04:10 -0000	1.1.2.1
  +++ JetspeedUserManagement.java	5 Jun 2002 01:21:53 -0000	1.1.2.2
  @@ -72,7 +72,7 @@
    * Static accessor for the PortalAccessController service
    *
    * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
  - * @version $Id: JetspeedUserManagement.java,v 1.1.2.1 2002/05/30 07:04:10 taylor Exp $
  + * @version $Id: JetspeedUserManagement.java,v 1.1.2.2 2002/06/05 01:21:53 taylor Exp $
    */
   public abstract class JetspeedUserManagement
   {
  @@ -101,7 +101,7 @@
       /**
        * @see org.apache.jetspeed.services.security.UserManagement#getUsers
        */
  -    Iterator getUsers()
  +    public static Iterator getUsers()
           throws JetspeedSecurityException
       {
           return getService().getUsers();
  @@ -110,7 +110,7 @@
       /**
        * @see org.apache.jetspeed.services.security.UserManagement#getUsers
        */
  -    Iterator getUsers(String filter)
  +    public static Iterator getUsers(String filter)
           throws JetspeedSecurityException
       {
           return getService().getUsers(filter);
  @@ -119,7 +119,7 @@
       /**
        * @see org.apache.jetspeed.services.security.UserManagement#saveUser
        */
  -    void saveUser(JetspeedUser user)
  +    public static void saveUser(JetspeedUser user)
           throws JetspeedSecurityException
       {
           getService().saveUser(user);
  @@ -128,7 +128,7 @@
       /**
        * @see org.apache.jetspeed.services.security.UserManagement#addUser
        */
  -    void addUser(JetspeedUser user)
  +    public static void addUser(JetspeedUser user)
           throws JetspeedSecurityException
       {
           getService().addUser(user);
  @@ -137,7 +137,7 @@
       /**
        * @see org.apache.jetspeed.services.security.UserManagement#removeUser
        */
  -    void removeUser(Principal principal)
  +    public static void removeUser(Principal principal)
           throws JetspeedSecurityException
       {
           getService().removeUser(principal);
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.3   +36 -20    jakarta-jetspeed/src/java/org/apache/jetspeed/services/security/Attic/TestPortalAuthentication.java
  
  Index: TestPortalAuthentication.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/security/Attic/TestPortalAuthentication.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- TestPortalAuthentication.java	4 Jun 2002 04:52:29 -0000	1.1.2.2
  +++ TestPortalAuthentication.java	5 Jun 2002 01:21:53 -0000	1.1.2.3
  @@ -76,7 +76,7 @@
    * Unit test for PortalAuthentication interface
    * 
    * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
  - * @version $Id: TestPortalAuthentication.java,v 1.1.2.2 2002/06/04 04:52:29 taylor Exp
$
  + * @version $Id: TestPortalAuthentication.java,v 1.1.2.3 2002/06/05 01:21:53 taylor Exp
$
    */
   
   public class TestPortalAuthentication extends TestCase {    
  @@ -103,19 +103,6 @@
       public void setup() 
       {
           System.out.println("Setup: Testing Turbine Authentication");
  -
  -        DBConnection con = null;
  -
  -        try
  -        {
  -            con = Torque.getConnection();
  -        }
  -        catch (Exception e)
  -        {
  -            System.out.println("Error in Setup: failed to start Torque: " + e);
  -        }
  -        System.out.println("Setup Done");
  -         
       }
   
       /**
  @@ -132,7 +119,8 @@
   
   
       /**
  -     * Tests ConfigElement unmarshaling entryset base stuff
  +     * Tests PortalAuthentication.login()
  +     *
        * @throws Exception
        */
   
  @@ -161,21 +149,49 @@
               System.out.println("e = " + e);
               assertTrue(e instanceof FailedLoginException);
           }
  -        boolean loggedOn = true;
           try
           {
               user = service.login("turbine", "turbine");
           }
           catch (Exception e)
           {
  -            e.printStackTrace();
  -            loggedOn = false;
  +            fail(StringUtils.stackTrace(e));
           }
   
  -        assertTrue(loggedOn);
  -        System.out.println("Completed Login Test OK for Turbine");
  +        assertTrue(user.hasLoggedIn() == true);
  +
  +        System.out.println("Completed Login Test OK ");
   
       }
  +
  +    /**
  +     * Tests PortalAuthentication.login()
  +     *
  +     * @throws Exception
  +     */
  +
  +    public void testGetAnonymousUser()
  +    {
  +        setup();
  +
  +        PortalAuthentication service = getService();
  +        JetspeedUser user = null;
  +
  +        try
  +        {
  +            user = service.getAnonymousUser();
  +        }
  +        catch (Exception e)
  +        {
  +            fail(StringUtils.stackTrace(e));
  +        }
  +
  +        assertTrue(user.hasLoggedIn() == false);
  +
  +        System.out.println("Completed Anonymous Test OK");
  +
  +    }
  +
   
     /*
       Configuration object to run Turbine outside a servlet container
  
  
  
  1.1.2.3   +250 -16   jakarta-jetspeed/src/java/org/apache/jetspeed/services/security/Attic/TestUserManagement.java
  
  Index: TestUserManagement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/security/Attic/TestUserManagement.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- TestUserManagement.java	4 Jun 2002 06:34:53 -0000	1.1.2.2
  +++ TestUserManagement.java	5 Jun 2002 01:21:53 -0000	1.1.2.3
  @@ -54,6 +54,8 @@
   
   package org.apache.jetspeed.services.security;
   
  +import java.util.Iterator;
  +
   // Junit imports
   import junit.framework.Test;
   import junit.framework.TestSuite;
  @@ -71,6 +73,7 @@
   
   import org.apache.jetspeed.services.JetspeedUserManagement;
   import org.apache.jetspeed.om.security.JetspeedUser;
  +import org.apache.jetspeed.om.security.JetspeedUserFactory;
   import org.apache.jetspeed.om.security.UserNamePrincipal;
   import org.apache.jetspeed.om.security.UserIdPrincipal;
   
  @@ -78,7 +81,7 @@
    * Unit test for UserManagement interface
    * 
    * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
  - * @version $Id: TestUserManagement.java,v 1.1.2.2 2002/06/04 06:34:53 taylor Exp $
  + * @version $Id: TestUserManagement.java,v 1.1.2.3 2002/06/05 01:21:53 taylor Exp $
    */
   
   public class TestUserManagement extends TestCase {    
  @@ -104,20 +107,7 @@
    
       public void setup() 
       {
  -        System.out.println("Setup: Testing Turbine User Management");
  -
  -        DBConnection con = null;
  -
  -        try
  -        {
  -            con = Torque.getConnection();
  -        }
  -        catch (Exception e)
  -        {
  -            System.out.println("Error in Setup: failed to start Torque: " + e);
  -        }
  -        System.out.println("Setup Done");
  -         
  +        System.out.println("Setup: Testing Turbine User Management");         
       }
   
       /**
  @@ -134,7 +124,7 @@
   
   
       /**
  -     * Tests ConfigElement unmarshaling entryset base stuff
  +     * Tests getUser method
        * @throws Exception
        */
   
  @@ -161,9 +151,253 @@
           assertTrue(user.getEmail().equals("david@bluesunrise.com"));
           assertTrue(user.getConfirmed().equals("CONFIRMED"));
           assertTrue(user.getDisabled() == false);
  +        subtestTemp(user);
  +
  +        try
  +        {
  +            JetspeedUserManagement.getUser(new UserNamePrincipal("nobody"));
  +        }
  +        catch (Exception e)
  +        {
  +            System.out.println("CAUGHT GETUSER EXCEPTION: " + e.toString());
  +            assertTrue(e instanceof UnknownUserException);
  +        }
   
           System.out.println("Completed getUser Test OK for Turbine");
   
  +    }
  +
  +    /**
  +     * Tests getUsers method
  +     * @throws Exception
  +     */
  +
  +    public void testGetUsers() throws Exception 
  +    {
  +        setup();
  +
  +        UserManagement service = getService();
  +        JetspeedUser user = null;
  +
  +        try
  +        {
  +            Iterator users = JetspeedUserManagement.getUsers();
  +            while (users.hasNext())
  +            {
  +                user = (JetspeedUser)users.next();
  +                //System.out.println("user name = " + user.getUserName());
  +                if (user.getUserName().equals("turbine"))
  +                {
  +                    assertTrue(user.getUserId().equals("0"));
  +                    assertTrue(user.getLastName().equals("Turbine"));
  +                }
  +                if (user.getUserName().equals("admin"))
  +                {
  +                    assertTrue(user.getUserId().equals("1"));
  +                    assertTrue(user.getLastName().equals("Admin"));
  +                }
  +            }
  +        }
  +        catch (Exception e)
  +        {
  +            fail(StringUtils.stackTrace(e));
  +        }
  +
  +        System.out.println("Completed getUsers Test OK for Turbine");
  +
  +    }
  +
  +
  +    static final String TEMP_LAST_NAME = "UnitTestLastName";
  +
  +    /**
  +     * Tests saveUser method
  +     * @throws Exception
  +     */
  +
  +    public void testSaveUser() throws Exception 
  +    {
  +        setup();
  +
  +        UserManagement service = getService();
  +        JetspeedUser user = null;
  +
  +        try
  +        {
  +            // test updating and retrieving some fields
  +            user = JetspeedUserManagement.getUser(new UserNamePrincipal("turbine"));
  +            String lastName = user.getLastName();
  +            user.setLastName(TEMP_LAST_NAME);
  +            user.setDisabled(true);
  +
  +            JetspeedUserManagement.saveUser(user);
  +            JetspeedUser user2 = JetspeedUserManagement.getUser(new UserNamePrincipal("turbine"));
  +            assertTrue(user2.getLastName().equals(TEMP_LAST_NAME));
  +            assertTrue(user2.getDisabled() == true);
  +
  +            user2.setLastName(lastName);
  +            user2.setDisabled(false);
  +            JetspeedUserManagement.saveUser(user2);
  +            JetspeedUser user3 = JetspeedUserManagement.getUser(new UserNamePrincipal("turbine"));
  +            assertTrue(user3.getLastName().equals(lastName));
  +            assertTrue(user3.getDisabled() == false);
  +
  +            // test set/get Perm
  +            user3.setPerm("unittest", "test");
  +            JetspeedUserManagement.saveUser(user3);
  +            JetspeedUser user4 = JetspeedUserManagement.getUser(new UserNamePrincipal("turbine"));
  +            String unitTestValue = (String)user4.getPerm("unittest");
  +            assertTrue(unitTestValue.equals("test"));
  +            user4.setPerm("unittest", null);
  +            JetspeedUserManagement.saveUser(user4);            
  +
  +            // try saving to an non-existing user
  +            try
  +            {
  +                JetspeedUser user5 = JetspeedUserFactory.getInstance();
  +                user5.setLastName("junk");
  +                JetspeedUserManagement.saveUser(user5);       
  +            }
  +            catch (Exception e)
  +            {
  +                assertTrue(e instanceof UnknownUserException);
  +            }
  +    
  +            // test dup on unique username key
  +            JetspeedUser user6 = JetspeedUserManagement.getUser(new UserNamePrincipal("turbine"));
  +            user6.setUserName("admin");
  +            try
  +            {
  +                JetspeedUserManagement.saveUser(user6);
  +            }
  +            catch (Exception e)
  +            {
  +                System.out.println("CAUGHT UNIQUE EXCEPTION: " + e.toString());
  +                assertTrue(e instanceof UserException);
  +            }
  +
  +        }
  +        catch (Exception e)
  +        {
  +            fail(StringUtils.stackTrace(e));
  +        }
  +
  +        System.out.println("Completed saveUser Test OK for Turbine");
  +
  +    }
  +
  +    /**
  +     * Tests addUser method
  +     * @throws Exception
  +     */
  +
  +    public void testAddUser() throws Exception 
  +    {
  +        setup();
  +
  +        UserManagement service = getService();
  +
  +        try
  +        {
  +            // test updating and retrieving some fields
  +            JetspeedUser user = JetspeedUserFactory.getInstance();
  +            user.setUserName("busby");
  +            user.setPassword("woof");
  +            user.setLastName("Taylor");
  +            user.setFirstName("Busby");
  +            user.setEmail("busby@bluesunrise.com");
  +            user.setDisabled(false);
  +            user.setConfirmed("NO");
  +            user.setPerm("dogfood", "alpo");
  +            JetspeedUserManagement.addUser(user);
  +
  +            JetspeedUser user2 = JetspeedUserManagement.getUser(new UserNamePrincipal("busby"));
  +            assertTrue(user2.getUserName().equals("busby"));
  +            assertTrue(user2.getLastName().equals("Taylor"));
  +            assertTrue(user2.getFirstName().equals("Busby"));
  +            assertTrue(user2.getEmail().equals("busby@bluesunrise.com"));
  +            assertTrue(user2.getConfirmed().equals("NO"));
  +            String dogfood = (String)user2.getPerm("dogfood");
  +            assertTrue(dogfood.equals("alpo"));
  +            assertTrue(user2.getDisabled() == false);
  +            assertTrue(user2.getUserId().equals(user.getUserId()));
  +
  +            // test adding an existing user
  +            JetspeedUser user3 = JetspeedUserFactory.getInstance();
  +            user3.setUserName("busby");
  +            try
  +            {
  +                JetspeedUserManagement.addUser(user3);
  +            }
  +            catch (Exception e)
  +            {
  +                System.out.println("CAUGHT UNIQUE EXCEPTION: " + e.toString());
  +                assertTrue(e instanceof NotUniqueUserException);
  +            }
  +
  +
  +        }
  +        catch (Exception e)
  +        {
  +            fail(StringUtils.stackTrace(e));
  +        }
  +
  +        System.out.println("Completed saveUser Test OK for Turbine");
  +
  +    }
  +
  +    /**
  +     * Tests removeUser method
  +     * @throws Exception
  +     */
  +
  +    public void testRemoveUser() throws Exception 
  +    {
  +        setup();
  +
  +        UserManagement service = getService();
  +
  +        try
  +        {
  +            // depending on testAddUser running first!
  +            JetspeedUserManagement.removeUser(new UserNamePrincipal("busby"));
  +
  +            try
  +            {
  +                JetspeedUserManagement.removeUser(new UserNamePrincipal("nobody"));
  +            }
  +            catch (Exception e)
  +            {
  +                System.out.println("CAUGHT REMOVE EXCEPTION: " + e.toString());
  +                assertTrue(e instanceof UnknownUserException);
  +            }
  +
  +            try
  +            {
  +                JetspeedUserManagement.getUser(new UserNamePrincipal("busby"));
  +            }
  +            catch (Exception e)
  +            {
  +                System.out.println("CAUGHT REMOVE EXCEPTION: " + e.toString());
  +                assertTrue(e instanceof UnknownUserException);
  +            }
  +
  +
  +        }
  +        catch (Exception e)
  +        {
  +            fail(StringUtils.stackTrace(e));
  +        }
  +
  +        System.out.println("Completed saveUser Test OK for Turbine");
  +
  +    }
  +
  +    private void subtestTemp(JetspeedUser user)
  +    {
  +        user.setTemp("test", new Integer(9));
  +        Integer test = (Integer)user.getTemp("test");
  +        assertTrue(test.intValue() == 9);
       }
   
     /*
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.5   +81 -13    jakarta-jetspeed/src/java/org/apache/jetspeed/services/security/turbine/Attic/TurbineAuthentication.java
  
  Index: TurbineAuthentication.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/security/turbine/Attic/TurbineAuthentication.java,v
  retrieving revision 1.1.2.4
  retrieving revision 1.1.2.5
  diff -u -r1.1.2.4 -r1.1.2.5
  --- TurbineAuthentication.java	4 Jun 2002 04:52:29 -0000	1.1.2.4
  +++ TurbineAuthentication.java	5 Jun 2002 01:21:53 -0000	1.1.2.5
  @@ -76,6 +76,9 @@
   import org.apache.jetspeed.services.security.UnknownUserException;
   import org.apache.jetspeed.services.security.UserException;
   import org.apache.jetspeed.services.security.JetspeedSecurityException;
  +import org.apache.jetspeed.services.rundata.JetspeedRunDataService;
  +import org.apache.jetspeed.services.rundata.JetspeedRunData;
  +import org.apache.turbine.services.rundata.RunDataService;
   
   // Password encryption
   import javax.mail.internet.MimeUtility;
  @@ -93,7 +96,7 @@
    * This service does not use any of the Turbine security or user management classes.
    * 
    * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
  - * @version $Id: TurbineAuthentication.java,v 1.1.2.4 2002/06/04 04:52:29 taylor Exp $
  + * @version $Id: TurbineAuthentication.java,v 1.1.2.5 2002/06/05 01:21:53 taylor Exp $
    */
                                                                
   public class TurbineAuthentication  extends TurbineBaseService
  @@ -115,9 +118,11 @@
       int strikeMax = 20;              // 20 total failures 
       long strikeInterval = 300;  // five minutes
   
  -    boolean securePasswords = false;
  -    String passwordsAlgorithm = "SHA";
  +    static boolean securePasswords = false;
  +    static String passwordsAlgorithm = "SHA";
   
  +    /** The JetspeedRunData Service. */
  +    private JetspeedRunDataService runDataService = null;
   
       /**
        * Given a public credential(username) and private credential(password), 
  @@ -143,8 +148,6 @@
           try
           {
               user = JetspeedUserManagement.getUser(new UserNamePrincipal(username));
  -            // TODO: put user in context
  -
           }                            
           catch (UnknownUserException e)
           {
  @@ -154,16 +157,28 @@
           {
               throw new LoginException(e.toString());
           }
  -        // validated ok, encrypt if necessary
  -        if (securePasswords)
  -        {
  -            password = encryptPassword(password, this.passwordsAlgorithm);
  -        }
  +
  +        password = encryptPassword(password);
  +
           if(!user.getPassword().equals(password))
           {
               throw new FailedLoginException("Credential authentication failure");
           }        
   
  +        // Mark the user as being logged in.
  +        user.setHasLoggedIn(new Boolean(true));
  +
  +        // Set the last_login date in the database.
  +        try
  +        {
  +            user.updateLastLogin();
  +            putUserIntoContext(user);
  +        }
  +        catch (Exception e)
  +        {
  +            throw new LoginException("Failed to update last login ", e);
  +        }
  +
           return user;
           
       }
  @@ -177,7 +192,17 @@
       public JetspeedUser getAnonymousUser()
           throws LoginException
       {
  -        return null;
  +        JetspeedUser user = null;
  +        try
  +        {
  +            user = JetspeedUserFactory.getInstance();
  +        }
  +        catch (UserException e)
  +        {
  +            throw new LoginException("Failed to get user from UserFactory " + e);
  +        }
  +        user.setUserName("");
  +        return user;
       }
   
       /**
  @@ -192,6 +217,14 @@
       public void logout()
           throws LoginException
       {
  +        try
  +        {
  +            putUserIntoContext(getAnonymousUser());
  +        }
  +        catch (Exception e)
  +        {
  +            throw new LoginException("Exception logging user out ", e );
  +        }
       }
   
       /**
  @@ -229,6 +262,11 @@
           passwordsAlgorithm = serviceConf.getString(CONFIG_SECURE_PASSWORDS_ALGORITHM,
                                                     passwordsAlgorithm);
   
  +        this.runDataService =
  +            (JetspeedRunDataService)TurbineServices.getInstance()
  +                .getService(RunDataService.SERVICE_NAME);
  +
  +
           setInit(true);
        }
   
  @@ -275,14 +313,17 @@
        * @param algorithm the encryption algorithm to use.
        * @return processed password
        */
  -    static String encryptPassword(String password, String algorithm)
  +    public static String encryptPassword(String password)
       {
  +        if (!securePasswords)
  +            return password;
  +
           if(password == null)
               return null;
   
           try
           {
  -            MessageDigest md = MessageDigest.getInstance(algorithm);
  +            MessageDigest md = MessageDigest.getInstance(passwordsAlgorithm);
               // We need to use unicode here, to be independent of platform's
               // default encoding. Thanks to SGawin for spotting this.
               byte[] digest = md.digest(password.getBytes("UTF-8"));
  @@ -300,4 +341,31 @@
           }
       }
   
  +    protected JetspeedUser getUserFromContext()
  +    {
  +        if (this.runDataService != null)
  +        {
  +            JetspeedRunData rundata = this.runDataService.getCurrentRunData();
  +            if (rundata != null)
  +            {
  +                JetspeedUser user = (JetspeedUser)rundata.getUser();
  +                System.out.println("@@@@@@@ got user" + user.getName());
  +
  +            }
  +        }
  +        return null;
  +    }
  +
  +    protected void putUserIntoContext(JetspeedUser user)
  +    {
  +        if (this.runDataService != null)
  +        {
  +            JetspeedRunData rundata = this.runDataService.getCurrentRunData();
  +            if (rundata != null)
  +            {
  +                System.out.println("@@@@@@@ putting user" + user.getName());
  +                rundata.setUser(user);
  +            }
  +        }
  +    }
   }
  
  
  
  1.1.2.3   +132 -9    jakarta-jetspeed/src/java/org/apache/jetspeed/services/security/turbine/Attic/TurbineUserManagement.java
  
  Index: TurbineUserManagement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/security/turbine/Attic/TurbineUserManagement.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- TurbineUserManagement.java	4 Jun 2002 02:43:36 -0000	1.1.2.2
  +++ TurbineUserManagement.java	5 Jun 2002 01:21:53 -0000	1.1.2.3
  @@ -61,7 +61,7 @@
   
   // Torque
   import org.apache.torque.util.Criteria;
  -
  +import org.apache.torque.om.NumberKey;
   
   // Turbine 
   import org.apache.turbine.util.Log;
  @@ -76,6 +76,7 @@
   
   // Jetspeed Security
   import org.apache.jetspeed.om.security.JetspeedUser;
  +import org.apache.jetspeed.om.security.BaseJetspeedUser;
   import org.apache.jetspeed.om.security.UserNamePrincipal;
   import org.apache.jetspeed.om.security.UserIdPrincipal;
   
  @@ -84,6 +85,7 @@
   
   import org.apache.jetspeed.services.security.UserException;
   import org.apache.jetspeed.services.security.UnknownUserException;
  +import org.apache.jetspeed.services.security.NotUniqueUserException;
   import org.apache.jetspeed.services.security.JetspeedSecurityException;
   
   
  @@ -92,7 +94,7 @@
    *
    * 
    * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
  - * @version $Id: TurbineUserManagement.java,v 1.1.2.2 2002/06/04 02:43:36 taylor Exp $
  + * @version $Id: TurbineUserManagement.java,v 1.1.2.3 2002/06/05 01:21:53 taylor Exp $
    */
   
   public class TurbineUserManagement extends TurbineBaseService
  @@ -168,7 +170,17 @@
       public Iterator getUsers()
           throws JetspeedSecurityException
       {
  -        return null; // TODO
  +        Criteria criteria = new Criteria();
  +        Vector users;
  +        try
  +        {
  +            users = TurbineUserPeer.doSelectUsers(criteria);
  +        }
  +        catch(Exception e)
  +        {
  +            throw new UserException("Failed to retrieve users ", e);
  +        }        
  +        return users.iterator();
       }
   
       /**
  @@ -184,9 +196,20 @@
       public Iterator getUsers(String filter)
           throws JetspeedSecurityException
       {
  -        return null; // TODO
  -    }
  +        // TODO: implement this with a SQL string
   
  +        Criteria criteria = new Criteria();
  +        Vector users;
  +        try
  +        {
  +            users = TurbineUserPeer.doSelectUsers(criteria);
  +        }
  +        catch(Exception e)
  +        {
  +            throw new UserException("Failed to retrieve users ", e);
  +        }        
  +        return users.iterator();
  +    }
   
       /**
        * Saves a <code>JetspeedUser</code>'s attributes into permanent storage.

  @@ -200,7 +223,21 @@
       public void saveUser(JetspeedUser user)
           throws JetspeedSecurityException
       {
  -        // TODO
  +        if(!accountExists(user, true))
  +        {
  +            throw new UnknownUserException("Cannot save user '" + user.getUserName() +
  +                                           "', User doesn't exist");
  +        }
  +        Criteria criteria = TurbineUserPeer.buildCriteria(user);
  +        try
  +        {
  +            TurbineUserPeer.doUpdate(criteria);
  +        }
  +        catch(Exception e)
  +        {
  +            throw new UserException("Failed to save user object ", e);
  +        }
  +
       }
   
   
  @@ -219,7 +256,29 @@
       public void addUser(JetspeedUser user)
           throws JetspeedSecurityException
       {
  -        // TODO
  +        if(accountExists(user, false))
  +        {
  +            throw new NotUniqueUserException("The account '" + 
  +                user.getUserName() + "' already exists");
  +        }
  +        String initialPassword = user.getPassword();
  +        String encrypted = TurbineAuthentication.encryptPassword(initialPassword);
  +        user.setPassword(encrypted);
  +        Criteria criteria = TurbineUserPeer.buildCriteria(user);
  +        try
  +        {
  +
  +            NumberKey key = (NumberKey)TurbineUserPeer.doInsert(criteria);     
  +
  +            ((BaseJetspeedUser)user).setUserId(key.toString());
  +
  +        }
  +        catch(Exception e)
  +        {
  +            throw new UserException("Failed to create account '" +
  +                user.getUserName() + "'", e);
  +        }
  +
       }
   
   
  @@ -237,7 +296,32 @@
       public void removeUser(Principal principal)
           throws JetspeedSecurityException
       {
  -        // TODO
  +        JetspeedUser user = getUser(principal);
  +
  +        Criteria criteria = new Criteria();
  +        if (principal instanceof UserNamePrincipal)
  +        {
  +            criteria.add(TurbineUserPeer.LOGIN_NAME, principal.getName());
  +        }
  +        else if (principal instanceof UserIdPrincipal)
  +        {
  +            criteria.add(TurbineUserPeer.USER_ID, principal.getName());
  +        }
  +        else
  +        {
  +            throw new UserException("Invalid Principal Type in removeUser: " + principal.getClass().getName());
  +        }
  +
  +        try
  +        {
  +            TurbineUserPeer.doDelete(criteria);
  +        }
  +        catch(Exception e)
  +        {
  +            throw new UserException("Failed to remove account '" +
  +                user.getUserName() + "'", e);
  +        }
  +
       }
   
   
  @@ -263,7 +347,46 @@
           setInit(true);
        }
   
  -
  +    /**
  +     * Check whether a specified user's account exists.
  +     *
  +     * The login name is used for looking up the account.
  +     *
  +     * @param user the user to be checked.
  +     * @param checkUniqueId make sure that we aren't overwriting another user with different
id
  +     * @return true if the specified account exists
  +     * @throws UserException if there was a general db access error 
  +     *         
  +     */
  +    protected boolean accountExists( JetspeedUser user, boolean checkUniqueId )
  +        throws UserException
  +    {
  +        String id = user.getUserId();
  +        Criteria criteria = new Criteria();
  +        criteria.add(TurbineUserPeer.LOGIN_NAME, user.getUserName());
  +        Vector users;
  +        try
  +        {
  +            users = TurbineUserPeer.doSelect(criteria);
  +        }
  +        catch(Exception e)
  +        {
  +            throw new UserException(
  +                "Failed to check account's presence", e);
  +        }
  +        if (users.size() < 1)
  +        {
  +            return false;
  +        }
  +        TurbineUser retrieved = (TurbineUser)users.get(0);
  +        NumberKey key = retrieved.getUserId();
  +        String keyId = key.toString();
  +        if (checkUniqueId && !keyId.equals(id))
  +        {
  +            throw new UserException("User exists but under a different unique ID");
  +        }
  +        return true;
  +    }
   
   }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:jetspeed-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:jetspeed-dev-help@jakarta.apache.org>


Mime
View raw message