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 : 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 password : hex_md5(seed + hex_md5(args.passwd)),
109 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.complete', loginInfo);
110 authReq.oncomplete = function(rr) {
111 var data = rr.recv().content();
112 _u.authtoken = data.payload.authtoken;
113 if (!openils.User.authtoken) openils.User.authtoken = _u.authtoken;
114 _u.authtime = data.payload.authtime;
115 if (!openils.User.authtime) openils.User.authtime = _u.authtime;
116 _u.getBySession(onComplete);
125 * Returns a list of the "highest" org units where the user
126 * has the given permission.
128 getPermOrgList : function(perm, onload) {
130 var req = OpenSRF.CachedClientSession('open-ils.actor').request(
131 'open-ils.actor.user.work_perm.highest_org_set',
132 this.authtoken, perm);
134 req.oncomplete = function(r) {
135 org_list = r.recv().content();
143 * Builds a dijit.Tree using the orgs where the user has the requested permission
144 * @param perm The permission to check
145 * @param domId The DOM node where the tree widget should live
146 * @param onClick If defined, this will be connected to the tree widget for
149 buildPermOrgTreePicker : function(perm, domId, onClick) {
151 dojo.require('dojo.data.ItemFileReadStore');
152 dojo.require('dijit.Tree');
153 function buildTreePicker(r) {
154 var orgList = r.recv().content();
155 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
156 var model = new dijit.tree.ForestStoreModel({
158 query: {_top:'true'},
159 childrenAttrs: ["children"],
160 rootLabel : "Location" /* XXX i18n */
163 var tree = new dijit.Tree({model : model}, dojo.byId(domId));
165 dojo.connect(tree, 'onClick', onClick);
169 fieldmapper.standardRequest(
170 ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'],
171 { params: [this.authtoken, perm],
172 oncomplete: buildTreePicker,
179 * Sets the store for an existing openils.widget.OrgUnitFilteringSelect
180 * using the orgs where the user has the requested permission.
181 * @param perm The permission to check
182 * @param selector The pre-created dijit.form.FilteringSelect object.
184 buildPermOrgSelector : function(perm, selector) {
187 dojo.require('dojo.data.ItemFileReadStore');
189 function buildTreePicker(r) {
190 var orgList = r.recv().content();
191 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
192 selector.store = store;
194 selector.setValue(_u.user.ws_ou());
197 fieldmapper.standardRequest(
198 ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'],
199 { params: [this.authtoken, perm],
200 oncomplete: buildTreePicker,
208 openils.User.user = null;
209 openils.User.authtoken = null;
210 openils.User.authtime = null;