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