]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/auth/controller.js
35d885c9bf6684c529cd2955e3fcdfd494a2eb9d
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / auth / controller.js
1 dump('entering auth/controller.js\n');
2 // vim:sw=4:ts=4:noet:
3
4 if (typeof auth == 'undefined') auth = {};
5 auth.controller = function (params) {
6     JSAN.use('util.error'); this.error = new util.error();
7     this.w = params.window;
8
9     return this;
10 };
11
12 auth.controller.prototype = {
13
14     'init' : function () {
15
16         var obj = this;  // so the 'this' in event handlers don't confuse us
17         var w = obj.w;
18
19         JSAN.use('OpenILS.data');
20         obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
21
22         // MVC
23         JSAN.use('util.controller'); obj.controller = new util.controller();
24         obj.controller.init(
25             {
26                 'control_map' : {
27                     'cmd_login' : [
28                         ['command'],
29                         function() {
30                             obj.login();
31                         }
32                     ],
33                     'cmd_standalone' : [
34                         ['command'],
35                         function() {
36                             obj.standalone();
37                         }
38                     ],
39                     'cmd_standalone_import' : [
40                         ['command'],
41                         function() {
42                             obj.standalone_import();
43                         }
44                     ],
45                     'cmd_standalone_export' : [
46                         ['command'],
47                         function() {
48                             obj.standalone_export();
49                         }
50                     ],
51                     'cmd_clear_cache' : [
52                         ['command'],
53                         function() {
54                             obj.debug('clear_cache');
55                         }
56                     ],
57                     'cmd_js_console' : [
58                         ['command'],
59                         function() {
60                             obj.debug('js_console');
61                         }
62                     ],
63                     'cmd_debugger' : [
64                         ['command'],
65                         function() {
66                             start_debugger();
67                         }
68                     ],
69                     'cmd_inspector' : [
70                         ['command'],
71                         function() {
72                             start_inspector();
73                         }
74                     ],
75                     'cmd_chrome_list' : [
76                         ['command'],
77                         function() {
78                             start_chrome_list();
79                         }
80                     ],
81                     'cmd_js_shell' : [
82                         ['command'],
83                         function() {
84                             start_js_shell();
85                         }
86                     ],
87                     'cmd_override' : [
88                         ['command'],
89                         function() {
90                             obj.override();
91                         }
92                     ],
93                     'cmd_logoff' : [
94                         ['command'],
95                         function() {
96                             obj.logoff()
97                         }
98                     ],
99                     'cmd_close_window' : [
100                         ['command'],
101                         function() {
102                             obj.close()
103                         }
104                     ],
105                     'cmd_test_server' : [
106                         ['command'],
107                         function() {
108                             obj.test_server( obj.controller.view.server_prompt.value );
109                         }
110                     ],
111                     'ssl_exception' : [
112                         ['render'],
113                         function(e) {
114                             return function() {
115                                 try {
116                                     obj.controller.view.cmd_ssl_exception.setAttribute('hidden','true');
117                                     var x = new XMLHttpRequest();
118                                     x.open("GET",'chrome://pippki/content/exceptionDialog.xul',false);
119                                     x.send(null);
120                                     obj.controller.view.cmd_ssl_exception.setAttribute('hidden','false');
121                                 } catch(E) {
122                                     obj.controller.view.cmd_ssl_exception.setAttribute('hidden','true');
123                                 }
124                             };
125                         }
126                     ],
127                     'cmd_ssl_exception' : [
128                         ['command'],
129                         function() {
130                             window.openDialog(
131                                 'chrome://pippki/content/exceptionDialog.xul',
132                                 '', 
133                                 'chrome,centerscreen,modal', 
134                                 { 
135                                     'location' : 'https://' + obj.controller.view.server_prompt.value.match(/^[^\/]*/), 
136                                     'prefetchCert' : true 
137                                 } 
138                             );
139                             obj.test_server( obj.controller.view.server_prompt.value );
140                         }
141                     ],
142                     'server_prompt' : [
143                         ['keypress'],
144                         handle_keypress
145                     ],
146                     'server_menu' : [
147                         ['render'],
148                         function(e) {
149                             return function() {
150                                 for (var s in obj.data.ws_info) {
151                                     var mi = document.createElement('menuitem');
152                                     mi.setAttribute('label',s);
153                                     mi.setAttribute('value',s);
154                                     e.appendChild(mi);
155                                 }
156                             };
157                         }
158                     ],
159                     'name_prompt' : [
160                         ['keypress'],
161                         handle_keypress
162                     ],
163                     'password_prompt' : [
164                         ['keypress'],
165                         handle_keypress
166                     ],
167                     'submit_button' : [
168                         ['render'],
169                         function(e) { return function() {} }
170                     ],
171                     'apply_locale_btn' : [
172                         ['render'],
173                         function(e) { return function() {} }
174                     ],
175                     'progress_bar' : [
176                         ['render'],
177                         function(e) { return function() {} }
178                     ],
179                     'status' : [
180                         ['render'],
181                         function(e) { return function() {
182                         } }
183                     ],
184                     'ws_deck' : [
185                         ['render'],
186                         function(e) { return function() {
187                             try {
188                                 JSAN.use('util.widgets'); util.widgets.remove_children(e);
189                                 var x = document.createElement('description');
190                                 e.appendChild(x);
191                                 if (obj.data.ws_info 
192                                     && obj.data.ws_info[ obj.controller.view.server_prompt.value ]) {
193                                     var ws = obj.data.ws_info[ obj.controller.view.server_prompt.value ];
194                                     x.appendChild(
195                                         document.createTextNode(
196                                             ws.name /*+ ' @  ' + ws.lib_shortname*/
197                                         )
198                                     );
199                                     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
200                                     JSAN.use('util.file'); var file = new util.file('last_ws_server');
201                                     file.set_object(obj.controller.view.server_prompt.value);
202                                     file.close();
203                                 } else {
204                                     x.appendChild(
205                                         document.createTextNode(
206                                             document.getElementById('authStrings').getString('staff.auth.controller.not_configured')
207                                         )
208                                     );
209                                 }
210                             } catch(E) {
211                                 alert(E);
212                             }
213                         } }
214                     ],
215                     'menu_spot' : [
216                         ['render'],
217                         function(e) { return function() {
218                         } }
219                     ],
220
221                 }
222             }
223         );
224         obj.controller.view.name_prompt.focus();
225
226         function handle_keypress(ev) {
227             try {
228                 if (ev.keyCode && ev.keyCode == 13) {
229                     switch(this) {
230                         case obj.controller.view.server_prompt:
231                             ev.preventDefault();
232                             obj.controller.view.name_prompt.focus(); obj.controller.view.name_prompt.select();
233                         break;
234                         case obj.controller.view.name_prompt:
235                             ev.preventDefault();
236                             obj.controller.view.password_prompt.focus(); obj.controller.view.password_prompt.select();
237                         break;
238                         case obj.controller.view.password_prompt:
239                             ev.preventDefault();
240                             obj.controller.view.submit_button.focus(); 
241                             obj.login();
242                         break;
243                         default: break;
244                     }
245                 }
246             } catch(E) {
247                 alert(E);
248             }
249         }
250
251         obj.controller.view.server_prompt.addEventListener(
252             'change',
253             function (ev) { 
254                 obj.test_server(ev.target.value);
255                 obj.controller.render('ws_deck'); 
256             },
257             false
258         );
259         obj.controller.view.server_prompt.addEventListener(
260             'command',
261             function (ev) {
262                 obj.controller.view.name_prompt.focus();
263                 obj.controller.view.name_prompt.select();
264                 obj.test_server(ev.target.value);
265                 obj.controller.render('ws_deck'); 
266             },
267             false
268         );
269
270         // This talks to our ILS
271         JSAN.use('auth.session');
272         obj.session = new auth.session(obj.controller.view);
273
274         obj.controller.render();
275         obj.test_server( obj.controller.view.server_prompt.value );
276         obj.controller.render('ws_deck'); 
277
278         if (typeof this.on_init == 'function') {
279             this.error.sdump('D_AUTH','auth.controller.on_init()\n');
280             this.on_init();
281         }
282     },
283
284     'test_server' : function(url) {
285         var obj = this;
286         if (!url) {
287             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
288             JSAN.use('util.file'); var file = new util.file('last_ws_server');
289             if (file._file.exists()) {
290                 url = file.get_object(); file.close();
291                 obj.controller.view.server_prompt.value = url;
292             }
293         }
294         url = url.match(/^[^\/]*/).toString(); // Only test the pre-slash URL
295         obj.controller.view.submit_button.disabled = true;
296         obj.controller.view.server_prompt.disabled = true;
297         var s = document.getElementById('status');
298         s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.testing_hostname'));
299         s.setAttribute('style','color: orange;');
300         document.getElementById('version').value = '';
301         if (!url) {
302             s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.prompt_hostname'));
303             s.setAttribute('style','color: red;');
304             obj.controller.view.server_prompt.disabled = false;
305             obj.controller.view.server_prompt.focus();
306             return;
307         }
308         try {
309             if ( ! url.match(/^https:\/\//) ) url = 'https://' + url;
310             var x = new XMLHttpRequest();
311             dump('server url = ' + url + '\n');
312             x.open("GET",url,true);
313             x.onreadystatechange = function() {
314                 try {
315                     if (x.readyState != 4) return;
316                     s.setAttribute('value', document.getElementById('authStrings').getFormattedString('staff.auth.controller.status', [x.status, x.statusText]));
317                     if (x.status == 200) {
318                         s.setAttribute('style','color: green;');
319                     } else {
320                         s.setAttribute('style','color: red;');
321                     }
322                     obj.test_version(url);
323                 } catch(E) {
324                     obj.controller.view.server_prompt.disabled = false;
325                     obj.controller.view.server_prompt.focus();
326                     s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.error_hostname'));
327                     s.setAttribute('style','color: red;');
328                     obj.error.sdump('D_ERROR',E);
329                 }
330             }
331             x.send(null);
332         } catch(E) {
333             s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.error_hostname'));
334             s.setAttribute('style','color: brown;');
335             obj.error.sdump('D_ERROR',E);
336             obj.controller.view.server_prompt.disabled = false;
337             obj.controller.view.server_prompt.focus();
338         }
339     },
340
341     'test_version' : function(url) {
342         var obj = this;
343         var s = document.getElementById('version');
344         s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.testing_version'));
345         s.setAttribute('style','color: orange;');
346         try {
347             var x = new XMLHttpRequest();
348             var url2 = url + '/xul/server/';
349             dump('version url = ' + url2 + '\n');
350             x.open("GET",url2,true);
351             x.onreadystatechange = function() {
352                 try {
353                     if (x.readyState != 4) return;
354                     s.setAttribute('value', document.getElementById('authStrings').getFormattedString('staff.auth.controller.status', [x.status, x.statusText]));
355                     if (x.status == 200) {
356                         s.setAttribute('style','color: green;');
357                         obj.controller.view.submit_button.disabled = false;
358                     } else {
359                         s.setAttribute('style','color: red;');
360                         obj.test_upgrade_instructions(url);
361                     }
362                     obj.controller.view.server_prompt.disabled = false;
363                 } catch(E) {
364                     s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.error_version'));
365                     s.setAttribute('style','color: red;');
366                     obj.error.sdump('D_ERROR',E);
367                     obj.controller.view.server_prompt.disabled = false;
368                 }
369             }
370             x.send(null);
371         } catch(E) {
372             s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.error_version'));
373             s.setAttribute('style','color: brown;');
374             obj.error.sdump('D_ERROR',E);
375             obj.controller.view.server_prompt.disabled = false;
376         }
377     },
378
379     'test_upgrade_instructions' : function(url) {
380         var obj = this;
381         try {
382             var x = new XMLHttpRequest();
383             var url2 = url + '/xul/versions.html';
384             dump('upgrade url = ' + url2 + '\n');
385             x.open("GET",url2,true);
386             x.onreadystatechange = function() {
387                 try {
388                     if (x.readyState != 4) return;
389                     if (x.status == 200) {
390                         window.open('data:text/html,'+window.escape(x.responseText),'upgrade','chrome,resizable,modal,centered');
391                     } else {
392                         if(typeof(G.upgradeCheck) == "function")
393                         {
394                             if (confirm("This server does not support your version of the staff client, an upgrade may be required. If you wish to check for an upgrade please press Ok. Otherwise please press cancel."))
395                                 G.upgradeCheck();
396                         } else {
397                             alert(document.getElementById('authStrings').getString('staff.auth.controller.version_mismatch'));
398                         }
399                     }
400                     obj.controller.view.server_prompt.disabled = false;
401                 } catch(E) {
402                     obj.error.sdump('D_ERROR',E);
403                     obj.controller.view.server_prompt.disabled = false;
404                 }
405             }
406             x.send(null);
407         } catch(E) {
408             obj.error.sdump('D_ERROR',E);
409             obj.controller.view.server_prompt.disabled = false;
410         }
411     },
412
413     'login' : function() { 
414
415         var obj = this;
416
417         this.error.sdump('D_AUTH',
418             document.getElementById('authStrings').getFormattedString(
419                 'staff.auth.controller.error_login', [
420                     this.controller.view.name_prompt.value,
421                     this.controller.view.password_prompt.value,
422                     this.controller.view.server_prompt.value
423                 ]
424             )
425         ); 
426         this.controller.view.server_prompt.disabled = true;
427         this.controller.view.name_prompt.disabled = true;
428         this.controller.view.password_prompt.disabled = true;
429         this.controller.view.submit_button.disabled = true;
430         this.controller.view.apply_locale_btn.disabled = true;
431         XML_HTTP_SERVER = this.controller.view.server_prompt.value.match(/^[^\/]*/).toString();
432
433         try {
434
435             if (typeof this.on_login == 'function') {
436                 this.error.sdump('D_AUTH','auth.controller.session.on_init = ' +
437                     'auth.controller.on_login\n');
438                 this.session.on_init = this.on_login;
439                 this.session.on_error = function() { obj.logoff(); };
440             }
441             
442             this.session.init();
443
444         } catch(E) {
445             var error = '!! ' + E + '\n';
446             this.error.sdump('D_ERROR',error); 
447             alert(error);
448             this.logoff();
449             if (E == 'open-ils.auth.authenticate.init returned false\n') {
450                 this.controller.view.server_prompt.focus();
451                 this.controller.view.server_prompt.select();
452             }
453
454             if (typeof this.on_login_error == 'function') {
455                 this.error.sdump('D_AUTH','auth.controller.on_login_error()\n');
456                 this.on_login_error(E);
457             }
458         }
459
460     },
461
462     'standalone' : function() {
463         var obj = this;
464         try {
465             if (typeof this.on_standalone == 'function') {
466                 obj.on_standalone();
467             }
468         } catch(E) {
469             var error = '!! ' + E + '\n';
470             obj.error.sdump('D_ERROR',error); 
471             alert(error);
472         }
473     },
474
475     'standalone_import' : function() {
476         var obj = this;
477         try {
478             if (typeof this.on_standalone_import == 'function') {
479                 obj.on_standalone_import();
480             }
481         } catch(E) {
482             var error = '!! ' + E + '\n';
483             obj.error.sdump('D_ERROR',error); 
484             alert(error);
485         }
486     },
487
488     'standalone_export' : function() {
489         var obj = this;
490         try {
491             if (typeof this.on_standalone_export == 'function') {
492                 obj.on_standalone_export();
493             }
494         } catch(E) {
495             var error = '!! ' + E + '\n';
496             obj.error.sdump('D_ERROR',error); 
497             alert(error);
498         }
499     },
500
501     'debug' : function(action) {
502         var obj = this;
503         try {
504             if (typeof this.on_debug == 'function') {
505                 obj.on_debug(action);
506             }
507         } catch(E) {
508             var error = '!! ' + E + '\n';
509             obj.error.sdump('D_ERROR',error);
510             alert(error);
511         }
512     },
513
514     'logoff' : function() { 
515
516         this.data.stash_retrieve();
517         if (typeof this.data.unsaved_data != 'undefined') {
518             if (this.data.unsaved_data > 0) {
519                 var confirmation = window.confirm( document.getElementById('offlineStrings').getString('menu.logoff.unsaved_data_warning') );
520                 if (!confirmation) { return; }
521                 this.data.unsaved_data = 0;
522                 this.data.stash('unsaved_data');
523             }
524         }
525     
526         this.error.sdump('D_AUTH','logoff' + this.w + '\n'); 
527         this.controller.view.progress_bar.value = 0; 
528         this.controller.view.progress_bar.setAttribute('real','0.0');
529         this.controller.view.submit_button.disabled = false;
530         this.controller.view.apply_locale_btn.disabled = false;
531         this.controller.view.password_prompt.disabled = false;
532         this.controller.view.password_prompt.value = '';
533         this.controller.view.name_prompt.disabled = false;
534         this.controller.view.name_prompt.focus(); 
535         this.controller.view.name_prompt.select();
536         this.controller.view.server_prompt.disabled = false;
537
538         var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService();
539         var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
540         var enumerator = windowManagerInterface.getEnumerator(null);
541
542         var w; // close all other windows
543         while ( w = enumerator.getNext() ) {
544             if (w != window) {
545                 if (w.xulG) { w.close(); } // FIXME: kludge so we don't close Firefox windows as an extension.  We should define a @windowtype for all the staff client windows and have the enumerator just pull those
546             }
547         }
548
549         this.controller.render('ws_deck');
550
551         this.session.close();
552         this.data.menu_perms = false;
553         this.data.current_hotkeyset = undefined;
554         this.data.stash('menu_perms');
555         this.data.stash('current_hotkeyset');
556
557         /* FIXME - need some locking or object destruction for the async tests */
558         /* this.test_server( this.controller.view.server_prompt.value ); */
559
560         if (typeof this.on_logoff == 'function') {
561             this.error.sdump('D_AUTH','auth.controller.on_logoff()\n');
562             this.on_logoff();
563         }
564         
565     },
566     'close' : function() { 
567     
568         this.error.sdump('D_AUTH','close' + this.w + '\n');
569
570         var confirm_string = document.getElementById('authStrings').getString('staff.auth.controller.confirm_close');
571
572         this.data.stash_retrieve();
573         if (typeof this.data.unsaved_data != 'undefined') {
574             if (this.data.unsaved_data > 0) {
575                 confirm_string = document.getElementById('offlineStrings').getString('menu.shutdown.unsaved_data_warning');
576             }
577         }
578  
579         if (window.confirm(confirm_string)) {
580             this.data.unsaved_data = 0;
581             this.data.stash('unsaved_data');
582             this.logoff();
583             this.w.close(); /* Probably won't go any further */
584
585             if (typeof this.on_close == 'function') {
586                 this.error.sdump('D_AUTH','auth.controller.on_close()\n');
587                 this.on_close();
588             }
589         }
590         
591     }
592 }
593
594 dump('exiting auth/controller.js\n');