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