fixed bug when user was not existing in LDAP it was not exported/updated

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/social-mail-servlet@117389 82a268e6-3cf1-43bd-a215-b396298e98cf
Feature/21689
Massimiliano Assante 9 years ago
parent d027c49643
commit eb38012be0

@ -82,14 +82,15 @@ public class PortalSchedulerService extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ScheduledExecutorService pop3Scheduler = Executors.newScheduledThreadPool(1);
pop3Scheduler.scheduleAtFixedRate(new PeriodicTask(store, portalName, host, user, password), 0, POP3_MINUTES_DELAY, TimeUnit.MINUTES);
String toReturn = "<DIV>Check Notification Email Started ... </DIV>";
ScheduledExecutorService ldapScheduler = Executors.newScheduledThreadPool(1);
ldapScheduler.scheduleAtFixedRate(new LDAPSync(), 0, LDAP_MINUTES_DELAY, TimeUnit.MINUTES);
toReturn += "<DIV>LDAPSync SCRIPT Started ... </DIV>";
ScheduledExecutorService pop3Scheduler = Executors.newScheduledThreadPool(1);
pop3Scheduler.scheduleAtFixedRate(new PeriodicTask(store, portalName, host, user, password), 0, POP3_MINUTES_DELAY, TimeUnit.MINUTES);
String toReturn = "<DIV>Check Notification Email Started ... </DIV>";
toReturn += "<DIV>LDAPSync SCRIPT Started ... </DIV>";
response.setContentType("text/html");
response.getWriter().write(toReturn);

@ -2,7 +2,6 @@ package org.gcube.portal.ldapexport;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
@ -31,15 +30,18 @@ import com.liferay.portal.service.UserLocalServiceUtil;
public class LDAPSync implements Runnable {
private static final Logger _log = LoggerFactory.getLogger(LDAPSync.class);
private ResourceBundle rb;
private static final String ldapUrl = "ldap://ldap-liferay.d4science.org";
private static final String ldapPrincipal = "cn=admin,dc=d4science,dc=org";
private static final String ldapPwd = "poiwefhaewfkhj";
private static final String filter = "(objectClass=inetOrgPerson)";
public LDAPSync() {
super();
_log.debug("LDAPSync()");
}
@Override
public void run() {
ResourceBundle rb = ResourceBundle.getBundle("org.gcube.portal.settings");
_log.debug("Reading Portal Users ...");
List<User> users = null;
try {
@ -50,10 +52,7 @@ public class LDAPSync implements Runnable {
}
_log.debug("Initializing LDAP exporter ...");
String ldapUrl = rb.getString("LDAP_PROVIDER_URL");
String ldapPrincipal = rb.getString("LDAP_SECURITY_PRINCIPAL");
String ldapPwd = rb.getString("LDAP_SECURITY_CREDENTIALS");
String filter = rb.getString("LDAP_FILTER");
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
@ -66,9 +65,9 @@ public class LDAPSync implements Runnable {
_log.debug("Initiating LDAP Sync ...");
for (User user : users) {
updateUserInLDAP(user.getScreenName(), user.getFirstName(), user.getLastName(), user.getEmailAddress(), "{SHA}"+user.getPassword(), ctx, filter);
_log.debug("Updated " + user.getScreenName());
//_log.debug("Updated " + user.getScreenName());
}
_log.debug("LDAP Sync done ... reading LDAP users now ..");
_log.debug("LDAP Sync cycle done");
} catch (NamingException e) {
_log.error("Something went Wrong during LDAP Sync");
@ -83,10 +82,16 @@ public class LDAPSync implements Runnable {
}
private boolean checkIfLDAPUserExists(String username, DirContext ctx, String filter) throws NamingException {
private boolean checkIfLDAPUserExists(String username, DirContext ctx, String filter) {
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> answer = ctx.search(getSubContext(username), filter, ctls);
NamingEnumeration<SearchResult> answer;
try {
answer = ctx.search(getSubContext(username), filter, ctls);
} catch (NamingException e) {
_log.info("user: " + username + " not found checkIfLDAPUserExists returning false");
return false;
}
return answer.hasMoreElements();
}
/**
@ -121,12 +126,12 @@ public class LDAPSync implements Runnable {
attributes.put(userPassword);
if (checkIfLDAPUserExists(username, ctx, filter)) {
_log.debug("User " + username + " already exists, replacing attributes");
//_log.debug("User " + username + " already exists, replacing attributes");
ctx.modifyAttributes(getSubContext(username), DirContext.REPLACE_ATTRIBUTE, attributes);
}
else {
ctx.createSubcontext(getSubContext(username),attributes);
_log.debug("New User with uid=" + username + " created");
_log.debug("New User Found with uid=" + username + " created");
}
}

@ -149,7 +149,7 @@ public class PeriodicTask implements Runnable {
for (int i = 0, n = messages.length; i < n; i++) {
Message message = messages[i];
_log.debug("--------------- FOUND EMAIL ------------------");
_log.info("--------------- FOUND EMAIL ------------------");
String subject = message.getSubject();
_log.debug("Parsing email of " + message.getFrom()[0] + " with subject: " + subject);

@ -1,5 +1,7 @@
<html>
<body>
<h2>Hello From Portal Scheduler service!</h2>
<a href="./start-peridic-tasks">RUN Scheduler</a>
</body>
</html>

Loading…
Cancel
Save