fixed what appears to be some copy paste errors. no longer requiring kwargs to be...
[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 : 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                     password : hex_md5(seed + hex_md5(args.passwd)), 
105                     type : args.type,
106                     org : args.location,
107                 };
108     
109                 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.complete', loginInfo);
110                 authReq.oncomplete = function(rr) {
111                     var data = rr.recv().content();
112                     _u.authtoken = data.payload.authtoken;
113                                         if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
114                     _u.authtime = data.payload.authtime;
115                                         if (!openils.User.authtime) openils.User.authtime = _u.authtime;
116                     _u.getBySession(onComplete);
117                 }
118                 authReq.send();
119             }
120     
121             initReq.send();
122         },
123     
124         /**
125          * Returns a list of the "highest" org units where the user
126          * has the given permission.
127          */
128         getPermOrgList : function(perm, onload) {
129     
130             var req = OpenSRF.CachedClientSession('open-ils.actor').request(
131                 'open-ils.actor.user.work_perm.highest_org_set',
132                 this.authtoken, perm);
133     
134             req.oncomplete = function(r) {
135                 org_list = r.recv().content();
136                 onload(org_list);
137             }
138     
139             req.send();
140         },
141     
142         /**
143          * Builds a dijit.Tree using the orgs where the user has the requested permission
144          * @param perm The permission to check
145          * @param domId The DOM node where the tree widget should live
146          * @param onClick If defined, this will be connected to the tree widget for
147          * onClick events
148          */
149         buildPermOrgTreePicker : function(perm, domId, onClick) {
150
151             dojo.require('dojo.data.ItemFileReadStore');
152             dojo.require('dijit.Tree');
153             function buildTreePicker(r) {
154                 var orgList = r.recv().content();
155                 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
156                 var model = new dijit.tree.ForestStoreModel({
157                     store: store,
158                     query: {_top:'true'},
159                     childrenAttrs: ["children"],
160                     rootLabel : "Location" /* XXX i18n */
161                 });
162     
163                 var tree = new dijit.Tree({model : model}, dojo.byId(domId));
164                 if(onClick)
165                     dojo.connect(tree, 'onClick', onClick);
166                 tree.startup()
167             }
168     
169             fieldmapper.standardRequest(
170                 ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'],
171                 {   params: [this.authtoken, perm],
172                     oncomplete: buildTreePicker,
173                     async: true
174                 }
175             )
176         },
177     
178         /**
179          * Sets the store for an existing openils.widget.OrgUnitFilteringSelect 
180          * using the orgs where the user has the requested permission.
181          * @param perm The permission to check
182          * @param selector The pre-created dijit.form.FilteringSelect object.  
183          */
184         buildPermOrgSelector : function(perm, selector) {
185             var _u = this;
186     
187             dojo.require('dojo.data.ItemFileReadStore');
188
189             function buildTreePicker(r) {
190                 var orgList = r.recv().content();
191                 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
192                 selector.store = store;
193                 selector.startup();
194                 selector.setValue(_u.user.ws_ou());
195             }
196     
197             fieldmapper.standardRequest(
198                 ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'],
199                 {   params: [this.authtoken, perm],
200                     oncomplete: buildTreePicker,
201                     async: true
202                 }
203             )
204         }
205
206     });
207
208         openils.User.user = null;
209         openils.User.authtoken = null;
210         openils.User.authtime = null;
211
212 }
213
214