org.apache.syncope.core.sync.LDAPDomainSyncActions.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.syncope.core.sync.LDAPDomainSyncActions.java

Source

/*
 * Copyright 2013 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.syncope.core.sync;

import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
import org.apache.syncope.common.mod.AbstractAttributableMod;
import org.apache.syncope.common.mod.AttributeMod;
import org.apache.syncope.common.to.AbstractAttributableTO;
import org.apache.syncope.common.to.AttributeTO;
import org.apache.syncope.common.types.ConnConfProperty;
import org.apache.syncope.core.persistence.beans.ConnInstance;
import org.apache.syncope.core.persistence.beans.user.SyncopeUser;
import org.apache.syncope.core.persistence.dao.UserDAO;
import org.apache.syncope.core.sync.impl.SyncopeSyncResultHandler;
import org.identityconnectors.framework.common.objects.ConnectorObject;
import org.identityconnectors.framework.common.objects.Name;
import org.identityconnectors.framework.common.objects.ObjectClass;
import org.identityconnectors.framework.common.objects.SyncDelta;
import org.identityconnectors.framework.common.objects.SyncResultsHandler;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public class LDAPDomainSyncActions extends DefaultSyncActions {

    @Autowired
    private UserDAO userDAO;

    private static final Logger LOG = LoggerFactory.getLogger(LDAPDomainSyncActions.class);

    @Override
    public <T extends AbstractAttributableTO> SyncDelta beforeCreate(SyncResultsHandler srh, SyncDelta sd, T t)
            throws JobExecutionException {

        if (!ObjectClass.ACCOUNT_NAME.equals(sd.getObject().getObjectClass().toString())) {
            if (t != null) {
                LOG.debug("CREATION OF A NEW USER");
                String rdn = "/";
                ConnectorObject conn = sd.getObject();

                try {
                    LdapName dn = new LdapName(conn.getAttributeByName(Name.NAME).getValue().toString()
                            .replace("[", "").replace("]", ""));
                    if (dn.size() == 4) {
                        rdn = dn.getRdn(2).getValue().toString();
                    } else {
                        rdn = "/";
                    }
                } catch (InvalidNameException ex) {
                    LOG.error("ERROR CONSTRUCTING LDAP DN FROM NAME ATTRIBUTE: ".concat(ex.getMessage()));
                }
                //Creation of new attribute to assign to new user in Syncope
                AttributeTO domain = new AttributeTO();
                domain.setSchema("domain");
                domain.addValue(rdn);
                t.addAttribute(domain);
            } else {
                LOG.error("SUBJECT OF SYNCHRONIZATION IS NULL");
            }
        }
        return sd;
    }

    @Transactional(readOnly = true)
    @Override
    public <T extends AbstractAttributableTO, K extends AbstractAttributableMod> SyncDelta beforeUpdate(
            SyncResultsHandler srh, SyncDelta sd, T t, K k) throws JobExecutionException {

        if (!ObjectClass.ACCOUNT_NAME.equals(sd.getObject().getObjectClass().toString())) {

            if (t != null) {
                SyncopeUser user = userDAO.find(t.getId());

                if (user != null && !user.isSuspended()) {

                    ConnectorObject conn = sd.getObject();
                    // Get dn of current user to be updated on Syncope 
                    LdapName dnOnSyncope = resolveDnOnSyncope(user, srh);
                    try {
                        LdapName dn = new LdapName(conn.getAttributeByName(Name.NAME).getValue().toString()
                                .replace("[", "").replace("]", ""));
                        // Check if dn on Syncope and dn on Ldap are the same, if so returns
                        if (dnOnSyncope.compareTo(dn) != 0) {
                            String rdn;
                            if (dn.size() == 4) {
                                rdn = dn.getRdn(2).getValue().toString();
                            } else {
                                rdn = "/";
                            }
                            //Creation of new attribute to assign to new user in Syncope
                            AttributeMod attr = new AttributeMod();
                            attr.setSchema("domain");
                            attr.addValueToBeAdded(rdn);
                            k.addAttributeToBeUpdated(attr);
                        } else {
                            LOG.info("NO CHANGES APPLIED TO DOMAIN ATTRIBUTE");
                            return sd;
                        }
                    } catch (InvalidNameException ex) {
                        LOG.error("ERROR CONSTRUCTING LDAP DN FROM NAME ATTRIBUTE: ".concat(ex.getMessage()));
                    }
                } else {
                    LOG.error("USER WITH ID: " + t.getId() + " DOESN'T EXIST OR IS SUSPENDED ON SYNCOPE ");
                }
            } else {
                LOG.error("SUBJECT OF SYNCHRONIZATION IS NULL");
            }
        }
        return sd;
    }

    private LdapName resolveDnOnSyncope(SyncopeUser user, SyncResultsHandler handler) {

        String domain = user.getAttribute("domain").getValuesAsStrings().iterator().next();
        SyncopeSyncResultHandler intHandler = (SyncopeSyncResultHandler) handler;

        LdapName dnOnSyncope = null;
        // Get ConnInstance object to retrieve Configuration of current connector
        String baseContextUser = null;
        StringBuilder sb = new StringBuilder();
        ConnInstance connInstance = intHandler.getSyncTask().getResource().getConnector();
        // Search of connector property containing base context(s)
        for (ConnConfProperty property : connInstance.getConfiguration()) {
            if ("baseContexts".equals(property.getSchema().getName())) {
                baseContextUser = (String) property.getValues().get(0);
            }
        }
        try {
            if (!"/".equals(user.getAttribute("domain").getValuesAsStrings().iterator().next())) {
                sb.append("uid=").append(user.getUsername()).append(",ou=").append(domain).append(",")
                        .append(baseContextUser);
                dnOnSyncope = new LdapName(sb.toString());
            } else {
                sb.append("uid=").append(user.getUsername()).append(",").append(baseContextUser);
                dnOnSyncope = new LdapName(sb.toString());
            }
        } catch (InvalidNameException ex) {
            LOG.error("ERROR CONSTRUCTING LDAP DN" + ex.getMessage());
        }
        return dnOnSyncope;
    }
}