Subversion Repositories Applications.reseau

Rev

Rev 68 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
56 mathias 1
$(document).ready(function() {
2
	// infobulles
3
	$('[data-toggle="tooltip"]').tooltip();
4
 
5
	// trucage ordre des champs pour passer du login au mdp sans passer
6
	// par le lien "oublié ?" => pas standard, c'est mal - @TODO faire mieux
7
	// Ordre : login => mdp => oublie => s'inscrire
8
	$('#login').keypress(function(e) {
9
		if(e.keyCode == 9) {
10
			if(! e.shiftKey) {
11
				$('#mdp').focus();
12
				return false;
13
			}
14
		}
15
	});
16
	$('#mdp').keypress(function(e) {
17
		if(e.keyCode == 9) {
18
			if(e.shiftKey) {
19
				$('#login').focus();
20
			} else {
21
				$('#mdp-oublie').focus();
22
			}
23
			return false;
24
		}
25
	});
26
	$('#mdp-oublie').keypress(function(e) {
27
		if(e.keyCode == 9) {
28
			if(e.shiftKey) {
29
				$('#mdp').focus();
30
			} else {
31
				$('#lien-inscription').focus();
32
			}
33
		}
34
		return false;
35
	});
36
	$('#lien-inscription').keypress(function(e) {
37
		if(e.keyCode == 9) {
38
			if(e.shiftKey) {
39
				$('#mdp-oublie').focus();
40
				return false;
41
			}
42
		}
43
	});
44
 
45
	// partenaire avec lequel se connecter
46
	$('#liste-fournisseurs ul li a').click(function() {
47
		$('#liste-fournisseurs ul li a').attr('data-focus', 'false');
48
		$(this).attr('data-focus', 'true');
49
		var nomPartenaire = $(this).data('nom-partenaire');
50
		$('#nom-partenaire').html(nomPartenaire);
51
	});
52
 
53
	// Connexion au SSO
54
	$('#formulaire-identification').submit(function() {
55
		var login = $('#login').val(),
56
			mdp = $('#mdp').val();
57
		if (login == '' || mdp == '') {
58
			alert('Veuillez entrer votre login et votre mot de passe');
59
		} else {
60
			var urlAuth = baseUrlAuth + '/connexion?login=' + login + '&password=' + mdp;
61
			$.ajax({
62
			    url: urlAuth,
63
			    type: "GET",
64
			    dataType: 'json',
65
			    xhrFields: {
66
			         withCredentials: true
67
			    }
68
			})
69
			.done(function(data) {
70
				definirUtilisateur(data.token);
71
				afficherPanneauBienvenue();
72
				masquerErreurs();
73
			})
74
			.fail(function(error) {
75
				afficherErreurLogin();
76
			});
77
		}
78
		return false;
79
	});
80
 
81
	// Déconnexion du SSO
82
	$('#deconnexion').click(function() {
83
		var urlAuth = baseUrlAuth + '/deconnexion';
84
		$.ajax({
85
		    url: urlAuth,
86
		    type: "GET",
87
		    dataType: 'json',
88
		    xhrFields: {
89
		         withCredentials: true
90
		    }
91
		})
92
		.done(function(data) {
93
			definirUtilisateur();
94
			afficherPanneauIdentification();
95
			masquerErreurs();
96
		})
97
		.fail(function(error) {
98
			// @TODO gérer l'affichage de l'erreur, mais pas facile à placer
99
			// dans l'interface actuelle sans que ce soit moche
100
			//afficherErreurServeur();
101
		});
102
		return false;
103
	});
104
 
105
	// vérification de l'état au chargement
106
	var urlAuth = baseUrlAuth + '/identite';
107
	$.ajax({
108
	    url: urlAuth,
109
	    type: "GET",
110
	    dataType: 'json',
111
	    xhrFields: {
112
	         withCredentials: true
113
	    }
114
	}).done(function(data) {
115
		// connecté
116
		definirUtilisateur(data.token);
117
		afficherPanneauBienvenue();
118
	})
119
	.fail(function(error) {
120
		// non connecté
121
		afficherPanneauIdentification();
122
	});
123
 
124
	function definirUtilisateur(jeton) {
125
		var nomComplet = '';
126
		if (jeton != undefined) {
127
			// décodage jeton
128
			var jetonDecode = decoderJeton(jeton);
129
			nomComplet = jetonDecode.intitule;
130
		}
131
		// affichage
132
		$('#nom-complet').html(nomComplet);
133
	}
134
 
135
    /**
136
     * Décodage à l'arrache d'un jeton JWT, ATTENTION CONSIDERE QUE LE
137
     * JETON EST VALIDE, ne pas décoder n'importe quoi - pas trouvé de lib simple
138
     * Si pb de cross-browser, tenter ceci : https://code.google.com/p/javascriptbase64/
139
     * ou ceci : https://code.google.com/p/crypto-js
140
     */
141
    function decoderJeton(jeton) {
142
        parts = jeton.split('.');
143
        payload = parts[1];
144
        payload = atob(payload);
145
        payload = JSON.parse(payload, true);
146
 
147
        return payload;
148
    }
149
 
150
	function afficherPanneauIdentification() {
151
		$('#panneau-identification').show();
152
		$('#panneau-bienvenue').hide();
153
	}
154
 
155
	function afficherPanneauBienvenue() {
156
		$('#panneau-identification').hide();
157
		$('#panneau-bienvenue').show();
158
	}
159
 
160
	function afficherErreurLogin() {
161
		$('#info-erreur-login').show();
162
	}
163
 
164
	function afficherErreurServeur() {
165
		$('#info-erreur-serveur').show();
166
	}
167
 
168
	function masquerErreurs() {
169
		$('.info-erreur').hide();
170
	}
171
});