use of org.apache.directory.api.ldap.model.name.Dn in project syncope by apache.
the class LdifInputStreamLoader method execute.
/**
* Opens the LDIF file and loads the entries into the context.
*
* @return The count of entries created.
*/
public int execute() {
try {
try {
for (LdifEntry ldifEntry : new LdifReader(ldif)) {
Dn dn = ldifEntry.getDn();
if (ldifEntry.isEntry()) {
Entry entry = ldifEntry.getEntry();
try {
coreSession.lookup(dn);
LOG.debug("Found {}, will not create.", dn);
} catch (Exception e) {
try {
coreSession.add(new DefaultEntry(coreSession.getDirectoryService().getSchemaManager(), entry));
count++;
LOG.debug("Created {}.", dn);
} catch (LdapException e1) {
LOG.error("Could not create entry " + entry, e1);
}
}
} else {
// modify
List<Modification> items = ldifEntry.getModifications();
try {
coreSession.modify(dn, items);
LOG.debug("Modified: " + dn + " with modificationItems: " + items);
} catch (LdapException e) {
LOG.debug("Could not modify: " + dn + " with modificationItems: " + items, e);
}
}
}
} finally {
ldif.close();
}
} catch (Exception ioe) {
LOG.error(I18n.err(I18n.ERR_174), ioe);
}
return count;
}
use of org.apache.directory.api.ldap.model.name.Dn in project openmeetings by apache.
the class LdapLoginManager method login.
/**
* Ldap Login
*
* Connection Data is retrieved from ConfigurationFile
*
* @param _login - user login
* @param passwd - user password
* @param domainId - user domain id
* @return - {@link User} with this credentials or <code>null</code>
* @throws OmException - in case of any error
*/
public User login(String _login, String passwd, Long domainId) throws OmException {
log.debug("LdapLoginmanager.doLdapLogin");
if (!userDao.validLogin(_login)) {
log.error("Invalid login provided");
return null;
}
User u = null;
try (LdapWorker w = new LdapWorker(domainId)) {
String login = w.options.useLowerCase ? _login.toLowerCase() : _login;
boolean authenticated = true;
Dn userDn = null;
Entry entry = null;
switch(w.options.type) {
case SEARCHANDBIND:
{
bindAdmin(w.conn, w.options);
Dn baseDn = new Dn(w.options.searchBase);
String searchQ = String.format(w.options.searchQuery, login);
try (EntryCursor cursor = new EntryCursorImpl(w.conn.search(new SearchRequestImpl().setBase(baseDn).setFilter(searchQ).setScope(w.options.scope).addAttributes("*").setDerefAliases(w.options.derefMode)))) {
while (cursor.next()) {
try {
Entry e = cursor.get();
if (userDn != null) {
log.error("more than 1 user found in LDAP");
throw UNKNOWN;
}
userDn = e.getDn();
if (w.options.useAdminForAttrs) {
entry = e;
}
} catch (CursorLdapReferralException cle) {
log.warn("Referral LDAP entry found, ignore it");
}
}
}
if (userDn == null) {
log.error("NONE users found in LDAP");
throw BAD_CREDENTIALS;
}
w.conn.bind(userDn, passwd);
}
break;
case SIMPLEBIND:
userDn = new Dn(String.format(w.options.userDn, login));
w.conn.bind(userDn, passwd);
break;
case NONE:
default:
authenticated = false;
break;
}
u = authenticated ? userDao.getByLogin(login, Type.ldap, domainId) : userDao.login(login, passwd);
log.debug("getByLogin:: authenticated ? {}, login = '{}', domain = {}, user = {}", authenticated, login, domainId, u);
if (u == null && Provisionning.AUTOCREATE != w.options.prov) {
log.error("User not found in OM DB and Provisionning.AUTOCREATE was not set");
throw BAD_CREDENTIALS;
}
if (authenticated && entry == null) {
if (w.options.useAdminForAttrs) {
bindAdmin(w.conn, w.options);
}
entry = w.conn.lookup(userDn);
}
switch(w.options.prov) {
case AUTOUPDATE:
case AUTOCREATE:
u = w.getUser(entry, u);
if (w.options.syncPasswd) {
u.updatePassword(cfgDao, passwd);
}
u = userDao.update(u, null);
break;
case NONE:
default:
break;
}
} catch (LdapAuthenticationException ae) {
log.error("Not authenticated.", ae);
throw BAD_CREDENTIALS;
} catch (OmException e) {
throw e;
} catch (Exception e) {
log.error("Unexpected exception.", e);
throw new OmException(e);
}
return u;
}
use of org.apache.directory.api.ldap.model.name.Dn in project directory-ldap-api by apache.
the class UserClass_NameTest method initNames.
/**
* Initialize name instances
*/
@Before
public void initNames() throws LdapInvalidDnException {
Set<String> dnSetA = new HashSet<>();
dnSetA.add(new Dn("a=aa").getNormName());
dnSetA.add(new Dn("b=bb").getNormName());
Set<String> dnSetB = new HashSet<>();
dnSetB.add(new Dn("b=bb").getNormName());
dnSetB.add(new Dn("a=aa").getNormName());
Set<String> dnSetC = new HashSet<>();
dnSetC.add(new Dn("a=aa").getNormName());
dnSetC.add(new Dn("b=bb").getNormName());
Set<String> dnSetD = new HashSet<>();
dnSetD.add(new Dn("b=bb").getNormName());
dnSetD.add(new Dn("c=cc").getNormName());
nameA = new Name(dnSetA);
nameACopy = new Name(dnSetB);
nameB = new Name(dnSetC);
nameC = new Name(dnSetD);
}
use of org.apache.directory.api.ldap.model.name.Dn in project directory-ldap-api by apache.
the class UserClass_SubtreeTest method initNames.
/**
* Initialize name instances
*/
@Before
public void initNames() throws Exception {
SubtreeSpecification subtreeSpecA = new BaseSubtreeSpecification();
SubtreeSpecification subtreeSpecB = new BaseSubtreeSpecification();
SubtreeSpecification subtreeSpecC = new BaseSubtreeSpecification();
SubtreeSpecification subtreeSpecD = new BaseSubtreeSpecification(new Dn("cn=dummy"));
Set<SubtreeSpecification> colA = new HashSet<SubtreeSpecification>();
colA.add(subtreeSpecA);
colA.add(subtreeSpecB);
colA.add(subtreeSpecC);
Set<SubtreeSpecification> colB = new HashSet<SubtreeSpecification>();
colB.add(subtreeSpecA);
colB.add(subtreeSpecB);
colB.add(subtreeSpecC);
Set<SubtreeSpecification> colC = new HashSet<SubtreeSpecification>();
colC.add(subtreeSpecB);
colC.add(subtreeSpecC);
colC.add(subtreeSpecD);
subtreeA = new Subtree(colA);
subtreeACopy = new Subtree(colA);
subtreeB = new Subtree(colB);
subtreeC = new Subtree(colC);
}
use of org.apache.directory.api.ldap.model.name.Dn in project directory-ldap-api by apache.
the class EntryChangeControlTest method testEncodeEntryChangeControl.
/**
* Test encoding of a EntryChangeControl.
*/
@Test
public void testEncodeEntryChangeControl() throws Exception {
ByteBuffer bb = ByteBuffer.allocate(0x0D);
bb.put(new byte[] { // EntryChangeNotification ::= SEQUENCE {
0x30, // EntryChangeNotification ::= SEQUENCE {
0x0B, 0x0A, 0x01, // changeType ENUMERATED {
0x08, // }
0x04, 0x03, 'a', '=', // previousDN LDAPDN OPTIONAL, -- modifyDN ops. only
'b', 0x02, 0x01, // changeNumber INTEGER OPTIONAL -- if supported
0x10 });
String expected = Strings.dumpBytes(bb.array());
bb.flip();
EntryChangeDecorator decorator = new EntryChangeDecorator(codec);
EntryChange entryChange = (EntryChange) decorator.getDecorated();
entryChange.setChangeType(ChangeType.MODDN);
entryChange.setChangeNumber(16);
entryChange.setPreviousDn(new Dn("a=b"));
bb = decorator.encode(ByteBuffer.allocate(decorator.computeLength()));
String decoded = Strings.dumpBytes(bb.array());
assertEquals(expected, decoded);
}
Aggregations