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