dojo.require OpenSRF, since we use it herein. it will only be loaded if it has not...
[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     dojo.require('OpenSRF');
23
24     dojo.declare('openils.User', null, {});
25
26     openils.User.user = null;
27     openils.User.authtoken = null;
28     openils.User.authtime = null;
29
30     var ses = new OpenSRF.ClientSession('open-ils.auth');
31
32     openils.User.getBySession = function(onComplete) {
33         var req = ses.request('open-ils.auth.session.retrieve', openils.User.authtoken);
34         if(onComplete) {
35             req.oncomplete = function(r) {
36                 var user = r.recv().content();
37                 openils.User.user = user;
38                 if(onComplete)
39                     onComplete(user);
40             }
41             req.send();
42         } else {
43             req.timeout = 10;
44             req.send();
45             return openils.User.user = req.recv().content();
46         }
47     }
48
49     openils.User.getById = function(id, onComplete) {
50         var ases = new OpenSRF.ClientSession('open-ils.actor');
51         var req = ases.request('open-ils.actor.user.retrieve', openils.User.authtoken, id);
52         if(onComplete) {
53             req.oncomplete = function(r) {
54                 var user = r.recv().content();
55                 onComplete(user);
56             }
57             req.send();
58         } else {
59             req.timeout = 10;
60             req.send();
61             return req.recv().content();
62         }
63     }
64
65
66     /**
67      * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
68      */
69     openils.User.login = function(args, onComplete) {
70         var initReq = ses.request('open-ils.auth.authenticate.init', args.username);
71
72         initReq.oncomplete = function(r) {
73             var seed = r.recv().content(); 
74             alert(seed);
75             var loginInfo = {
76                 password : hex_md5(seed + hex_md5(args.passwd)), 
77                 type : args.type || 'opac',
78                 org : args.location,
79             };
80
81             var authReq = ses.request('open-ils.auth.authenticate.complete', loginInfo);
82             authReq.oncomplete = function(rr) {
83                 var data = rr.recv().content();
84                 openils.User.authtoken = data.payload.authtoken;
85                 openils.User.authtime = data.payload.authtime;
86                 openils.User.getBySession(onComplete);
87             }
88             authReq.send();
89         }
90
91         initReq.send();
92     }
93
94     /**
95      * Returns a list of the "highest" org units where the user
96      * has the given permission.
97      */
98     openils.User.getPermOrgList = function(perm, onload) {
99
100         var ases = new OpenSRF.ClientSession('open-ils.actor');
101         var req = ases.request(
102             'open-ils.actor.user.work_perm.highest_org_set',
103             openils.User.authtoken, perm);
104
105         req.oncomplete = function(r) {
106             org_list = r.recv().content();
107             onload(org_list);
108         }
109
110         req.send();
111     }
112 }
113
114