includes/clientside/static/login.js
changeset 582 a38876c0793c
parent 550 685e839d934e
child 585 35e91d16ecf5
equal deleted inserted replaced
581:5e8fd89c02ea 582:a38876c0793c
     8 
     8 
     9 /**
     9 /**
    10  * Performs a logon as a regular member.
    10  * Performs a logon as a regular member.
    11  */
    11  */
    12 
    12 
    13 function ajaxLogonToMember()
    13 window.ajaxLogonToMember = function()
    14 {
    14 {
    15   // IE <6 pseudo-compatibility
    15   // IE <6 pseudo-compatibility
    16   if ( KILL_SWITCH )
    16   if ( KILL_SWITCH )
    17     return true;
    17     return true;
    18   if ( auth_level >= USER_LEVEL_MEMBER )
    18   if ( auth_level >= USER_LEVEL_MEMBER )
    25 
    25 
    26 /**
    26 /**
    27  * Authenticates to the highest level the current user is allowed to go to.
    27  * Authenticates to the highest level the current user is allowed to go to.
    28  */
    28  */
    29 
    29 
    30 function ajaxLogonToElev()
    30 window.ajaxLogonToElev = function()
    31 {
    31 {
    32   if ( auth_level == user_level )
    32   if ( auth_level == user_level )
    33     return true;
    33     return true;
    34   
    34   
    35   ajaxLoginInit(function(k)
    35   ajaxLoginInit(function(k)
    86  * Performs the AJAX request to get an encryption key and from there spawns the login form.
    86  * Performs the AJAX request to get an encryption key and from there spawns the login form.
    87  * @param function The function that will be called once authentication completes successfully.
    87  * @param function The function that will be called once authentication completes successfully.
    88  * @param int The security level to authenticate at - see http://docs.enanocms.org/Help:Appendix_B
    88  * @param int The security level to authenticate at - see http://docs.enanocms.org/Help:Appendix_B
    89  */
    89  */
    90 
    90 
    91 function ajaxLoginInit(call_on_finish, user_level)
    91 window.ajaxLoginInit = function(call_on_finish, user_level)
    92 {
    92 {
       
    93   load_component('messagebox');
       
    94   load_component('flyin');
       
    95   load_component('SpryEffects');
       
    96   load_component('l10n');
       
    97   load_component('crypto');
       
    98   
    93   logindata = {};
    99   logindata = {};
    94   
   100   
    95   var title = ( user_level > USER_LEVEL_MEMBER ) ? $lang.get('user_login_ajax_prompt_title_elev') : $lang.get('user_login_ajax_prompt_title');
   101   var title = ( user_level > USER_LEVEL_MEMBER ) ? $lang.get('user_login_ajax_prompt_title_elev') : $lang.get('user_login_ajax_prompt_title');
    96   logindata.mb_object = new MessageBox(MB_OKCANCEL | MB_ICONLOCK, title, '');
   102   logindata.mb_object = new MessageBox(MB_OKCANCEL | MB_ICONLOCK, title, '');
    97   
   103   
   141 
   147 
   142 /**
   148 /**
   143  * For compatibility only.
   149  * For compatibility only.
   144  */
   150  */
   145 
   151 
   146 function ajaxLogonInit(call_on_finish, user_level)
   152 window.ajaxLogonInit = function(call_on_finish, user_level)
   147 {
   153 {
   148   return ajaxLoginInit(call_on_finish, user_level);
   154   return ajaxLoginInit(call_on_finish, user_level);
   149 }
   155 }
   150 
   156 
   151 /**
   157 /**
   152  * Sets the contents of the AJAX login window to the appropriate status message.
   158  * Sets the contents of the AJAX login window to the appropriate status message.
   153  * @param int One of AJAX_STATUS_*
   159  * @param int One of AJAX_STATUS_*
   154  */
   160  */
   155 
   161 
   156 function ajaxLoginSetStatus(status)
   162 window.ajaxLoginSetStatus = function(status)
   157 {
   163 {
   158   if ( !logindata.mb_inner )
   164   if ( !logindata.mb_inner )
   159     return false;
   165     return false;
   160   if ( logindata.showing_status )
   166   if ( logindata.showing_status )
   161   {
   167   {
   303 /**
   309 /**
   304  * Performs an AJAX logon request to the server and calls ajaxLoginProcessResponse() on the result.
   310  * Performs an AJAX logon request to the server and calls ajaxLoginProcessResponse() on the result.
   305  * @param object JSON packet to send
   311  * @param object JSON packet to send
   306  */
   312  */
   307 
   313 
   308 function ajaxLoginPerformRequest(json)
   314 window.ajaxLoginPerformRequest = function(json)
   309 {
   315 {
   310   json = toJSONString(json);
   316   json = toJSONString(json);
   311   json = ajaxEscape(json);
   317   json = ajaxEscape(json);
   312   ajaxPost(makeUrlNS('Special', 'Login/action.json'), 'r=' + json, function()
   318   ajaxPost(makeUrlNS('Special', 'Login/action.json'), 'r=' + json, function()
   313     {
   319     {
   329 /**
   335 /**
   330  * Processes a response from the login server
   336  * Processes a response from the login server
   331  * @param object JSON response
   337  * @param object JSON response
   332  */
   338  */
   333 
   339 
   334 function ajaxLoginProcessResponse(response)
   340 window.ajaxLoginProcessResponse = function(response)
   335 {
   341 {
   336   // Did the server send a plaintext error?
   342   // Did the server send a plaintext error?
   337   if ( response.mode == 'error' )
   343   if ( response.mode == 'error' )
   338   {
   344   {
   339     logindata.mb_object.destroy();
   345     logindata.mb_object.destroy();
   401 /**
   407 /**
   402  * Builds the login form.
   408  * Builds the login form.
   403  * @param object Metadata to build off of
   409  * @param object Metadata to build off of
   404  */
   410  */
   405 
   411 
   406 function ajaxLoginBuildForm(data)
   412 window.ajaxLoginBuildForm = function(data)
   407 {
   413 {
   408   // let's hope this effectively preloads the image...
   414   // let's hope this effectively preloads the image...
   409   var _ = document.createElement('img');
   415   var _ = document.createElement('img');
   410   _.src = ( ajax_login_successimg_path ) ? ajax_login_successimg_path : scriptPath + '/images/check.png';
   416   _.src = ( ajax_login_successimg_path ) ? ajax_login_successimg_path : scriptPath + '/images/check.png';
   411   
   417   
   618     };
   624     };
   619     ajaxLoginShowFriendlyError(fake_packet);
   625     ajaxLoginShowFriendlyError(fake_packet);
   620   }
   626   }
   621 }
   627 }
   622 
   628 
   623 function ajaxLoginSubmitForm(real, username, password, captcha)
   629 window.ajaxLoginSubmitForm = function(real, username, password, captcha)
   624 {
   630 {
   625   // Perform AES test to make sure it's all working
   631   // Perform AES test to make sure it's all working
   626   if ( !aes_self_test() )
   632   if ( !aes_self_test() )
   627   {
   633   {
   628     alert('BUG: AES self-test failed');
   634     alert('BUG: AES self-test failed');
   759     }
   765     }
   760   }
   766   }
   761   ajaxLoginPerformRequest(json_packet);
   767   ajaxLoginPerformRequest(json_packet);
   762 }
   768 }
   763 
   769 
   764 function ajaxLoginShowFriendlyError(response)
   770 window.ajaxLoginShowFriendlyError = function(response)
   765 {
   771 {
   766   if ( !response.respawn_info )
   772   if ( !response.respawn_info )
   767     return false;
   773     return false;
   768   if ( !response.error_code )
   774   if ( !response.error_code )
   769     return false;
   775     return false;
   801   
   807   
   802   var body = document.getElementsByTagName('body')[0];
   808   var body = document.getElementsByTagName('body')[0];
   803   body.appendChild(errbox);
   809   body.appendChild(errbox);
   804 }
   810 }
   805 
   811 
   806 function ajaxLoginGetErrorText(response)
   812 window.ajaxLoginGetErrorText = function(response)
   807 {
   813 {
   808   switch ( response.error_code )
   814   switch ( response.error_code )
   809   {
   815   {
   810     default:
   816     default:
   811       return $lang.get('user_err_' + response.error_code);
   817       return $lang.get('user_err_' + response.error_code);
   866       return base;
   872       return base;
   867       break;
   873       break;
   868   }
   874   }
   869 }
   875 }
   870 
   876 
       
   877 window.ajaxInitLogout = function()
       
   878 {
       
   879   load_component('messagebox');
       
   880   load_component('l10n');
       
   881   var mb = new MessageBox(MB_YESNO|MB_ICONQUESTION, $lang.get('user_logout_confirm_title'), $lang.get('user_logout_confirm_body'));
       
   882   mb.onclick['Yes'] = function()
       
   883     {
       
   884       window.location = makeUrlNS('Special', 'Logout/' + csrf_token + '/' + title);
       
   885     }
       
   886 }
       
   887 
       
   888 window.mb_logout = function()
       
   889 {
       
   890   ajaxInitLogout();
       
   891 }
       
   892 
       
   893 window.ajaxStartLogin = function()
       
   894 {
       
   895   ajaxLogonToMember();
       
   896 }
       
   897 
       
   898 window.ajaxStartAdminLogin = function()
       
   899 {
       
   900   // IE <6 pseudo-compatibility
       
   901   if ( KILL_SWITCH )
       
   902     return true;
       
   903   if ( auth_level < USER_LEVEL_ADMIN )
       
   904   {
       
   905     ajaxLoginInit(function(k) {
       
   906       ENANO_SID = k;
       
   907       auth_level = USER_LEVEL_ADMIN;
       
   908       var loc = makeUrlNS('Special', 'Administration');
       
   909       if ( (ENANO_SID + ' ').length > 1 )
       
   910         window.location = loc;
       
   911     }, USER_LEVEL_ADMIN);
       
   912     return false;
       
   913   }
       
   914   var loc = makeUrlNS('Special', 'Administration');
       
   915   window.location = loc;
       
   916 }
       
   917 
       
   918 window.ajaxAdminPage = function()
       
   919 {
       
   920   // IE <6 pseudo-compatibility
       
   921   if ( KILL_SWITCH )
       
   922     return true;
       
   923   if ( auth_level < USER_LEVEL_ADMIN )
       
   924   {
       
   925     ajaxPromptAdminAuth(function(k) {
       
   926       ENANO_SID = k;
       
   927       auth_level = USER_LEVEL_ADMIN;
       
   928       var loc = String(window.location + '');
       
   929       window.location = append_sid(loc);
       
   930       var loc = makeUrlNS('Special', 'Administration', 'module=' + namespace_list['Admin'] + 'PageManager&source=ajax&page_id=' + ajaxEscape(title));
       
   931       if ( (ENANO_SID + ' ').length > 1 )
       
   932         window.location = loc;
       
   933     }, 9);
       
   934     return false;
       
   935   }
       
   936   var loc = makeUrlNS('Special', 'Administration', 'module=' + namespace_list['Admin'] + 'PageManager&source=ajax&page_id=' + ajaxEscape(title));
       
   937   window.location = loc;
       
   938 }
       
   939 
       
   940 var navto_ns;
       
   941 var navto_pg;
       
   942 var navto_ul;
       
   943 
       
   944 window.ajaxLoginNavTo = function(namespace, page_id, min_level)
       
   945 {
       
   946   // IE <6 pseudo-compatibility
       
   947   if ( KILL_SWITCH )
       
   948     return true;
       
   949   navto_pg = page_id;
       
   950   navto_ns = namespace;
       
   951   navto_ul = min_level;
       
   952   if ( auth_level < min_level )
       
   953   {
       
   954     ajaxPromptAdminAuth(function(k) {
       
   955       ENANO_SID = k;
       
   956       auth_level = navto_ul;
       
   957       var loc = makeUrlNS(navto_ns, navto_pg);
       
   958       if ( (ENANO_SID + ' ').length > 1 )
       
   959         window.location = loc;
       
   960     }, min_level);
       
   961     return false;
       
   962   }
       
   963   var loc = makeUrlNS(navto_ns, navto_pg);
       
   964   window.location = loc;
       
   965 }
       
   966 
       
   967 window.ajaxAdminUser = function(username)
       
   968 {
       
   969   // IE <6 pseudo-compatibility
       
   970   if ( KILL_SWITCH )
       
   971     return true;
       
   972   if ( auth_level < USER_LEVEL_ADMIN )
       
   973   {
       
   974     ajaxPromptAdminAuth(function(k) {
       
   975       ENANO_SID = k;
       
   976       auth_level = USER_LEVEL_ADMIN;
       
   977       var loc = String(window.location + '');
       
   978       window.location = append_sid(loc);
       
   979       var loc = makeUrlNS('Special', 'Administration', 'module=' + namespace_list['Admin'] + 'UserManager&src=get&user=' + ajaxEscape(username));
       
   980       if ( (ENANO_SID + ' ').length > 1 )
       
   981         window.location = loc;
       
   982     }, 9);
       
   983     return false;
       
   984   }
       
   985   var loc = makeUrlNS('Special', 'Administration', 'module=' + namespace_list['Admin'] + 'UserManager&src=get&user=' + ajaxEscape(username));
       
   986   window.location = loc;
       
   987 }