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;
}
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());
}
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));
}
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()));
}
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());
}
Aggregations