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