Subversion Repositories Applications.reseau

Rev

Rev 68 | Rev 78 | Go to most recent revision | Details | Compare with Previous | 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() {
68 mathias 55
		connecterUtilisateur();
56 mathias 56
		return false;
57
	});
58
 
59
	// Déconnexion du SSO
60
	$('#deconnexion').click(function() {
68 mathias 61
		deconnecterUtilisateur();
62
		return false;
63
	});
64
 
65
	// vérification de l'état au chargement
66
	var urlAuth = baseUrlAuth + '/identite';
67
	$.ajax({
68
	    url: urlAuth,
69
	    type: "GET",
70
	    dataType: 'json',
71
	    xhrFields: {
72
	         withCredentials: true
73
	    }
74
	}).done(function(data) {
75
		// connecté
76
		definirUtilisateur(data.token);
77
		afficherPanneauBienvenue();
78
	})
79
	.fail(function(error) {
80
		// non connecté
81
		afficherPanneauIdentification();
82
	});
83
 
84
	// exécuter une action si définie (par ex: déconnexion)
85
	executerAction();
86
});
87
 
88
/**
89
 * Connecte l'utilisateur au SSO
90
 */
91
function connecterUtilisateur() {
92
	var login = $('#login').val(),
93
		mdp = $('#mdp').val();
94
	if (login == '' || mdp == '') {
95
		alert('Veuillez entrer votre login et votre mot de passe');
96
	} else {
97
		var urlAuth = baseUrlAuth + '/connexion?login=' + login + '&password=' + mdp;
56 mathias 98
		$.ajax({
99
		    url: urlAuth,
100
		    type: "GET",
101
		    dataType: 'json',
102
		    xhrFields: {
103
		         withCredentials: true
104
		    }
105
		})
106
		.done(function(data) {
68 mathias 107
			rediriger();
108
			definirUtilisateur(data.token);
109
			afficherPanneauBienvenue();
56 mathias 110
			masquerErreurs();
111
		})
112
		.fail(function(error) {
68 mathias 113
			afficherErreurLogin();
56 mathias 114
		});
68 mathias 115
	}
116
}
56 mathias 117
 
68 mathias 118
/**
119
 * Déconnecte l'utilisateur du SSO
120
 */
121
function deconnecterUtilisateur() {
122
	var urlAuth = baseUrlAuth + '/deconnexion';
56 mathias 123
	$.ajax({
124
	    url: urlAuth,
125
	    type: "GET",
126
	    dataType: 'json',
127
	    xhrFields: {
128
	         withCredentials: true
129
	    }
130
	})
68 mathias 131
	.done(function(data) {
132
		rediriger();
133
		definirUtilisateur();
134
		afficherPanneauIdentification();
135
		masquerErreurs();
136
	})
56 mathias 137
	.fail(function(error) {
68 mathias 138
		// @TODO gérer l'affichage de l'erreur, mais pas facile à placer
139
		// dans l'interface actuelle sans que ce soit moche
140
		//afficherErreurServeur();
56 mathias 141
	});
68 mathias 142
}
56 mathias 143
 
68 mathias 144
/**
145
 * Si la variable "action" est définie (provenant du paramètre GET "origine"),
146
 * exécute l'action associée (par ex: déconnexion)
147
 */
148
function executerAction() {
149
	switch (action) {
150
		case "deconnexion":
151
			deconnecterUtilisateur();
152
			break;
153
		default :
154
			// on ne fait rien
56 mathias 155
	}
68 mathias 156
}
56 mathias 157
 
68 mathias 158
function definirUtilisateur(jeton) {
159
	var nomComplet = '';
160
	if (jeton != undefined) {
161
		// décodage jeton
162
		var jetonDecode = decoderJeton(jeton);
163
		nomComplet = jetonDecode.intitule;
72 mathias 164
		email = jetonDecode.sub;
68 mathias 165
	}
166
	// affichage
167
	$('#nom-complet').html(nomComplet);
72 mathias 168
	$('#nom-complet').attr('title', email);
68 mathias 169
}
56 mathias 170
 
68 mathias 171
/**
172
 * Décodage à l'arrache d'un jeton JWT, ATTENTION CONSIDERE QUE LE
173
 * JETON EST VALIDE, ne pas décoder n'importe quoi - pas trouvé de lib simple
174
 * Si pb de cross-browser, tenter ceci : https://code.google.com/p/javascriptbase64/
175
 * ou ceci : https://code.google.com/p/crypto-js
176
 */
177
function decoderJeton(jeton) {
178
	parts = jeton.split('.');
179
	payload = parts[1];
180
	payload = atob(payload);
181
	payload = JSON.parse(payload, true);
56 mathias 182
 
68 mathias 183
	return payload;
184
}
56 mathias 185
 
68 mathias 186
/**
187
 * Si la variable "origine" est définie (provenant du paramètre GET "origine"),
188
 * redirige l'utilisateur vers cette page
189
 */
190
function rediriger() {
191
	if (origine != '') {
192
		window.location.href = origine;
56 mathias 193
	}
68 mathias 194
}
56 mathias 195
 
68 mathias 196
function afficherPanneauIdentification() {
197
	$('#panneau-identification').show();
198
	$('#panneau-bienvenue').hide();
199
}
56 mathias 200
 
68 mathias 201
function afficherPanneauBienvenue() {
202
	$('#panneau-identification').hide();
203
	$('#panneau-bienvenue').show();
204
}
56 mathias 205
 
68 mathias 206
function afficherErreurLogin() {
207
	$('#info-erreur-login').show();
208
}
209
 
210
function afficherErreurServeur() {
211
	$('#info-erreur-serveur').show();
212
}
213
 
214
function masquerErreurs() {
215
	$('.info-erreur').hide();
216
}