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