136cdd1c54754a0471c4c36e9121dadacd28a818
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / auth / auth.js
1 // the master Global variable
2 var G = { 
3 /////////////////////////////////////////////////////////////////////////////////////
4
5         // pointer to the auth window
6         'main_window' : self,   
7
8         // list of open window references, used for debugging in shell
9         'win_list' : [],        
10
11         // list of Top Level menu interface window references
12         'appshell_list' : [],   
13
14         // list of documents for debugging.  BROKEN
15         'doc_list' : [],        
16
17         // Windows need unique names.  This number helps.
18         'window_name_increment' : 0, 
19
20         // This number gets put into the title bar for Top Level menu interface windows
21         'appshell_name_increment' : 0,
22
23         // I was using this to make sure I could shove references into new windows
24         // correctly.  However, it's JSON that tends to behave weirdly when crossing
25         // window boundaries.  [ 'a', 'b', 'c' ] could turn into { '1' : 'a', '2' : 'b',
26         'main_test_variable' : 'Hello World',
27
28 /////////////////////////////////////////////////////////////////////////////////////
29
30         // Flag for whether the staff client should act as if it were offline or not
31         'offline' : false,
32
33         // Array of Session Keys.  This is an array mostly by accident, we usually
34         // only deal with one session.  But this could be useful for implementing
35         // overrides with other logins.
36         'auth_ses' = [],
37
38         // Org Unit for the login user
39         'user_ou' : ''
40
41 /////////////////////////////////////////////////////////////////////////////////////
42 /////////////////////////////////////////////////////////////////////////////////////
43 };
44
45 var debug_ignore_auth_failures = false;
46
47 G['org_tree'] = '';
48 G['my_orgs'] = [];
49 G['my_orgs_hash'] = {};
50
51 G['ap_list'] = []; // actor::profile
52 G['ap_hash'] = {};
53 G['cit_list'] = []; // config::identification_type
54 G['cit_hash'] = {};
55 G['cst_list'] = []; // config::standing
56 G['cst_hash'] = {};
57 G['acpl_list'] = []; // asset::copy_location
58 G['acpl_hash'] = {}; G['acpl_my_orgs'] = []; G['acpl_my_orgs_hash'] = {};
59 G['aout_list'] = []; // actor::org_unit_type
60 G['aout_hash'] = {};
61 G['ccs_list'] = []; // config::copy_status
62 G['ccs_hash'] = {};
63 G['asc_list'] = []; // asset::stat_cat
64 G['actsc_list'] = []; // actor::stat_cat
65 G['actsc_hash']; // actor::stat_cat
66
67 G['itemsout_header'] = 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!\r\nYou have the following items:<hr/><ol>';
68 G['itemsout_line_item'] = '<li>%TITLE: 50%\r\nBarcode: %COPY_BARCODE% Due: %DUE_D%\r\n';
69 G['itemsout_footer'] = '</ol><hr />%PINES_CODE% %TODAY%\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%';
70
71 G['checkout_header'] = 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!\r\nYou checked out the following items:<hr/><ol>';
72 G['checkout_line_item'] = '<li>%TITLE%\r\nBarcode: %COPY_BARCODE% Due: %DUE_D%\r\n';
73 G['checkout_footer'] = '</ol><hr />%PINES_CODE% %TODAY%\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%';
74
75 var mw = G['main_window'];
76 var auth_meter_incr = 10;
77
78 function auth_init() {
79         sdump('D_AUTH','TESTING: auth.js: ' + mw.G['main_test_variable'] + '\n');
80         sdump('D_AUTH',arg_dump(arguments));
81         var np = document.getElementById('name_prompt');
82         np.addEventListener("keypress",handle_keypress,false);
83         np.focus();
84         var pp = document.getElementById('password_prompt');
85         pp.addEventListener("keypress",handle_keypress,false);
86         self.addEventListener("unload",nice_shutdown,false);
87         G['sound'] = xp_sound_init(); 
88         //G.sound.beep();
89         snd_logon();
90 }
91
92 function handle_keypress(ev) {
93         if (ev.keyCode && ev.keyCode == 13) {
94                 switch(this) {
95                         case document.getElementById('name_prompt') :
96                                 ev.preventDefault();
97                                 var pp = document.getElementById('password_prompt');
98                                 pp.focus(); pp.select();
99                         break;
100                         case document.getElementById('password_prompt') :
101                                 ev.preventDefault();
102                                 var sb = document.getElementById('submit_button');
103                                 sb.focus();
104                                 authenticate();
105                         break;
106                         default:
107                         break;
108                 }
109         }
110 }
111
112 function disable_login_prompts() {
113         sdump('D_AUTH',arg_dump(arguments));
114         disable_widgets(document,'password_prompt','name_prompt','submit_button');
115         G.sound.beep();
116 }
117
118 function enable_login_prompts() {
119         sdump('D_AUTH',arg_dump(arguments));
120         enable_widgets(document,'password_prompt','name_prompt','submit_button');
121         document.getElementById('password_prompt').value = '';
122         var np = document.getElementById('name_prompt');
123         np.focus(); np.select();
124         document.getElementById('auth_meter').value = 0;
125         document.getElementById('auth_meter').setAttribute('real', '0.0');
126         G.sound.beep();
127 }
128
129 function authenticate() {
130         sdump('D_AUTH',arg_dump(arguments));
131         timer_init('cat');
132         var name = document.getElementById('name_prompt').value;
133         if (name.length == 0) { enable_login_prompts(); return; }
134         // Talk to the system and authenticate the user.
135         user_async_request(
136                 'open-ils.auth',
137                 'open-ils.auth.authenticate.init',
138                 [ name ],
139                 auth_init_callback
140         );
141 }
142
143 function auth_init_callback(request) {
144         sdump('D_AUTH',arg_dump(arguments));
145         var auth_init;
146         try {
147                 auth_init = request.getResultObject();
148                 if (!auth_init) { throw('null result'); }
149         } catch(E) {
150                 G.offline = true;
151                 sdump('D_ERROR','Error trying to communicate with the server.  Entering OFFLINE mode.\n');
152                 s_alert('Error trying to communicate with the server.  Entering OFFLINE mode.\n');
153         }
154
155         sdump( 'D_AUTH', 'D_AUTH_INIT: ' + typeof(auth_init) + ' : ' + auth_init + '\n');
156         var name = document.getElementById('name_prompt').value;
157         var pw = document.getElementById('password_prompt').value;
158         G.name = name; G.pw = pw;
159
160         user_async_request(
161                 'open-ils.auth',
162                 'open-ils.auth.authenticate.complete',
163                 [ name, hex_md5(auth_init + hex_md5(pw)) ],
164                 auth_ses_callback
165         );
166         incr_progressmeter(document,'auth_meter',auth_meter_incr);
167 }
168
169 function auth_ses_callback(request) {
170         sdump('D_AUTH',arg_dump(arguments));
171         var auth_ses;
172         try {
173                 auth_ses = request.getResultObject();
174                 if (!auth_ses) { if (!G.offline) { throw('null result'); } }
175                 if (auth_ses == 0) { throw('0 result'); }
176                 if (instanceOf(auth_ses,ex)) {
177                         throw(auth_ses.err_msg());
178                 }
179         } catch(E) {
180                 alert('Login failed on auth_ses: ' + js2JSON(E)); 
181                 if (!debug_ignore_auth_failures) {
182                         enable_login_prompts(); return;
183                 }
184         }
185         mw.G.auth_ses = [ auth_ses ];
186         sdump( 'D_AUTH', 'D_AUTH_SES: ' + typeof(mw.G['auth_ses'][0]) + ' : ' + mw.G['auth_ses'][0] + '\n');
187
188         //'open-ils.actor.user.profiles.retrieve',
189         user_async_request(
190                 'open-ils.actor',
191                 'open-ils.actor.groups.retrieve',
192                 [],
193                 ap_list_callback
194         );
195         incr_progressmeter(document,'auth_meter',auth_meter_incr);
196 }
197
198 function ap_list_callback(request) {
199         sdump('D_AUTH',arg_dump(arguments));
200         var ap_list;
201         try {
202                 ap_list = request.getResultObject();
203                 if (!ap_list) { 
204                         if (!G.offline) {
205                                 throw('null result'); 
206                         } else {
207                                 var f = create_input_stream('ap_list');
208                                 ap_list = JSON2js( f.read(-1) ); f.close();
209                         }
210                 }
211                 if (ap_list.length == 0) { throw('zero length result'); }
212         } catch(E) {
213                 alert('Login failed on ap_list: ' + js2JSON(E)); 
214                 if (!debug_ignore_auth_failures) {
215                         enable_login_prompts(); return;
216                 }
217         }
218         var f = create_output_stream('ap_list');
219         var ap_list_json = js2JSON( ap_list );
220         f.write( ap_list_json, ap_list_json.length ); f.close();
221         mw.G.ap_list = ap_list;
222         mw.G.ap_hash = convert_object_list_to_hash( ap_list );
223
224         user_async_request(
225                 'open-ils.actor',
226                 'open-ils.actor.user.ident_types.retrieve',
227                 [],
228                 cit_list_callback
229         );
230         incr_progressmeter(document,'auth_meter',auth_meter_incr);
231 }
232
233 function cit_list_callback(request) {
234         sdump('D_AUTH',arg_dump(arguments));
235         var cit_list;
236         try {
237                 cit_list = request.getResultObject();
238                 if (!cit_list) { throw('null result'); }
239                 if (cit_list.length == 0) { throw('zero length result'); }
240         } catch(E) {
241                 alert('Login failed on cit_list: ' + js2JSON(E)); 
242                 if (!debug_ignore_auth_failures) {
243                         enable_login_prompts(); return;
244                 }
245         }
246         mw.G.cit_list = cit_list;
247         mw.G.cit_hash = convert_object_list_to_hash( cit_list );
248         
249         user_async_request(
250                 'open-ils.actor',
251                 'open-ils.actor.standings.retrieve',
252                 [],
253                 cst_list_callback
254         );
255
256         incr_progressmeter(document,'auth_meter',auth_meter_incr);
257 }
258
259 function cst_list_callback(request) {
260         sdump('D_AUTH',arg_dump(arguments));
261         var cst_list;
262         try {
263                 cst_list = request.getResultObject();
264                 if (!cst_list) { throw('null result'); }
265                 if (cst_list.length == 0) { throw('zero length result'); }
266         } catch(E) {
267                 alert('Login failed on cst_list: ' + js2JSON(E)); 
268                 if (!debug_ignore_auth_failures) {
269                         enable_login_prompts(); return;
270                 }
271         }
272         mw.G.cst_list = cst_list;
273         mw.G.cst_hash = convert_object_list_to_hash( cst_list );
274         sdump('D_AUTH', 'cst_list = ' + js2JSON(cst_list) + '\n');
275
276         user_async_request(
277                 'open-ils.search',
278                 'open-ils.search.config.copy_location.retrieve.all',
279                 [],
280                 acpl_list_callback
281         );
282         incr_progressmeter(document,'auth_meter',auth_meter_incr);
283
284 }
285
286 function acpl_list_callback(request) {
287         sdump('D_AUTH',arg_dump(arguments));
288         var acpl_list;
289         try {
290                 acpl_list = request.getResultObject();
291                 if (!acpl_list) { throw('null result'); }
292                 if (acpl_list.length == 0) { throw('zero length result'); }
293         } catch(E) {
294                 alert('Login failed on acpl_list: ' + js2JSON(E)); 
295                 if (!debug_ignore_auth_failures) {
296                         enable_login_prompts(); return;
297                 }
298         }
299         mw.G.acpl_list = acpl_list;
300         mw.G.acpl_hash = convert_object_list_to_hash( acpl_list );
301         sdump('D_AUTH', 'acpl_list = ' + js2JSON(acpl_list) + '\n');
302
303         user_async_request(
304                 'open-ils.search',
305                 'open-ils.search.config.copy_status.retrieve.all',
306                 [],
307                 ccs_list_callback
308         );
309         incr_progressmeter(document,'auth_meter',auth_meter_incr);
310 }
311
312 function ccs_list_callback(request) {
313         sdump('D_AUTH',arg_dump(arguments));
314         var ccs_list;
315         try {
316                 ccs_list = request.getResultObject();
317                 if (!ccs_list) { throw('null result'); }
318                 if (ccs_list.length == 0) { throw('zero length result'); }
319         } catch(E) {
320                 alert('Login failed on ccs_list: ' + js2JSON(E)); 
321                 if (!debug_ignore_auth_failures) {
322                         enable_login_prompts(); return;
323                 }
324         }
325         mw.G.ccs_list = ccs_list;
326         mw.G.ccs_hash = convert_object_list_to_hash( ccs_list );
327         sdump('D_AUTH', 'ccs_list = ' + js2JSON(ccs_list) + '\n');
328
329         user_async_request(
330                 'open-ils.search',
331                 'open-ils.search.actor.user.session',
332                 [ mw.G['auth_ses'][0] ],
333                 user_callback
334         );
335         incr_progressmeter(document,'auth_meter',auth_meter_incr);
336 }
337
338 function user_callback(request) {
339         sdump('D_AUTH',arg_dump(arguments));
340         var user;
341         var user_ou;
342         try {
343                 user = request.getResultObject();
344                 if (!user) { throw('null result'); }
345                 if (typeof(user) != 'object') { throw('result not an object' + user); }
346         } catch(E) {
347                 alert('Login failed on user: ' + js2JSON(E)); 
348                 if (!debug_ignore_auth_failures) {
349                         enable_login_prompts(); return;
350                 }
351         }
352         mw.G.user = user;
353         mw.G.user_ou = user.home_ou();
354         sdump('D_AUTH', "user: " + js2JSON(mw.G['user']) + '\n');
355         sdump('D_AUTH', "user_ou: " + js2JSON(mw.G['user_ou']) + '\n');
356         user_async_request(
357                 'open-ils.actor',
358                 'open-ils.actor.org_tree.retrieve',
359                 [],
360                 org_tree_callback
361         );
362         incr_progressmeter(document,'auth_meter',auth_meter_incr);
363 }
364
365 function org_tree_callback(request) {
366         var org_tree;
367         try {
368                 org_tree = request.getResultObject();
369                 if (!org_tree) { throw('null result'); }
370                 if (typeof(org_tree) != 'object') { throw('result not an object' + org_tree); }
371         } catch(E) {
372                 alert('Login failed on org_tree: ' + js2JSON(E)); enable_login_prompts(); return;
373         }
374
375         //mw.G.org_tree = globalOrgTree;
376         mw.G.org_tree = org_tree;
377         mw.G.org_tree_hash = convert_object_list_to_hash( flatten_ou_branch( mw.G.org_tree ) );
378         mw.G.user_ou = find_ou( mw.G.org_tree, mw.G.user_ou );
379
380         /*user_async_request(
381                 'open-ils.actor',
382                 'open-ils.actor.org_types.retrieve',
383                 [ mw.G.auth_ses[0] ],
384                 org_type_callback
385         );*/
386         org_type_callback();
387 }
388
389 function org_type_callback(request) {
390         sdump('D_AUTH',arg_dump(arguments));
391         var aout_list = globalOrgTypes;
392         /*try {
393                 aout_list = request.getResultObject();
394                 if (!aout_list) { throw('null result'); }
395                 if (typeof(aout_list) != 'object') { throw('result not an object' + aout_list); }
396                 if (aout_list.length == 0) { throw('empty aout_list'); }
397         } catch(E) {
398                 alert('Login failed on aout_list: ' + js2JSON(E)); enable_login_prompts(); return;
399         }*/
400         mw.G.aout_list = aout_list;
401         mw.G.aout_hash = convert_object_list_to_hash( aout_list );
402
403         user_async_request(
404                 'open-ils.actor',
405                 'open-ils.actor.org_unit.full_path.retrieve',
406                 [ mw.G.auth_ses[0] ],
407                 my_orgs_callback
408         );
409         incr_progressmeter(document,'auth_meter',auth_meter_incr);
410 }
411
412 function my_orgs_callback(request) {
413         sdump('D_AUTH',arg_dump(arguments));
414         var my_orgs;
415         try {
416                 my_orgs = request.getResultObject();
417                 if (!my_orgs) { throw('null result'); }
418                 if (typeof(my_orgs) != 'object') { throw('result not an object' + my_orgs); }
419                 if (my_orgs.length == 0) { throw('empty my_orgs'); }
420         } catch(E) {
421                 alert('Login failed on my_orgs: ' + js2JSON(E)); 
422                 if (!debug_ignore_auth_failures) {
423                         enable_login_prompts(); return;
424                 }
425         }
426
427         mw.G.my_orgs = my_orgs;
428         mw.G.my_orgs_hash = convert_object_list_to_hash( my_orgs );
429         sdump('D_AUTH','my_orgs = ' + js2JSON(my_orgs) + '\n');
430         mw.G.acpl_my_orgs = filter_list( 
431                 mw.G.acpl_list, 
432                 function (obj) {
433                         if ( typeof obj != 'object' ) return null;
434                         if ( mw.G.my_orgs_hash[ obj.owning_lib() ] ) return obj;
435                 }
436         );
437         mw.G.acpl_my_orgs_hash = convert_object_list_to_hash( mw.G.acpl_my_orgs );
438         //sdump('D_AUTH', 'my_orgs.length = ' + mw.G.my_orgs.length + '   other_orgs.length = ' + mw.G.other_orgs.length + '\n');
439
440         user_async_request(
441                 'open-ils.circ',
442                 'open-ils.circ.stat_cat.actor.retrieve.all',
443                 [ mw.G.auth_ses[0], mw.G.user_ou.id() ],
444                 my_actsc_list_callback
445         );
446
447         incr_progressmeter(document,'auth_meter',auth_meter_incr);
448 }
449
450 function my_actsc_list_callback(request) {
451         sdump('D_AUTH',arg_dump(arguments));
452         var actsc_list;
453         try {
454                 actsc_list = request.getResultObject();
455                 if (!actsc_list) { throw('null result'); }
456                 //if (actsc_list.length == 0) { throw('zero length result'); }
457         } catch(E) {
458                 alert('Login failed on actsc_list: ' + js2JSON(E)); 
459                 if (!debug_ignore_auth_failures) {
460                         enable_login_prompts(); return;
461                 }
462         }
463         mw.G.actsc_list = actsc_list;
464         mw.G.actsc_hash = convert_object_list_to_hash( actsc_list );
465         sdump('D_AUTH', 'actsc_list = ' + js2JSON(actsc_list) + '\n');
466
467         incr_progressmeter(document,'auth_meter',auth_meter_incr);
468
469         spawn_main();
470
471         mw.minimize();
472
473 }
474
475
476 function logoff() {
477         sdump('D_AUTH',arg_dump(arguments));
478         mw.G['auth_ses'] = '';
479         close_all_windows();
480         enable_login_prompts();
481         incr_progressmeter(document,'auth_meter',-100);
482         snd_logoff();
483 }
484
485 function nice_shutdown() {
486         sdump('D_AUTH',arg_dump(arguments));
487         if (ses) { logoff(); ses.disconnect(); }
488         snd_exit;
489         close_all_windows();
490         window.close();
491 }
492