⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usercontext.java

📁 OPIAM stands for Open Identity and Access Management. This Suite will provide modules for user & rig
💻 JAVA
字号:
/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.service;

import opiam.admin.faare.config.javabeans.JBLdapConfig;
import opiam.admin.faare.config.javabeans.JBProfile;
import opiam.admin.faare.config.javabeans.JBRessource;
import opiam.admin.faare.exception.ServiceException;
import opiam.admin.faare.persistence.javabeans.JBUser;
import opiam.admin.faare.service.services.references.ReferenceList;

import netscape.ldap.LDAPConnection;
import netscape.ldap.LDAPException;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * This object is the context of a user connected to the application.
 * Most services methods take this object as parameter.
 *
 */
public class UserContext implements Serializable
{
    /** logger. */
    private static Logger _logger = Logger.getLogger(UserContext.class);

    /** Connected user dname. */
    private String dn;

    /** Connected user dname. */
    private String login;

    /** Connected user password.*/
    private String pwd;

    /** User. */
    private JBUser jbUser;

    /** LDAP server configuration. */
    private JBLdapConfig ldapConfig;

    /** Connected user session id. */
    private String sessionId;

    /** Connected user current profile. */
    private JBProfile jbProfile;

    /** List of profiles the user belongs to. */
    private List profiles = new ArrayList();

    /** Connected user objects cache. */
    private transient Map cache = new HashMap();

    /** Application specific parameters table. */
    private Map parameters = new HashMap();

    /**
     * LDAP connection.
     * Created with the UserContext but bound at PersistenceLDAP.logon.
     */
    private transient LDAPConnection ld = null;

    /** Application connection, used to search user dname from login. */
    private LDAPConnection appliConnection = null;

    /** Logon counter.
     *  Incr閙ented at every unsuccessful login attempt
     *  and reset at each successfull login attempt.
     */
    private int logonCounter = 0;

    /** Authentication succeeded flag. */
    private boolean loggedIn = false;

    /** session scoped reference lists. */
    private Map userRefListsMap = new HashMap();

    /**
     * Creates a new UserContext object.
     */
    public UserContext()
    {
    }

    /**
     * Creates a new UserContext object.
     *
     * @param asessionId  Session identifier.
     */
    public UserContext(String asessionId)
    {
        this.sessionId = asessionId;
    }

    /**
     * Gets the user dname.
     *
     * @return User dname.
     */
    public String getDn()
    {
        return dn;
    }

    /**
     * Sets the ser dname.
     *
     * @param adn user dname.
     */
    public void setDn(String adn)
    {
        this.dn = adn;
    }

    /**
     * Gets the user password.
     *
     * @return user password.
     */
    public String getPwd()
    {
        return pwd;
    }

    /**
     * Sets the user password.
     *
     * @param apwd New value of password.
     */
    public void setPwd(String apwd)
    {
        this.pwd = apwd;
    }

    /**
     * Gets the session identifier.
     *
     * @return Value of session identifier.
     */
    public String getSessionId()
    {
        return sessionId;
    }

    /**
     * Sets the session identifier.
     *
     * @param asessionId New value of session identifier.
     */
    public void setSessionId(String asessionId)
    {
        this.sessionId = asessionId;
    }

    /**
     * Gets the user login.
     *
     * @return Value of login.
     */
    public String getLogin()
    {
        return login;
    }

    /**
     * Sets the user login.
     *
     * @param alogin New value of login.
     */
    public void setLogin(String alogin)
    {
        this.login = alogin;
    }

    /**
     * Gets the user current profile.
     *
     * @return profile.
     */
    public JBProfile getJbProfile()
    {
        return jbProfile;
    }

    /**
     * Sets the user current profile.
     *
     * @param ajbProfile profile.
     */
    public void setJbProfile(JBProfile ajbProfile)
    {
        this.jbProfile = ajbProfile;
    }

    /**
     * Gets the user LDAP connection.
     *
     * @return LDAP connection.
     */
    public LDAPConnection getLd()
    {
        return ld;
    }

    /**
     * Sets the user LDAP connection.
     *
     * @param ald LDAP COnnection.
     */
    public void setLd(LDAPConnection ald)
    {
        this.ld = ald;
    }

    /**
     * Gets the user objects cache.
     *
     * @return cache.
     */
    public Map getCache()
    {
        return cache;
    }

    /**
     * Sets the user objects cache.
     *
     * @param acache cache
     */
    public void setCache(Hashtable acache)
    {
        this.cache = acache;
    }

    /**
     * Returns the application specific parameters.
     *
     * @return Hashtable of parameters.
     */
    public Map getParameters()
    {
        return parameters;
    }

    /**
     * Sets the application specific parameters.
     *
     * @param aparameters The parameters to set.
     */
    public void setParameters(Hashtable aparameters)
    {
        this.parameters = aparameters;
    }

    /**
     * Returns the user object.
     *
     * @return JBUser.
     */
    public JBUser getJbUser()
    {
        return jbUser;
    }

    /**
     * Sets the user object.
     *
     * @param ajbUser The jbUser to set.
     */
    public void setJbUser(JBUser ajbUser)
    {
        this.jbUser = ajbUser;
    }

    /**
     * Gets the unsuccessful login attempts number.
     *
     * @return unsuccessful login attempts number.
     */
    public int getLogonCounter()
    {
        return logonCounter;
    }

    /**
     * Increments the logon counter.
     */
    public void incrementLogonCounter()
    {
        this.logonCounter++;
    }

    /**
     * Resets the logon counter.
     */
    public void resetLogonCounter()
    {
        this.logonCounter = 0;
    }

    /**
     * Returns the "Authentication succeeded" flag.
     *
     * @return flag
     */
    public boolean isLoggedIn()
    {
        return loggedIn;
    }

    /**
     * Sets the "Authentication succeeded" flag.
     *
     * @param aloggedIn  Flag to set.
     */
    public void setLoggedIn(boolean aloggedIn)
    {
        this.loggedIn = aloggedIn;
    }

    /**
     * Cleans up this object (disconnecting + destroying cached objects).
     */
    public void clear()
    {
        try
        {
            getLd().disconnect();
        }
        catch (LDAPException e)
        {
            // do nothing, no need
            _logger.warn("Error while disconnecting.");
        }

        getCache().clear();
    }

    /**
     * Adds a scoped session reference list.
     *
     * @param ref  Reference list to add.
     */
    public void addReferenceList(ReferenceList ref)
    {
        userRefListsMap.put(ref.getName().trim().toLowerCase(), ref);
    }

    /**
     * Searches a scoped session reference list.
     *
     * @param name  Name of the reference list to search.
     *
     * @return Reference list if found, else null.
     */
    public ReferenceList findReferenceListByName(String name)
    {
        // rendre case insensitive en mettant le param en lowercase
        String key = name.trim().toLowerCase();

        return (ReferenceList) userRefListsMap.get(key);
    }

    /**
     * Gets all the profiles the user belongs to.<br>
     * List elements are opiam.admin.faare.config.javabeans.JBProfile objects
     *
     * @return List of profiles.
     */
    public List getProfiles()
    {
        return profiles;
    }

    /**
     * Sets the profiles the user belongs to.
     *
     * @param aprofiles The profiles to set.
     */
    public void setProfiles(List aprofiles)
    {
        this.profiles = aprofiles;
    }

    /**
     * Gets a resource object from its name as defined in profiles_config.xml.
     *
     * @param name resource name in the profile.
     *
     * @return resource, or null if not found in the current profile
     */
    public JBRessource findJBRessourceByName(String name)
    {
        // the GLOBAL resource is already included in each profile
        try
        {
            return jbProfile.getJbRessources().findRessourceByName(name);
        }
        catch (Exception e)
        {
            return null;
        }
    }

    /**
     * Sets the current profile from its index in the user profiles list.
     *
     * @param idx  Profile index.
     */
    public void chooseProfile(int idx)
    {
        jbProfile = (JBProfile) profiles.get(idx);
    }

    /**
     * Sets the current profile from its name.
     *
     * @param name Profile name.
     *
     * @throws ServiceException if the profile is unknown or nor allowed
     *         for this user.
     */
    public void chooseProfile(String name) throws ServiceException
    {
        boolean found = false;
        Iterator iter = profiles.iterator();
        JBProfile curProf;

        while (iter.hasNext() && !found)
        {
            curProf = (JBProfile) iter.next();

            if (curProf.getName().trim().toLowerCase().equals(name.trim()
                                                                      .toLowerCase()))
            {
                found = true;
                jbProfile = curProf;
            }
        }

        if (!found)
        {
            throw new ServiceException("unknown");
        }
    }

    /**
     * Serialization method.
     * Takes into account the case when cache == null.
     *
     * @param s Serizalization stream
     *
     * @throws IOException see ObjectOutputStream.defaultWriteObject.
     */
    private void writeObject(ObjectOutputStream s) throws IOException
    {
        s.defaultWriteObject();

        // customized serialization code
        cache = new HashMap();
    }

    /**
     * Returns the LDAP server configuration.
     *
     * @return LDAP server configuration.
     */
    public JBLdapConfig getLdapConfig()
    {
        return ldapConfig;
    }

    /**
     * Sets the LDAP server configuration.
     *
     * @param aldapConfig The LDAP server configuration to set.
     */
    public void setLdapConfig(JBLdapConfig aldapConfig)
    {
        this.ldapConfig = aldapConfig;
    }

    /**
     * Returns the applicative connection.
     *
     * @return the applicative connection.
     */
    public LDAPConnection getAppliConnection()
    {
        return appliConnection;
    }

    /**
     * Sets the applicative connection.
     *
     * @param aappliConnection The applicative connection to set.
     */
    public void setAppliConnection(LDAPConnection aappliConnection)
    {
        this.appliConnection = aappliConnection;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -