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