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