]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/staff_client/chrome/content/evergreen/auth/auth.js
4083dedd0051cdf252428942d49965fb8d6474ba
[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         var pref = Components.classes["@mozilla.org/preferences-service;1"]
110                 .getService(Components.interfaces.nsIPrefBranch);
111         if (pref) {
112                 pref.setCharPref("capability.principal.codebase.p0.granted", "UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead");
113                 pref.setCharPref("capability.principal.codebase.p0.id", "http://dev.gapines.org");
114         }
115 }
116
117 function handle_keypress(ev) {
118         if (ev.keyCode && ev.keyCode == 13) {
119                 switch(this) {
120                         case document.getElementById('name_prompt') :
121                                 ev.preventDefault();
122                                 var pp = document.getElementById('password_prompt');
123                                 pp.focus(); pp.select();
124                         break;
125                         case document.getElementById('password_prompt') :
126                                 ev.preventDefault();
127                                 var sb = document.getElementById('submit_button');
128                                 sb.focus();
129                                 authenticate();
130                         break;
131                         default:
132                         break;
133                 }
134         }
135 }
136
137 function disable_login_prompts() {
138         sdump('D_AUTH',arg_dump(arguments));
139         disable_widgets(document,'password_prompt','name_prompt','submit_button');
140         G.sound.beep();
141 }
142
143 function enable_login_prompts() {
144         sdump('D_AUTH',arg_dump(arguments));
145         enable_widgets(document,'password_prompt','name_prompt','submit_button');
146         document.getElementById('password_prompt').value = '';
147         var np = document.getElementById('name_prompt');
148                 np.focus(); np.select();
149         document.getElementById('auth_meter').value = 0;
150         document.getElementById('auth_meter').setAttribute('real', '0.0');
151         G.sound.beep();
152 }
153
154 /////////////////////////////////////////////////////////////////////////////////////
155
156 function authenticate() {
157         sdump('D_AUTH',arg_dump(arguments));
158         timer_init('cat');
159         var name = document.getElementById('name_prompt').value;
160         if (name.length == 0) { enable_login_prompts(); return; }
161         // Talk to the system and authenticate the user.
162         user_async_request(
163                 'open-ils.auth',
164                 'open-ils.auth.authenticate.init',
165                 [ name ],
166                 auth_init_callback
167         );
168 }
169
170 function auth_init_callback(request) {
171         sdump('D_AUTH',arg_dump(arguments));
172         var auth_init;
173         try {
174                 auth_init = request.getResultObject();
175                 if (!auth_init) { throw('null result'); }
176         } catch(E) {
177                 G.offline = true;
178                 sdump('D_ERROR','Error trying to communicate with the server.  Entering OFFLINE mode.\n' + js2JSON(E) + '\n');
179                 s_alert('Error trying to communicate with the server.  Entering OFFLINE mode.\n' + js2JSON(E) + '\n');
180         }
181
182         sdump( 'D_AUTH', 'D_AUTH_INIT: ' + typeof(auth_init) + ' : ' + auth_init + '\n');
183         var name = document.getElementById('name_prompt').value;
184         var pw = document.getElementById('password_prompt').value;
185         G.name = name; G.pw = pw;
186
187         /////////////////////////////////////////////////////////////////////////////////////////////////////
188         user_async_request(
189                 'open-ils.auth',
190                 'open-ils.auth.authenticate.complete',
191                 [ name, hex_md5(auth_init + hex_md5(pw)) ],
192                 auth_ses_callback
193         );
194         incr_progressmeter(document,'auth_meter',auth_meter_incr);
195 }
196
197 function auth_ses_callback(request) {
198         sdump('D_AUTH',arg_dump(arguments));
199         var auth_ses;
200         try {
201                 auth_ses = request.getResultObject();
202                 if (!auth_ses) { if (!G.offline) { throw('null result'); } }
203                 if (auth_ses == 0) { throw('0 result'); }
204                 if (instanceOf(auth_ses,ex)) {
205                         throw(auth_ses.err_msg());
206                 }
207         } catch(E) {
208                 alert('Login failed on auth_ses: ' + js2JSON(E)); 
209                 if (!debug_ignore_auth_failures) {
210                         enable_login_prompts(); return;
211                 }
212         }
213         mw.G.auth_ses = [ auth_ses ];
214         sdump( 'D_AUTH', 'D_AUTH_SES: ' + typeof(mw.G['auth_ses'][0]) + ' : ' + mw.G['auth_ses'][0] + '\n');
215
216         /////////////////////////////////////////////////////////////////////////////////////////////////////
217         //'open-ils.actor.user.profiles.retrieve',
218         user_async_request(
219                 'open-ils.actor',
220                 'open-ils.actor.groups.retrieve',
221                 [],
222                 ap_list_callback
223         );
224         incr_progressmeter(document,'auth_meter',auth_meter_incr);
225 }
226
227 function ap_list_callback(request) {
228         sdump('D_AUTH',arg_dump(arguments));
229         //var ap_file = get_file('ap_list');
230         var ap_list;
231         try {
232                 ap_list = request.getResultObject();
233                 if (!ap_list && G.offline) { 
234                         //ap_list = get_object_in_file('ap_list');
235                 }
236                 if (!ap_list) { throw('null result'); }
237                 if (ap_list.length == 0) { throw('zero length result'); }
238         } catch(E) {
239                 handle_error('Login failed on ap_list: ' + js2JSON(E)); 
240                 if (!debug_ignore_auth_failures) {
241                         enable_login_prompts(); return;
242                 }
243         }
244         //try { set_object_in_file('ap_list',ap_list); } catch(E) { handle_error(E); }
245         mw.G.ap_list = ap_list;
246         mw.G.ap_hash = convert_object_list_to_hash( ap_list );
247
248         /////////////////////////////////////////////////////////////////////////////////////////////////////
249         user_async_request(
250                 'open-ils.actor',
251                 'open-ils.actor.user.ident_types.retrieve',
252                 [],
253                 cit_list_callback
254         );
255         incr_progressmeter(document,'auth_meter',auth_meter_incr);
256 }
257
258 function cit_list_callback(request) {
259         sdump('D_AUTH',arg_dump(arguments));
260         var cit_list;
261         try {
262                 cit_list = request.getResultObject();
263                 //if (!cit_list && G.offline) { cit_list = get_object_in_file('cit_list'); }
264                 if (!cit_list) { throw('null result'); }
265                 if (cit_list.length == 0) { throw('zero length result'); }
266         } catch(E) {
267                 alert('Login failed on cit_list: ' + js2JSON(E)); 
268                 if (!debug_ignore_auth_failures) {
269                         enable_login_prompts(); return;
270                 }
271         }
272         //try { set_object_in_file('cit_list',cit_list); } catch(E) { handle_error(E); }
273         mw.G.cit_list = cit_list;
274         mw.G.cit_hash = convert_object_list_to_hash( cit_list );
275         
276         /////////////////////////////////////////////////////////////////////////////////////////////////////
277         user_async_request(
278                 'open-ils.actor',
279                 'open-ils.actor.standings.retrieve',
280                 [],
281                 cst_list_callback
282         );
283
284         incr_progressmeter(document,'auth_meter',auth_meter_incr);
285 }
286
287 function cst_list_callback(request) {
288         sdump('D_AUTH',arg_dump(arguments));
289         var cst_list;
290         try {
291                 cst_list = request.getResultObject();
292                 //if (!cst_list && G.offline) { cst_list = get_object_in_file('cst_list'); }
293                 if (!cst_list) { throw('null result'); }
294                 if (cst_list.length == 0) { throw('zero length result'); }
295         } catch(E) {
296                 alert('Login failed on cst_list: ' + js2JSON(E)); 
297                 if (!debug_ignore_auth_failures) {
298                         enable_login_prompts(); return;
299                 }
300         }
301         //try { set_object_in_file('cst_list',cst_list); } catch(E) { handle_error(E); }
302         mw.G.cst_list = cst_list;
303         mw.G.cst_hash = convert_object_list_to_hash( cst_list );
304         sdump('D_AUTH', 'cst_list = ' + js2JSON(cst_list) + '\n');
305
306         /////////////////////////////////////////////////////////////////////////////////////////////////////
307         user_async_request(
308                 'open-ils.search',
309                 'open-ils.search.config.copy_location.retrieve.all',
310                 [],
311                 acpl_list_callback
312         );
313         incr_progressmeter(document,'auth_meter',auth_meter_incr);
314
315 }
316
317 function acpl_list_callback(request) {
318         sdump('D_AUTH',arg_dump(arguments));
319         var acpl_list;
320         try {
321                 acpl_list = request.getResultObject();
322                 //if (!acpl_list && G.offline) { acpl_list = get_object_in_file('acpl_list'); }
323                 if (!acpl_list) { throw('null result'); }
324                 if (acpl_list.length == 0) { throw('zero length result'); }
325         } catch(E) {
326                 alert('Login failed on acpl_list: ' + js2JSON(E)); 
327                 if (!debug_ignore_auth_failures) {
328                         enable_login_prompts(); return;
329                 }
330         }
331         //try { set_object_in_file('acpl_list',acpl_list); } catch(E) { handle_error(E); }
332         mw.G.acpl_list = acpl_list;
333         mw.G.acpl_hash = convert_object_list_to_hash( acpl_list );
334         sdump('D_AUTH', 'acpl_list = ' + js2JSON(acpl_list) + '\n');
335
336         /////////////////////////////////////////////////////////////////////////////////////////////////////
337         user_async_request(
338                 'open-ils.search',
339                 'open-ils.search.config.copy_status.retrieve.all',
340                 [],
341                 ccs_list_callback
342         );
343         incr_progressmeter(document,'auth_meter',auth_meter_incr);
344 }
345
346 function ccs_list_callback(request) {
347         sdump('D_AUTH',arg_dump(arguments));
348         var ccs_list;
349         try {
350                 ccs_list = request.getResultObject();
351                 //if (!ccs_list && G.offline) { ccs_list = get_object_in_file('ccs_list'); }
352                 if (!ccs_list) { throw('null result'); }
353                 if (ccs_list.length == 0) { throw('zero length result'); }
354         } catch(E) {
355                 alert('Login failed on ccs_list: ' + js2JSON(E)); 
356                 if (!debug_ignore_auth_failures) {
357                         enable_login_prompts(); return;
358                 }
359         }
360         //try { set_object_in_file('ccs_list',ccs_list); } catch(E) { handle_error(E); }
361         mw.G.ccs_list = ccs_list;
362         mw.G.ccs_hash = convert_object_list_to_hash( ccs_list );
363         sdump('D_AUTH', 'ccs_list = ' + js2JSON(ccs_list) + '\n');
364
365         /////////////////////////////////////////////////////////////////////////////////////////////////////
366         user_async_request(
367                 'open-ils.search',
368                 'open-ils.search.actor.user.session',
369                 [ mw.G['auth_ses'][0] ],
370                 user_callback
371         );
372         incr_progressmeter(document,'auth_meter',auth_meter_incr);
373 }
374
375 function user_callback(request) {
376         sdump('D_AUTH',arg_dump(arguments));
377         var user;
378         var user_ou;
379         try {
380                 user = request.getResultObject();
381                 if (!user && G.offline) { 
382                         //user = new au(); 
383                         //user.home_ou( get_object_in_file('user_ou') );
384                 }
385                 if (!user) { throw('null result'); }
386                 if (typeof(user) != 'object') { throw('result not an object' + user); }
387         } catch(E) {
388                 alert('Login failed on user: ' + js2JSON(E)); 
389                 if (!debug_ignore_auth_failures) {
390                         enable_login_prompts(); return;
391                 }
392         }
393         //try { set_object_in_file('user_ou',user.home_ou()); } catch(E) { handle_error(E); }
394         mw.G.user = user;
395         mw.G.user_ou = user.home_ou();
396         sdump('D_AUTH', "user: " + js2JSON(mw.G['user']) + '\n');
397         sdump('D_AUTH', "user_ou: " + js2JSON(mw.G['user_ou']) + '\n');
398
399         /////////////////////////////////////////////////////////////////////////////////////////////////////
400         user_async_request(
401                 'open-ils.actor',
402                 'open-ils.actor.org_tree.retrieve',
403                 [],
404                 org_tree_callback
405         );
406         incr_progressmeter(document,'auth_meter',auth_meter_incr);
407 }
408
409 function org_tree_callback(request) {
410         var org_tree;
411         try {
412                 org_tree = request.getResultObject();
413                 //if (!org_tree && G.offline) { org_tree = get_object_in_file('org_tree'); }
414                 if (!org_tree) { throw('null result'); }
415                 if (typeof(org_tree) != 'object') { throw('result not an object' + org_tree); }
416         } catch(E) {
417                 alert('Login failed on org_tree: ' + js2JSON(E)); enable_login_prompts(); return;
418         }
419
420         //mw.G.org_tree = globalOrgTree;
421         //try { set_object_in_file('org_tree',org_tree); } catch(E) { handle_error(E); }
422         mw.G.org_tree = org_tree;
423         mw.G.org_tree_hash = convert_object_list_to_hash( flatten_ou_branch( mw.G.org_tree ) );
424         mw.G.user_ou = find_ou( mw.G.org_tree, mw.G.user_ou );
425
426         /////////////////////////////////////////////////////////////////////////////////////////////////////
427         /*user_async_request(
428                 'open-ils.actor',
429                 'open-ils.actor.org_types.retrieve',
430                 [ mw.G.auth_ses[0] ],
431                 org_type_callback
432         );*/
433         org_type_callback();
434 }
435
436 function org_type_callback(request) {
437         sdump('D_AUTH',arg_dump(arguments));
438         var aout_list = globalOrgTypes;
439         /*try {
440                 aout_list = request.getResultObject();
441                 if (!aout_list) { throw('null result'); }
442                 if (typeof(aout_list) != 'object') { throw('result not an object' + aout_list); }
443                 if (aout_list.length == 0) { throw('empty aout_list'); }
444         } catch(E) {
445                 alert('Login failed on aout_list: ' + js2JSON(E)); enable_login_prompts(); return;
446         }*/
447         mw.G.aout_list = aout_list;
448         mw.G.aout_hash = convert_object_list_to_hash( aout_list );
449
450         /////////////////////////////////////////////////////////////////////////////////////////////////////
451         user_async_request(
452                 'open-ils.actor',
453                 'open-ils.actor.org_unit.full_path.retrieve',
454                 [ mw.G.auth_ses[0] ],
455                 my_orgs_callback
456         );
457         incr_progressmeter(document,'auth_meter',auth_meter_incr);
458 }
459
460 function my_orgs_callback(request) {
461         sdump('D_AUTH',arg_dump(arguments));
462         var my_orgs;
463         try {
464                 my_orgs = request.getResultObject();
465                 //if (!my_orgs && G.offline) { my_orgs = get_object_in_file('my_orgs'); }
466                 if (!my_orgs) { throw('null result'); }
467                 if (typeof(my_orgs) != 'object') { throw('result not an object' + my_orgs); }
468                 if (my_orgs.length == 0) { throw('empty my_orgs'); }
469         } catch(E) {
470                 alert('Login failed on my_orgs: ' + js2JSON(E)); 
471                 if (!debug_ignore_auth_failures) {
472                         enable_login_prompts(); return;
473                 }
474         }
475
476         //try { set_object_in_file('my_orgs',my_orgs); } catch(E) { handle_error(E); }
477         mw.G.my_orgs = my_orgs;
478         mw.G.my_orgs_hash = convert_object_list_to_hash( my_orgs );
479         sdump('D_AUTH','my_orgs = ' + js2JSON(my_orgs) + '\n');
480         mw.G.acpl_my_orgs = filter_list( 
481                 mw.G.acpl_list, 
482                 function (obj) {
483                         if ( typeof obj != 'object' ) return null;
484                         if ( mw.G.my_orgs_hash[ obj.owning_lib() ] ) return obj;
485                 }
486         );
487         mw.G.acpl_my_orgs_hash = convert_object_list_to_hash( mw.G.acpl_my_orgs );
488         //sdump('D_AUTH', 'my_orgs.length = ' + mw.G.my_orgs.length + '   other_orgs.length = ' + mw.G.other_orgs.length + '\n');
489
490         /////////////////////////////////////////////////////////////////////////////////////////////////////
491         user_async_request(
492                 'open-ils.circ',
493                 'open-ils.circ.stat_cat.actor.retrieve.all',
494                 [ mw.G.auth_ses[0], mw.G.user_ou.id() ],
495                 my_actsc_list_callback
496         );
497
498         incr_progressmeter(document,'auth_meter',auth_meter_incr);
499 }
500
501 function my_actsc_list_callback(request) {
502         sdump('D_AUTH',arg_dump(arguments));
503         var actsc_list;
504         try {
505                 actsc_list = request.getResultObject();
506                 //if (!actsc_list && G.offline) { actsc_list = get_object_in_file('actsc_list'); }
507                 if (!actsc_list) { throw('null result'); }
508                 //if (actsc_list.length == 0) { throw('zero length result'); }
509         } catch(E) {
510                 alert('Login failed on actsc_list: ' + js2JSON(E)); 
511                 if (!debug_ignore_auth_failures) {
512                         enable_login_prompts(); return;
513                 }
514         }
515         //try { set_object_in_file('actsc_list',actsc_list); } catch(E) { handle_error(E); }
516         mw.G.actsc_list = actsc_list;
517         mw.G.actsc_hash = convert_object_list_to_hash( actsc_list );
518         sdump('D_AUTH', 'actsc_list = ' + js2JSON(actsc_list) + '\n');
519
520         incr_progressmeter(document,'auth_meter',auth_meter_incr);
521
522         spawn_main();
523
524         mw.minimize();
525
526 }
527
528
529 function logoff() {
530         sdump('D_AUTH',arg_dump(arguments));
531         mw.G['auth_ses'] = '';
532         close_all_windows();
533         enable_login_prompts();
534         incr_progressmeter(document,'auth_meter',-100);
535         snd_logoff();
536 }
537
538 function nice_shutdown() {
539         sdump('D_AUTH',arg_dump(arguments));
540         if (ses) { logoff(); ses.disconnect(); }
541         snd_exit;
542         close_all_windows();
543         window.close();
544 }
545