Search in sources :

Example 61 with Attributes

use of javax.naming.directory.Attributes in project teiid by teiid.

the class LDAPQueryExecution method getRow.

/**
 * Create a row using the searchResult and add it to the supplied batch.
 * @param batch the supplied batch
 * @param result the search result
 * @throws InvalidNameException
 */
// GHH 20080326 - added fetching of DN of result, for directories that
// do not include it as an attribute
private List<?> getRow(SearchResult result) throws TranslatorException, InvalidNameException {
    Attributes attrs = result.getAttributes();
    ArrayList<Column> attributeList = searchDetails.getElementList();
    final List<Object> row = new ArrayList<Object>(attributeList.size());
    for (int i = 0; i < attributeList.size(); i++) {
        Column col = attributeList.get(i);
        // GHH 20080326 - added resultDN parameter to call
        Object val = getValue(col, result, attrs, i == unwrapPos);
        row.add(val);
    }
    if (unwrapPos > -1) {
        Object toUnwrap = row.get(unwrapPos);
        if (toUnwrap == null) {
            // missing value
            return row;
        }
        if (toUnwrap instanceof ArrayImpl) {
            final Object[] val = ((ArrayImpl) toUnwrap).getValues();
            if (val.length == 0) {
                // empty value
                row.set(unwrapPos, null);
            } else {
                unwrapIterator = new Iterator<List<Object>>() {

                    int i = 0;

                    @Override
                    public boolean hasNext() {
                        return i < val.length;
                    }

                    @Override
                    public List<Object> next() {
                        List<Object> newRow = new ArrayList<Object>(row);
                        newRow.set(unwrapPos, val[i++]);
                        return newRow;
                    }

                    @Override
                    public void remove() {
                    }
                };
                if (unwrapIterator.hasNext()) {
                    return unwrapIterator.next();
                }
            }
        }
    }
    return row;
}
Also used : ArrayImpl(org.teiid.core.types.ArrayImpl) Attributes(javax.naming.directory.Attributes) ArrayList(java.util.ArrayList) Column(org.teiid.metadata.Column) ArrayList(java.util.ArrayList) List(java.util.List)

Example 62 with Attributes

use of javax.naming.directory.Attributes in project OpenOLAT by OpenOLAT.

the class LDAPLoginTest method testCheckUser.

// need to sync the user
@Test
// need to sync the user
@Ignore
public void testCheckUser() {
    Assume.assumeTrue(ldapLoginModule.isLDAPEnabled());
    LDAPError errors = new LDAPError();
    // should create error entry
    String uid = "Administrator";
    Attributes attrs = ldapManager.bindUser(uid, "olat", errors);
    Identity identity = ldapManager.findIdentityByLdapAuthentication(attrs, errors);
    Assert.assertEquals("findIdentyByLdapAuthentication: attrs::null", errors.get());
    // should return identity, since is existing in OLAT and Managed by LDAP
    uid = "mrohrer";
    attrs = ldapManager.bindUser(uid, "olat", errors);
    identity = ldapManager.findIdentityByLdapAuthentication(attrs, errors);
    Assert.assertEquals(uid, identity.getName());
    Assert.assertTrue(errors.isEmpty());
}
Also used : Attributes(javax.naming.directory.Attributes) Identity(org.olat.core.id.Identity) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 63 with Attributes

use of javax.naming.directory.Attributes in project OpenOLAT by OpenOLAT.

the class LDAPLoginTest method testCreateUser.

@Test
@Ignore
public void testCreateUser() {
    Assume.assumeTrue(ldapLoginModule.isLDAPEnabled());
    String uid = "mrohrer";
    String userPW = "olat";
    LDAPError errors = new LDAPError();
    boolean usersSyncedAtStartup = ldapLoginModule.isLdapSyncOnStartup();
    // user should not exits in OLAT when not synced during startup
    assertEquals(usersSyncedAtStartup, (securityManager.findIdentityByName(uid) != null));
    // bind user
    Attributes attrs = ldapManager.bindUser(uid, userPW, errors);
    assertEquals(usersSyncedAtStartup, (securityManager.findIdentityByName(uid) != null));
    // user should be created
    ldapManager.createAndPersistUser(attrs);
    assertEquals(true, (securityManager.findIdentityByName(uid) != null));
    // should fail, user is existing
    ldapManager.createAndPersistUser(attrs);
    assertEquals(true, (securityManager.findIdentityByName(uid) != null));
}
Also used : Attributes(javax.naming.directory.Attributes) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 64 with Attributes

use of javax.naming.directory.Attributes in project OpenOLAT by OpenOLAT.

the class LDAPLoginTest method testCronSync.

@Test
@Ignore
public void testCronSync() throws Exception {
    Assume.assumeTrue(ldapLoginModule.isLDAPEnabled());
    LdapContext ctx;
    List<LDAPUser> ldapUserList;
    List<Attributes> newLdapUserList;
    Map<Identity, Map<String, String>> changedMapIdenityMap;
    List<Identity> deletedUserList;
    LDAPError errors = new LDAPError();
    // find user changed after 2010,01,09,00,00
    ctx = ldapManager.bindSystem();
    Calendar cal = Calendar.getInstance();
    cal.set(2010, 0, 10, 0, 0, 0);
    Date syncDate = cal.getTime();
    ldapUserList = ldapDao.getUserAttributesModifiedSince(syncDate, ctx);
    assertEquals(1, ldapUserList.size());
    // find all users
    syncDate = null;
    ldapUserList = ldapDao.getUserAttributesModifiedSince(syncDate, ctx);
    assertEquals(6, ldapUserList.size());
    // prepare create- and sync-Lists for each user from defined syncTime
    Identity idenity;
    Map<String, String> changedAttrMap;
    newLdapUserList = new LinkedList<Attributes>();
    changedMapIdenityMap = new HashMap<Identity, Map<String, String>>();
    for (int i = 0; i < ldapUserList.size(); i++) {
        Attributes userAttrs = ldapUserList.get(i).getAttributes();
        String user = getAttributeValue(userAttrs.get(syncConfiguration.getOlatPropertyToLdapAttribute("userID")));
        idenity = ldapManager.findIdentityByLdapAuthentication(userAttrs, errors);
        if (idenity != null) {
            changedAttrMap = ldapManager.prepareUserPropertyForSync(userAttrs, idenity);
            if (changedAttrMap != null)
                changedMapIdenityMap.put(idenity, changedAttrMap);
        } else {
            if (errors.isEmpty()) {
                String[] reqAttrs = syncConfiguration.checkRequestAttributes(userAttrs);
                if (reqAttrs == null)
                    newLdapUserList.add(userAttrs);
                else
                    System.out.println("Cannot create User " + user + " required Attributes are missing");
            } else
                System.out.println(errors.get());
        }
    }
    // create Users in LDAP Group only existing in OLAT
    User user1 = UserManager.getInstance().createUser("hansi", "hürlima", "hansi@hansli.com");
    Identity identity1 = securityManager.createAndPersistIdentityAndUser("hansi", null, user1, "LDAP", "hansi");
    SecurityGroup secGroup1 = securityManager.findSecurityGroupByName(LDAPConstants.SECURITY_GROUP_LDAP);
    securityManager.addIdentityToSecurityGroup(identity1, secGroup1);
    user1 = UserManager.getInstance().createUser("chaspi", "meier", "chaspi@hansli.com");
    identity1 = securityManager.createAndPersistIdentityAndUser("chaspi", null, user1, "LDAP", "chaspi");
    securityManager.addIdentityToSecurityGroup(identity1, secGroup1);
    // create User to Delete List
    deletedUserList = ldapManager.getIdentitysDeletedInLdap(ctx);
    assertEquals(4, (deletedUserList.size()));
    // sync users
    Iterator<Identity> itrIdent = changedMapIdenityMap.keySet().iterator();
    while (itrIdent.hasNext()) {
        Identity ident = itrIdent.next();
        ldapManager.syncUser(changedMapIdenityMap.get(ident), ident);
    }
    // create all users
    for (int i = 0; i < newLdapUserList.size(); i++) {
        ldapManager.createAndPersistUser(newLdapUserList.get(i));
    }
    // delete all users
    ldapManager.deletIdentities(deletedUserList);
    // check if users are deleted
    deletedUserList = ldapManager.getIdentitysDeletedInLdap(ctx);
    assertEquals(0, (deletedUserList.size()));
}
Also used : LDAPUser(org.olat.ldap.model.LDAPUser) User(org.olat.core.id.User) Calendar(java.util.Calendar) Attributes(javax.naming.directory.Attributes) LDAPUser(org.olat.ldap.model.LDAPUser) SecurityGroup(org.olat.basesecurity.SecurityGroup) Date(java.util.Date) Identity(org.olat.core.id.Identity) HashMap(java.util.HashMap) Map(java.util.Map) LdapContext(javax.naming.ldap.LdapContext) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 65 with Attributes

use of javax.naming.directory.Attributes in project OpenOLAT by OpenOLAT.

the class LDAPLoginTest method testUserBind.

@Test
public void testUserBind() throws NamingException {
    Assume.assumeTrue(ldapLoginModule.isLDAPEnabled());
    LDAPError errors = new LDAPError();
    String uid = "mrohrer";
    String userPW = "olat";
    // normal bind, should work
    Attributes attrs = ldapManager.bindUser(uid, userPW, errors);
    Assert.assertNotNull(attrs);
    Assert.assertEquals("Rohrer", attrs.get("sn").get());
    // wrong password, should fail
    userPW = "haha";
    attrs = ldapManager.bindUser(uid, userPW, errors);
    Assert.assertNull(attrs);
    Assert.assertEquals("Username or password incorrect", errors.get());
    // wrong username, should fail
    uid = "ruedisueli";
    userPW = "olat";
    attrs = ldapManager.bindUser(uid, userPW, errors);
    Assert.assertNull(attrs);
    Assert.assertEquals("Username or password incorrect", errors.get());
    // no password, should fail
    uid = "mrohrer";
    userPW = null;
    attrs = ldapManager.bindUser(uid, userPW, errors);
    Assert.assertNull(attrs);
    Assert.assertEquals("Username and password must be selected", errors.get());
}
Also used : Attributes(javax.naming.directory.Attributes) Test(org.junit.Test)

Aggregations

Attributes (javax.naming.directory.Attributes)252 Attribute (javax.naming.directory.Attribute)135 SearchResult (javax.naming.directory.SearchResult)87 NamingException (javax.naming.NamingException)84 BasicAttributes (javax.naming.directory.BasicAttributes)72 ArrayList (java.util.ArrayList)61 BasicAttribute (javax.naming.directory.BasicAttribute)56 SearchControls (javax.naming.directory.SearchControls)55 DirContext (javax.naming.directory.DirContext)49 NamingEnumeration (javax.naming.NamingEnumeration)44 Test (org.junit.Test)34 InitialDirContext (javax.naming.directory.InitialDirContext)32 LdapContext (javax.naming.ldap.LdapContext)29 HashMap (java.util.HashMap)25 InitialLdapContext (javax.naming.ldap.InitialLdapContext)24 Hashtable (java.util.Hashtable)20 HashSet (java.util.HashSet)18 Map (java.util.Map)17 IOException (java.io.IOException)16 Identity (org.olat.core.id.Identity)16