log.info(LogManager.getHeader(context, "autoregister", "netid=" + netid));  if ((ldap.ldapEmail!=null)&&(!ldap.ldapEmail.equals(""))) { try { eperson = EPerson.findByEmail(context, ldap.ldapEmail); } catch (AuthorizeException e) { eperson = null; } if (eperson!=null) { log.info(LogManager.getHeader(context, "failed_autoregister", "type=ldap_but_already_email")); return SUCCESS; } }
    public int authenticate(Context context,                            String netid,                            String password,                            String realm,                            HttpServletRequest request)        throws SQLException    {        log.info(LogManager.getHeader(context, "auth", "attempting trivial auth of user="+netid));        // Locate the eperson        EPerson eperson = null;        try        {            eperson = EPerson.findByNetid(context, netid.toLowerCase());        }        catch (SQLException e)        {        }        boolean loggedIn = false;        SpeakerToLDAP ldap = new SpeakerToLDAP(log);        // if they entered a netid that matches an eperson        if (eperson != null)        {            // e-mail address corresponds to active account            if (eperson.getRequireCertificate())                return CERT_REQUIRED;            else if (!eperson.canLogIn())                return BAD_ARGS;            {                if (ldap.ldapAuthenticate(netid, password, context))                {                    log.info(LogManager                        .getHeader(context, "authenticate", "type=ldap"));                    return SUCCESS;                }                else                   return BAD_CREDENTIALS;            }        }        // the user does not already exist so try and authenticate them        // with ldap and create an eperson for them        else        {            if (ldap.ldapAuthenticate(netid, password, context))            {                if (canSelfRegister(context, request, netid))                {                    // Register the new user automatically                    log.info(LogManager.getHeader(context,                                    "autoregister", "netid=" + netid));                    if ((ldap.ldapEmail!=null)&&(!ldap.ldapEmail.equals("")))                    {                        try                        {                            eperson = EPerson.findByEmail(context, ldap.ldapEmail);                        }                        catch (AuthorizeException e)                        {                            eperson = null;                        }                        if (eperson!=null)                        {                            log.info(LogManager.getHeader(context,                                    "failed_autoregister", "type=ldap_but_already_email"));                            return SUCCESS;                        }                    }                    // TEMPORARILY turn off authorisation                    try                    {                        context.setIgnoreAuthorization(true);                        eperson = EPerson.create(context);                        if ((ldap.ldapEmail!=null)&&(!ldap.ldapEmail.equals(""))) eperson.setEmail(ldap.ldapEmail);                        else eperson.setEmail(netid);                        if ((ldap.ldapGivenName!=null)&&(!ldap.ldapGivenName.equals(""))) eperson.setFirstName(ldap.ldapGivenName);                        if ((ldap.ldapSurname!=null)&&(!ldap.ldapSurname.equals(""))) eperson.setLastName(ldap.ldapSurname);                        if ((ldap.ldapPhone!=null)&&(!ldap.ldapPhone.equals(""))) eperson.setMetadata("phone", ldap.ldapPhone);                        eperson.setNetid(netid);                        eperson.setCanLogIn(true);                        AuthenticationManager.initEPerson(context, request, eperson);                        eperson.update();                        context.commit();                    }                    catch (AuthorizeException e)                    {                        return NO_SUCH_USER;                    }                    finally                    {                        context.setIgnoreAuthorization(false);                    }                    log.info(LogManager.getHeader(context, "authenticate",                                "type=ldap-login, created ePerson"));                    return SUCCESS;                }                else                {                    // No auto-registration for valid certs                    log.info(LogManager.getHeader(context,                                    "failed_login", "type=ldap_but_no_record"));                    return NO_SUCH_USER;                }            }        }        return BAD_ARGS;    }