Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.trf.plugins.UserProfileCheckinHook.ldap;
- import com.dotmarketing.business.APILocator;
- import com.dotmarketing.exception.DotRuntimeException;
- import com.dotmarketing.plugin.business.PluginAPI;
- import com.dotmarketing.util.Logger;
- import com.dotmarketing.util.UtilMethods;
- import com.novell.ldap.LDAPAttribute;
- import com.novell.ldap.LDAPAttributeSet;
- import com.novell.ldap.LDAPConnection;
- import com.novell.ldap.LDAPEntry;
- import com.novell.ldap.LDAPException;
- import com.novell.ldap.LDAPJSSESecureSocketFactory;
- import com.novell.ldap.LDAPModification;
- import com.novell.ldap.LDAPResponse;
- import com.novell.ldap.LDAPResponseQueue;
- import com.novell.ldap.LDAPSearchResults;
- import com.novell.ldap.LDAPSocketFactory;
- public class UserProfileLDAPCheckinHook {
- private final static PluginAPI pluginAPI = APILocator.getPluginAPI();
- private final static String pluginId = "org.trf.plugins.UserProfileCheckinHook";
- private static String SECURITY_AUTHENTICATION;
- private static String SECURITY_KEYSTORE_PATH;
- private static String HOST;
- private static String PORT;
- private static String USERID;
- private static String PASSWORD;
- private static String DOMAINLOOKUP;
- private static String USER_ATTRIB;
- private static boolean inited = false;
- private synchronized static void init() {
- if (inited)
- return;
- try {
- SECURITY_AUTHENTICATION = pluginAPI.loadProperty(pluginId, "ldap.security.authentication");
- SECURITY_KEYSTORE_PATH = pluginAPI.loadProperty(pluginId, "ldap.security.keystore.path");
- HOST = pluginAPI.loadProperty(pluginId, "ldap.host");
- PORT = pluginAPI.loadProperty(pluginId, "ldap.port");
- USERID = pluginAPI.loadProperty(pluginId, "ldap.userid");
- PASSWORD = pluginAPI.loadProperty(pluginId, "ldap.password");
- DOMAINLOOKUP = pluginAPI.loadProperty(pluginId, "ldap.domainlookup");
- USER_ATTRIB = pluginAPI.loadProperty(pluginId, "ldap.attrib.user");
- inited = true;
- } catch (Exception e) {
- Logger.fatal(UserProfileLDAPCheckinHook.class, e.toString());
- }
- }
- public static String getDomainLookup() {
- return DOMAINLOOKUP;
- }
- public static String getUserAttrib() {
- return USER_ATTRIB;
- }
- public static LDAPConnection getBindedConnection() throws DotRuntimeException {
- if (!inited)
- init();
- if (!inited) {
- return null;
- } else {
- if (SECURITY_AUTHENTICATION.equalsIgnoreCase("SSL")) {
- System.setProperty("javax.net.ssl.trustStore", SECURITY_KEYSTORE_PATH);
- Logger.debug(UserProfileLDAPCheckinHook.class, "The trust store is " + System.getProperty("javax.net.ssl.trustStore"));
- LDAPSocketFactory ssf = new LDAPJSSESecureSocketFactory();
- // Set the socket factory as the default for all future connections
- LDAPConnection.setSocketFactory(ssf);
- }
- LDAPConnection lc = new LDAPConnection();
- // connect to the server
- try {
- lc.connect(HOST, Integer.valueOf(PORT));
- } catch (Exception e) {
- Logger.error(UserProfileLDAPCheckinHook.class, e.getMessage(), e);
- }
- LDAPResponseQueue queue = null;
- LDAPResponse rsp = null;
- try {
- queue = lc.bind(LDAPConnection.LDAP_V3, USERID, PASSWORD.getBytes(), (LDAPResponseQueue) null);
- rsp = (LDAPResponse) queue.getResponse();
- }catch(Exception ex){
- Logger.error(UserProfileLDAPCheckinHook.class, ex.getMessage(), ex);
- }
- int rc = rsp.getResultCode();
- String msg = rsp.getErrorMessage();
- if (rc == LDAPException.SUCCESS) {
- Logger.debug(UserProfileLDAPCheckinHook.class, "LDAP connection is now bound");
- return lc;
- } else {
- throw new DotRuntimeException("Unable to bind to ldap " + msg);
- }
- }
- }
- public static LDAPEntry getUserEntry(LDAPConnection lc, String username) {
- LDAPSearchResults searchResults = null;
- try {
- searchResults = lc.search(DOMAINLOOKUP, LDAPConnection.SCOPE_SUB, USER_ATTRIB + "=" + username, null, false);
- } catch(Exception ex) {
- Logger.error(UserProfileLDAPCheckinHook.class, "Unable to search for username " + username + " : ", ex);
- }
- if (searchResults.hasMore()) {
- LDAPEntry ldapEntry = null;
- try{
- ldapEntry = searchResults.next();
- } catch(Exception e) {
- Logger.error(UserProfileLDAPCheckinHook.class, "Error while trying to bind user " + username + " : ", e);
- return null;
- }
- return ldapEntry;
- } else {
- Logger.info(UserProfileLDAPCheckinHook.class, "Cannot find username: " + username);
- return null;
- }
- }
- public static boolean addEntry(LDAPConnection lc, String DN, LDAPAttributeSet attributeSet) {
- if ((attributeSet == null) || (attributeSet.size() == 0))
- return false;
- try {
- LDAPEntry newEntry = new LDAPEntry(DN, attributeSet);
- lc.add(newEntry);
- return true;
- } catch (Exception e) {
- Logger.warn(UserProfileLDAPCheckinHook.class, e.toString());
- }
- return false;
- }
- public static boolean modifyEntryAttribute(LDAPConnection lc, String DN, String attrName, String attrValue) {
- if ((lc == null) || !UtilMethods.isSet(DN) || !UtilMethods.isSet(attrName))
- return false;
- try {
- LDAPAttribute ldapAttribute = new LDAPAttribute(attrName, attrValue);
- LDAPModification ldapModification = new LDAPModification(LDAPModification.REPLACE, ldapAttribute);
- lc.modify(DN, ldapModification);
- return true;
- } catch (Exception e) {
- Logger.warn(UserProfileLDAPCheckinHook.class, e.toString());
- }
- return false;
- }
- public static boolean modifyEntryAttributes(LDAPConnection lc, String DN, LDAPModification[] ldapModifications) {
- if ((ldapModifications == null) || (ldapModifications.length == 0))
- return false;
- try {
- lc.modify(DN, ldapModifications);
- return true;
- } catch (Exception e) {
- Logger.warn(UserProfileLDAPCheckinHook.class, e.toString());
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement