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