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, {
39 constructor : function ( kwargs ) {
40 kwargs = kwargs || {};
42 this.user = kwargs.user;
43 this.passwd = kwargs.passwd;
44 this.authtoken = kwargs.authtoken || openils.User.authtoken;
45 this.authtime = kwargs.authtime || openils.User.authtime;
46 this.login_type = kwargs.login_type;
47 this.location = kwargs.location;
48 this.authcookie = kwargs.authcookie || openils.User.authcookie;
49 this.permOrgStoreCache = {}; /* permName => permOrgUnitStore map */
51 if (this.id && this.authtoken) this.user = this.getById( this.id );
52 else if (this.authtoken) this.getBySession();
53 else if (kwargs.login) this.login();
56 getBySession : function(onComplete) {
58 var req = ['open-ils.auth', 'open-ils.auth.session.retrieve'];
59 var params = [_u.authtoken];
62 fieldmapper.standardRequest(
66 oncomplete : function(r) {
67 var user = r.recv().content();
69 if (!openils.User.user) openils.User.user = _u.user;
76 _u.user = fieldmapper.standardRequest(req, params);
77 if (!openils.User.user) openils.User.user = _u.user;
82 getById : function(id, onComplete) {
83 var req = OpenSRF.CachedClientSession('open-ils.actor').request('open-ils.actor.user.retrieve', this.authtoken, id);
85 req.oncomplete = function(r) {
86 var user = r.recv().content();
93 return req.recv().content();
99 * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
101 login_async : function(args, onComplete) {
104 if (!args) args = {};
105 if (!args.username) args.username = _u.username;
106 if (!args.passwd) args.passwd = _u.passwd;
107 if (!args.type) args.type = _u.login_type;
108 if (!args.location) args.location = _u.location;
110 var initReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.init', args.username);
112 initReq.oncomplete = function(r) {
113 var seed = r.recv().content();
115 username : args.username,
116 password : hex_md5(seed + hex_md5(args.passwd)),
119 workstation : args.workstation
122 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.complete', loginInfo);
123 authReq.oncomplete = function(rr) {
124 var data = rr.recv().content();
125 _u.authtoken = data.payload.authtoken;
126 if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
127 _u.authtime = data.payload.authtime;
128 if (!openils.User.authtime) openils.User.authtime = _u.authtime;
129 _u.getBySession(onComplete);
131 dojo.require('dojo.cookie');
132 dojo.cookie(_u.authcookie, _u.authtoken, {path:'/'});
141 login : function(args) {
143 if (!args) args = {};
144 if (!args.username) args.username = _u.username;
145 if (!args.passwd) args.passwd = _u.passwd;
146 if (!args.type) args.type = _u.login_type;
147 if (!args.location) args.location = _u.location;
149 var seed = fieldmapper.standardRequest(
150 ['open-ils.auth', 'open-ils.auth.authenticate.init'],
155 username : args.username,
156 password : hex_md5(seed + hex_md5(args.passwd)),
159 workstation : args.workstation,
162 var data = fieldmapper.standardRequest(
163 ['open-ils.auth', 'open-ils.auth.authenticate.complete'],
167 _u.authtoken = data.payload.authtoken;
168 if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
169 _u.authtime = data.payload.authtime;
170 if (!openils.User.authtime) openils.User.authtime = _u.authtime;
173 dojo.require('dojo.cookie');
174 dojo.cookie(_u.authcookie, _u.authtoken, {path:'/'});
180 * Returns a list of the "highest" org units where the user has the given permission(s).
181 * @param permList A single permission or list of permissions
182 * @param includeDescendents If true, return a list of 'highest' orgs plus descendents
183 * @idlist If true, return a list of IDs instead of org unit objects
185 getPermOrgList : function(permList, onload, includeDescendents, idlist) {
186 if(typeof permList == 'string') permList = [permList];
188 console.log('loading org perms ' + permList + ' for user ' + this.user.id());
189 var oncomplete = function(r) {
190 var permMap = openils.Util.readResponse(r);
192 for(var perm in permMap) {
193 var permOrgList = permMap[perm];
194 for(var i in permOrgList) {
195 if(includeDescendents) {
196 orgList = orgList.concat(
197 fieldmapper.aou.descendantNodeList(permOrgList[i]));
199 orgList = orgList.concat(fieldmapper.aou.findOrgUnit(permOrgList[i]));
206 for(var idx in orgList) {
207 var val = (idlist) ? orgList[idx].id() : orgList[idx];
208 if(trimmed.indexOf(val) < 0)
214 fieldmapper.standardRequest(
215 ['open-ils.actor', 'open-ils.actor.user.has_work_perm_at.batch'],
217 params: [this.authtoken, permList],
218 oncomplete: oncomplete
225 * Sets the store for an existing openils.widget.OrgUnitFilteringSelect
226 * using the orgs where the user has the requested permission.
227 * @param perm The permission to check
228 * @param selector The pre-created dijit.form.FilteringSelect object.
230 buildPermOrgSelector : function(perm, selector, selectedOrg, onload) {
233 dojo.require('dojo.data.ItemFileReadStore');
235 function hookupStore(store) {
236 selector.store = store;
238 if(selectedOrg != null)
239 selector.setValue(selectedOrg);
241 selector.setValue(_u.user.ws_ou());
245 function buildTreePicker(orgList) {
246 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
248 _u.permOrgStoreCache[perm] = store;
251 if (_u.permOrgStoreCache[perm])
252 hookupStore(_u.permOrgStoreCache[perm]);
254 _u.getPermOrgList(perm, buildTreePicker, true);
258 openils.User.user = null;
259 openils.User.authtoken = null;
260 openils.User.authtime = null;
261 openils.User.authcookie = null;