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 * ---------------------------------------------------------------------------
17 if(!dojo._hasResource["openils.User"]) {
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');
25 dojo.declare('openils.User', null, {
35 constructor : function ( kwargs ) {
36 kwargs = kwargs || {};
38 this.user = kwargs.user;
39 this.passwd = kwargs.passwd;
40 this.authtoken = kwargs.authtoken || openils.User.authtoken;
41 this.authtime = kwargs.authtime || openils.User.authtime;
42 this.login_type = kwargs.login_type;
43 this.location = kwargs.location;
44 this.authcookie = kwargs.authcookie || openils.User.authcookie;
45 this.permOrgStoreCache = {}; /* permName => permOrgUnitStore map */
47 if (this.id && this.authtoken) this.user = this.getById( this.id );
48 else if (this.authtoken) this.getBySession();
49 else if (kwargs.login) this.login();
53 getBySession : function(onComplete) {
55 var req = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.session.retrieve', _u.authtoken);
57 req.oncomplete = function(r) {
58 var user = r.recv().content();
60 if (!openils.User.user) openils.User.user = _u.user;
68 return _u.user = req.recv().content();
72 getById : function(id, onComplete) {
73 var req = OpenSRF.CachedClientSession('open-ils.actor').request('open-ils.actor.user.retrieve', this.authtoken, id);
75 req.oncomplete = function(r) {
76 var user = r.recv().content();
83 return req.recv().content();
89 * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
91 login_async : function(args, onComplete) {
95 if (!args.username) args.username = _u.username;
96 if (!args.passwd) args.passwd = _u.passwd;
97 if (!args.type) args.type = _u.login_type;
98 if (!args.location) args.location = _u.location;
100 var initReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.init', args.username);
102 initReq.oncomplete = function(r) {
103 var seed = r.recv().content();
106 username : args.username,
107 password : hex_md5(seed + hex_md5(args.passwd)),
112 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.complete', loginInfo);
113 authReq.oncomplete = function(rr) {
114 var data = rr.recv().content();
115 _u.authtoken = data.payload.authtoken;
116 if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
117 _u.authtime = data.payload.authtime;
118 if (!openils.User.authtime) openils.User.authtime = _u.authtime;
119 _u.getBySession(onComplete);
121 dojo.require('dojo.cookie');
122 dojo.cookie(_u.authcookie, _u.authtoken);
131 login : function(args) {
133 if (!args) args = {};
134 if (!args.username) args.username = _u.username;
135 if (!args.passwd) args.passwd = _u.passwd;
136 if (!args.type) args.type = _u.login_type;
137 if (!args.location) args.location = _u.location;
139 var seed = fieldmapper.standardRequest(
140 ['open-ils.auth', 'open-ils.auth.authenticate.init'],
145 username : args.username,
146 password : hex_md5(seed + hex_md5(args.passwd)),
151 var data = fieldmapper.standardRequest(
152 ['open-ils.auth', 'open-ils.auth.authenticate.complete'],
156 _u.authtoken = data.payload.authtoken;
157 if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
158 _u.authtime = data.payload.authtime;
159 if (!openils.User.authtime) openils.User.authtime = _u.authtime;
162 dojo.require('dojo.cookie');
163 dojo.cookie(_u.authcookie, _u.authtoken);
169 * Returns a list of the "highest" org units where the user
170 * has the given permission.
172 getPermOrgList : function(perm, onload) {
173 fieldmapper.standardRequest(
174 ['open-ils.actor', 'open-ils.actor.user.work_perm.highest_org_set'],
176 params: [this.authtoken, perm],
177 oncomplete: function(r) {
178 org_list = r.recv().content();
186 * Builds a dijit.Tree using the orgs where the user has the requested permission
187 * @param perm The permission to check
188 * @param domId The DOM node where the tree widget should live
189 * @param onClick If defined, this will be connected to the tree widget for
192 buildPermOrgTreePicker : function(perm, domId, onClick) {
194 dojo.require('dojo.data.ItemFileReadStore');
195 dojo.require('dijit.Tree');
196 function buildTreePicker(r) {
197 var orgList = r.recv().content();
198 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
199 var model = new dijit.tree.ForestStoreModel({
201 query: {_top:'true'},
202 childrenAttrs: ["children"],
203 rootLabel : "Location" /* XXX i18n */
206 var tree = new dijit.Tree({model : model}, dojo.byId(domId));
208 dojo.connect(tree, 'onClick', onClick);
212 fieldmapper.standardRequest(
213 ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'],
214 { params: [this.authtoken, perm],
215 oncomplete: buildTreePicker,
222 * Sets the store for an existing openils.widget.OrgUnitFilteringSelect
223 * using the orgs where the user has the requested permission.
224 * @param perm The permission to check
225 * @param selector The pre-created dijit.form.FilteringSelect object.
227 buildPermOrgSelector : function(perm, selector) {
230 dojo.require('dojo.data.ItemFileReadStore');
232 function hookupStore(store) {
233 selector.store = store;
235 selector.setValue(_u.user.ws_ou());
238 function buildTreePicker(orgList) {
239 var orgNodeList = [];
240 for(var i = 0; i < orgList.length; i++)
241 orgNodeList = orgNodeList.concat(
242 fieldmapper.aou.descendantNodeList(orgList[i]));
244 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgNodeList)});
246 _u.permOrgStoreCache[perm] = store;
249 if (_u.permOrgStoreCache[perm])
250 hookupStore(_u.permOrgStoreCache[perm]);
252 _u.getPermOrgList(perm, buildTreePicker);
256 openils.User.user = null;
257 openils.User.authtoken = null;
258 openils.User.authtime = null;
259 openils.User.authcookie = null;