]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Evergreen/staff_client/chrome/content/evergreen/auth/auth.js
put this guy with the others
[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 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_incr = 8;
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         document.getElementById('auth_meter').setAttribute('real', '0.0');
78         G.sound.beep();
79 }
80
81 function authenticate() {
82         timer_init('cat');
83         var name = document.getElementById('name_prompt').value;
84         if (name.length == 0) { enable_login_prompts(); return; }
85         // Talk to the system and authenticate the user.
86         user_async_request(
87                 'open-ils.auth',
88                 'open-ils.auth.authenticate.init',
89                 [ name ],
90                 auth_init_callback
91         );
92 }
93
94 function auth_init_callback(request) {
95         var auth_init;
96         try {
97                 auth_init = request.getResultObject();
98                 if (!auth_init) { throw('null result'); }
99         } catch(E) {
100                 alert('Login failed on auth_init: ' + js2JSON(E)); enable_login_prompts(); return;
101         }
102
103         sdump( 'D_AUTH', 'D_AUTH_INIT: ' + typeof(auth_init) + ' : ' + auth_init + '\n');
104         var name = document.getElementById('name_prompt').value;
105         var pw = document.getElementById('password_prompt').value;
106
107         user_async_request(
108                 'open-ils.auth',
109                 'open-ils.auth.authenticate.complete',
110                 [ name, hex_md5(auth_init + hex_md5(pw)) ],
111                 auth_ses_callback
112         );
113         incr_progressmeter('auth_meter',auth_meter_incr);
114 }
115
116 function auth_ses_callback(request) {
117         var auth_ses;
118         try {
119                 auth_ses = request.getResultObject();
120                 if (!auth_ses) { throw('null result'); }
121                 if (auth_ses == 0) { throw('0 result'); }
122         } catch(E) {
123                 alert('Login failed on auth_ses: ' + js2JSON(E)); enable_login_prompts(); return;
124         }
125         mw.G.auth_ses = [ auth_ses ];
126         sdump( 'D_AUTH', 'D_AUTH_SES: ' + typeof(mw.G['auth_ses'][0]) + ' : ' + mw.G['auth_ses'][0] + '\n');
127
128         user_async_request(
129                 'open-ils.actor',
130                 'open-ils.actor.user.profiles.retrieve',
131                 [],
132                 ap_list_callback
133         );
134         incr_progressmeter('auth_meter',auth_meter_incr);
135 }
136
137 function ap_list_callback(request) {
138         var ap_list;
139         try {
140                 ap_list = request.getResultObject();
141                 if (!ap_list) { throw('null result'); }
142                 if (ap_list.length == 0) { throw('zero length result'); }
143         } catch(E) {
144                 alert('Login failed on ap_list: ' + js2JSON(E)); enable_login_prompts(); return;
145         }
146         mw.G.ap_list = ap_list;
147         mw.G.ap_hash = convert_object_list_to_hash( ap_list );
148
149         user_async_request(
150                 'open-ils.actor',
151                 'open-ils.actor.user.ident_types.retrieve',
152                 [],
153                 cit_list_callback
154         );
155         incr_progressmeter('auth_meter',auth_meter_incr);
156 }
157
158 function cit_list_callback(request) {
159         var cit_list;
160         try {
161                 cit_list = request.getResultObject();
162                 if (!cit_list) { throw('null result'); }
163                 if (cit_list.length == 0) { throw('zero length result'); }
164         } catch(E) {
165                 alert('Login failed on cit_list: ' + js2JSON(E)); enable_login_prompts(); return;
166         }
167         mw.G.cit_list = cit_list;
168         mw.G.cit_hash = convert_object_list_to_hash( cit_list );
169         
170         user_async_request(
171                 'open-ils.actor',
172                 'open-ils.actor.standings.retrieve',
173                 [],
174                 cst_list_callback
175         );
176
177         incr_progressmeter('auth_meter',auth_meter_incr);
178 }
179
180 function cst_list_callback(request) {
181         var cst_list;
182         try {
183                 cst_list = request.getResultObject();
184                 if (!cst_list) { throw('null result'); }
185                 if (cst_list.length == 0) { throw('zero length result'); }
186         } catch(E) {
187                 alert('Login failed on cst_list: ' + js2JSON(E)); enable_login_prompts(); return;
188         }
189         mw.G.cst_list = cst_list;
190         mw.G.cst_hash = convert_object_list_to_hash( cst_list );
191         sdump('D_AUTH', 'cst_list = ' + js2JSON(cst_list) + '\n');
192
193         user_async_request(
194                 'open-ils.search',
195                 'open-ils.search.config.copy_location.retrieve.all',
196                 [],
197                 acpl_list_callback
198         );
199         incr_progressmeter('auth_meter',auth_meter_incr);
200
201 }
202
203 function acpl_list_callback(request) {
204         var acpl_list;
205         try {
206                 acpl_list = request.getResultObject();
207                 if (!acpl_list) { throw('null result'); }
208                 if (acpl_list.length == 0) { throw('zero length result'); }
209         } catch(E) {
210                 alert('Login failed on acpl_list: ' + js2JSON(E)); enable_login_prompts(); return;
211         }
212         mw.G.acpl_list = acpl_list;
213         mw.G.acpl_hash = convert_object_list_to_hash( acpl_list );
214         sdump('D_AUTH', 'acpl_list = ' + js2JSON(acpl_list) + '\n');
215
216         user_async_request(
217                 'open-ils.search',
218                 'open-ils.search.config.copy_status.retrieve.all',
219                 [],
220                 ccs_list_callback
221         );
222         incr_progressmeter('auth_meter',auth_meter_incr);
223 }
224
225 function ccs_list_callback(request) {
226         var ccs_list;
227         try {
228                 ccs_list = request.getResultObject();
229                 if (!ccs_list) { throw('null result'); }
230                 if (ccs_list.length == 0) { throw('zero length result'); }
231         } catch(E) {
232                 alert('Login failed on ccs_list: ' + js2JSON(E)); enable_login_prompts(); return;
233         }
234         mw.G.ccs_list = ccs_list;
235         mw.G.ccs_hash = convert_object_list_to_hash( ccs_list );
236         sdump('D_AUTH', 'ccs_list = ' + js2JSON(ccs_list) + '\n');
237
238         user_async_request(
239                 'open-ils.search',
240                 'open-ils.search.actor.user.session',
241                 [ mw.G['auth_ses'][0] ],
242                 user_callback
243         );
244         incr_progressmeter('auth_meter',auth_meter_incr);
245 }
246
247 function user_callback(request) {
248         var user;
249         var user_ou;
250         try {
251                 user = request.getResultObject();
252                 if (!user) { throw('null result'); }
253                 if (typeof(user) != 'object') { throw('result not an object' + user); }
254         } catch(E) {
255                 alert('Login failed on user: ' + js2JSON(E)); enable_login_prompts(); return;
256         }
257         mw.G.user = user;
258         mw.G.user_ou = user.home_ou();
259         sdump('D_AUTH', "user: " + js2JSON(mw.G['user']) + '\n');
260         sdump('D_AUTH', "user_ou: " + js2JSON(mw.G['user_ou']) + '\n');
261         /*user_async_request(
262                 'open-ils.search',
263                 'open-ils.search.actor.org_tree.retrieve',
264                 [],
265                 org_tree_callback
266         );*/
267         /*user_async_request(
268                 'open-ils.actor',
269                 'open-ils.actor.org_types.retrieve',
270                 [ mw.G.auth_ses[0] ],
271                 org_type_callback
272         );*/
273         org_type_callback();
274         incr_progressmeter('auth_meter',auth_meter_incr);
275 }
276
277 function org_type_callback(request) {
278         var aout_list = globalOrgTypes;
279         /*try {
280                 aout_list = request.getResultObject();
281                 if (!aout_list) { throw('null result'); }
282                 if (typeof(aout_list) != 'object') { throw('result not an object' + aout_list); }
283                 if (aout_list.length == 0) { throw('empty aout_list'); }
284         } catch(E) {
285                 alert('Login failed on aout_list: ' + js2JSON(E)); enable_login_prompts(); return;
286         }*/
287         mw.G.aout_list = aout_list;
288         mw.G.aout_hash = convert_object_list_to_hash( aout_list );
289         mw.G.org_tree = globalOrgTree;
290         mw.G.org_tree_hash = convert_object_list_to_hash( flatten_ou_branch( globalOrgTree ) );
291         mw.G.user_ou = find_ou( mw.G.org_tree, mw.G.user_ou );
292
293         user_async_request(
294                 'open-ils.actor',
295                 'open-ils.actor.org_unit.full_path.retrieve',
296                 [ mw.G.auth_ses[0] ],
297                 my_orgs_callback
298         );
299         incr_progressmeter('auth_meter',auth_meter_incr);
300
301 }
302
303 function my_orgs_callback(request) {
304         var my_orgs;
305         try {
306                 my_orgs = request.getResultObject();
307                 if (!my_orgs) { throw('null result'); }
308                 if (typeof(my_orgs) != 'object') { throw('result not an object' + my_orgs); }
309                 if (my_orgs.length == 0) { throw('empty my_orgs'); }
310         } catch(E) {
311                 alert('Login failed on my_orgs: ' + js2JSON(E)); enable_login_prompts(); return;
312         }
313
314         mw.G.my_orgs = my_orgs;
315         mw.G.my_orgs_hash = convert_object_list_to_hash( my_orgs );
316         sdump('D_AUTH','my_orgs = ' + js2JSON(my_orgs) + '\n');
317         mw.G.acpl_my_orgs = filter_list( 
318                 mw.G.acpl_list, 
319                 function (obj) {
320                         if ( typeof obj != 'object' ) return null;
321                         if ( mw.G.my_orgs_hash[ obj.owning_lib() ] ) return obj;
322                 }
323         );
324         mw.G.acpl_my_orgs_hash = convert_object_list_to_hash( mw.G.acpl_my_orgs );
325         //sdump('D_AUTH', 'my_orgs.length = ' + mw.G.my_orgs.length + '   other_orgs.length = ' + mw.G.other_orgs.length + '\n');
326
327         user_async_request(
328                 'open-ils.circ',
329                 'open-ils.circ.stat_cat.actor.retrieve.all',
330                 [ mw.G.auth_ses[0], mw.G.user_ou.id() ],
331                 my_actsc_list_callback
332         );
333
334         incr_progressmeter('auth_meter',auth_meter_incr);
335 }
336
337 function my_actsc_list_callback(request) {
338         var actsc_list;
339         try {
340                 actsc_list = request.getResultObject();
341                 if (!actsc_list) { throw('null result'); }
342                 if (actsc_list.length == 0) { throw('zero length result'); }
343         } catch(E) {
344                 alert('Login failed on asc_list: ' + js2JSON(E)); enable_login_prompts(); return;
345         }
346         mw.G.actsc_list = actsc_list;
347         mw.G.actsc_hash = convert_object_list_to_hash( actsc_list );
348         sdump('D_AUTH', 'actsc_list = ' + js2JSON(actsc_list) + '\n');
349
350         incr_progressmeter('auth_meter',auth_meter_incr);
351
352         spawn_main();
353
354 }
355
356
357 function logoff() {
358         mw.G['auth_ses'] = '';
359         close_all_windows();
360         enable_login_prompts();
361         incr_progressmeter('auth_meter',-100);
362         snd_logoff();
363 }
364
365 function nice_shutdown() {
366         if (ses) { logoff(); ses.disconnect(); }
367         snd_exit;
368         close_all_windows();
369         window.close();
370 }
371