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