includes/clientside/static/login.js
changeset 811 5c807fe77020
parent 794 720516162012
child 816 44b8446533e1
equal deleted inserted replaced
810:7fd2b8a58ae4 811:5c807fe77020
   391 window.ajaxLoginProcessResponse = function(response)
   391 window.ajaxLoginProcessResponse = function(response)
   392 {
   392 {
   393   // Did the server send a plaintext error?
   393   // Did the server send a plaintext error?
   394   if ( response.mode == 'error' )
   394   if ( response.mode == 'error' )
   395   {
   395   {
   396     logindata.mb_object.destroy();
   396     if ( logindata.mb_object )
   397     var error_msg = $lang.get('user_' + ( response.error.toLowerCase() ));
   397     {
   398     new MessageBox(MB_ICONSTOP | MB_OK, $lang.get('user_err_login_generic_title'), error_msg);
   398       logindata.mb_object.destroy();
       
   399       var error_msg = $lang.get('user_' + ( response.error.toLowerCase() ));
       
   400       new MessageBox(MB_ICONSTOP | MB_OK, $lang.get('user_err_login_generic_title'), error_msg);
       
   401     }
       
   402     else
       
   403     {
       
   404       alert(response.error);
       
   405     }
   399     return false;
   406     return false;
   400   }
   407   }
   401   // Main mode switch
   408   // Main mode switch
   402   switch ( response.mode )
   409   switch ( response.mode )
   403   {
   410   {
   444             ajaxLoginBuildForm(response.respawn_info);
   451             ajaxLoginBuildForm(response.respawn_info);
   445             // don't show an error here, just silently respawn
   452             // don't show an error here, just silently respawn
   446           }, 2500);
   453           }, 2500);
   447       }
   454       }
   448       break;
   455       break;
       
   456     case 'logout_success':
       
   457       if ( ENANO_SID )
       
   458       {
       
   459         ajaxLoginReplaceSIDInline(false, ENANO_SID, USER_LEVEL_MEMBER);
       
   460       }
       
   461       break;
   449     case 'noop':
   462     case 'noop':
   450       break;
   463       break;
   451   }
   464   }
   452 }
   465 }
   453 
   466 
  1157   }
  1170   }
  1158   var loc = makeUrlNS('Special', 'Administration', 'module=' + namespace_list['Admin'] + 'UserManager&src=get&user=' + ajaxEscape(username));
  1171   var loc = makeUrlNS('Special', 'Administration', 'module=' + namespace_list['Admin'] + 'UserManager&src=get&user=' + ajaxEscape(username));
  1159   window.location = loc;
  1172   window.location = loc;
  1160 }
  1173 }
  1161 
  1174 
  1162 window.ajaxDynamicReauth = function(adminpage)
  1175 window.ajaxDynamicReauth = function(adminpage, level)
  1163 {
  1176 {
  1164   var old_sid = ENANO_SID;
  1177   var old_sid = ENANO_SID;
  1165   var targetpage = adminpage;
  1178   var targetpage = adminpage;
       
  1179   if ( !level )
       
  1180   {
       
  1181     level = USER_LEVEL_ADMIN;
       
  1182   }
  1166   ajaxLogonInit(function(k)
  1183   ajaxLogonInit(function(k)
  1167     {
  1184     {
  1168       var body = document.getElementsByTagName('body')[0];
  1185       ajaxLoginReplaceSIDInline(k, old_sid, level);
  1169       var replace = new RegExp(old_sid, 'g');
       
  1170       body.innerHTML = body.innerHTML.replace(replace, k);
       
  1171       ENANO_SID = k;
       
  1172       mb_current_obj.destroy();
  1186       mb_current_obj.destroy();
  1173       console.debug(targetpage);
  1187       console.debug(targetpage);
  1174       if ( typeof(targetpage) == 'string' )
  1188       if ( typeof(targetpage) == 'string' )
  1175       {
  1189       {
  1176         ajaxPage(targetpage);
  1190         ajaxPage(targetpage);
  1177       }
  1191       }
  1178       else if ( typeof(targetpage) == 'function' )
  1192       else if ( typeof(targetpage) == 'function' )
  1179       {
  1193       {
  1180         targetpage();
  1194         targetpage();
  1181       }
  1195       }
  1182     }, USER_LEVEL_ADMIN);
  1196     }, level);
  1183   ajaxLoginShowFriendlyError({
  1197   ajaxLoginShowFriendlyError({
  1184       error_code: 'admin_session_timed_out',
  1198       error_code: 'admin_session_timed_out',
  1185       respawn_info: {}
  1199       respawn_info: {}
  1186   });
  1200   });
  1187 }
  1201 }
  1188 
  1202 
  1189 window.ajaxRenewSession = function()
  1203 window.ajaxRenewSession = function()
  1190 {
  1204 {
  1191   ajaxDynamicReauth(false);
  1205   ajaxDynamicReauth(false);
  1192 }
  1206 }
       
  1207 
       
  1208 window.ajaxTrashElevSession = function()
       
  1209 {
       
  1210   load_component(['messagebox', 'fadefilter', 'l10n', 'flyin', 'jquery', 'jquery-ui']);
       
  1211   miniPromptMessage({
       
  1212     title: $lang.get('user_logout_confirm_title_elev'),
       
  1213     message: $lang.get('user_logout_confirm_body_elev'),
       
  1214     buttons: [
       
  1215       {
       
  1216         text: $lang.get('user_logout_confirm_btn_logout'),
       
  1217         color: 'red',
       
  1218         style: {
       
  1219           fontWeight: 'bold'
       
  1220         },
       
  1221         onclick: function()
       
  1222         {
       
  1223           ajaxLoginPerformRequest({
       
  1224               mode:  'logout',
       
  1225               level: auth_level,
       
  1226               csrf_token: csrf_token
       
  1227           });
       
  1228           miniPromptDestroy(this);
       
  1229         }
       
  1230       },
       
  1231       {
       
  1232         text: $lang.get('etc_cancel'),
       
  1233         onclick: function()
       
  1234         {
       
  1235           miniPromptDestroy(this);
       
  1236         }
       
  1237       }
       
  1238     ]
       
  1239   });
       
  1240 }
       
  1241 
       
  1242 /**
       
  1243  * Take an SID and patch all internal links on the page.
       
  1244  * @param string New key. If false, removes keys from the page.
       
  1245  * @param string Old key. If false, only appends the new SID (more work as it uses DOM, use when dynamically going up to elevated)
       
  1246  * @param int New level, not a huge deal but sets auth_level. Try to specify it as some functions depend on it.
       
  1247  */
       
  1248 
       
  1249 window.ajaxLoginReplaceSIDInline = function(key, oldkey, level)
       
  1250 {
       
  1251   var host = String(window.location.hostname);
       
  1252   var exp = new RegExp('^https?://' + host.replace('.', '\.') + contentPath.replace('.', '\.'), 'g');
       
  1253   var rexp = new RegExp('^https?://' + host.replace('.', '\.'), 'g');
       
  1254   
       
  1255   if ( key )
       
  1256   {
       
  1257     if ( oldkey )
       
  1258     {
       
  1259       var body = document.getElementsByTagName('body')[0];
       
  1260       var replace = new RegExp(oldkey, 'g');
       
  1261       body.innerHTML = body.innerHTML.replace(replace, key);
       
  1262       ENANO_SID = key;
       
  1263     }
       
  1264     else
       
  1265     {
       
  1266       // append SID to all internal links
       
  1267       ENANO_SID = key;
       
  1268       
       
  1269       var links = document.getElementsByTagName('a');
       
  1270       for ( var i = 0; i < links.length; i++ )
       
  1271       {
       
  1272         if ( links[i].href.match(exp, links[i]) && links[i].href.indexOf('#') == -1 )
       
  1273         {
       
  1274           var newurl = (String(append_sid(links[i].href))).replace(rexp, '');
       
  1275           links[i].href = newurl;
       
  1276         }
       
  1277       }
       
  1278       
       
  1279       var forms = document.getElementsByTagName('form');
       
  1280       for ( var i = 0; i < forms.length; i++ )
       
  1281       {
       
  1282         if ( forms[i].method.toLowerCase() == 'post' )
       
  1283         {
       
  1284           if ( forms[i].action.match(exp, links[i]) )
       
  1285           {
       
  1286             var newurl = (String(append_sid(forms[i].action))).replace(rexp, '');
       
  1287             forms[i].action = newurl;
       
  1288           }
       
  1289         }
       
  1290         else
       
  1291         {
       
  1292           if ( !forms[i].auth )
       
  1293           {
       
  1294             var auth = document.createElement('input');
       
  1295             auth.type = 'hidden';
       
  1296             auth.name = 'auth';
       
  1297             auth.value = key;
       
  1298             forms[i].appendChild(auth);
       
  1299           }
       
  1300           else
       
  1301           {
       
  1302             forms[i].auth.value = key;
       
  1303           }
       
  1304         }
       
  1305       }
       
  1306     }
       
  1307     if ( level )
       
  1308     {
       
  1309       auth_level = level;
       
  1310     }
       
  1311   }
       
  1312   else
       
  1313   {
       
  1314     auth_level = USER_LEVEL_MEMBER;
       
  1315     ENANO_SID = false;
       
  1316     if ( oldkey )
       
  1317     {
       
  1318       var links = document.getElementsByTagName('a');
       
  1319       for ( var i = 0; i < links.length; i++ )
       
  1320       {
       
  1321         if ( links[i].href.match(exp, links[i]) && links[i].href.indexOf('#') == -1 )
       
  1322         {
       
  1323           links[i].href = links[i].href.replace(/\?auth=([a-f0-9]+)(&|#|$)/, '$2').replace(/&auth=([a-f0-9]+)/, '').replace(rexp, '');
       
  1324         }
       
  1325       }
       
  1326     }
       
  1327   }
       
  1328 }