<commit_before>/*
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
package org.gridlab.gridsphere.services.core.user.impl;

import org.gridlab.gridsphere.portlet.User;
import org.gridlab.gridsphere.portlet.PortletLog;
import org.gridlab.gridsphere.portlet.PortletGroup;
import org.gridlab.gridsphere.portlet.PortletGroupFactory;
import org.gridlab.gridsphere.portlet.impl.SportletLog;
import org.gridlab.gridsphere.portlet.service.PortletServiceUnavailableException;
import org.gridlab.gridsphere.portlet.service.spi.PortletServiceConfig;
import org.gridlab.gridsphere.portlet.service.spi.PortletServiceProvider;
import org.gridlab.gridsphere.portlet.service.spi.PortletServiceAuthorizer;
import org.gridlab.gridsphere.services.core.security.auth.AuthorizationException;
import org.gridlab.gridsphere.services.core.user.LoginService;
import org.gridlab.gridsphere.services.core.user.UserSessionManager;
import org.gridlab.gridsphere.services.core.security.auth.LoginAuthModule;

import java.util.*;
import java.lang.reflect.Constructor;

/**
 * The <code>LoginService</code> is the primary interface that defines the login method used to obtain a
 * <code>User</code> from a username and password. The <code>LoginService</code> is configured
 * dynamically at run-time with login authorization modules. By default the PASSWORD_AUTH_MODULE is
 * selected which uses the GridSphere database to store passwords. Other authorization modules
 * can use external directory servers such as LDAP, etc
 */
public class LoginServiceImpl implements LoginService, PortletServiceProvider {

    private GridSphereUserManager userManager = GridSphereUserManager.getInstance();
    private UserSessionManager userSessionManager = UserSessionManager.getInstance();
    private PortletLog log = SportletLog.getInstance(LoginServiceImpl.class);
    private static boolean inited = false;
    private Map authModules = new HashMap();
    private List activeModules = new ArrayList();
    PortletServiceAuthorizer authorizer = null;

    public LoginServiceImpl(PortletServiceAuthorizer authorizer) {
        this.authorizer = authorizer;
    }

    public List getActiveAuthModules() {
        authorizer.authorizeSuperUser();
        return activeModules;
    }

    public void setActiveAuthModules(List activeModules) {
        authorizer.authorizeSuperUser();
        this.activeModules = activeModules;
    }

    public List getSupportedAuthModules() {
        authorizer.authorizeSuperUser();
        List mods = new ArrayList(authModules.size());
        Iterator it = authModules.values().iterator();
        while (it.hasNext()) {
            mods.add(it.next());
        }
        return mods;
    }

    public List getActiveUserIds() {
        authorizer.authorizeAdminUser(PortletGroupFactory.GRIDSPHERE_GROUP);
        return userSessionManager.getUserIds();
    }

    /**
     * Initializes the portlet service.
     * The init method is invoked by the portlet container immediately after a portlet service has
     * been instantiated and before it is passed to the requestor.
     *
     * @param config the service configuration
     * @throws PortletServiceUnavailableException if an error occurs during initialization
     */
    public void init(PortletServiceConfig config) throws PortletServiceUnavailableException {
        log.debug("in login service init");
        if (!inited) {
            Enumeration enum = config.getInitParameterNames();
            while (enum.hasMoreElements()) {
                String authModuleName = (String)enum.nextElement();
                String authClassName = config.getInitParameter(authModuleName);
                LoginAuthModule authModule = createNewAuthModule(authModuleName, authClassName);
                if (authModule != null) authModules.put(authModuleName, authModule);
            }
            LoginAuthModule activeModule = (LoginAuthModule)authModules.get("PASSWORD_AUTH_MODULE");
            activeModules.add(activeModule);
        }
    }

    private LoginAuthModule createNewAuthModule(String authModuleName, String authClassName) {
        LoginAuthModule authModule = null;
        try {
            Class c = Class.forName(authClassName);
            Class[] parameterTypes = new Class[] {String.class};
            Object[] obj = new Object[] { authModuleName };
            Constructor con = c.getConstructor(parameterTypes);
            authModule = (LoginAuthModule)con.newInstance(obj);
        } catch (ClassNotFoundException cne) {
            log.error("LoginServiceImpl: Unable to locate class: " + authClassName);
        } catch(Exception e) {
            log.error("LoginServiceImpl: Unable to create new LoginAuthModule " + authClassName + "(" + authModuleName + ")");
        }
        log.debug("LoginServiceImpl: created module: " + authModuleName);
        return authModule;
    }

    /**<commit_msg>*** empty log message ***<commit_after>/*
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
package org.gridlab.gridsphere.services.core.user.impl;

import org.gridlab.gridsphere.portlet.User;
import org.gridlab.gridsphere.portlet.PortletLog;
import org.gridlab.gridsphere.portlet.PortletGroup;
import org.gridlab.gridsphere.portlet.PortletGroupFactory;
import org.gridlab.gridsphere.portlet.impl.SportletLog;
import org.gridlab.gridsphere.portlet.service.PortletServiceUnavailableException;
import org.gridlab.gridsphere.portlet.service.spi.PortletServiceConfig;
import org.gridlab.gridsphere.portlet.service.spi.PortletServiceProvider;
import org.gridlab.gridsphere.portlet.service.spi.PortletServiceAuthorizer;
import org.gridlab.gridsphere.services.core.security.auth.AuthorizationException;
import org.gridlab.gridsphere.services.core.user.LoginService;
import org.gridlab.gridsphere.services.core.user.UserSessionManager;
import org.gridlab.gridsphere.services.core.security.auth.LoginAuthModule;

import java.util.*;
import java.lang.reflect.Constructor;

/**
 * The <code>LoginService</code> is the primary interface that defines the login method used to obtain a
 * <code>User</code> from a username and password. The <code>LoginService</code> is configured
 * dynamically at run-time with login authorization modules. By default the PASSWORD_AUTH_MODULE is
 * selected which uses the GridSphere database to store passwords. Other authorization modules
 * can use external directory servers such as LDAP, etc
 */
public class LoginServiceImpl implements LoginService, PortletServiceProvider {

    private GridSphereUserManager userManager = GridSphereUserManager.getInstance();
    private UserSessionManager userSessionManager = UserSessionManager.getInstance();
    private PortletLog log = SportletLog.getInstance(LoginServiceImpl.class);
    private static boolean inited = false;
    private Map authModules = new HashMap();
    private List activeModules = new ArrayList();
    PortletServiceAuthorizer authorizer = null;

    public LoginServiceImpl(PortletServiceAuthorizer authorizer) {
        this.authorizer = authorizer;
    }

    public List getActiveAuthModules() {
        authorizer.authorizeSuperUser();
        return activeModules;
    }

    public void setActiveAuthModules(List activeModules) {
        authorizer.authorizeSuperUser();
        this.activeModules = activeModules;
    }

    public List getSupportedAuthModules() {
        authorizer.authorizeSuperUser();
        List mods = new ArrayList(authModules.size());
        Iterator it = authModules.values().iterator();
        while (it.hasNext()) {
            mods.add(it.next());
        }
        return mods;
    }

    public List getActiveUserIds() {
        authorizer.authorizeAdminUser(PortletGroupFactory.GRIDSPHERE_GROUP);
        return userSessionManager.getUserIds();
    }

    /**
     * Initializes the portlet service.
     * The init method is invoked by the portlet container immediately after a portlet service has
     * been instantiated and before it is passed to the requestor.
     *
     * @param config the service configuration
     * @throws PortletServiceUnavailableException if an error occurs during initialization
     */
    public void init(PortletServiceConfig config) throws PortletServiceUnavailableException {
        log.debug("in login service init");
        if (!inited) {
            Enumeration enum = config.getInitParameterNames();
            while (enum.hasMoreElements()) {
                String authModuleName = (String)enum.nextElement();
                String authClassName = config.getInitParameter(authModuleName);
                LoginAuthModule authModule = createNewAuthModule(authModuleName, authClassName);
                if (authModule != null) authModules.put(authModuleName, authModule);
            }
            LoginAuthModule activeModule = (LoginAuthModule)authModules.get("PASSWORD_AUTH_MODULE");
            activeModules.add(activeModule);
            inited = true;
        }
    }

    private LoginAuthModule createNewAuthModule(String authModuleName, String authClassName) {
        LoginAuthModule authModule = null;
        try {
            Class c = Class.forName(authClassName);
            Class[] parameterTypes = new Class[] {String.class};
            Object[] obj = new Object[] { authModuleName };
            Constructor con = c.getConstructor(parameterTypes);
            authModule = (LoginAuthModule)con.newInstance(obj);
        } catch (ClassNotFoundException cne) {
            log.error("LoginServiceImpl: Unable to locate class: " + authClassName);
        } catch(Exception e) {
            log.error("LoginServiceImpl: Unable to create new LoginAuthModule " + authClassName + "(" + authModuleName + ")");
        }
        log.debug("LoginServiceImpl: created module: " + authModuleName);
        return authModule;
    }

    /**
{'fraction_non_alphanumeric': 0.0886859462201928, 'fraction_numerical': 0.0, 'mean_word_length': 3.575673166202414, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6891064', 'n_tokens_mistral': 2576, 'n_tokens_neox': 2531, 'n_words': 612}