All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.com.novell.ldapchai.impl.AbstractChaiUser Maven / Gradle / Ivy

/*
 * LDAP Chai API
 * Copyright (c) 2006-2010 Novell, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.novell.ldapchai.impl;

import com.novell.ldapchai.ChaiConstant;
import com.novell.ldapchai.ChaiFactory;
import com.novell.ldapchai.ChaiGroup;
import com.novell.ldapchai.ChaiPasswordPolicy;
import com.novell.ldapchai.ChaiUser;
import com.novell.ldapchai.exception.ChaiErrors;
import com.novell.ldapchai.exception.ChaiOperationException;
import com.novell.ldapchai.exception.ChaiPasswordPolicyException;
import com.novell.ldapchai.exception.ChaiUnavailableException;
import com.novell.ldapchai.provider.ChaiProvider;

import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * A complete implementation of {@code ChaiUser} interface.
 *
 * Clients looking to obtain a {@code ChaiUser} instance should look to {@link com.novell.ldapchai.ChaiFactory}.
 *
 * @author Jason D. Rivard
 */

public abstract class AbstractChaiUser extends AbstractChaiEntry implements ChaiUser {
    /**
     * This construtor is used to instantiate an ChaiUserImpl instance representing an inetOrgPerson user object in ldap.
     *
     * @param userDN       The DN of the user
     * @param chaiProvider Helper to connect to LDAP.
     */
    public AbstractChaiUser(final String userDN, final ChaiProvider chaiProvider)
    {
        super(userDN, chaiProvider);
    }

// ------------------------ INTERFACE METHODS ------------------------


// --------------------- Interface ChaiUser ---------------------


    public final ChaiUser getAssistant()
            throws ChaiOperationException, ChaiUnavailableException
    {
        final String mgrDN = this.readStringAttribute(ATTR_MANAGER);
        if (mgrDN == null) {
            return null;
        }
        return ChaiFactory.createChaiUser(mgrDN, this.getChaiProvider());
    }

    public final Set getDirectReports()
            throws ChaiOperationException, ChaiUnavailableException
    {
        final Set reportDNs = this.readMultiStringAttribute(ATTR_MANAGER);
        final Set reports = new HashSet(reportDNs.size());
        for (final String reporteeDN : reportDNs) {
            reports.add(ChaiFactory.createChaiUser(reporteeDN, this.getChaiProvider()));
        }
        return Collections.unmodifiableSet(reports);
    }

    public Set getGroups()
            throws ChaiOperationException, ChaiUnavailableException
    {
        final Set returnGroups = new HashSet();
        final Set groups = this.readMultiStringAttribute(ChaiConstant.ATTR_LDAP_GROUP_MEMBERSHIP);
        for (final String group : groups) {
            returnGroups.add(ChaiFactory.createChaiGroup(group, this.getChaiProvider()));
        }
        return Collections.unmodifiableSet(returnGroups);
    }

    public final ChaiUser getManager()
            throws ChaiOperationException, ChaiUnavailableException
    {
        final String mgrDN = this.readStringAttribute(ATTR_MANAGER);
        if (mgrDN == null) {
            return null;
        }
        return ChaiFactory.createChaiUser(mgrDN, this.getChaiProvider());
    }


    public boolean testPassword(final String password)
            throws ChaiUnavailableException, ChaiPasswordPolicyException
    {
        try {
            return this.compareStringAttribute(ATTR_PASSWORD, password);
        } catch (ChaiOperationException e) {
            throw new ChaiPasswordPolicyException(e.getMessage(), ChaiErrors.getErrorForMessage(e.getMessage()));
        }
    }

    public final String readSurname()
            throws ChaiOperationException, ChaiUnavailableException
    {
        return this.readStringAttribute(ATTR_SURNAME);
    }

    public String readUsername()
            throws ChaiOperationException, ChaiUnavailableException
    {
        return this.readStringAttribute(ATTR_COMMON_NAME);
    }

    public void addGroupMembership(final ChaiGroup theGroup) throws ChaiOperationException, ChaiUnavailableException {
        this.addAttribute(ChaiConstant.ATTR_LDAP_GROUP_MEMBERSHIP, theGroup.getEntryDN());
        theGroup.addAttribute(ChaiConstant.ATTR_LDAP_MEMBER, this.getEntryDN());
    }

    public void removeGroupMembership(final ChaiGroup theGroup) throws ChaiOperationException, ChaiUnavailableException {
        this.deleteAttribute(ChaiConstant.ATTR_LDAP_GROUP_MEMBERSHIP, theGroup.getEntryDN());
        theGroup.deleteAttribute(ChaiConstant.ATTR_LDAP_MEMBER, this.getEntryDN());
    }

    public String readGivenName()
            throws ChaiOperationException, ChaiUnavailableException
    {
        return this.readStringAttribute(ATTR_GIVEN_NAME);
    }

    public void setPassword(final String newPassword)
            throws ChaiUnavailableException, ChaiPasswordPolicyException, ChaiOperationException
    {
        this.setPassword(newPassword, false);
    }

    public void setPassword(final String newPassword, final boolean enforcePasswordPolicy)
            throws ChaiUnavailableException, ChaiPasswordPolicyException, ChaiOperationException
    {
        try {
            writeStringAttribute(ATTR_PASSWORD, newPassword);
        } catch (ChaiOperationException e) {
            throw ChaiPasswordPolicyException.forErrorMessage(e.getMessage());
        }
    }

    public void changePassword(final String oldPassword, final String newPassword)
            throws ChaiUnavailableException, ChaiPasswordPolicyException, ChaiOperationException
    {
        try {
            writeStringAttribute(ATTR_PASSWORD, newPassword);
        } catch (ChaiOperationException e) {
            throw ChaiPasswordPolicyException.forErrorMessage(e.getMessage());
        }
    }

    public void expirePassword() throws ChaiOperationException, ChaiUnavailableException {
    }

    public ChaiPasswordPolicy getPasswordPolicy() throws ChaiUnavailableException, ChaiOperationException {
        return null;
    }

    public boolean isPasswordExpired() throws ChaiUnavailableException, ChaiOperationException {
        return false;
    }

    public Date readLastLoginTime() throws ChaiOperationException, ChaiUnavailableException {
        return null;
    }

    public String readPassword() throws ChaiUnavailableException, ChaiOperationException {
        return null;
    }

    public Date readPasswordExpirationDate() throws ChaiUnavailableException, ChaiOperationException {
        return null;
    }

    public boolean testPasswordPolicy(final String testPassword) throws ChaiUnavailableException, ChaiPasswordPolicyException {
        return true;
    }

    public void unlockPassword() throws ChaiOperationException, ChaiUnavailableException {
    }

    public boolean isPasswordLocked() throws ChaiOperationException, ChaiUnavailableException {
        return false;
    }

    public Date readPasswordModificationDate() throws ChaiOperationException, ChaiUnavailableException {
        return null;
    }

    public boolean isAccountEnabled() throws ChaiOperationException, ChaiUnavailableException {
        return !readBooleanAttribute(ATTR_LOGIN_DISABLED);
    }

    public void unlock()
            throws ChaiOperationException, ChaiUnavailableException
    {
        this.unlockPassword();
    }

    public boolean isLocked()
            throws ChaiOperationException, ChaiUnavailableException
    {
        return this.isPasswordLocked();
    }

    public Date readAccountExpirationDate() throws ChaiUnavailableException, ChaiOperationException {
        return null;
    }

    public boolean isAccountExpired() throws ChaiOperationException, ChaiUnavailableException {
        final Date accountExpirationDate = readAccountExpirationDate();
        return accountExpirationDate != null && accountExpirationDate.before(new Date());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy