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