]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/openils/User.js
if optional authcookie is set for the user, the login authtoken will be stored at...
[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("DojoSRF");
22     dojo.require('openils.Event');
23     dojo.require('fieldmapper.Fieldmapper');
24
25     dojo.declare('openils.User', null, {
26
27         user : null,
28         username : null,
29         passwd : null,
30         login_type : 'opac',
31         location : null,
32         authtoken : null,
33         authtime : null,
34     
35         constructor : function ( kwargs ) {
36             kwargs = kwargs || {};
37             this.id = kwargs.id;
38             this.user = kwargs.user;
39             this.passwd = kwargs.passwd;
40             this.authtoken = kwargs.authtoken || openils.User.authtoken;
41             this.authtime = kwargs.authtime || openils.User.authtime;
42             this.login_type = kwargs.login_type;
43             this.location = kwargs.location;
44             this.authcookie = kwargs.authcookie || openils.User.authcookie;
45
46             if (this.authtoken) this.getBySession();
47             else if (this.id && this.authtoken) this.user = this.getById( this.id );
48             else if (kwargs.login) this.login();
49
50         },
51
52         getBySession : function(onComplete) {
53             var _u = this;
54             var req = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.session.retrieve', _u.authtoken);
55             if(onComplete) {
56                 req.oncomplete = function(r) {
57                     var user = r.recv().content();
58                     _u.user = user;
59                                         if (!openils.User.user) openils.User.user = _u.user;
60                     if(onComplete)
61                         onComplete(user);
62                 }
63                 req.send();
64             } else {
65                 req.timeout = 10;
66                 req.send();
67                 return _u.user = req.recv().content();
68             }
69         },
70     
71         getById : function(id, onComplete) {
72             var req = OpenSRF.CachedClientSession('open-ils.actor').request('open-ils.actor.user.retrieve', this.authtoken, id);
73             if(onComplete) {
74                 req.oncomplete = function(r) {
75                     var user = r.recv().content();
76                     onComplete(user);
77                 }
78                 req.send();
79             } else {
80                 req.timeout = 10;
81                 req.send();
82                 return req.recv().content();
83             }
84         },
85     
86     
87         /**
88          * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
89          */
90         login_async : function(args, onComplete) {
91             var _u = this;
92
93             if (!args) args = {};
94             if (!args.username) args.username = _u.username;
95             if (!args.passwd) args.passwd = _u.passwd;
96             if (!args.type) args.type = _u.login_type;
97             if (!args.location) args.location = _u.location;
98
99             var initReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.init', args.username);
100     
101             initReq.oncomplete = function(r) {
102                 var seed = r.recv().content(); 
103                 alert(seed);
104                 var loginInfo = {
105                     username : args.username,
106                     password : hex_md5(seed + hex_md5(args.passwd)), 
107                     type : args.type,
108                     org : args.location,
109                 };
110     
111                 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.complete', loginInfo);
112                 authReq.oncomplete = function(rr) {
113                     var data = rr.recv().content();
114                     _u.authtoken = data.payload.authtoken;
115                                         if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
116                     _u.authtime = data.payload.authtime;
117                                         if (!openils.User.authtime) openils.User.authtime = _u.authtime;
118                     _u.getBySession(onComplete);
119                     if(_u.authcookie) {
120                         dojo.require('dojo.cookie');
121                         dojo.cookie(_u.authcookie, _u.authtoken);
122                     }
123                 }
124                 authReq.send();
125             }
126     
127             initReq.send();
128         },
129
130         login : function(args) {
131             var _u = this;
132             if (!args) args = {};
133             if (!args.username) args.username = _u.username;
134             if (!args.passwd) args.passwd = _u.passwd;
135             if (!args.type) args.type = _u.login_type;
136             if (!args.location) args.location = _u.location;
137
138             var seed = fieldmapper.standardRequest(
139                 ['open-ils.auth', 'open-ils.auth.authenticate.init'],
140                 [args.username]
141             );
142
143             var loginInfo = {
144                 username : args.username,
145                 password : hex_md5(seed + hex_md5(args.passwd)), 
146                 type : args.type,
147                 org : args.location,
148             };
149
150             var data = fieldmapper.standardRequest(
151                 ['open-ils.auth', 'open-ils.auth.authenticate.complete'],
152                 [loginInfo]
153             );
154
155             _u.authtoken = data.payload.authtoken;
156             if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
157             _u.authtime = data.payload.authtime;
158             if (!openils.User.authtime) openils.User.authtime = _u.authtime;
159
160             if(_u.authcookie) {
161                 dojo.require('dojo.cookie');
162                 dojo.cookie(_u.authcookie, _u.authtoken);
163             }
164         },
165
166     
167         /**
168          * Returns a list of the "highest" org units where the user
169          * has the given permission.
170          */
171         getPermOrgList : function(perm, onload) {
172     
173             var req = OpenSRF.CachedClientSession('open-ils.actor').request(
174                 'open-ils.actor.user.work_perm.highest_org_set',
175                 this.authtoken, perm);
176     
177             req.oncomplete = function(r) {
178                 org_list = r.recv().content();
179                 onload(org_list);
180             }
181     
182             req.send();
183         },
184     
185         /**
186          * Builds a dijit.Tree using the orgs where the user has the requested permission
187          * @param perm The permission to check
188          * @param domId The DOM node where the tree widget should live
189          * @param onClick If defined, this will be connected to the tree widget for
190          * onClick events
191          */
192         buildPermOrgTreePicker : function(perm, domId, onClick) {
193
194             dojo.require('dojo.data.ItemFileReadStore');
195             dojo.require('dijit.Tree');
196             function buildTreePicker(r) {
197                 var orgList = r.recv().content();
198                 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
199                 var model = new dijit.tree.ForestStoreModel({
200                     store: store,
201                     query: {_top:'true'},
202                     childrenAttrs: ["children"],
203                     rootLabel : "Location" /* XXX i18n */
204                 });
205     
206                 var tree = new dijit.Tree({model : model}, dojo.byId(domId));
207                 if(onClick)
208                     dojo.connect(tree, 'onClick', onClick);
209                 tree.startup()
210             }
211     
212             fieldmapper.standardRequest(
213                 ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'],
214                 {   params: [this.authtoken, perm],
215                     oncomplete: buildTreePicker,
216                     async: true
217                 }
218             )
219         },
220     
221         /**
222          * Sets the store for an existing openils.widget.OrgUnitFilteringSelect 
223          * using the orgs where the user has the requested permission.
224          * @param perm The permission to check
225          * @param selector The pre-created dijit.form.FilteringSelect object.  
226          */
227         buildPermOrgSelector : function(perm, selector) {
228             var _u = this;
229     
230             dojo.require('dojo.data.ItemFileReadStore');
231
232             function buildTreePicker(r) {
233                 var orgList = r.recv().content();
234                 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
235                 selector.store = store;
236                 selector.startup();
237                 selector.setValue(_u.user.ws_ou());
238             }
239     
240             fieldmapper.standardRequest(
241                 ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'],
242                 {   params: [this.authtoken, perm],
243                     oncomplete: buildTreePicker,
244                     async: true
245                 }
246             )
247         }
248
249     });
250
251         openils.User.user = null;
252         openils.User.authtoken = null;
253         openils.User.authtime = null;
254     openils.User.authcookie = null;
255 }
256
257