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 ) {
37 this.user = kwargs.user;
38 this.passwd = kwargs.passwd;
39 this.authtoken = kwargs.authtoken || openils.User.authtoken;
40 this.authtime = kwargs.authtime || openils.User.authtime;
41 this.login_type = kwargs.login_type;
42 this.location = kwargs.location;
44 if (this.authtoken) this.getBySession();
45 else if (this.id && this.authtoken) this.user = this.getById( this.id );
46 else if (kwargs.login) this.login();
50 getBySession : function(onComplete) {
52 var req = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.session.retrieve', _u.authtoken);
54 req.oncomplete = function(r) {
55 var user = r.recv().content();
57 if (!openils.User.user) !openils.User.user = _u.user;
65 return _u.user = req.recv().content();
69 getById : function(id, onComplete) {
70 var req = OpenSRF.CachedClientSession('open-ils.actor').request('open-ils.actor.user.retrieve', this.authtoken, id);
72 req.oncomplete = function(r) {
73 var user = r.recv().content();
80 return req.recv().content();
86 * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
88 login : function(args, onComplete) {
92 if (!args.username) args.username = _u.username;
93 if (!args.passwd) args.passwd = _u.passwd;
94 if (!args.type) args.type = _u.login_type;
95 if (!args.location) args.location = _u.location;
97 var initReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.init', args.username);
99 initReq.oncomplete = function(r) {
100 var seed = r.recv().content();
103 password : hex_md5(seed + hex_md5(args.passwd)),
108 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.complete', loginInfo);
109 authReq.oncomplete = function(rr) {
110 var data = rr.recv().content();
111 _u.authtoken = data.payload.authtoken;
112 if (!openils.User.authtoken) !openils.User.authtoken = _u.authtoken;
113 _u.authtime = data.payload.authtime;
114 if (!openils.User.authtime) !openils.User.authtime = _u.authtime;
115 _u.getBySession(onComplete);
124 * Returns a list of the "highest" org units where the user
125 * has the given permission.
127 getPermOrgList : function(perm, onload) {
129 var req = OpenSRF.CachedClientSession('open-ils.actor').request(
130 'open-ils.actor.user.work_perm.highest_org_set',
131 this.authtoken, perm);
133 req.oncomplete = function(r) {
134 org_list = r.recv().content();
142 * Builds a dijit.Tree using the orgs where the user has the requested permission
143 * @param perm The permission to check
144 * @param domId The DOM node where the tree widget should live
145 * @param onClick If defined, this will be connected to the tree widget for
148 buildPermOrgTreePicker : function(perm, domId, onClick) {
150 dojo.require('dojo.data.ItemFileReadStore');
151 dojo.require('dijit.Tree');
152 function buildTreePicker(r) {
153 var orgList = r.recv().content();
154 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
155 var model = new dijit.tree.ForestStoreModel({
157 query: {_top:'true'},
158 childrenAttrs: ["children"],
159 rootLabel : "Location" /* XXX i18n */
162 var tree = new dijit.Tree({model : model}, dojo.byId(domId));
164 dojo.connect(tree, 'onClick', onClick);
168 fieldmapper.standardRequest(
169 ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'],
170 { params: [this.authtoken, perm],
171 oncomplete: buildTreePicker,
178 * Sets the store for an existing openils.widget.OrgUnitFilteringSelect
179 * using the orgs where the user has the requested permission.
180 * @param perm The permission to check
181 * @param selector The pre-created dijit.form.FilteringSelect object.
183 buildPermOrgSelector : function(perm, selector) {
186 dojo.require('dojo.data.ItemFileReadStore');
188 function buildTreePicker(r) {
189 var orgList = r.recv().content();
190 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
191 selector.store = store;
193 selector.setValue(_u.user.ws_ou());
196 fieldmapper.standardRequest(
197 ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'],
198 { params: [this.authtoken, perm],
199 oncomplete: buildTreePicker,
207 openils.User.user = null;
208 openils.User.authtoken = null;
209 openils.User.authtime = null;