]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/staff_client/chrome/content/evergreen/auth/auth.js
some offline stuff
[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         // The related org units for the login user
42         'my_orgs' : [], 'my_orgs_hash' : {},
43
44 /////////////////////////////////////////////////////////////////////////////////////
45
46         // The Org Unit tree
47         'org_tree' : '', 'org_tree_hash' : {},
48
49 /////////////////////////////////////////////////////////////////////////////////////
50
51         // Historically, was the list of actor::profile's, but now it's user groups.
52         'ap_list' : [], 'ap_hash' : {},
53
54         // config::identification_type
55         'cit_list' : [], 'cit_hash' : {},
56
57         // config::standing
58         'cst_list' : [], 'cst_hash' : {},
59
60         // assett::copy_location, and for my_orgs
61         'acpl_list' : [], 'acpl_hash' : {},
62         'acpl_my_orgs' : [], 'acpl_my_orgs_hash' : {},
63
64         // actor::org_unit_type
65         'aout_list' : [], 'aout_hash' : {},
66
67         // config::copy_status
68         'ccs_list' : [], 'ccs_hash' : {},
69
70         // asset::stat_cat.   WHERE IS THIS POPULATED AGAIN?
71         'asc_list' : [],
72
73         // actor::stat_cat
74         'actsc_list' : [], 'actsc_hash' : {},
75
76 /////////////////////////////////////////////////////////////////////////////////////
77
78         'itemsout_header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!\r\nYou have the following items:<hr/><ol>',
79         'itemsout_line_item' : '<li>%TITLE: 50%\r\nBarcode: %COPY_BARCODE% Due: %DUE_D%\r\n',
80         'itemsout_footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%',
81
82         'checkout_header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!\r\nYou checked out the following items:<hr/><ol>',
83         'checkout_line_item' : '<li>%TITLE%\r\nBarcode: %COPY_BARCODE% Due: %DUE_D%\r\n',
84         'checkout_footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%'
85
86 /////////////////////////////////////////////////////////////////////////////////////
87 /////////////////////////////////////////////////////////////////////////////////////
88 };
89
90 var debug_ignore_auth_failures = false;
91
92 var mw = G['main_window'];
93 var auth_meter_incr = 10;
94
95 /////////////////////////////////////////////////////////////////////////////////////
96
97 function auth_init() {
98         sdump('D_AUTH','TESTING: auth.js: ' + mw.G['main_test_variable'] + '\n');
99         sdump('D_AUTH',arg_dump(arguments));
100
101         var np = document.getElementById('name_prompt');
102                 np.addEventListener("keypress",handle_keypress,false);
103                 np.focus();
104         var pp = document.getElementById('password_prompt');
105                 pp.addEventListener("keypress",handle_keypress,false);
106         self.addEventListener("unload",nice_shutdown,false);
107
108         G['sound'] = xp_sound_init(); snd_logon();
109 }
110
111 function handle_keypress(ev) {
112         if (ev.keyCode && ev.keyCode == 13) {
113                 switch(this) {
114                         case document.getElementById('name_prompt') :
115                                 ev.preventDefault();
116                                 var pp = document.getElementById('password_prompt');
117                                 pp.focus(); pp.select();
118                         break;
119                         case document.getElementById('password_prompt') :
120                                 ev.preventDefault();
121                                 var sb = document.getElementById('submit_button');
122                                 sb.focus();
123                                 authenticate();
124                         break;
125                         default:
126                         break;
127                 }
128         }
129 }
130
131 function disable_login_prompts() {
132         sdump('D_AUTH',arg_dump(arguments));
133         disable_widgets(document,'password_prompt','name_prompt','submit_button');
134         G.sound.beep();
135 }
136
137 function enable_login_prompts() {
138         sdump('D_AUTH',arg_dump(arguments));
139         enable_widgets(document,'password_prompt','name_prompt','submit_button');
140         document.getElementById('password_prompt').value = '';
141         var np = document.getElementById('name_prompt');
142                 np.focus(); np.select();
143         document.getElementById('auth_meter').value = 0;
144         document.getElementById('auth_meter').setAttribute('real', '0.0');
145         G.sound.beep();
146 }
147
148 /////////////////////////////////////////////////////////////////////////////////////
149
150 function authenticate() {
151         sdump('D_AUTH',arg_dump(arguments));
152         timer_init('cat');
153         var name = document.getElementById('name_prompt').value;
154         if (name.length == 0) { enable_login_prompts(); return; }
155         // Talk to the system and authenticate the user.
156         user_async_request(
157                 'open-ils.auth',
158                 'open-ils.auth.authenticate.init',
159                 [ name ],
160                 auth_init_callback
161         );
162 }
163
164 function auth_init_callback(request) {
165         sdump('D_AUTH',arg_dump(arguments));
166         var auth_init;
167         try {
168                 auth_init = request.getResultObject();
169                 if (!auth_init) { throw('null result'); }
170         } catch(E) {
171                 G.offline = true;
172                 sdump('D_ERROR','Error trying to communicate with the server.  Entering OFFLINE mode.\n');
173                 s_alert('Error trying to communicate with the server.  Entering OFFLINE mode.\n');
174         }
175
176         sdump( 'D_AUTH', 'D_AUTH_INIT: ' + typeof(auth_init) + ' : ' + auth_init + '\n');
177         var name = document.getElementById('name_prompt').value;
178         var pw = document.getElementById('password_prompt').value;
179         G.name = name; G.pw = pw;
180
181         /////////////////////////////////////////////////////////////////////////////////////////////////////
182         user_async_request(
183                 'open-ils.auth',
184                 'open-ils.auth.authenticate.complete',
185                 [ name, hex_md5(auth_init + hex_md5(pw)) ],
186                 auth_ses_callback
187         );
188         incr_progressmeter(document,'auth_meter',auth_meter_incr);
189 }
190
191 function auth_ses_callback(request) {
192         sdump('D_AUTH',arg_dump(arguments));
193         var auth_ses;
194         try {
195                 auth_ses = request.getResultObject();
196                 if (!auth_ses) { if (!G.offline) { throw('null result'); } }
197                 if (auth_ses == 0) { throw('0 result'); }
198                 if (instanceOf(auth_ses,ex)) {
199                         throw(auth_ses.err_msg());
200                 }
201         } catch(E) {
202                 alert('Login failed on auth_ses: ' + js2JSON(E)); 
203                 if (!debug_ignore_auth_failures) {
204                         enable_login_prompts(); return;
205                 }
206         }
207         mw.G.auth_ses = [ auth_ses ];
208         sdump( 'D_AUTH', 'D_AUTH_SES: ' + typeof(mw.G['auth_ses'][0]) + ' : ' + mw.G['auth_ses'][0] + '\n');
209
210         /////////////////////////////////////////////////////////////////////////////////////////////////////
211         //'open-ils.actor.user.profiles.retrieve',
212         user_async_request(
213                 'open-ils.actor',
214                 'open-ils.actor.groups.retrieve',
215                 [],
216                 ap_list_callback
217         );
218         incr_progressmeter(document,'auth_meter',auth_meter_incr);
219 }
220
221 function ap_list_callback(request) {
222         sdump('D_AUTH',arg_dump(arguments));
223         var ap_file = get_file('ap_list');
224         var ap_list;
225         try {
226                 ap_list = request.getResultObject();
227                 if (!ap_list && G.offline) { 
228                         ap_list = get_object_in_file('ap_list');
229                 }
230                 if (!ap_list) { throw('null result'); }
231                 if (ap_list.length == 0) { throw('zero length result'); }
232         } catch(E) {
233                 handle_error('Login failed on ap_list: ' + js2JSON(E)); 
234                 if (!debug_ignore_auth_failures) {
235                         enable_login_prompts(); return;
236                 }
237         }
238         try { set_object_in_file('ap_list',ap_list); } catch(E) { handle_error(E); }
239         mw.G.ap_list = ap_list;
240         mw.G.ap_hash = convert_object_list_to_hash( ap_list );
241
242         /////////////////////////////////////////////////////////////////////////////////////////////////////
243         user_async_request(
244                 'open-ils.actor',
245                 'open-ils.actor.user.ident_types.retrieve',
246                 [],
247                 cit_list_callback
248         );
249         incr_progressmeter(document,'auth_meter',auth_meter_incr);
250 }
251
252 function cit_list_callback(request) {
253         sdump('D_AUTH',arg_dump(arguments));
254         var cit_list;
255         try {
256                 cit_list = request.getResultObject();
257                 if (!cit_list && G.offline) { cit_list = get_object_in_file('cit_list'); }
258                 if (!cit_list) { throw('null result'); }
259                 if (cit_list.length == 0) { throw('zero length result'); }
260         } catch(E) {
261                 alert('Login failed on cit_list: ' + js2JSON(E)); 
262                 if (!debug_ignore_auth_failures) {
263                         enable_login_prompts(); return;
264                 }
265         }
266         try { set_object_in_file('cit_list',cit_list); } catch(E) { handle_error(E); }
267         mw.G.cit_list = cit_list;
268         mw.G.cit_hash = convert_object_list_to_hash( cit_list );
269         
270         /////////////////////////////////////////////////////////////////////////////////////////////////////
271         user_async_request(
272                 'open-ils.actor',
273                 'open-ils.actor.standings.retrieve',
274                 [],
275                 cst_list_callback
276         );
277
278         incr_progressmeter(document,'auth_meter',auth_meter_incr);
279 }
280
281 function cst_list_callback(request) {
282         sdump('D_AUTH',arg_dump(arguments));
283         var cst_list;
284         try {
285                 cst_list = request.getResultObject();
286                 if (!cst_list && G.offline) { cst_list = get_object_in_file('cst_list'); }
287                 if (!cst_list) { throw('null result'); }
288                 if (cst_list.length == 0) { throw('zero length result'); }
289         } catch(E) {
290                 alert('Login failed on cst_list: ' + js2JSON(E)); 
291                 if (!debug_ignore_auth_failures) {
292                         enable_login_prompts(); return;
293                 }
294         }
295         try { set_object_in_file('cst_list',cst_list); } catch(E) { handle_error(E); }
296         mw.G.cst_list = cst_list;
297         mw.G.cst_hash = convert_object_list_to_hash( cst_list );
298         sdump('D_AUTH', 'cst_list = ' + js2JSON(cst_list) + '\n');
299
300         /////////////////////////////////////////////////////////////////////////////////////////////////////
301         user_async_request(
302                 'open-ils.search',
303                 'open-ils.search.config.copy_location.retrieve.all',
304                 [],
305                 acpl_list_callback
306         );
307         incr_progressmeter(document,'auth_meter',auth_meter_incr);
308
309 }
310
311 function acpl_list_callback(request) {
312         sdump('D_AUTH',arg_dump(arguments));
313         var acpl_list;
314         try {
315                 acpl_list = request.getResultObject();
316                 if (!acpl_list && G.offline) { acpl_list = get_object_in_file('acpl_list'); }
317                 if (!acpl_list) { throw('null result'); }
318                 if (acpl_list.length == 0) { throw('zero length result'); }
319         } catch(E) {
320                 alert('Login failed on acpl_list: ' + js2JSON(E)); 
321                 if (!debug_ignore_auth_failures) {
322                         enable_login_prompts(); return;
323                 }
324         }
325         try { set_object_in_file('acpl_list',acpl_list); } catch(E) { handle_error(E); }
326         mw.G.acpl_list = acpl_list;
327         mw.G.acpl_hash = convert_object_list_to_hash( acpl_list );
328         sdump('D_AUTH', 'acpl_list = ' + js2JSON(acpl_list) + '\n');
329
330         /////////////////////////////////////////////////////////////////////////////////////////////////////
331         user_async_request(
332                 'open-ils.search',
333                 'open-ils.search.config.copy_status.retrieve.all',
334                 [],
335                 ccs_list_callback
336         );
337         incr_progressmeter(document,'auth_meter',auth_meter_incr);
338 }
339
340 function ccs_list_callback(request) {
341         sdump('D_AUTH',arg_dump(arguments));
342         var ccs_list;
343         try {
344                 ccs_list = request.getResultObject();
345                 if (!ccs_list && G.offline) { ccs_list = get_object_in_file('ccs_list'); }
346                 if (!ccs_list) { throw('null result'); }
347                 if (ccs_list.length == 0) { throw('zero length result'); }
348         } catch(E) {
349                 alert('Login failed on ccs_list: ' + js2JSON(E)); 
350                 if (!debug_ignore_auth_failures) {
351                         enable_login_prompts(); return;
352                 }
353         }
354         try { set_object_in_file('ccs_list',ccs_list); } catch(E) { handle_error(E); }
355         mw.G.ccs_list = ccs_list;
356         mw.G.ccs_hash = convert_object_list_to_hash( ccs_list );
357         sdump('D_AUTH', 'ccs_list = ' + js2JSON(ccs_list) + '\n');
358
359         /////////////////////////////////////////////////////////////////////////////////////////////////////
360         user_async_request(
361                 'open-ils.search',
362                 'open-ils.search.actor.user.session',
363                 [ mw.G['auth_ses'][0] ],
364                 user_callback
365         );
366         incr_progressmeter(document,'auth_meter',auth_meter_incr);
367 }
368
369 function user_callback(request) {
370         sdump('D_AUTH',arg_dump(arguments));
371         var user;
372         var user_ou;
373         try {
374                 user = request.getResultObject();
375                 if (!user && G.offline) { 
376                         user = new au(); 
377                         user.home_ou( get_object_in_file('user_ou') );
378                 }
379                 if (!user) { throw('null result'); }
380                 if (typeof(user) != 'object') { throw('result not an object' + user); }
381         } catch(E) {
382                 alert('Login failed on user: ' + js2JSON(E)); 
383                 if (!debug_ignore_auth_failures) {
384                         enable_login_prompts(); return;
385                 }
386         }
387         try { set_object_in_file('user_ou',user_ou); } catch(E) { handle_error(E); }
388         mw.G.user = user;
389         mw.G.user_ou = user.home_ou();
390         sdump('D_AUTH', "user: " + js2JSON(mw.G['user']) + '\n');
391         sdump('D_AUTH', "user_ou: " + js2JSON(mw.G['user_ou']) + '\n');
392
393         /////////////////////////////////////////////////////////////////////////////////////////////////////
394         user_async_request(
395                 'open-ils.actor',
396                 'open-ils.actor.org_tree.retrieve',
397                 [],
398                 org_tree_callback
399         );
400         incr_progressmeter(document,'auth_meter',auth_meter_incr);
401 }
402
403 function org_tree_callback(request) {
404         var org_tree;
405         try {
406                 org_tree = request.getResultObject();
407                 if (!org_tree && G.offline) { org_tree = get_object_in_file('org_tree'); }
408                 if (!org_tree) { throw('null result'); }
409                 if (typeof(org_tree) != 'object') { throw('result not an object' + org_tree); }
410         } catch(E) {
411                 alert('Login failed on org_tree: ' + js2JSON(E)); enable_login_prompts(); return;
412         }
413
414         //mw.G.org_tree = globalOrgTree;
415         try { set_object_in_file('org_tree',org_tree); } catch(E) { handle_error(E); }
416         mw.G.org_tree = org_tree;
417         mw.G.org_tree_hash = convert_object_list_to_hash( flatten_ou_branch( mw.G.org_tree ) );
418         mw.G.user_ou = find_ou( mw.G.org_tree, mw.G.user_ou );
419
420         /////////////////////////////////////////////////////////////////////////////////////////////////////
421         /*user_async_request(
422                 'open-ils.actor',
423                 'open-ils.actor.org_types.retrieve',
424                 [ mw.G.auth_ses[0] ],
425                 org_type_callback
426         );*/
427         org_type_callback();
428 }
429
430 function org_type_callback(request) {
431         sdump('D_AUTH',arg_dump(arguments));
432         var aout_list = globalOrgTypes;
433         /*try {
434                 aout_list = request.getResultObject();
435                 if (!aout_list) { throw('null result'); }
436                 if (typeof(aout_list) != 'object') { throw('result not an object' + aout_list); }
437                 if (aout_list.length == 0) { throw('empty aout_list'); }
438         } catch(E) {
439                 alert('Login failed on aout_list: ' + js2JSON(E)); enable_login_prompts(); return;
440         }*/
441         mw.G.aout_list = aout_list;
442         mw.G.aout_hash = convert_object_list_to_hash( aout_list );
443
444         /////////////////////////////////////////////////////////////////////////////////////////////////////
445         user_async_request(
446                 'open-ils.actor',
447                 'open-ils.actor.org_unit.full_path.retrieve',
448                 [ mw.G.auth_ses[0] ],
449                 my_orgs_callback
450         );
451         incr_progressmeter(document,'auth_meter',auth_meter_incr);
452 }
453
454 function my_orgs_callback(request) {
455         sdump('D_AUTH',arg_dump(arguments));
456         var my_orgs;
457         try {
458                 my_orgs = request.getResultObject();
459                 if (!my_orgs && G.offline) { my_orgs = get_object_in_file('my_orgs'); }
460                 if (!my_orgs) { throw('null result'); }
461                 if (typeof(my_orgs) != 'object') { throw('result not an object' + my_orgs); }
462                 if (my_orgs.length == 0) { throw('empty my_orgs'); }
463         } catch(E) {
464                 alert('Login failed on my_orgs: ' + js2JSON(E)); 
465                 if (!debug_ignore_auth_failures) {
466                         enable_login_prompts(); return;
467                 }
468         }
469
470         try { set_object_in_file('my_orgs',my_orgs); } catch(E) { handle_error(E); }
471         mw.G.my_orgs = my_orgs;
472         mw.G.my_orgs_hash = convert_object_list_to_hash( my_orgs );
473         sdump('D_AUTH','my_orgs = ' + js2JSON(my_orgs) + '\n');
474         mw.G.acpl_my_orgs = filter_list( 
475                 mw.G.acpl_list, 
476                 function (obj) {
477                         if ( typeof obj != 'object' ) return null;
478                         if ( mw.G.my_orgs_hash[ obj.owning_lib() ] ) return obj;
479                 }
480         );
481         mw.G.acpl_my_orgs_hash = convert_object_list_to_hash( mw.G.acpl_my_orgs );
482         //sdump('D_AUTH', 'my_orgs.length = ' + mw.G.my_orgs.length + '   other_orgs.length = ' + mw.G.other_orgs.length + '\n');
483
484         /////////////////////////////////////////////////////////////////////////////////////////////////////
485         user_async_request(
486                 'open-ils.circ',
487                 'open-ils.circ.stat_cat.actor.retrieve.all',
488                 [ mw.G.auth_ses[0], mw.G.user_ou.id() ],
489                 my_actsc_list_callback
490         );
491
492         incr_progressmeter(document,'auth_meter',auth_meter_incr);
493 }
494
495 function my_actsc_list_callback(request) {
496         sdump('D_AUTH',arg_dump(arguments));
497         var actsc_list;
498         try {
499                 actsc_list = request.getResultObject();
500                 if (!actsc_list && G.offline) { actsc_list = get_object_in_file('actsc_list'); }
501                 if (!actsc_list) { throw('null result'); }
502                 //if (actsc_list.length == 0) { throw('zero length result'); }
503         } catch(E) {
504                 alert('Login failed on actsc_list: ' + js2JSON(E)); 
505                 if (!debug_ignore_auth_failures) {
506                         enable_login_prompts(); return;
507                 }
508         }
509         try { set_object_in_file('actsc_list',actsc_list); } catch(E) { handle_error(E); }
510         mw.G.actsc_list = actsc_list;
511         mw.G.actsc_hash = convert_object_list_to_hash( actsc_list );
512         sdump('D_AUTH', 'actsc_list = ' + js2JSON(actsc_list) + '\n');
513
514         incr_progressmeter(document,'auth_meter',auth_meter_incr);
515
516         spawn_main();
517
518         mw.minimize();
519
520 }
521
522
523 function logoff() {
524         sdump('D_AUTH',arg_dump(arguments));
525         mw.G['auth_ses'] = '';
526         close_all_windows();
527         enable_login_prompts();
528         incr_progressmeter(document,'auth_meter',-100);
529         snd_logoff();
530 }
531
532 function nice_shutdown() {
533         sdump('D_AUTH',arg_dump(arguments));
534         if (ses) { logoff(); ses.disconnect(); }
535         snd_exit;
536         close_all_windows();
537         window.close();
538 }
539