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