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;
46 if (this.id && this.authtoken) this.user = this.getById( this.id );
47 else if (this.authtoken) this.getBySession();
48 else if (kwargs.login) this.login();
52 getBySession : function(onComplete) {
54 var req = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.session.retrieve', _u.authtoken);
56 req.oncomplete = function(r) {
57 var user = r.recv().content();
59 if (!openils.User.user) openils.User.user = _u.user;
67 return _u.user = req.recv().content();
71 getById : function(id, onComplete) {
72 var req = OpenSRF.CachedClientSession('open-ils.actor').request('open-ils.actor.user.retrieve', this.authtoken, id);
74 req.oncomplete = function(r) {
75 var user = r.recv().content();
82 return req.recv().content();
88 * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
90 login_async : function(args, onComplete) {
94 if (!args.username) args.username = _u.username;
95 if (!args.passwd) args.passwd = _u.passwd;
96 if (!args.type) args.type = _u.login_type;
97 if (!args.location) args.location = _u.location;
99 var initReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.init', args.username);
101 initReq.oncomplete = function(r) {
102 var seed = r.recv().content();
105 username : args.username,
106 password : hex_md5(seed + hex_md5(args.passwd)),
111 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.complete', loginInfo);
112 authReq.oncomplete = function(rr) {
113 var data = rr.recv().content();
114 _u.authtoken = data.payload.authtoken;
115 if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
116 _u.authtime = data.payload.authtime;
117 if (!openils.User.authtime) openils.User.authtime = _u.authtime;
118 _u.getBySession(onComplete);
120 dojo.require('dojo.cookie');
121 dojo.cookie(_u.authcookie, _u.authtoken);
130 login : function(args) {
132 if (!args) args = {};
133 if (!args.username) args.username = _u.username;
134 if (!args.passwd) args.passwd = _u.passwd;
135 if (!args.type) args.type = _u.login_type;
136 if (!args.location) args.location = _u.location;
138 var seed = fieldmapper.standardRequest(
139 ['open-ils.auth', 'open-ils.auth.authenticate.init'],
144 username : args.username,
145 password : hex_md5(seed + hex_md5(args.passwd)),
150 var data = fieldmapper.standardRequest(
151 ['open-ils.auth', 'open-ils.auth.authenticate.complete'],
155 _u.authtoken = data.payload.authtoken;
156 if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
157 _u.authtime = data.payload.authtime;
158 if (!openils.User.authtime) openils.User.authtime = _u.authtime;
161 dojo.require('dojo.cookie');
162 dojo.cookie(_u.authcookie, _u.authtoken);
168 * Returns a list of the "highest" org units where the user
169 * has the given permission.
171 getPermOrgList : function(perm, onload) {
173 var req = OpenSRF.CachedClientSession('open-ils.actor').request(
174 'open-ils.actor.user.work_perm.highest_org_set',
175 this.authtoken, perm);
177 req.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 buildTreePicker(r) {
233 var orgList = r.recv().content();
234 var orgNodeList = [];
236 for(var i = 0; i < orgList.length; i++)
237 orgNodeList = orgNodeList.concat(
238 fieldmapper.aou.descendantNodeList(orgList[i]));
240 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgNodeList)});
241 selector.store = store;
243 selector.setValue(_u.user.ws_ou());
246 fieldmapper.standardRequest(
247 ['open-ils.actor', 'open-ils.actor.user.work_perm.highest_org_set'],
248 { params: [this.authtoken, perm],
249 oncomplete: buildTreePicker,
257 openils.User.user = null;
258 openils.User.authtoken = null;
259 openils.User.authtime = null;
260 openils.User.authcookie = null;