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