Move inline calls into my_init(); messagecatalog isn't visible until after onload...
[Evergreen.git] / Open-ILS / web / js / dojo / openils / User.js
1 /* ---------------------------------------------------------------------------
2  * Copyright (C) 2008  Georgia Public Library Service
3  * Bill Erickson <erickson@esilibrary.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * ---------------------------------------------------------------------------
15  */
16
17 if(!dojo._hasResource["openils.User"]) {
18
19     dojo._hasResource["openils.User"] = true;
20     dojo.provide("openils.User");
21     dojo.require('openils.Event');
22
23     dojo.declare('openils.User', null, {});
24
25     openils.User.user = null;
26     openils.User.authtoken = null;
27     openils.User.authtime = null;
28
29     var ses = new OpenSRF.ClientSession('open-ils.auth');
30
31     openils.User.getBySession = function(onComplete) {
32         var req = ses.request('open-ils.auth.session.retrieve', openils.User.authtoken);
33         if(onComplete) {
34             req.oncomplete = function(r) {
35                 var user = r.recv().content();
36                 openils.User.user = user;
37                 if(onComplete)
38                     onComplete(user);
39             }
40             req.send();
41         } else {
42             req.timeout = 10;
43             req.send();
44             return openils.User.user = req.recv().content();
45         }
46     }
47
48     openils.User.getById = function(id, onComplete) {
49         var ases = new OpenSRF.ClientSession('open-ils.actor');
50         var req = ases.request('open-ils.actor.user.retrieve', openils.User.authtoken, id);
51         if(onComplete) {
52             req.oncomplete = function(r) {
53                 var user = r.recv().content();
54                 onComplete(user);
55             }
56             req.send();
57         } else {
58             req.timeout = 10;
59             req.send();
60             return req.recv().content();
61         }
62     }
63
64
65     /**
66      * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
67      */
68     openils.User.login = function(args, onComplete) {
69         var initReq = ses.request('open-ils.auth.authenticate.init', args.username);
70
71         initReq.oncomplete = function(r) {
72             var seed = r.recv().content(); 
73             alert(seed);
74             var loginInfo = {
75                 password : hex_md5(seed + hex_md5(args.passwd)), 
76                 type : args.type || 'opac',
77                 org : args.location,
78             };
79
80             var authReq = ses.request('open-ils.auth.authenticate.complete', loginInfo);
81             authReq.oncomplete = function(rr) {
82                 var data = rr.recv().content();
83                 openils.User.authtoken = data.payload.authtoken;
84                 openils.User.authtime = data.payload.authtime;
85                 openils.User.getBySession(onComplete);
86             }
87             authReq.send();
88         }
89
90         initReq.send();
91     }
92
93     /**
94      * Returns a list of the "highest" org units where the user
95      * has the given permission.
96      */
97     openils.User.getPermOrgList = function(perm, onload) {
98
99         var ases = new OpenSRF.ClientSession('open-ils.actor');
100         var req = ases.request(
101             'open-ils.actor.user.work_perm.highest_org_set',
102             openils.User.authtoken, perm);
103
104         req.oncomplete = function(r) {
105             org_list = r.recv().content();
106             onload(org_list);
107         }
108
109         req.send();
110     }
111 }
112
113