]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/staff_client/chrome/content/evergreen/auth/auth.js
some smarter debugging
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / auth / auth.js
1 var G = {}; // the master Global variable
2 G['main_window'] = self;
3 G['win_list'] = new Array();
4 G['window_name_increment'] = 0;
5 G['auth_ses'] = '';
6 G['user_ou'] = '';
7 G['main_test_variable'] = 'Hello World';
8 G['org_tree'] = '';
9 G['my_orgs'] = [];
10 G['my_orgs_hash'] = {};
11 G['fieldmap'] = '';
12 G['patrons'] = {};
13
14 G['ap_list'] = []; // actor::profile
15 G['ap_hash'] = {};
16 G['cit_list'] = []; // config::identification_type
17 G['cit_hash'] = {};
18 G['cst_list'] = []; // config::standing
19 G['cst_hash'] = {};
20 G['acpl_list'] = []; // asset::copy_location
21 G['acpl_hash'] = {}; G['acpl_my_orgs'] = []; G['acpl_my_orgs_hash'] = {};
22 G['aout_list'] = []; // actor::org_unit_type
23 G['aout_hash'] = {};
24 G['ccs_list'] = []; // config::copy_status
25 G['ccs_hash'] = {};
26 G['asc_list'] = []; // asset::stat_cat
27 G['actsc_list'] = []; // actor::stat_cat
28 G['actsc_hash']; // actor::stat_cat
29
30 var mw = G['main_window'];
31 var auth_meter_per = 10;
32
33 function auth_init() {
34         sdump('D_AUTH','TESTING: auth.js: ' + mw.G['main_test_variable'] + '\n');
35         var np = document.getElementById('name_prompt');
36         np.addEventListener("keypress",handle_keypress,false);
37         np.focus();
38         var pp = document.getElementById('password_prompt');
39         pp.addEventListener("keypress",handle_keypress,false);
40         self.addEventListener("unload",nice_shutdown,false);
41         G['sound'] = xp_sound_init(); 
42         //G.sound.beep();
43         snd_logon();
44 }
45
46 function handle_keypress(ev) {
47         if (ev.keyCode && ev.keyCode == 13) {
48                 switch(this) {
49                         case document.getElementById('name_prompt') :
50                                 ev.preventDefault();
51                                 var pp = document.getElementById('password_prompt');
52                                 pp.focus(); pp.select();
53                         break;
54                         case document.getElementById('password_prompt') :
55                                 ev.preventDefault();
56                                 var sb = document.getElementById('submit_button');
57                                 sb.focus();
58                                 authenticate();
59                         break;
60                         default:
61                         break;
62                 }
63         }
64 }
65
66 function disable_login_prompts() {
67         disable_widgets('password_prompt','name_prompt','submit_button');
68         G.sound.beep();
69 }
70
71 function enable_login_prompts() {
72         enable_widgets('password_prompt','name_prompt','submit_button');
73         document.getElementById('password_prompt').value = '';
74         var np = document.getElementById('name_prompt');
75         np.focus(); np.select();
76         document.getElementById('auth_meter').value = 0;
77         G.sound.beep();
78 }
79
80 function authenticate() {
81         timer_init('cat');
82         var name = document.getElementById('name_prompt').value;
83         if (name.length == 0) { enable_login_prompts(); return; }
84         // Talk to the system and authenticate the user.
85         user_async_request(
86                 'open-ils.auth',
87                 'open-ils.auth.authenticate.init',
88                 [ name ],
89                 auth_init_callback
90         );
91 }
92
93 function auth_init_callback(request) {
94         var auth_init;
95         try {
96                 auth_init = request.getResultObject();
97                 if (!auth_init) { throw('null result'); }
98         } catch(E) {
99                 alert('Login failed on auth_init: ' + js2JSON(E)); enable_login_prompts(); return;
100         }
101
102         sdump( 'D_AUTH', 'D_AUTH_INIT: ' + typeof(auth_init) + ' : ' + auth_init + '\n');
103         var name = document.getElementById('name_prompt').value;
104         var pw = document.getElementById('password_prompt').value;
105
106         user_async_request(
107                 'open-ils.auth',
108                 'open-ils.auth.authenticate.complete',
109                 [ name, hex_md5(auth_init + hex_md5(pw)) ],
110                 auth_ses_callback
111         );
112         document.getElementById('auth_meter').value += auth_meter_per;
113 }
114
115 function auth_ses_callback(request) {
116         var auth_ses;
117         try {
118                 auth_ses = request.getResultObject();
119                 if (!auth_ses) { throw('null result'); }
120                 if (auth_ses == 0) { throw('0 result'); }
121         } catch(E) {
122                 alert('Login failed on auth_ses: ' + js2JSON(E)); enable_login_prompts(); return;
123         }
124         mw.G.auth_ses = [ auth_ses ];
125         sdump( 'D_AUTH', 'D_AUTH_SES: ' + typeof(mw.G['auth_ses'][0]) + ' : ' + mw.G['auth_ses'][0] + '\n');
126
127         user_async_request(
128                 'open-ils.actor',
129                 'open-ils.actor.user.profiles.retrieve',
130                 [],
131                 ap_list_callback
132         );
133         document.getElementById('auth_meter').value += auth_meter_per;
134 }
135
136 function ap_list_callback(request) {
137         var ap_list;
138         try {
139                 ap_list = request.getResultObject();
140                 if (!ap_list) { throw('null result'); }
141                 if (ap_list.length == 0) { throw('zero length result'); }
142         } catch(E) {
143                 alert('Login failed on ap_list: ' + js2JSON(E)); enable_login_prompts(); return;
144         }
145         mw.G.ap_list = ap_list;
146         mw.G.ap_hash = convert_object_list_to_hash( ap_list );
147
148         user_async_request(
149                 'open-ils.actor',
150                 'open-ils.actor.user.ident_types.retrieve',
151                 [],
152                 cit_list_callback
153         );
154         document.getElementById('auth_meter').value += auth_meter_per;
155 }
156
157 function cit_list_callback(request) {
158         var cit_list;
159         try {
160                 cit_list = request.getResultObject();
161                 if (!cit_list) { throw('null result'); }
162                 if (cit_list.length == 0) { throw('zero length result'); }
163         } catch(E) {
164                 alert('Login failed on cit_list: ' + js2JSON(E)); enable_login_prompts(); return;
165         }
166         mw.G.cit_list = cit_list;
167         mw.G.cit_hash = convert_object_list_to_hash( cit_list );
168         
169         user_async_request(
170                 'open-ils.actor',
171                 'open-ils.actor.standings.retrieve',
172                 [],
173                 cst_list_callback
174         );
175
176         document.getElementById('auth_meter').value += auth_meter_per;
177 }
178
179 function cst_list_callback(request) {
180         var cst_list;
181         try {
182                 cst_list = request.getResultObject();
183                 if (!cst_list) { throw('null result'); }
184                 if (cst_list.length == 0) { throw('zero length result'); }
185         } catch(E) {
186                 alert('Login failed on cst_list: ' + js2JSON(E)); enable_login_prompts(); return;
187         }
188         mw.G.cst_list = cst_list;
189         mw.G.cst_hash = convert_object_list_to_hash( cst_list );
190         sdump('D_AUTH', 'cst_list = ' + js2JSON(cst_list) + '\n');
191
192         user_async_request(
193                 'open-ils.search',
194                 'open-ils.search.config.copy_location.retrieve.all',
195                 [],
196                 acpl_list_callback
197         );
198         document.getElementById('auth_meter').value += auth_meter_per;
199
200 }
201
202 function acpl_list_callback(request) {
203         var acpl_list;
204         try {
205                 acpl_list = request.getResultObject();
206                 if (!acpl_list) { throw('null result'); }
207                 if (acpl_list.length == 0) { throw('zero length result'); }
208         } catch(E) {
209                 alert('Login failed on acpl_list: ' + js2JSON(E)); enable_login_prompts(); return;
210         }
211         mw.G.acpl_list = acpl_list;
212         mw.G.acpl_hash = convert_object_list_to_hash( acpl_list );
213         sdump('D_AUTH', 'acpl_list = ' + js2JSON(acpl_list) + '\n');
214
215         user_async_request(
216                 'open-ils.search',
217                 'open-ils.search.config.copy_status.retrieve.all',
218                 [],
219                 ccs_list_callback
220         );
221         document.getElementById('auth_meter').value += auth_meter_per;
222 }
223
224 function ccs_list_callback(request) {
225         var ccs_list;
226         try {
227                 ccs_list = request.getResultObject();
228                 if (!ccs_list) { throw('null result'); }
229                 if (ccs_list.length == 0) { throw('zero length result'); }
230         } catch(E) {
231                 alert('Login failed on ccs_list: ' + js2JSON(E)); enable_login_prompts(); return;
232         }
233         mw.G.ccs_list = ccs_list;
234         mw.G.ccs_hash = convert_object_list_to_hash( ccs_list );
235         sdump('D_AUTH', 'ccs_list = ' + js2JSON(ccs_list) + '\n');
236
237         user_async_request(
238                 'open-ils.search',
239                 'open-ils.search.actor.user.session',
240                 [ mw.G['auth_ses'][0] ],
241                 user_callback
242         );
243         document.getElementById('auth_meter').value += auth_meter_per;
244 }
245
246 function user_callback(request) {
247         var user;
248         var user_ou;
249         try {
250                 user = request.getResultObject();
251                 if (!user) { throw('null result'); }
252                 if (typeof(user) != 'object') { throw('result not an object' + user); }
253         } catch(E) {
254                 alert('Login failed on user: ' + js2JSON(E)); enable_login_prompts(); return;
255         }
256         mw.G.user = user;
257         mw.G.user_ou = user.home_ou();
258         sdump('D_AUTH', "user: " + js2JSON(mw.G['user']) + '\n');
259         sdump('D_AUTH', "user_ou: " + js2JSON(mw.G['user_ou']) + '\n');
260         /*user_async_request(
261                 'open-ils.search',
262                 'open-ils.search.actor.org_tree.retrieve',
263                 [],
264                 org_tree_callback
265         );*/
266         /*user_async_request(
267                 'open-ils.actor',
268                 'open-ils.actor.org_types.retrieve',
269                 [ mw.G.auth_ses[0] ],
270                 org_type_callback
271         );*/
272         org_type_callback();
273         document.getElementById('auth_meter').value += auth_meter_per;
274 }
275
276 function org_type_callback(request) {
277         var aout_list = globalOrgTypes;
278         /*try {
279                 aout_list = request.getResultObject();
280                 if (!aout_list) { throw('null result'); }
281                 if (typeof(aout_list) != 'object') { throw('result not an object' + aout_list); }
282                 if (aout_list.length == 0) { throw('empty aout_list'); }
283         } catch(E) {
284                 alert('Login failed on aout_list: ' + js2JSON(E)); enable_login_prompts(); return;
285         }*/
286         mw.G.aout_list = aout_list;
287         mw.G.aout_hash = convert_object_list_to_hash( aout_list );
288         mw.G.org_tree = globalOrgTree;
289         mw.G.org_tree_hash = convert_object_list_to_hash( flatten_ou_branch( globalOrgTree ) );
290         mw.G.user_ou = find_ou( mw.G.org_tree, mw.G.user_ou );
291
292         user_async_request(
293                 'open-ils.actor',
294                 'open-ils.actor.org_unit.full_path.retrieve',
295                 [ mw.G.auth_ses[0] ],
296                 my_orgs_callback
297         );
298         document.getElementById('auth_meter').value += auth_meter_per;
299
300 }
301
302 function my_orgs_callback(request) {
303         var my_orgs;
304         try {
305                 my_orgs = request.getResultObject();
306                 if (!my_orgs) { throw('null result'); }
307                 if (typeof(my_orgs) != 'object') { throw('result not an object' + my_orgs); }
308                 if (my_orgs.length == 0) { throw('empty my_orgs'); }
309         } catch(E) {
310                 alert('Login failed on my_orgs: ' + js2JSON(E)); enable_login_prompts(); return;
311         }
312
313         mw.G.my_orgs = my_orgs;
314         mw.G.my_orgs_hash = convert_object_list_to_hash( my_orgs );
315         sdump('D_AUTH','my_orgs = ' + js2JSON(my_orgs) + '\n');
316         mw.G.acpl_my_orgs = filter_list( 
317                 mw.G.acpl_list, 
318                 function (obj) {
319                         if ( typeof obj != 'object' ) return null;
320                         if ( mw.G.my_orgs_hash[ obj.owning_lib() ] ) return obj;
321                 }
322         );
323         mw.G.acpl_my_orgs_hash = convert_object_list_to_hash( mw.G.acpl_my_orgs );
324         //sdump('D_AUTH', 'my_orgs.length = ' + mw.G.my_orgs.length + '   other_orgs.length = ' + mw.G.other_orgs.length + '\n');
325
326         user_async_request(
327                 'open-ils.circ',
328                 'open-ils.circ.stat_cat.actor.retrieve.all',
329                 [ mw.G.auth_ses[0], mw.G.user_ou.id() ],
330                 my_actsc_list_callback
331         );
332
333         document.getElementById('auth_meter').value += auth_meter_per;
334 }
335
336 function my_actsc_list_callback(request) {
337         var actsc_list;
338         try {
339                 actsc_list = request.getResultObject();
340                 if (!actsc_list) { throw('null result'); }
341                 if (actsc_list.length == 0) { throw('zero length result'); }
342         } catch(E) {
343                 alert('Login failed on asc_list: ' + js2JSON(E)); enable_login_prompts(); return;
344         }
345         mw.G.actsc_list = actsc_list;
346         mw.G.actsc_hash = convert_object_list_to_hash( actsc_list );
347         sdump('D_AUTH', 'actsc_list = ' + js2JSON(actsc_list) + '\n');
348
349         document.getElementById('auth_meter').value += auth_meter_per;
350
351         spawn_main();
352
353 }
354
355
356 function spawn_main() {
357         try {
358                 var w = new_window('chrome://evergreen/content/evergreen/main.xul');
359                 if (!w) { throw('window ref == null'); }
360                 try {
361                         w.document.title = mw.G.user.usrname() + '@' + mw.G.user_ou.name();
362                 } catch(E) {
363                         alert('Hrmm. ' + pretty_print( js2JSON(E) ) );
364                 }
365         } catch(E) {
366                 alert('Login failed on new_window: ' + js2JSON(E)); enable_login_prompts(); return;
367         }
368         document.getElementById('auth_meter').value += auth_meter_per;
369 }
370
371 function logoff() {
372         mw.G['auth_ses'] = '';
373         close_all_windows();
374         enable_login_prompts();
375         snd_logoff();
376 }
377
378 function nice_shutdown() {
379         if (ses) { logoff(); ses.disconnect(); }
380         snd_exit;
381         close_all_windows();
382         window.close();
383 }
384