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');
27 dojo.require('dojo.cookie');
28 dojo.requireLocalization("openils.User", "User");
30 dojo.declare('openils.User', null, {
44 constructor : function ( kwargs ) {
45 kwargs = kwargs || {};
47 this.user = kwargs.user;
48 this.passwd = kwargs.passwd;
49 this.authtoken = kwargs.authtoken || openils.User.authtoken;
50 this.authtime = kwargs.authtime || openils.User.authtime;
51 this.login_type = kwargs.login_type;
52 this.login_agent = kwargs.login_agent || openils.User.default_login_agent || 'staffclient';
53 this.location = kwargs.location;
54 this.authcookie = kwargs.authcookie || openils.User.authcookie;
55 this.permOrgStoreCache = {}; /* permName => permOrgUnitStore map */
57 if (this.authcookie) this.authtoken = dojo.cookie(this.authcookie);
58 if (this.id && this.authtoken) this.user = this.getById( this.id );
59 else if (this.authtoken) this.getBySession();
60 else if (kwargs.login) this.login();
62 var id = this.id || (this.user && this.user.id) ? this.user.id() : null;
63 if(id && !this.permOrgCache[id])
64 this.permOrgCache[id] = {};
67 getBySession : function(onComplete) {
69 var req = ['open-ils.auth', 'open-ils.auth.session.retrieve'];
70 var params = [_u.authtoken];
72 if(this.sessionCache[this.authtoken]) {
73 this.user = this.sessionCache[this.authtoken];
74 if (!openils.User.user)
75 openils.User.user = this.user;
80 fieldmapper.standardRequest(
84 oncomplete : function(r) {
85 var user = r.recv().content();
87 _u.sessionCache[_u.authtoken] = user;
88 if (!openils.User.user) openils.User.user = _u.user;
95 _u.user = fieldmapper.standardRequest(req, params);
96 if (!openils.User.user) openils.User.user = _u.user;
97 _u.sessionCache[_u.authtoken] = _u.user;
102 getById : function(id, onComplete) {
103 var req = OpenSRF.CachedClientSession('open-ils.actor').request('open-ils.actor.user.retrieve', this.authtoken, id);
105 req.oncomplete = function(r) {
106 var user = r.recv().content();
113 return req.recv().content();
118 * Tests the given username and password. This version is async only.
120 auth_verify : function(args, onComplete) {
122 if (!args) args = {};
123 if (!args.username) args.username = _u.username;
124 if (!args.passwd) args.passwd = _u.passwd;
125 if (!args.agent) args.agent = _u.login_agent;
126 if (!args.type) args.type = _u.type;
128 var initReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.init', args.username);
130 initReq.oncomplete = function(r) {
131 var seed = r.recv().content();
134 username : args.username,
135 barcode : args.barcode,
136 password : hex_md5(seed + hex_md5(args.passwd)),
140 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.verify', loginInfo);
141 authReq.oncomplete = function(rr) {
142 var data = rr.recv().content();
143 var evt = openils.Event.parse(data);
144 if (evt && evt.code == 0) onComplete(true);
145 else onComplete(false);
155 * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
157 login_async : function(args, onComplete) {
160 if (!args) args = {};
161 if (!args.username) args.username = _u.username;
162 if (!args.passwd) args.passwd = _u.passwd;
163 if (!args.type) args.type = _u.login_type;
164 if (!args.agent) args.agent = _u.login_agent;
165 if (!args.location) args.location = _u.location;
167 var initReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.init', args.username);
169 initReq.oncomplete = function(r) {
170 var seed = r.recv().content();
172 username : args.username,
173 password : hex_md5(seed + hex_md5(args.passwd)),
177 workstation : args.workstation
180 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.complete', loginInfo);
181 authReq.oncomplete = function(rr) {
182 var data = rr.recv().content();
184 if(!data || !data.payload)
185 throw new Error("Login Failed: " + js2JSON(data));
187 _u.authtoken = data.payload.authtoken;
188 if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
189 _u.authtime = data.payload.authtime;
190 if (!openils.User.authtime) openils.User.authtime = _u.authtime;
191 _u.getBySession(onComplete);
193 dojo.cookie(_u.authcookie, _u.authtoken, {path:'/'});
202 login : function(args) {
204 if (!args) args = {};
205 if (!args.username) args.username = _u.username;
206 if (!args.passwd) args.passwd = _u.passwd;
207 if (!args.type) args.type = _u.login_type;
208 if (!args.agent) args.agent = _u.login_agent;
209 if (!args.location) args.location = _u.location;
211 var seed = fieldmapper.standardRequest(
212 ['open-ils.auth', 'open-ils.auth.authenticate.init'],
217 username : args.username,
218 password : hex_md5(seed + hex_md5(args.passwd)),
222 workstation : args.workstation,
225 var data = fieldmapper.standardRequest(
226 ['open-ils.auth', 'open-ils.auth.authenticate.complete'],
230 if(!data || !data.payload) return false;
232 _u.authtoken = data.payload.authtoken;
233 if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
234 _u.authtime = data.payload.authtime;
235 if (!openils.User.authtime) openils.User.authtime = _u.authtime;
238 dojo.cookie(_u.authcookie, _u.authtoken, {path:'/'});
246 * Returns a list of the "highest" org units where the user has the given permission(s).
247 * @param permList A single permission or list of permissions
248 * @param includeDescendents If true, return a list of 'highest' orgs plus descendents
249 * @idlist If true, return a list of IDs instead of org unit objects
251 getPermOrgList : function(permList, onload, includeDescendents, idlist) {
252 if(typeof permList == 'string') permList = [permList];
255 var oncomplete = function(r) {
257 if(r) permMap = openils.Util.readResponse(r);
260 for(var i = 0; i < permList.length; i++) {
261 var perm = permList[i];
262 var permOrgList = permMap[perm] || self.permOrgCache[self.user.id()][perm];
263 self.permOrgCache[self.user.id()][perm] = permOrgList;
265 for(var j in permOrgList) {
266 if(includeDescendents) {
267 orgList = orgList.concat(
268 fieldmapper.aou.descendantNodeList(permOrgList[j]));
270 orgList = orgList.concat(fieldmapper.aou.findOrgUnit(permOrgList[j]));
277 for(var idx in orgList) {
278 var val = (idlist) ? orgList[idx].id() : orgList[idx];
279 if(trimmed.indexOf(val) < 0)
286 for(var i = 0; i < permList.length; i++) {
287 if(!self.permOrgCache[self.user.id()][permList[i]])
288 fetchList.push(permList[i]);
291 if(fetchList.length == 0)
294 fieldmapper.standardRequest(
295 ['open-ils.actor', 'open-ils.actor.user.has_work_perm_at.batch'],
297 params: [this.authtoken, fetchList],
298 oncomplete: oncomplete
305 * Sets the store for an existing openils.widget.OrgUnitFilteringSelect
306 * using the orgs where the user has the requested permission.
307 * @param perm The permission to check
308 * @param selector The pre-created dijit.form.FilteringSelect object.
310 buildPermOrgSelector : function(perm, selector, selectedOrg, onload) {
313 dojo.require('dojo.data.ItemFileReadStore');
315 function hookupStore(store) {
316 selector.store = store;
318 if(selectedOrg != null)
319 selector.setValue(selectedOrg);
321 selector.setValue(_u.user.ws_ou());
325 function buildTreePicker(orgList) {
326 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
328 _u.permOrgStoreCache[perm] = store;
331 if (_u.permOrgStoreCache[perm])
332 hookupStore(_u.permOrgStoreCache[perm]);
334 _u.getPermOrgList(perm, buildTreePicker, true);
339 openils.User.user = null;
340 openils.User.authtoken = null;
341 openils.User.authtime = null;
342 openils.User.authcookie = null;
343 openils.User.default_login_agent = null; // global agent override
344 openils.User.localeStrings =
345 dojo.i18n.getLocalization("openils.User", "User");
347 openils.User.formalName = function(u) {
348 if (!u) u = openils.User.user;
349 return dojo.string.substitute(
350 openils.User.localeStrings.FULL_NAME, [
351 u.family_name(), u.first_given_name(),
352 u.second_given_name() ? u.second_given_name() : "",
353 u.prefix() ? u.prefix() : "",
354 u.suffix() ? u.suffix() : ""
356 ).replace(/\s{2,}/g, " ").replace(/\s$/, "");