'Validates numerous cases that might happen during the login time.'

def __validate_login(self, trans, **kwd):

    status = kwd.get('status', 'error')
    login = kwd.get('login', '')
    password = kwd.get('password', '')
    referer = (trans.request.referer or '')
    redirect = kwd.get('redirect', referer).strip()
    success = False
    user = trans.sa_session.query(trans.app.model.User).filter(or_((trans.app.model.User.table.c.email == login), (trans.app.model.User.table.c.username == login))).first()
    log.debug(('trans.app.config.auth_config_file:    %s' % trans.app.config.auth_config_file))
    if (not user):
        autoreg = trans.app.auth_manager.check_auto_registration(trans, login, password)
        if autoreg[0]:
            kwd['email'] = autoreg[1]
            kwd['username'] = autoreg[2]
            message = '    '.join([validate_email(trans, kwd['email']), validate_publicname(trans, kwd['username'])]).rstrip()
            if (not message):
                (message, status, user, success) = self.__register(trans, 'user', False, **kwd)
                if success:
                    trans.handle_user_login(user)
                    trans.log_event('User    (auto)    created    a    new    account')
                    trans.log_event('User    logged    in')
                else:
                    message = ('Auto-registration    failed,    contact    your    local    Galaxy    administrator.    %s' % message)
            else:
                message = ('Auto-registration    failed,    contact    your    local    Galaxy    administrator.    %s' % message)
        else:
            message = 'No    such    user    or    invalid    password'
    elif user.deleted:
        message = 'This    account    has    been    marked    deleted,    contact    your    local    Galaxy    administrator    to    restore    the    account.'
        if (trans.app.config.error_email_to is not None):
            message += ('    Contact:    %s' % trans.app.config.error_email_to)
    elif user.external:
        message = 'This    account    was    created    for    use    with    an    external    authentication    method,    contact    your    local    Galaxy    administrator    to    activate    it.'
        if (trans.app.config.error_email_to is not None):
            message += ('    Contact:    %s' % trans.app.config.error_email_to)
    elif (not trans.app.auth_manager.check_password(user, password)):
        message = 'Invalid    password'
    elif (trans.app.config.user_activation_on and (not user.active)):
        if (trans.app.config.activation_grace_period != 0):
            if self.is_outside_grace_period(trans, user.create_time):
                (message, status) = self.resend_verification_email(trans, user.email, user.username)
            else:
                (message, success, status) = self.proceed_login(trans, user, redirect)
        else:
            (message, status) = self.resend_verification_email(trans, user.email, user.username)
    else:
        pw_expires = trans.app.config.password_expiration_period
        if (pw_expires and (user.last_password_change < (datetime.today() - pw_expires))):
            trans.response.send_redirect(web.url_for(controller='user', action='change_password', message='Your    password    has    expired.    Please    change    it    to    access    Galaxy.', redirect_home=True, status='error'))
        (message, success, status) = self.proceed_login(trans, user, redirect)
        if (pw_expires and (user.last_password_change < (datetime.today() - timedelta(days=(pw_expires.days / 10))))):
            expiredate = ((datetime.today() - user.last_password_change) + pw_expires)
            message = ('You    are    now    logged    in    as    %s.    Your    password    will    expire    in    %s    days.<br>You    can    <a    target="_top"    href="%s">go    back    to    the    page    you    were    visiting</a>    or    <a    target="_top"    href="%s">go    to    the    home    page</a>.' % (expiredate.days, user.email, redirect, url_for('/')))
            status = 'warning'
    return (message, status, user, success)
