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