/*
 * $Id: VKCLdapUser.java,v 1.5 2004/12/07 15:14:08 jonyp Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCLdapUser.java,v $
 * Revision 1.5  2004/12/07 15:14:08  jonyp
 * added method "isMemberOfLdapGroup"
 *
 * Revision 1.4  2004/11/29 13:42:18  jonyp
 * bug: display users-dn correctly (bas dn was missing in getUserDescription)
 *
 * Revision 1.3  2004/10/22 15:27:26  jonyp
 * *** empty log message ***
 *
 * Revision 1.2  2004/10/15 20:47:56  jonyp
 * *** empty log message ***
 *
 * Revision 1.1  2004/10/15 15:16:03  jonyp
 * preliminary: do not use
 *
 * 
 */ 
package vkc.ldap;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.SearchControls;
import javax.naming.ldap.LdapContext;

import vkc.db.entities.VKCUser;
import vkc.ldap.db.entities.VKCLdapGroup;
import vkc.ldap.db.entities.VKCLdapServer;
import vkc.ldap.db.serializer.VKCLdapGroupSerializer;
import vkc.ldap.db.serializer.VKCLdapServerSerializer;
import vkc.exception.DBException;

/**
 * @author       Johannes Plaßmann
 * @version      1.0
 */

public class VKCLdapUser extends VKCUser
{
    private LdapContext ldapContext = null;
    private VKCLdapServer ldapServer = null;
    
    public LdapContext getLdapContext() {
        return ldapContext;
    }

    public void setLdapContext(LdapContext ldapContext) {
        this.ldapContext = ldapContext;
    }    
    
    public VKCLdapServer getLdapServer() throws DBException {
        
        if (ldapServer != null)       
            return ldapServer;
        
        return VKCLdapServerSerializer.retrieveLdapServer(this.getExternalServerId());     
    }
    
    public String getUserDescription() {

        VKCLdapServer ldapServer = null;
        String desc = null;
        
        try {
             ldapServer = this.getLdapServer();
             desc = ldapServer.getLdapServerURL() + "/" + ldapServer.getMapping_loginattr() + "=" + this.getUser() + "," 
                    + ldapServer.getUsersBaseRDN() + "," + ldapServer.getBaseDN();
        } catch (DBException ex) {
            desc = "unkown";	
        }
        
        return "VKCLdapUser: " + desc;                 
    } 
    
    public boolean isMemberOfLdapGroup(VKCLdapGroup ldapGroup) throws DBException, NamingException {
        
        if (ldapGroup == null) {
            return false;
        }

        // ldap-group must be from the same ldap-server as this ldap-user
        if ( !ldapGroup.getLdapServerId().equals(this.getLdapServer().getLdapServerId())) {
		    return false;
		}
        
		// check this ldap-user for membership of the given group
		LdapContext ldapContext 	= this.getLdapContext();
		String searchBase 			= ldapGroup.getLdapGroupRDN() + "," + this.getLdapServer().getGroupsBaseRDN() + "," + this.getLdapServer().getBaseDN();
		String searchFilter			= "(" + this.getLdapServer().getMapping_loginattr() + "=" + this.getUser() + ")";
		
		NamingEnumeration namingEnumeration = LdapProxy.search(ldapContext, searchBase, searchFilter, SearchControls.SUBTREE_SCOPE);

		if (namingEnumeration != null) {
		    // TODO: Auswertung verändern: Nicht nur auf hasMore prüfen, da dies hier nur "in erster Näherung" geschehen ist
			if (namingEnumeration.hasMore()) {
				// entry found, must be a member	
			    return true;
			}
		}
        
        // no entries found: go away, make my day...
		return false;
    }
    
    public boolean isMemberOfLdapGroup(Integer ldapGroupId) throws DBException, NamingException {
	
		VKCLdapGroup ldapGroup = VKCLdapGroupSerializer.retrieveLdapGroup(ldapGroupId);
		
		return (this.isMemberOfLdapGroup(ldapGroup));
		
   }
    
}