}
public String authenticate(String authorizedName, String cred)
throws AuthenticationException, FatalErrorException {
if (authorizedName == null || "".equals(authorizedName)) {
throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));
}
boolean isLdapUser = false;
try {
env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.PROVIDER_URL, url); // organization ldap url, example ldap://localhost:389
env.put(Context.SECURITY_PRINCIPAL, authorizedName);
env.put(Context.SECURITY_CREDENTIALS, cred);
ctx = new InitialLdapContext(env, null);
isLdapUser = true;
logger.info(authorizedName + " is authenticated");
} catch (NamingException e) {
logger.error(authorizedName + " is not authenticated");
throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));
} finally {
try {
ctx.close();
} catch (NamingException e) {
logger.error("Context close failure " + e);
}
}
if (isLdapUser) {
EntityManager em = PersistenceManager.getEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Publisher publisher = em.find(Publisher.class, authorizedName);
if (publisher == null) {
logger.warn("Publisher was not found, adding the publisher in on the fly.");
publisher = new Publisher();
publisher.setAuthorizedName(authorizedName);
publisher.setIsAdmin("false");
publisher.setIsEnabled("true");
publisher.setMaxBindingsPerService(199);
publisher.setMaxBusinesses(100);
publisher.setMaxServicesPerBusiness(100);
publisher.setMaxTmodels(100);
publisher.setPublisherName("Unknown");
em.persist(publisher);
tx.commit();
}
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} else {
throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));
}
return authorizedName;
}