]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/User.js
added some user sesssion and object-list caching
[working/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
18 if(!dojo._hasResource["openils.User"]) {
19
20     dojo._hasResource["openils.User"] = true;
21     dojo.provide("openils.User");
22     dojo.require("DojoSRF");
23     dojo.require('openils.Event');
24     dojo.require('fieldmapper.Fieldmapper');
25     dojo.require('fieldmapper.OrgUtils');
26     dojo.require('openils.Util');
27
28     dojo.declare('openils.User', null, {
29
30         user : null,
31         username : null,
32         passwd : null,
33         login_type : 'opac',
34         location : null,
35         authtoken : null,
36         authtime : null,
37         workstation : null,
38         permOrgCache : {},
39         sessionCache : {},
40     
41         constructor : function ( kwargs ) {
42             kwargs = kwargs || {};
43             this.id = kwargs.id;
44             this.user = kwargs.user;
45             this.passwd = kwargs.passwd;
46             this.authtoken = kwargs.authtoken || openils.User.authtoken;
47             this.authtime = kwargs.authtime || openils.User.authtime;
48             this.login_type = kwargs.login_type;
49             this.location = kwargs.location;
50             this.authcookie = kwargs.authcookie || openils.User.authcookie;
51             this.permOrgStoreCache = {}; /* permName => permOrgUnitStore map */
52
53             if (this.id && this.authtoken) this.user = this.getById( this.id );
54             else if (this.authtoken) this.getBySession();
55             else if (kwargs.login) this.login();
56
57             var id = this.id || (this.user) ? this.user.id() : null;
58             if(id && !this.permOrgCache[id])
59                 this.permOrgCache[id] = {};
60         },
61
62         getBySession : function(onComplete) {
63             var _u = this;
64             var req = ['open-ils.auth', 'open-ils.auth.session.retrieve'];
65             var params = [_u.authtoken];
66
67             if(this.sessionCache[this.authtoken]) {
68                 this.user = this.sessionCache[this.authtoken];
69                                 if (!openils.User.user) 
70                     openils.User.user = this.user;
71                 return this.user;
72             }
73
74             if(onComplete) {
75                 fieldmapper.standardRequest(
76                     req, {   
77                         async: true,
78                         params: params,
79                         oncomplete : function(r) {
80                             var user = r.recv().content();
81                             _u.user = user;
82                             _u.sessionCache[_u.authtoken] = user;
83                                                 if (!openils.User.user) openils.User.user = _u.user;
84                             if(onComplete)
85                                 onComplete(user);
86                         }
87                     }
88                 );
89             } else {
90                 _u.user = fieldmapper.standardRequest(req, params);
91                                 if (!openils.User.user) openils.User.user = _u.user;
92                 _u.sessionCache[_u.authtoken] = _u.user;
93                 return _u.user;
94             }
95         },
96     
97         getById : function(id, onComplete) {
98             var req = OpenSRF.CachedClientSession('open-ils.actor').request('open-ils.actor.user.retrieve', this.authtoken, id);
99             if(onComplete) {
100                 req.oncomplete = function(r) {
101                     var user = r.recv().content();
102                     onComplete(user);
103                 }
104                 req.send();
105             } else {
106                 req.timeout = 10;
107                 req.send();
108                 return req.recv().content();
109             }
110         },
111     
112     
113         /**
114          * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
115          */
116         login_async : function(args, onComplete) {
117             var _u = this;
118
119             if (!args) args = {};
120             if (!args.username) args.username = _u.username;
121             if (!args.passwd) args.passwd = _u.passwd;
122             if (!args.type) args.type = _u.login_type;
123             if (!args.location) args.location = _u.location;
124
125             var initReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.init', args.username);
126     
127             initReq.oncomplete = function(r) {
128                 var seed = r.recv().content(); 
129                 var loginInfo = {
130                     username : args.username,
131                     password : hex_md5(seed + hex_md5(args.passwd)), 
132                     type : args.type,
133                     org : args.location,
134                     workstation : args.workstation
135                 };
136     
137                 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.complete', loginInfo);
138                 authReq.oncomplete = function(rr) {
139                     var data = rr.recv().content();
140                     _u.authtoken = data.payload.authtoken;
141                                         if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
142                     _u.authtime = data.payload.authtime;
143                                         if (!openils.User.authtime) openils.User.authtime = _u.authtime;
144                     _u.getBySession(onComplete);
145                     if(_u.authcookie) {
146                         dojo.require('dojo.cookie');
147                         dojo.cookie(_u.authcookie, _u.authtoken, {path:'/'});
148                     }
149                 }
150                 authReq.send();
151             }
152     
153             initReq.send();
154         },
155
156         login : function(args) {
157             var _u = this;
158             if (!args) args = {};
159             if (!args.username) args.username = _u.username;
160             if (!args.passwd) args.passwd = _u.passwd;
161             if (!args.type) args.type = _u.login_type;
162             if (!args.location) args.location = _u.location;
163
164             var seed = fieldmapper.standardRequest(
165                 ['open-ils.auth', 'open-ils.auth.authenticate.init'],
166                 [args.username]
167             );
168
169             var loginInfo = {
170                 username : args.username,
171                 password : hex_md5(seed + hex_md5(args.passwd)), 
172                 type : args.type,
173                 org : args.location,
174                 workstation : args.workstation,
175             };
176
177             var data = fieldmapper.standardRequest(
178                 ['open-ils.auth', 'open-ils.auth.authenticate.complete'],
179                 [loginInfo]
180             );
181
182             _u.authtoken = data.payload.authtoken;
183             if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
184             _u.authtime = data.payload.authtime;
185             if (!openils.User.authtime) openils.User.authtime = _u.authtime;
186
187             if(_u.authcookie) {
188                 dojo.require('dojo.cookie');
189                 dojo.cookie(_u.authcookie, _u.authtoken, {path:'/'});
190             }
191         },
192
193     
194         /**
195          * Returns a list of the "highest" org units where the user has the given permission(s).
196          * @param permList A single permission or list of permissions
197          * @param includeDescendents If true, return a list of 'highest' orgs plus descendents
198          * @idlist If true, return a list of IDs instead of org unit objects
199          */
200         getPermOrgList : function(permList, onload, includeDescendents, idlist) {
201             if(typeof permList == 'string') permList = [permList];
202
203             var self = this;
204             var oncomplete = function(r) {
205                 var permMap = {};
206                 if(r) permMap = openils.Util.readResponse(r);
207                 var orgList = [];
208                 for(var i = 0; i < permList.length; i++) {
209                     var perm = permList[i];
210                     var permOrgList = permMap[perm] || self.permOrgCache[self.user.id()][perm];
211                     self.permOrgCache[self.user.id()][perm] = permOrgList;
212                     for(var i in permOrgList) {
213                         if(includeDescendents) {
214                             orgList = orgList.concat(
215                                 fieldmapper.aou.descendantNodeList(permOrgList[i]));
216                         } else {
217                             orgList = orgList.concat(fieldmapper.aou.findOrgUnit(permOrgList[i]));
218                         }
219                     }
220                 }
221
222                 // remove duplicates
223                 var trimmed = [];
224                 for(var idx in orgList) {
225                     var val = (idlist) ? orgList[idx].id() : orgList[idx];
226                     if(trimmed.indexOf(val) < 0)
227                         trimmed.push(val);
228                 }
229                 onload(trimmed);
230             };
231
232             var fetchList = [];
233             for(var i = 0; i < permList.length; i++) {
234                 if(!self.permOrgCache[self.user.id()][permList[i]])
235                     fetchList.push(permList[i]);
236             }
237
238             if(fetchList.length == 0) 
239                 return oncomplete();
240
241             fieldmapper.standardRequest(
242                 ['open-ils.actor', 'open-ils.actor.user.has_work_perm_at.batch'],
243                 {   async: true,
244                     params: [this.authtoken, fetchList],
245                     oncomplete: oncomplete
246                 }
247             );
248         },
249
250     
251         /**
252          * Sets the store for an existing openils.widget.OrgUnitFilteringSelect 
253          * using the orgs where the user has the requested permission.
254          * @param perm The permission to check
255          * @param selector The pre-created dijit.form.FilteringSelect object.  
256          */
257         buildPermOrgSelector : function(perm, selector, selectedOrg, onload) {
258             var _u = this;
259     
260             dojo.require('dojo.data.ItemFileReadStore');
261
262             function hookupStore(store) {
263                 selector.store = store;
264                 selector.startup();
265                 if(selectedOrg != null)
266                     selector.setValue(selectedOrg);
267                 else
268                     selector.setValue(_u.user.ws_ou());
269                 if(onload) onload();
270             }
271
272             function buildTreePicker(orgList) {
273                 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
274                 hookupStore(store);
275                 _u.permOrgStoreCache[perm] = store;
276             }
277     
278                 if (_u.permOrgStoreCache[perm])
279                         hookupStore(_u.permOrgStoreCache[perm]);
280                 else
281                 _u.getPermOrgList(perm, buildTreePicker, true);
282         },
283     });
284
285         openils.User.user = null;
286         openils.User.authtoken = null;
287         openils.User.authtime = null;
288     openils.User.authcookie = null;
289 }
290
291