переменная ответа ajax возвращает undefined в symfony


на странице входа я хочу, чтобы нажатие кнопки submit проверяло на сервере LDAP введенные данные, и если все в порядке, то перейдите к стандартному submit формы.

В событии нажатия кнопки у меня есть следующий код в javascript:

$('#btnLogin').click(function(){
    if (frmLogin.valid()){
        var user = $('#frmLoginUser').val();
        var pass = $('#frmLoginPass').val();  
        var sinLDAP = false;

        var pos1 = user.indexOf('DOMINIO\\');
        if(pos1==-1){
            user = 'DOMINIO'+'\\'+user;
        }

        console.log(user);
        console.log(pass);
        console.log(sinLDAP);

        $.ajax({
            type:"POST",
            data: {'user':user,'pass':pass, 'sinLDAP':sinLDAP},
            url:"{{ path('check_ldap') }}"
        }).done(function(respuesta){
            console.log(respuesta.result);
            console.log(respuesta.mensaje);
            if (respuesta.result==9){
                notificaciones('info','Sin validar LDAP','')
            }
            if (respuesta.mensaje==1 && respuesta.result==-1)
                notificaciones(respuesta.tipo_msg,respuesta.msg,respuesta.title_msg);
            else{
                notificaciones('success','Se logueo correctamente','')
            }
        });
    }
});

в контроллере default (php) у меня есть следующее:

public function checkLdapAction(Request $request)
{
    $user = $request->request->get('user');
    $pass = $request->request->get('pass');
    $ldap = $request->request->get('sinLDAP');

    if ($sinLdap=='true'){
        //********************************************************
        return new JsonResponse(array('mensaje' => '1',
            'result' => '-1',
            'tipo_msg' => 'info',
            'title_msg' => '',
            'msg' => 'No se esta validando contra el server LDAP'));
        //********************************************************
    }

    $ldapServer = 'nombre.server.ldap';
    $ldapServerIP = 'XXX.XXX.XXX.XXX';
    $ldapServerPort = 'XXXX';

    $hostip = @gethostbyname($ldapServer);
    $errorConx = ($hostip == $ldapServer || $hostip!=$ldapServerIP);

    if ($errorConx){
        //********************************************************
        return new JsonResponse(array('mensaje' => '1',
            'result' => '-1',
            'tipo_msg' => 'error',
            'title_msg' => 'Error en Conexión',
            'msg' => 'Error de conexión con el servidor LDAP "'.$ldapServer.'"'));
        //********************************************************
    }

    $ldap_conn = ldap_connect($ldapServer,$ldapServerPort);
    $binding = @ldap_bind($ldap_conn, $user, $pass);

    if ($binding){
        ldap_close($ldap_conn);
        //********************************************************
        return new JsonResponse(array('mensaje' => '0','result' => '1'));
        //********************************************************
    }
    else{
        ldap_close($ldap_conn);
        //********************************************************
        return new JsonResponse(array('mensaje' => '1',
            'result' => '-1',
            'tipo_msg' => 'warning',
            'title_msg' => 'Datos Incorrectos',
            'msg' => 'Usuario o contraseña incorrecto'));
        //********************************************************
    }
}
Author: rlazom, 2016-05-11

1 answers

Окончательное решение проблемы:

В app / config/security. yml в разделе access_control добавьте путь, указывающий на метод контроллера, чтобы пользователь мог получить к нему доступ перед входом.

access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/check_ldap, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/*, roles: IS_AUTHENTICATED_FULLY }

Остальная часть кода, показанного "выше", ОК.

 1
Author: rlazom, 2016-05-11 19:02:48