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