]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/User.js
let's not clobber the loop variable
[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) ? 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
209                 for(var i = 0; i < permList.length; i++) {
210                     var perm = permList[i];
211                     var permOrgList = permMap[perm] || self.permOrgCache[self.user.id()][perm];
212                     self.permOrgCache[self.user.id()][perm] = permOrgList;
213
214                     for(var j in permOrgList) {
215                         if(includeDescendents) {
216                             orgList = orgList.concat(
217                                 fieldmapper.aou.descendantNodeList(permOrgList[j]));
218                         } else {
219                             orgList = orgList.concat(fieldmapper.aou.findOrgUnit(permOrgList[j]));
220                         }
221                     }
222                 }
223
224                 // remove duplicates
225                 var trimmed = [];
226                 for(var idx in orgList) {
227                     var val = (idlist) ? orgList[idx].id() : orgList[idx];
228                     if(trimmed.indexOf(val) < 0)
229                         trimmed.push(val);
230                 }
231                 onload(trimmed);
232             };
233
234             var fetchList = [];
235             for(var i = 0; i < permList.length; i++) {
236                 if(!self.permOrgCache[self.user.id()][permList[i]])
237                     fetchList.push(permList[i]);
238             }
239
240             if(fetchList.length == 0) 
241                 return oncomplete();
242
243             fieldmapper.standardRequest(
244                 ['open-ils.actor', 'open-ils.actor.user.has_work_perm_at.batch'],
245                 {   async: true,
246                     params: [this.authtoken, fetchList],
247                     oncomplete: oncomplete
248                 }
249             );
250         },
251
252     
253         /**
254          * Sets the store for an existing openils.widget.OrgUnitFilteringSelect 
255          * using the orgs where the user has the requested permission.
256          * @param perm The permission to check
257          * @param selector The pre-created dijit.form.FilteringSelect object.  
258          */
259         buildPermOrgSelector : function(perm, selector, selectedOrg, onload) {
260             var _u = this;
261     
262             dojo.require('dojo.data.ItemFileReadStore');
263
264             function hookupStore(store) {
265                 selector.store = store;
266                 selector.startup();
267                 if(selectedOrg != null)
268                     selector.setValue(selectedOrg);
269                 else
270                     selector.setValue(_u.user.ws_ou());
271                 if(onload) onload();
272             }
273
274             function buildTreePicker(orgList) {
275                 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
276                 hookupStore(store);
277                 _u.permOrgStoreCache[perm] = store;
278             }
279     
280                 if (_u.permOrgStoreCache[perm])
281                         hookupStore(_u.permOrgStoreCache[perm]);
282                 else
283                 _u.getPermOrgList(perm, buildTreePicker, true);
284         },
285     });
286
287         openils.User.user = null;
288         openils.User.authtoken = null;
289         openils.User.authtime = null;
290     openils.User.authcookie = null;
291 }
292
293