]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/openils/User.js
another cookie path
[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
53         getBySession : function(onComplete) {
54             var _u = this;
55             var req = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.session.retrieve', _u.authtoken);
56             if(onComplete) {
57                 req.oncomplete = function(r) {
58                     var user = r.recv().content();
59                     _u.user = user;
60                                         if (!openils.User.user) openils.User.user = _u.user;
61                     if(onComplete)
62                         onComplete(user);
63                 }
64                 req.send();
65             } else {
66                 req.timeout = 10;
67                 req.send();
68                 return _u.user = req.recv().content();
69             }
70         },
71     
72         getById : function(id, onComplete) {
73             var req = OpenSRF.CachedClientSession('open-ils.actor').request('open-ils.actor.user.retrieve', this.authtoken, id);
74             if(onComplete) {
75                 req.oncomplete = function(r) {
76                     var user = r.recv().content();
77                     onComplete(user);
78                 }
79                 req.send();
80             } else {
81                 req.timeout = 10;
82                 req.send();
83                 return req.recv().content();
84             }
85         },
86     
87     
88         /**
89          * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
90          */
91         login_async : function(args, onComplete) {
92             var _u = this;
93
94             if (!args) args = {};
95             if (!args.username) args.username = _u.username;
96             if (!args.passwd) args.passwd = _u.passwd;
97             if (!args.type) args.type = _u.login_type;
98             if (!args.location) args.location = _u.location;
99
100             var initReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.init', args.username);
101     
102             initReq.oncomplete = function(r) {
103                 var seed = r.recv().content(); 
104                 alert(seed);
105                 var loginInfo = {
106                     username : args.username,
107                     password : hex_md5(seed + hex_md5(args.passwd)), 
108                     type : args.type,
109                     org : args.location,
110                 };
111     
112                 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.complete', loginInfo);
113                 authReq.oncomplete = function(rr) {
114                     var data = rr.recv().content();
115                     _u.authtoken = data.payload.authtoken;
116                                         if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
117                     _u.authtime = data.payload.authtime;
118                                         if (!openils.User.authtime) openils.User.authtime = _u.authtime;
119                     _u.getBySession(onComplete);
120                     if(_u.authcookie) {
121                         dojo.require('dojo.cookie');
122                         dojo.cookie(_u.authcookie, _u.authtoken, {path:'/'});
123                     }
124                 }
125                 authReq.send();
126             }
127     
128             initReq.send();
129         },
130
131         login : function(args) {
132             var _u = this;
133             if (!args) args = {};
134             if (!args.username) args.username = _u.username;
135             if (!args.passwd) args.passwd = _u.passwd;
136             if (!args.type) args.type = _u.login_type;
137             if (!args.location) args.location = _u.location;
138
139             var seed = fieldmapper.standardRequest(
140                 ['open-ils.auth', 'open-ils.auth.authenticate.init'],
141                 [args.username]
142             );
143
144             var loginInfo = {
145                 username : args.username,
146                 password : hex_md5(seed + hex_md5(args.passwd)), 
147                 type : args.type,
148                 org : args.location,
149             };
150
151             var data = fieldmapper.standardRequest(
152                 ['open-ils.auth', 'open-ils.auth.authenticate.complete'],
153                 [loginInfo]
154             );
155
156             _u.authtoken = data.payload.authtoken;
157             if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
158             _u.authtime = data.payload.authtime;
159             if (!openils.User.authtime) openils.User.authtime = _u.authtime;
160
161             if(_u.authcookie) {
162                 dojo.require('dojo.cookie');
163                 dojo.cookie(_u.authcookie, _u.authtoken, {path:'/'});
164             }
165         },
166
167     
168         /**
169          * Returns a list of the "highest" org units where the user
170          * has the given permission.
171          */
172         getPermOrgList : function(perm, onload) {
173             fieldmapper.standardRequest(
174                 ['open-ils.actor', 'open-ils.actor.user.work_perm.highest_org_set'],
175                 {   async: true,
176                     params: [this.authtoken, perm],
177                     oncomplete: function(r) {
178                         org_list = r.recv().content();
179                         onload(org_list);
180                     }
181                 }
182             );
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 hookupStore(store) {
233                 selector.store = store;
234                 selector.startup();
235                 selector.setValue(_u.user.ws_ou());
236             }
237
238             function buildTreePicker(orgList) {
239                 var orgNodeList = [];
240                 for(var i = 0; i < orgList.length; i++) 
241                     orgNodeList = orgNodeList.concat(
242                         fieldmapper.aou.descendantNodeList(orgList[i]));
243
244                 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgNodeList)});
245                 hookupStore(store);
246                 _u.permOrgStoreCache[perm] = store;
247             }
248     
249                 if (_u.permOrgStoreCache[perm])
250                         hookupStore(_u.permOrgStoreCache[perm]);
251                 else
252                 _u.getPermOrgList(perm, buildTreePicker);
253         }
254     });
255
256         openils.User.user = null;
257         openils.User.authtoken = null;
258         openils.User.authtime = null;
259     openils.User.authcookie = null;
260 }
261
262