Subversion Repositories Applications.reseau

Rev

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