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.authtoken = kwargs.authtoken;
39 this.authtime = kwargs.authtime;
40 this.login_type = kwargs.login_type;
41 this.location = kwargs.location;
43 if (this.id && this.authtoken) this.getById();
44 else if (this.authtoken) this.getBySession();
45 else if (kwargs.login) this.login();
49 getBySession : function(onComplete) {
51 var req = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.session.retrieve', _u.authtoken);
53 req.oncomplete = function(r) {
54 var user = r.recv().content();
63 return _u.user = req.recv().content();
67 getById : function(id, onComplete) {
68 var req = OpenSRF.CachedClientSession('open-ils.actor').request('open-ils.actor.user.retrieve', this.authtoken, id);
70 req.oncomplete = function(r) {
71 var user = r.recv().content();
78 return req.recv().content();
84 * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
86 login : function(args, onComplete) {
90 if (!args.username) args.username = _u.username;
91 if (!args.passwd) args.passwd = _u.passwd;
92 if (!args.type) args.type = _u.login_type;
93 if (!args.location) args.location = _u.location;
95 var initReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.init', args.username);
97 initReq.oncomplete = function(r) {
98 var seed = r.recv().content();
101 password : hex_md5(seed + hex_md5(args.passwd)),
106 var authReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.complete', loginInfo);
107 authReq.oncomplete = function(rr) {
108 var data = rr.recv().content();
109 _u.authtoken = data.payload.authtoken;
110 _u.authtime = data.payload.authtime;
111 _u.getBySession(onComplete);
120 * Returns a list of the "highest" org units where the user
121 * has the given permission.
123 getPermOrgList : function(perm, onload) {
125 var req = OpenSRF.CachedClientSession('open-ils.actor').request(
126 'open-ils.actor.user.work_perm.highest_org_set',
127 this.authtoken, perm);
129 req.oncomplete = function(r) {
130 org_list = r.recv().content();
138 * Builds a dijit.Tree using the orgs where the user has the requested permission
139 * @param perm The permission to check
140 * @param domId The DOM node where the tree widget should live
141 * @param onClick If defined, this will be connected to the tree widget for
144 buildPermOrgTreePicker : function(perm, domId, onClick) {
146 dojo.require('dojo.data.ItemFileReadStore');
147 dojo.require('dijit.Tree');
148 function buildTreePicker(r) {
149 var orgList = r.recv().content();
150 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
151 var model = new dijit.tree.ForestStoreModel({
153 query: {_top:'true'},
154 childrenAttrs: ["children"],
155 rootLabel : "Location" /* XXX i18n */
158 var tree = new dijit.Tree({model : model}, dojo.byId(domId));
160 dojo.connect(tree, 'onClick', onClick);
164 fieldmapper.standardRequest(
165 ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'],
166 { params: [this.authtoken, perm],
167 oncomplete: buildTreePicker,
174 * Sets the store for an existing openils.widget.OrgUnitFilteringSelect
175 * using the orgs where the user has the requested permission.
176 * @param perm The permission to check
177 * @param selector The pre-created dijit.form.FilteringSelect object.
179 buildPermOrgSelector : function(perm, selector) {
182 dojo.require('dojo.data.ItemFileReadStore');
184 function buildTreePicker(r) {
185 var orgList = r.recv().content();
186 var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)});
187 selector.store = store;
189 selector.setValue(_u.user.ws_ou());
192 fieldmapper.standardRequest(
193 ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'],
194 { params: [this.authtoken, perm],
195 oncomplete: buildTreePicker,