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;
45 if (this.authtoken) this.getBySession();
46 else if (this.id && this.authtoken) this.user = this.getById( this.id );
47 else if (kwargs.login) this.login();
51 getBySession : function(onComplete) {
53 var req = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.session.retrieve', _u.authtoken);
55 req.oncomplete = function(r) {
56 var user = r.recv().content();
58 if (!openils.User.user) openils.User.user = _u.user;
66 return _u.user = req.recv().content();
70 getById : function(id, onComplete) {
71 var req = OpenSRF.CachedClientSession('open-ils.actor').request('open-ils.actor.user.retrieve', this.authtoken, id);
73 req.oncomplete = function(r) {
74 var user = r.recv().content();
81 return req.recv().content();
87 * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
89 login_async : function(args, onComplete) {
93 if (!args.username) args.username = _u.username;
94 if (!args.passwd) args.passwd = _u.passwd;
95 if (!args.type) args.type = _u.login_type;
96 if (!args.location) args.location = _u.location;
98 var initReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.init', args.username);
100 initReq.oncomplete = function(r) {
101 var seed = r.recv().content();
104 username : args.username,
105 password : hex_md5(seed + hex_md5(args.passwd)),
110 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.complete', loginInfo);
111 authReq.oncomplete = function(rr) {
112 var data = rr.recv().content();
113 _u.authtoken = data.payload.authtoken;
114 if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
115 _u.authtime = data.payload.authtime;
116 if (!openils.User.authtime) openils.User.authtime = _u.authtime;
117 _u.getBySession(onComplete);
125 login : function(args) {
127 if (!args) args = {};
128 if (!args.username) args.username = _u.username;
129 if (!args.passwd) args.passwd = _u.passwd;
130 if (!args.type) args.type = _u.login_type;
131 if (!args.location) args.location = _u.location;
133 var seed = fieldmapper.standardRequest(
134 ['open-ils.auth', 'open-ils.auth.authenticate.init'],
139 username : args.username,
140 password : hex_md5(seed + hex_md5(args.passwd)),
145 var data = fieldmapper.standardRequest(
146 ['open-ils.auth', 'open-ils.auth.authenticate.complete'],
150 _u.authtoken = data.payload.authtoken;
151 if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
152 _u.authtime = data.payload.authtime;
153 if (!openils.User.authtime) openils.User.authtime = _u.authtime;
158 * Returns a list of the "highest" org units where the user
159 * has the given permission.
161 getPermOrgList : function(perm, onload) {
163 var req = OpenSRF.CachedClientSession('open-ils.actor').request(
164 'open-ils.actor.user.work_perm.highest_org_set',
165 this.authtoken, perm);
167 req.oncomplete = function(r) {
168 org_list = r.recv().content();
176 * Builds a dijit.Tree using the orgs where the user has the requested permission
177 * @param perm The permission to check
178 * @param domId The DOM node where the tree widget should live
179 * @param onClick If defined, this will be connected to the tree widget for
182 buildPermOrgTreePicker : function(perm, domId, onClick) {
184 dojo.require('dojo.data.ItemFileReadStore');
185 dojo.require('dijit.Tree');
186 function buildTreePicker(r) {
187 var orgList = r.recv().content();
188 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
189 var model = new dijit.tree.ForestStoreModel({
191 query: {_top:'true'},
192 childrenAttrs: ["children"],
193 rootLabel : "Location" /* XXX i18n */
196 var tree = new dijit.Tree({model : model}, dojo.byId(domId));
198 dojo.connect(tree, 'onClick', onClick);
202 fieldmapper.standardRequest(
203 ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'],
204 { params: [this.authtoken, perm],
205 oncomplete: buildTreePicker,
212 * Sets the store for an existing openils.widget.OrgUnitFilteringSelect
213 * using the orgs where the user has the requested permission.
214 * @param perm The permission to check
215 * @param selector The pre-created dijit.form.FilteringSelect object.
217 buildPermOrgSelector : function(perm, selector) {
220 dojo.require('dojo.data.ItemFileReadStore');
222 function buildTreePicker(r) {
223 var orgList = r.recv().content();
224 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
225 selector.store = store;
227 selector.setValue(_u.user.ws_ou());
230 fieldmapper.standardRequest(
231 ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'],
232 { params: [this.authtoken, perm],
233 oncomplete: buildTreePicker,
241 openils.User.user = null;
242 openils.User.authtoken = null;
243 openils.User.authtime = null;