Subversion Repositories Applications.reseau

Rev

Rev 68 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

$(document).ready(function() {
        // infobulles
        $('[data-toggle="tooltip"]').tooltip();

        // trucage ordre des champs pour passer du login au mdp sans passer
        // par le lien "oublié ?" => pas standard, c'est mal - @TODO faire mieux
        // Ordre : login => mdp => oublie => s'inscrire
        $('#login').keypress(function(e) {
                if(e.keyCode == 9) {
                        if(! e.shiftKey) {
                                $('#mdp').focus();
                                return false;
                        }
                }
        });
        $('#mdp').keypress(function(e) {
                if(e.keyCode == 9) {
                        if(e.shiftKey) {
                                $('#login').focus();
                        } else {
                                $('#mdp-oublie').focus();
                        }
                        return false;
                }
        });
        $('#mdp-oublie').keypress(function(e) {
                if(e.keyCode == 9) {
                        if(e.shiftKey) {
                                $('#mdp').focus();
                        } else {
                                $('#lien-inscription').focus();
                        }
                }
                return false;
        });
        $('#lien-inscription').keypress(function(e) {
                if(e.keyCode == 9) {
                        if(e.shiftKey) {
                                $('#mdp-oublie').focus();
                                return false;
                        }
                }
        });

        // partenaire avec lequel se connecter
        $('#liste-fournisseurs ul li a').click(function() {
                $('#liste-fournisseurs ul li a').attr('data-focus', 'false');
                $(this).attr('data-focus', 'true');
                var nomPartenaire = $(this).data('nom-partenaire');
                $('#nom-partenaire').html(nomPartenaire);
        });

        // Connexion au SSO
        $('#formulaire-identification').submit(function() {
                var login = $('#login').val(),
                        mdp = $('#mdp').val();
                if (login == '' || mdp == '') {
                        alert('Veuillez entrer votre login et votre mot de passe');
                } else {
                        var urlAuth = baseUrlAuth + '/connexion?login=' + login + '&password=' + mdp;
                        $.ajax({
                            url: urlAuth,
                            type: "GET",
                            dataType: 'json',
                            xhrFields: {
                                 withCredentials: true
                            }
                        })
                        .done(function(data) {
                                definirUtilisateur(data.token);
                                afficherPanneauBienvenue();
                                masquerErreurs();
                        })
                        .fail(function(error) {
                                afficherErreurLogin();
                        });
                }
                return false;
        });

        // Déconnexion du SSO
        $('#deconnexion').click(function() {
                var urlAuth = baseUrlAuth + '/deconnexion';
                $.ajax({
                    url: urlAuth,
                    type: "GET",
                    dataType: 'json',
                    xhrFields: {
                         withCredentials: true
                    }
                })
                .done(function(data) {
                        definirUtilisateur();
                        afficherPanneauIdentification();
                        masquerErreurs();
                })
                .fail(function(error) {
                        // @TODO gérer l'affichage de l'erreur, mais pas facile à placer
                        // dans l'interface actuelle sans que ce soit moche
                        //afficherErreurServeur();
                });
                return false;
        });

        // vérification de l'état au chargement
        var urlAuth = baseUrlAuth + '/identite';
        $.ajax({
            url: urlAuth,
            type: "GET",
            dataType: 'json',
            xhrFields: {
                 withCredentials: true
            }
        }).done(function(data) {
                // connecté
                definirUtilisateur(data.token);
                afficherPanneauBienvenue();
        })
        .fail(function(error) {
                // non connecté
                afficherPanneauIdentification();
        });

        function definirUtilisateur(jeton) {
                var nomComplet = '';
                if (jeton != undefined) {
                        // décodage jeton
                        var jetonDecode = decoderJeton(jeton);
                        nomComplet = jetonDecode.intitule;
                }
                // affichage
                $('#nom-complet').html(nomComplet);
        }

    /**
     * Décodage à l'arrache d'un jeton JWT, ATTENTION CONSIDERE QUE LE
     * JETON EST VALIDE, ne pas décoder n'importe quoi - pas trouvé de lib simple
     * Si pb de cross-browser, tenter ceci : https://code.google.com/p/javascriptbase64/
     * ou ceci : https://code.google.com/p/crypto-js
     */
    function decoderJeton(jeton) {
        parts = jeton.split('.');
        payload = parts[1];
        payload = atob(payload);
        payload = JSON.parse(payload, true);

        return payload;
    }

        function afficherPanneauIdentification() {
                $('#panneau-identification').show();
                $('#panneau-bienvenue').hide();
        }

        function afficherPanneauBienvenue() {
                $('#panneau-identification').hide();
                $('#panneau-bienvenue').show();
        }

        function afficherErreurLogin() {
                $('#info-erreur-login').show();
        }

        function afficherErreurServeur() {
                $('#info-erreur-serveur').show();
        }

        function masquerErreurs() {
                $('.info-erreur').hide();
        }
});