1 /* ---------------------------------------------------------------------------
2 * Copyright (C) 2008 Georgia Public Library Service
3 * Bill Erickson <erickson@esilibrary.com>
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.
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 * ---------------------------------------------------------------------------
18 if(!dojo._hasResource["openils.User"]) {
20 dojo._hasResource["openils.User"] = true;
21 dojo.provide("openils.User");
22 dojo.require("DojoSRF");
23 dojo.require('openils.Event');
24 dojo.require('fieldmapper.Fieldmapper');
25 dojo.require('fieldmapper.OrgUtils');
26 dojo.require('openils.Util');
28 dojo.declare('openils.User', null, {
41 constructor : function ( kwargs ) {
42 kwargs = kwargs || {};
44 this.user = kwargs.user;
45 this.passwd = kwargs.passwd;
46 this.authtoken = kwargs.authtoken || openils.User.authtoken;
47 this.authtime = kwargs.authtime || openils.User.authtime;
48 this.login_type = kwargs.login_type;
49 this.location = kwargs.location;
50 this.authcookie = kwargs.authcookie || openils.User.authcookie;
51 this.permOrgStoreCache = {}; /* permName => permOrgUnitStore map */
53 if (this.id && this.authtoken) this.user = this.getById( this.id );
54 else if (this.authtoken) this.getBySession();
55 else if (kwargs.login) this.login();
57 var id = this.id || (this.user && this.user.id) ? this.user.id() : null;
58 if(id && !this.permOrgCache[id])
59 this.permOrgCache[id] = {};
62 getBySession : function(onComplete) {
64 var req = ['open-ils.auth', 'open-ils.auth.session.retrieve'];
65 var params = [_u.authtoken];
67 if(this.sessionCache[this.authtoken]) {
68 this.user = this.sessionCache[this.authtoken];
69 if (!openils.User.user)
70 openils.User.user = this.user;
75 fieldmapper.standardRequest(
79 oncomplete : function(r) {
80 var user = r.recv().content();
82 _u.sessionCache[_u.authtoken] = user;
83 if (!openils.User.user) openils.User.user = _u.user;
90 _u.user = fieldmapper.standardRequest(req, params);
91 if (!openils.User.user) openils.User.user = _u.user;
92 _u.sessionCache[_u.authtoken] = _u.user;
97 getById : function(id, onComplete) {
98 var req = OpenSRF.CachedClientSession('open-ils.actor').request('open-ils.actor.user.retrieve', this.authtoken, id);
100 req.oncomplete = function(r) {
101 var user = r.recv().content();
108 return req.recv().content();
114 * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
116 login_async : function(args, onComplete) {
119 if (!args) args = {};
120 if (!args.username) args.username = _u.username;
121 if (!args.passwd) args.passwd = _u.passwd;
122 if (!args.type) args.type = _u.login_type;
123 if (!args.location) args.location = _u.location;
125 var initReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.init', args.username);
127 initReq.oncomplete = function(r) {
128 var seed = r.recv().content();
130 username : args.username,
131 password : hex_md5(seed + hex_md5(args.passwd)),
134 workstation : args.workstation
137 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.complete', loginInfo);
138 authReq.oncomplete = function(rr) {
139 var data = rr.recv().content();
140 _u.authtoken = data.payload.authtoken;
141 if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
142 _u.authtime = data.payload.authtime;
143 if (!openils.User.authtime) openils.User.authtime = _u.authtime;
144 _u.getBySession(onComplete);
146 dojo.require('dojo.cookie');
147 dojo.cookie(_u.authcookie, _u.authtoken, {path:'/'});
156 login : function(args) {
158 if (!args) args = {};
159 if (!args.username) args.username = _u.username;
160 if (!args.passwd) args.passwd = _u.passwd;
161 if (!args.type) args.type = _u.login_type;
162 if (!args.location) args.location = _u.location;
164 var seed = fieldmapper.standardRequest(
165 ['open-ils.auth', 'open-ils.auth.authenticate.init'],
170 username : args.username,
171 password : hex_md5(seed + hex_md5(args.passwd)),
174 workstation : args.workstation,
177 var data = fieldmapper.standardRequest(
178 ['open-ils.auth', 'open-ils.auth.authenticate.complete'],
182 _u.authtoken = data.payload.authtoken;
183 if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
184 _u.authtime = data.payload.authtime;
185 if (!openils.User.authtime) openils.User.authtime = _u.authtime;
188 dojo.require('dojo.cookie');
189 dojo.cookie(_u.authcookie, _u.authtoken, {path:'/'});
195 * Returns a list of the "highest" org units where the user has the given permission(s).
196 * @param permList A single permission or list of permissions
197 * @param includeDescendents If true, return a list of 'highest' orgs plus descendents
198 * @idlist If true, return a list of IDs instead of org unit objects
200 getPermOrgList : function(permList, onload, includeDescendents, idlist) {
201 if(typeof permList == 'string') permList = [permList];
204 var oncomplete = function(r) {
206 if(r) permMap = openils.Util.readResponse(r);
209 for(var i = 0; i < permList.length; i++) {
210 var perm = permList[i];
211 var permOrgList = permMap[perm] || self.permOrgCache[self.user.id()][perm];
212 self.permOrgCache[self.user.id()][perm] = permOrgList;
214 for(var j in permOrgList) {
215 if(includeDescendents) {
216 orgList = orgList.concat(
217 fieldmapper.aou.descendantNodeList(permOrgList[j]));
219 orgList = orgList.concat(fieldmapper.aou.findOrgUnit(permOrgList[j]));
226 for(var idx in orgList) {
227 var val = (idlist) ? orgList[idx].id() : orgList[idx];
228 if(trimmed.indexOf(val) < 0)
235 for(var i = 0; i < permList.length; i++) {
236 if(!self.permOrgCache[self.user.id()][permList[i]])
237 fetchList.push(permList[i]);
240 if(fetchList.length == 0)
243 fieldmapper.standardRequest(
244 ['open-ils.actor', 'open-ils.actor.user.has_work_perm_at.batch'],
246 params: [this.authtoken, fetchList],
247 oncomplete: oncomplete
254 * Sets the store for an existing openils.widget.OrgUnitFilteringSelect
255 * using the orgs where the user has the requested permission.
256 * @param perm The permission to check
257 * @param selector The pre-created dijit.form.FilteringSelect object.
259 buildPermOrgSelector : function(perm, selector, selectedOrg, onload) {
262 dojo.require('dojo.data.ItemFileReadStore');
264 function hookupStore(store) {
265 selector.store = store;
267 if(selectedOrg != null)
268 selector.setValue(selectedOrg);
270 selector.setValue(_u.user.ws_ou());
274 function buildTreePicker(orgList) {
275 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
277 _u.permOrgStoreCache[perm] = store;
280 if (_u.permOrgStoreCache[perm])
281 hookupStore(_u.permOrgStoreCache[perm]);
283 _u.getPermOrgList(perm, buildTreePicker, true);
287 openils.User.user = null;
288 openils.User.authtoken = null;
289 openils.User.authtime = null;
290 openils.User.authcookie = null;