Subversion Repositories eFlore/Applications.cel

Rev

Rev 3213 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3213 Rev 3217
Line 1... Line 1...
1
// configuration
1
// configuration
2
// prod :
2
// prod :
3
/*var urlRacine =
-
 
4
  'https://www.tela-botanica.org',
3
/*var urlRacine = 'https://www.tela-botanica.org',
5
  config = {
4
    config = {
6
    prod: {
5
      prod: {
7
      urlWidgetNavigation : urlRacine + '/widget:cel:saisie2',
6
        urlWidgetNavigation : urlRacine + '/widget:cel:saisie2',
8
      urlBaseAuth : 'https://www.tela-botanica.org/service:annuaire:auth'
7
        urlBaseAuth : 'https://www.tela-botanica.org/service:annuaire:auth'
9
    }
8
      }
10
  }
9
    };
11
  */
10
  */
12
// beta (test) :
11
// beta (test) :
13
var urlRacine =
-
 
14
  'https://beta.tela-botanica.org',
12
var urlRacine = 'https://beta.tela-botanica.org',
15
  config = {
13
    config = {
16
    test: {
14
      test: {
17
      urlWidgetNavigation : urlRacine + '/widget:cel:saisie2',
15
        urlWidgetNavigation : urlRacine + '/widget:cel:saisie2',
18
      urlBaseAuth : 'https://beta.tela-botanica.org/service:annuaire:auth'
16
        urlBaseAuth : 'https://beta.tela-botanica.org/service:annuaire:auth'
19
    }
17
      }
20
  }
18
    };
Line 21... Line 19...
21
 
19
 
22
// dev (localhost):
-
 
23
/*var urlRacine =
20
// dev (localhost):
24
  'http://localhost',
21
/*var urlRacine = 'http://localhost',
25
  config = {
22
    config = {
26
    local: {
23
      local: {
27
      urlWidgetNavigation : urlRacine + '/widget:cel:saisie2',
24
        urlWidgetNavigation : urlRacine + '/widget:cel:saisie2',
28
      urlBaseAuth : 'https://api.tela-botanica.test/service:annuaire:auth'
25
        urlBaseAuth : 'https://api.tela-botanica.test/service:annuaire:auth'
29
    }
26
      }
30
  }
27
    };
31
*/
28
*/
32
/**
29
/**
33
 * Charge la barre de navigation depuis le widget:reseau:navigation dans un <div id="tb-navigation"> , s'il existe
30
 * Charge la barre de navigation depuis le widget:reseau:navigation dans un <div id="tb-navigation"> , s'il existe
34
 * dans la page appelante.
31
 * dans la page appelante.
Line 52... Line 49...
52
        courant   = $div.data( 'courant' ),
49
        courant   = $div.data( 'courant' ),
53
        mode      = $div.data( 'mode' ) || 'prod',
50
        mode      = $div.data( 'mode' ) || 'prod',
54
        contenu   = $div.html();
51
        contenu   = $div.html();
Line 55... Line 52...
55
 
52
 
-
 
53
    // chargement de la barre
56
    // chargement de la barre
54
    var urlBaseAuth = config[mode]['urlBaseAuth'];
-
 
55
    var urlBarreNavigation = config[mode]['urlWidgetNavigation'];
Line 57... Line -...
57
    var urlBarreNavigation = config[mode]['urlWidgetNavigation'];
-
 
58
 
-
 
59
    if ( squelette ) {
-
 
60
      urlBarreNavigation += '?squelette=' + squelette;
-
 
61
    }
56
    urlBarreNavigation += ( squelette ) ? '?squelette=' + squelette : '';
62
 
57
 
63
    htmlBarre = $.ajax({
58
    $.ajax({
64
      url: urlBarreNavigation,
59
      url: urlBarreNavigation,
65
      type: 'get',
60
      type: 'get',
66
      success: function( data ) {
61
      success: function( data ) {
67
        // remplacement de la zone contenu-source
62
        // remplacement de la zone contenu-source
68
        var $zoneSource = $div.find( '#contenu-source' );
63
        var $zoneSource = $div.find( '#contenu-source' );
69
        if ( $zoneSource ) {
64
        if ( $zoneSource ) {
70
          $zoneSource.replaceWith( contenu );
-
 
71
          // Chargement des informations de connexion SSO
65
          $zoneSource.replaceWith( contenu );
72
          var urlBaseAuth = config[mode]['urlBaseAuth'];
66
          // Chargement des informations de connexion SSO
73
          chargerStatutSSO( config[mode]['urlBaseAuth'] );
67
          chargerStatutSSO( urlBaseAuth );
74
        }
68
        }
75
      },
69
      },
76
      error: function() {
70
      error: function() {
77
        $div.html( 'Erreur: impossible de charger la barre de navigation' );
71
        $div.html( 'Erreur: impossible de charger la barre de navigation' );
-
 
72
      }
-
 
73
    });
-
 
74
 
-
 
75
    $( '#deconnexion a' ).click( function() {
-
 
76
      deconnecterUtilisateur( urlBaseAuth );
78
      }
77
    });
79
    });
78
 
Line 80... Line 79...
80
  }
79
  }
81
});
80
});
82
 
81
 
83
/**
82
/**
84
 * Modifie les liens des boutons de connexion / déconnexion SSO pour prendre ne compte
83
 * Modifie les liens des boutons de connexion / déconnexion SSO pour prendre ne compte
85
 * l'adresse exacte de la page d'origine (si on fait ça en PHP, on obtient l'URL du
84
 * l'adresse exacte de la page d'origine (si on fait ça en PHP, on obtient l'URL du
86
 * widget de barre de navigation et pas de la page appelante)
85
 * widget de barre de navigation et pas de la page appelante)
-
 
86
 */
87
 */
87
function definirPageOrigineDansLiens() {
88
function definirPageOrigineDansLiens() {
88
  var page = window.location.href;
89
  var page = window.location.href;
89
 
Line 90... Line 90...
90
  $( '#bouton-connexion a' ).attr( 'href', $( '#bouton-connexion a' ).attr( 'href' ) + page );
90
  $( '#bouton-connexion a' ).attr( 'href', $( '#bouton-connexion a' ).attr( 'href' ) + page );
91
  $( '#deconnexion a' ).attr( 'href', $( '#deconnexion a' ).attr( 'href' ) + page );
91
  $( '#deconnexion a' ).attr( 'href', $( '#deconnexion a' ).attr( 'href' ) + page );
92
}
92
}
93
 
93
 
94
/**
94
/**
95
 * Interroge le SSO pour connaître le statut de l'utilisateur, et change le menu
95
 * Interroge le SSO pour connaître le statut de l'utilisateur, et change le menu
-
 
96
 * à droite de la barre en fonction
-
 
97
 */
-
 
98
function chargerStatutSSO( urlBaseAuth ) {
-
 
99
  var urlAuth = urlBaseAuth + '/identite';
-
 
100
 
-
 
101
  if( 'local' !== $( '#tb-navigation' ).data( 'mode' ) ) {
-
 
102
 
-
 
103
    connexion( urlAuth, true );
-
 
104
 
-
 
105
  } else {
-
 
106
    urlAuth ='http://localhost/widget:cel:modules/saisie2/test-token.json';
-
 
107
    $( '#connexion' ).click( function() {
-
 
108
      connexion( urlAuth, true );
-
 
109
      return false;
-
 
110
    });
-
 
111
  }
-
 
112
}
-
 
113
 
-
 
114
/**
-
 
115
 * Déconnecte l'utilisateur du SSO
-
 
116
 */
-
 
117
function deconnecterUtilisateur( urlBaseAuth ) {
-
 
118
  if( 'local' === $( '#tb-navigation' ).data( 'mode' ) ) {
-
 
119
    definirUtilisateur( undefined );
-
 
120
    window.location.reload();
-
 
121
    return;
-
 
122
  }
-
 
123
 
-
 
124
  var urlAuth = urlBaseAuth + '/deconnexion';
-
 
125
 
96
 * à droite de la barre en fonction
126
  connexion( urlAuth, false );
97
 */
127
}
98
function chargerStatutSSO( urlBaseAuth ) {
128
 
99
  var urlAuth = urlBaseAuth + '/identite';
129
function connexion( urlAuth, connexionOnOff ) {
100
  $.ajax({
130
  $.ajax({
101
    url: urlAuth,
131
    url: urlAuth,
102
    type: "GET",
132
    type: "GET",
-
 
133
    dataType: 'json',
103
    dataType: 'json',
134
    xhrFields: {
104
    xhrFields: {
135
      withCredentials: true
105
      withCredentials: true
136
    }
-
 
137
  })
106
    }
138
  .done( function( data ) {
107
  }).done( function( data ) {
139
    // connecté
-
 
140
    definirUtilisateur( data.token );
-
 
141
    if( !connexionOnOff ) {
-
 
142
      window.location.reload();
-
 
143
    }
-
 
144
  })
-
 
145
  .fail( function( error ) {
108
    // connecté
146
    // @TODO gérer l'affichage de l'erreur, mais pas facile à placer
109
    if ( undefined !== data.token ) {
147
    // dans l'interface actuelle sans que ce soit moche
Line -... Line 148...
-
 
148
    //afficherErreurServeur();
110
      definirUtilisateur( data.token );
149
    console.log( error );
111
    }
150
  });
-
 
151
}
112
  });
152
 
-
 
153
 
113
}
154
function definirUtilisateur( jeton ) {
-
 
155
  var idUtilisateur = '',
114
 
156
      nomComplet    = '',
-
 
157
      courriel      = '',
-
 
158
      prenom        = '',
-
 
159
      nom           = '';
-
 
160
 
-
 
161
  // affichage
-
 
162
  if ( undefined !== jeton ) {
-
 
163
    // décodage jeton
-
 
164
    var jetonDecode = decoderJeton( jeton );
-
 
165
    // console.log(jetonDecode);
-
 
166
 
-
 
167
    idUtilisateur = jetonDecode.id;
-
 
168
    nomComplet    = jetonDecode.intitule;
-
 
169
    courriel      = jetonDecode.sub;
-
 
170
    prenom        = jetonDecode.prenom;
-
 
171
    nom           = jetonDecode.nom;
-
 
172
 
-
 
173
    $( '#courriel, #courriel_confirmation' ).attr( 'disabled', 'disabled' );
-
 
174
    $( '#prenom' ).attr( 'disabled', 'disabled' );
-
 
175
    $( '#nom' ).attr( 'disabled', 'disabled' );
115
function definirUtilisateur( jeton ) {
176
    $( '#bouton-connexion, #creation-compte' ).addClass( 'hidden' );
116
  // décodage jeton
-
 
117
  var jetonDecode = decoderJeton( jeton );
-
 
118
  // console.log(jetonDecode);
177
    $( '#utilisateur-connecte, #anonyme, #zone-courriel-confirmation, #zone-prenom-nom' ).removeClass( 'hidden' );
119
  // affichage
178
    $( '#date-releve' ).focus();
120
  $( '.warning' ).remove();
179
  }
121
  $( '#bouton-connexion, #creation-compte' ).addClass( 'hidden' );
180
 
122
  $( '#utilisateur-connecte, #anonyme, #zone-courriel-confirmation, #zone-prenom-nom' ).removeClass( 'hidden' );
-
 
123
  $( '#nom-complet' ).html( jetonDecode.intitule );
181
  $( '.warning' ).remove();
124
  $( '#courriel, #courriel_confirmation' ).val( jetonDecode.sub ).attr( 'disabled', 'disabled' );
182
  $( '#nom-complet' ).html( nomComplet );
Line 125... Line 183...
125
  $( '#id_utilisateur' ).val( jetonDecode.id );
183
  $( '#courriel, #courriel_confirmation' ).val( courriel );
126
  $( '#prenom' ).val( jetonDecode.prenom ).attr( 'disabled', 'disabled' );
184
  $( '#id_utilisateur' ).val( idUtilisateur );
127
  $( '#nom' ).val( jetonDecode.nom ).attr( 'disabled', 'disabled' );
185
  $( '#prenom' ).val( prenom );
128
  $( '#date-releve' ).focus();
-
 
129
}
-
 
130
 
186
  $( '#nom' ).val( nom );
131
/**
187
}
132
 * Décodage à l'arrache d'un jeton JWT, ATTENTION CONSIDERE QUE LE
188
 
133
 * JETON EST VALIDE, ne pas décoder n'importe quoi - pas trouvé de lib simple
189
/**
134
 * Si pb de cross-browser, tenter ceci : https://code.google.com/p/javascriptbase64/
-
 
135
 * ou ceci : https://code.google.com/p/crypto-js
190
 * Décodage à l'arrache d'un jeton JWT, ATTENTION CONSIDERE QUE LE
136
 */
191
 * JETON EST VALIDE, ne pas décoder n'importe quoi - pas trouvé de lib simple
Line 137... Line 192...
137
function decoderJeton( jeton ) {
192
 */
138
  parts   = jeton.split( '.' );
193
function decoderJeton( jeton ) {
-
 
194
  parts = jeton.split( '.' );
-
 
195
  payload = parts[1];
-
 
196
  payload = b64d( payload );
-
 
197
  payload = JSON.parse( payload, true );
-
 
198
 
-
 
199
  return payload;
-
 
200
}
-
 
201
 
-
 
202
/**
-
 
203
 * Décodage "url-safe" des chaînes base64 retournées par le SSO (lib jwt)
-
 
204
 */
-
 
205
function b64d( input ) {
-
 
206
  var remainder = input.length % 4;
-
 
207
 
-
 
208
  if ( 0 !== remainder ) {
-
 
209
    var padlen = 4 - remainder;
-
 
210
 
-
 
211
    for ( var i = 0; i < padlen; i++ ) {
-
 
212
      input += '=';
-
 
213
    }