org.apache.syncope.core.propagation.LDAPDomainPropagationActions.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.syncope.core.propagation.LDAPDomainPropagationActions.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.propagation;

import java.util.Set;
import org.apache.syncope.common.types.ResourceOperation;
import org.apache.syncope.core.persistence.beans.PropagationTask;
import org.apache.syncope.core.persistence.beans.TaskExec;
import org.apache.syncope.core.persistence.beans.user.SyncopeUser;
import org.identityconnectors.framework.common.objects.ConnectorObject;
import org.apache.syncope.core.persistence.dao.UserDAO;
import org.identityconnectors.framework.common.objects.Attribute;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.identityconnectors.framework.common.objects.AttributeBuilder;
import org.identityconnectors.framework.common.objects.AttributeUtil;
import org.identityconnectors.framework.common.objects.Name;
import org.identityconnectors.framework.common.objects.ObjectClass;
import org.apache.syncope.common.types.ConnConfProperty;
import org.apache.syncope.core.persistence.beans.ConnInstance;

public class LDAPDomainPropagationActions implements PropagationActions {

    @Autowired
    private UserDAO userDAO;

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

    @Override
    public void before(PropagationTask pt, ConnectorObject co) {

        // Get ConnInstance object to retrieve Configuration of current connector
        ConnInstance connInstance = pt.getResource().getConnector();
        String baseContextUser = null;
        StringBuilder sb = new StringBuilder();
        // 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);
            }
        }
        if (!(ResourceOperation.DELETE == pt.getPropagationOperation())
                && ObjectClass.ACCOUNT_NAME.equals(pt.getObjectClassName())) {
            // Selection of current user to CREATE/UPDATE
            SyncopeUser user = userDAO.find(pt.getSubjectId());
            Attribute newAccountLink;
            if (user != null) {
                String domain = user.getAttribute("domain").getValuesAsStrings().iterator().next();
                if (domain != null) {
                    String dn;
                    if (!"/".equals(domain)) {
                        sb.append("uid=").append(user.getUsername()).append(",ou=").append(domain).append(",")
                                .append(baseContextUser);
                        dn = sb.toString();
                    } else {
                        sb.append("uid=").append(user.getUsername()).append(",").append(baseContextUser);
                        dn = sb.toString();
                    }
                    newAccountLink = AttributeBuilder.build(Name.NAME, dn);
                    LOG.debug("NEW ACCOUNTLINK: " + newAccountLink.getValue());
                    //Get task attributes
                    Set<Attribute> newAttributes = pt.getAttributes();
                    //Remove old account link from task attributes
                    boolean removeResult = newAttributes.remove(AttributeUtil.find(Name.NAME, pt.getAttributes()));
                    LOG.debug("DELETE OLD ACCOUNT LINK: " + removeResult);
                    //Add new attributes with new Account Link set
                    boolean addResult = newAttributes.add(newAccountLink);
                    LOG.debug("ADD NEW ACCOUNT LINK: " + addResult);
                    pt.setAttributes(newAttributes);
                } else {
                    LOG.debug("DOMAIN IN NULL");
                }
            }
        }
    }

    @Override
    public void after(PropagationTask pt, TaskExec te, ConnectorObject co) {
    }
}