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