Commit 6a68dd5b authored by Jeff Burke's avatar Jeff Burke
Browse files

s1890: updates for numericID as the uid

parent ec5770af
Loading
Loading
Loading
Loading
+23 −17
Original line number Diff line number Diff line
@@ -223,8 +223,11 @@ public class LdapUserDAO extends LdapDAO
    {
        try
        {
            HttpPrincipal httpPrincipal = new HttpPrincipal(username);
            User user = getUser(httpPrincipal);
            long id = user.getID().getUUID().getLeastSignificantBits();
            BindRequest bindRequest = new SimpleBindRequest(
                getUserDN(username, config.getUsersDN()), new String(password));
                getUserDN(String.valueOf(id), config.getUsersDN()), new String(password));

            LDAPConnection conn = this.getUnboundReadConnection();
            BindResult bindResult = conn.bind(bindRequest);
@@ -238,6 +241,10 @@ public class LdapUserDAO extends LdapDAO
                throw new AccessControlException("Invalid username or password");
            }
        }
        catch (UserNotFoundException e)
        {
            throw new AccessControlException("Invalid username");
        }
        catch (LDAPException e)
        {
            logger.debug("doLogin Exception: " + e, e);
@@ -325,8 +332,12 @@ public class LdapUserDAO extends LdapDAO
        try
        {
            String emailAddress = getEmailAddress(userRequest);
            Principal userID = getSupportedPrincipal(userRequest.getUser());
            if (userID instanceof HttpPrincipal)
            {
                getUserByEmailAddress(emailAddress, usersDN);
            }
        }
        catch (UserNotFoundException ok) { }
    }

@@ -375,12 +386,12 @@ public class LdapUserDAO extends LdapDAO
            addAttribute(attributes, LDAP_OBJECT_CLASS, LDAP_INET_USER);
            addAttribute(attributes, LDAP_OBJECT_CLASS, LDAP_CADC_ACCOUNT);
            addAttribute(attributes, LDAP_UID, numericID);
            addAttribute(attributes, LADP_USER_PASSWORD, new String(userRequest.getPassword()));

            if (user.getHttpPrincipal() == null)
            {
                addAttribute(attributes, LDAP_COMMON_NAME, EXTERNAL_USER_CN);
                addAttribute(attributes, LDAP_LAST_NAME, EXTERNAL_USER_SN);
                addAttribute(attributes, LADP_USER_PASSWORD, UUID.randomUUID().toString());
            }
            else
            {
@@ -397,7 +408,6 @@ public class LdapUserDAO extends LdapDAO
                     throw new IllegalArgumentException(error);
                 }
                addAttribute(attributes, LDAP_COMMON_NAME, userID.getName());
                addAttribute(attributes, LADP_USER_PASSWORD, new String(userRequest.getPassword()));
                addAttribute(attributes, LDAP_FIRST_NAME, user.personalDetails.getFirstName());
                addAttribute(attributes, LDAP_LAST_NAME, user.personalDetails.getLastName());
                addAttribute(attributes, LDAP_ADDRESS, user.personalDetails.address);
@@ -856,10 +866,10 @@ public class LdapUserDAO extends LdapDAO
            for (SearchResultEntry next : searchResult.getSearchEntries())
            {
                final String firstName =
                    next.getAttributeValue(LDAP_FIRST_NAME).trim();
                    next.getAttributeValue(LDAP_FIRST_NAME);
                final String lastName =
                    next.getAttributeValue(LDAP_LAST_NAME).trim();
                final String uid = next.getAttributeValue(LDAP_UID).trim();
                final String uid = next.getAttributeValue(LDAP_UID);

                User user = new User();
                user.getIdentities().add(new HttpPrincipal(uid));
@@ -868,7 +878,7 @@ public class LdapUserDAO extends LdapDAO
                if (StringUtil.hasLength(firstName) &&
                    StringUtil.hasLength(lastName))
                {
                    user.personalDetails = new PersonalDetails(firstName, lastName);
                    user.personalDetails = new PersonalDetails(firstName.trim(), lastName.trim());
                }

                users.add(user);
@@ -901,14 +911,11 @@ public class LdapUserDAO extends LdapDAO
        throws UserNotFoundException, TransientException, AccessControlException
    {
        User pendingUser = getPendingUser(userID);

        Set<HttpPrincipal> httpPrincipals = pendingUser.getIdentities(HttpPrincipal.class);
        if (httpPrincipals.isEmpty())
        if (pendingUser.getHttpPrincipal() == null)
        {
            throw new RuntimeException("BUG: missing HttpPrincipal for " + userID.getName());
        }
        HttpPrincipal httpPrincipal = httpPrincipals.iterator().next();
        String uid = "uid=" + httpPrincipal.getName();
        String uid = "uid=" + pendingUser.getID().getUUID().getLeastSignificantBits();
        String dn = uid + "," + config.getUserRequestsDN();

        try
@@ -1112,10 +1119,11 @@ public class LdapUserDAO extends LdapDAO
    private void deleteUser(final Principal userID, final String usersDN, boolean markDelete)
        throws UserNotFoundException, AccessControlException, TransientException
    {
        getUser(userID, usersDN);
        User user2Delete = getUser(userID, usersDN);
        try
        {
            DN userDN = getUserDN(userID.getName(), usersDN);
            long id = user2Delete.getID().getUUID().getLeastSignificantBits();
            DN userDN = getUserDN(String.valueOf(id), usersDN);
            if (markDelete)
            {
                List<Modification> modifs = new ArrayList<Modification>();
@@ -1132,11 +1140,9 @@ public class LdapUserDAO extends LdapDAO
            else // real delete
            {
                DeleteRequest delRequest = new DeleteRequest(userDN);
                //delRequest.addControl(
                //    new ProxiedAuthorizationV2RequestControl(
                //        "dn:" + getSubjectDN().toNormalizedString()));

                LDAPResult result = getReadWriteConnection().delete(delRequest);
                logger.info("delete result:" + delRequest);
                LdapDAO.checkLdapResult(result.getResultCode());
            }
        }
+24 −0
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@
package ca.nrc.cadc.ac.server.web.groups;

import java.io.IOException;
import java.lang.reflect.Field;
import java.security.AccessControlException;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
@@ -232,4 +233,27 @@ public abstract class AbstractGroupAction implements PrivilegedExceptionAction<O
        this.logInfo.deletedMembers = deletedMembers;
    }

    // set private field using reflection
    protected void setField(Object object, Object value, String name)
    {
        try
        {
            Field field = object.getClass().getDeclaredField(name);
            field.setAccessible(true);
            field.set(object, value);
        }
        catch (NoSuchFieldException e)
        {
            final String error = object.getClass().getSimpleName() +
                " field " + name + "not found";
            throw new RuntimeException(error, e);
        }
        catch (IllegalAccessException e)
        {
            final String error = "unable to update " + name + " in " +
                object.getClass().getSimpleName();
            throw new RuntimeException(error, e);
        }
    }

}
+1 −7
Original line number Diff line number Diff line
@@ -102,13 +102,7 @@ public class RemoveUserMemberAction extends AbstractGroupAction
        Group group = groupPersistence.getGroup(this.groupName);

        Principal userPrincipal = AuthenticationUtil.createPrincipal(this.userID, this.userIDType);
        User user = getUserPersistence().getAugmentedUser(userPrincipal);
        Set<X500Principal> x500Principals = user.getIdentities(X500Principal.class);
        X500Principal x500Principal = x500Principals.iterator().next();
        User toRemove = new User();
        toRemove.getIdentities().add(x500Principal);

        // User members is a Set of User<X500Principal>
        User toRemove = getUserPersistence().getUser(userPrincipal);
        if (!group.getUserMembers().remove(toRemove))
        {
            throw new MemberNotFoundException();
+56 −55
Original line number Diff line number Diff line
@@ -458,6 +458,7 @@ public class LdapUserDAOTest extends AbstractLdapDAOTest

        final User pendingUser = new User();
        pendingUser.personalDetails = new PersonalDetails("CADCtest", "Request");
        pendingUser.personalDetails.email = username + "@canada.ca";
        pendingUser.getIdentities().add(httpPrincipal);
        pendingUser.getIdentities().add(x500Principal);

@@ -566,12 +567,13 @@ public class LdapUserDAOTest extends AbstractLdapDAOTest
        testUser.getIdentities().add(httpPrincipal);

        testUser.personalDetails = new PersonalDetails("firstName", "lastName");
        testUser.personalDetails.email = username + "@canada.ca";
        final UserRequest userRequest = new UserRequest(testUser, password);

        // add the user
        Subject subject = new Subject();
        subject.getPrincipals().add(httpPrincipal);
        subject.getPrincipals().add(cadcDaoTest2_DNPrincipal);
//        subject.getPrincipals().add(cadcDaoTest2_DNPrincipal);
        final User newUser = (User) Subject.doAs(subject, new PrivilegedExceptionAction<Object>()
        {
            public User run()
@@ -592,7 +594,6 @@ public class LdapUserDAOTest extends AbstractLdapDAOTest
        });

        // update the user
        newUser.personalDetails.email = "email2";
        newUser.personalDetails.address = "address2";
        newUser.personalDetails.institute = "institute2";
        newUser.personalDetails.city = "city2";
@@ -603,7 +604,7 @@ public class LdapUserDAOTest extends AbstractLdapDAOTest

        // update the userexpected
        subject.getPrincipals().add(httpPrincipal);
        subject.getPrincipals().add(cadcDaoTest2_DNPrincipal);
//        subject.getPrincipals().add(cadcDaoTest2_DNPrincipal);
        User updatedUser = (User) Subject.doAs(subject, new PrivilegedExceptionAction<Object>()
            {
                public Object run()
@@ -623,7 +624,7 @@ public class LdapUserDAOTest extends AbstractLdapDAOTest
                }
            });
        assertNotNull(updatedUser);
        check(testUser, updatedUser);
        check(newUser, updatedUser);
    }

    // TODO testUpdateUser for a user that doesn't exist
@@ -637,11 +638,11 @@ public class LdapUserDAOTest extends AbstractLdapDAOTest
        String userID = createUsername();

        HttpPrincipal httpPrincipal = new HttpPrincipal(userID);
//        X500Principal x500Principal = new X500Principal("cn=" + userID + ",ou=cadc,o=hia,c=ca");
        X500Principal x500Principal = new X500Principal("cn=" + userID + ",ou=cadc,o=hia,c=ca");

        final User expected = new User();
        expected.getIdentities().add(httpPrincipal);
//        expected.getIdentities().add(x500Principal);
        expected.getIdentities().add(x500Principal);
        expected.personalDetails = new PersonalDetails("foo", "bar");

        final UserRequest userRequest = new UserRequest(expected, "123456".toCharArray());
@@ -682,11 +683,11 @@ public class LdapUserDAOTest extends AbstractLdapDAOTest
        String userID = createUsername();

        HttpPrincipal httpPrincipal = new HttpPrincipal(userID);
//        X500Principal x500Principal = new X500Principal("cn=" + userID + ",ou=cadc,o=hia,c=ca");
        X500Principal x500Principal = new X500Principal("cn=" + userID + ",ou=cadc,o=hia,c=ca");

        final User expected = new User();
        expected.getIdentities().add(httpPrincipal);
//        expected.getIdentities().add(x500Principal);
        expected.getIdentities().add(x500Principal);
        expected.personalDetails = new PersonalDetails("foo", "bar");
        expected.personalDetails.email = userID + "@canada.ca";

@@ -723,53 +724,53 @@ public class LdapUserDAOTest extends AbstractLdapDAOTest
     * Test of getMember.
     */
//    @Test
    public void testGetX500User() throws Exception
    {
        Subject subject = new Subject();
        subject.getPrincipals().add(cadcDaoTest1_X500Principal);
        subject.getPrincipals().add(cadcDaoTest1_DNPrincipal);

        // do everything as owner
        Subject.doAs(subject, new PrivilegedExceptionAction<Object>()
        {
            public Object run() throws Exception
            {
                try
                {
                    User actual = getUserDAO().getX500User(new DN(cadcDaoTest1_DN));
                    check(testMember, actual);
                    return null;
                }
                catch (Exception e)
                {
                    throw new Exception("Problems", e);
                }
            }
        });

        // should also work as a different user
        subject = new Subject();
        subject.getPrincipals().add(new HttpPrincipal("CadcDaoTest2"));

        // do everything as owner
        Subject.doAs(subject, new PrivilegedExceptionAction<Object>()
        {
            public Object run()
                throws Exception
            {
                try
                {
                    User actual = getUserDAO().getX500User(new DN(cadcDaoTest1_DN));
                    check(testMember, actual);
                    return null;
                }
                catch (Exception e)
                {
                    throw new Exception("Problems", e);
                }
            }
        });
    }
//    public void testGetX500User() throws Exception
//    {
//        Subject subject = new Subject();
//        subject.getPrincipals().add(cadcDaoTest1_X500Principal);
//        subject.getPrincipals().add(cadcDaoTest1_DNPrincipal);
//
//        // do everything as owner
//        Subject.doAs(subject, new PrivilegedExceptionAction<Object>()
//        {
//            public Object run() throws Exception
//            {
//                try
//                {
//                    User actual = getUserDAO().getX500User(new DN(cadcDaoTest1_DN));
//                    check(testMember, actual);
//                    return null;
//                }
//                catch (Exception e)
//                {
//                    throw new Exception("Problems", e);
//                }
//            }
//        });
//
//        // should also work as a different user
//        subject = new Subject();
//        subject.getPrincipals().add(new HttpPrincipal("CadcDaoTest2"));
//
//        // do everything as owner
//        Subject.doAs(subject, new PrivilegedExceptionAction<Object>()
//        {
//            public Object run()
//                throws Exception
//            {
//                try
//                {
//                    User actual = getUserDAO().getX500User(new DN(cadcDaoTest1_DN));
//                    check(testMember, actual);
//                    return null;
//                }
//                catch (Exception e)
//                {
//                    throw new Exception("Problems", e);
//                }
//            }
//        });
//    }

    @Test
    public void testGetUsers() throws Exception
+4 −2
Original line number Diff line number Diff line
@@ -126,7 +126,7 @@ public class RemoveUserMemberActionTest
            EasyMock.expect(mockGroupPersistence.getGroup("group")).andReturn(group);

            final UserPersistence mockUserPersistence = EasyMock.createMock(UserPersistence.class);
            EasyMock.expect(mockUserPersistence.getAugmentedUser(userPrincipal)).andReturn(user);
            EasyMock.expect(mockUserPersistence.getUser(userPrincipal)).andReturn(user);

            EasyMock.replay(mockGroupPersistence, mockUserPersistence);

@@ -176,7 +176,7 @@ public class RemoveUserMemberActionTest
            EasyMock.expectLastCall();

            final UserPersistence mockUserPersistence = EasyMock.createMock(UserPersistence.class);
            EasyMock.expect(mockUserPersistence.getAugmentedUser(userPrincipal)).andReturn(user);
            EasyMock.expect(mockUserPersistence.getUser(userPrincipal)).andReturn(user);

            EasyMock.replay(mockGroupPersistence, mockUserPersistence);

@@ -193,6 +193,8 @@ public class RemoveUserMemberActionTest
            GroupLogInfo logInfo = createMock(GroupLogInfo.class);
            action.setLogInfo(logInfo);
            action.doAction();

            EasyMock.verify(mockGroupPersistence, mockUserPersistence);
        }
        catch (Throwable t)
        {
Loading