5 var OILS_WORK_PERMS = {};
7 var XML_ELEMENT_NODE = 1;
10 var FETCH_ORG_UNIT = "open-ils.actor:open-ils.actor.org_unit.retrieve";
12 function debug(str) { try { dump(str + '\n'); } catch(e){} }
14 function fetchUser(session) {
15 if(session == null ) {
17 session = cgi.param('ses');
18 if(!session && (location.protocol == 'chrome:' || location.protocol == 'oils:')) {
20 var CacheClass = Components.classes["@open-ils.org/openils_data_cache;1"].getService();
21 session = CacheClass.wrappedJSObject.data.session.key;
23 console.log("Error loading XUL stash: " + e);
27 if(!session) throw "User session is not defined";
29 var request = new Request(FETCH_SESSION, session);
31 var user = request.result();
32 if(checkILSEvent(user)) throw user;
37 /* if defined, callback will get the user object asynchronously */
38 function fetchFleshedUser(id, callback) {
39 if(id == null) return null;
40 var req = new Request(
41 'open-ils.actor:open-ils.actor.user.fleshed.retrieve', SESSION, id );
44 req.callback( function(r){callback(r.getResultObject());} );
54 * Fetches the highest org at for each perm and stores the value in
55 * PERMS[ permName ]. It also returns the org list to the caller
57 function fetchHighestPermOrgs( session, userId, perms ) {
58 var req = new RemoteRequest(
60 'open-ils.actor.user.perm.highest_org.batch',
61 session, userId, perms );
63 var orgs = req.getResultObject();
64 for( var i = 0; i != orgs.length; i++ )
65 PERMS[perms[i]] = orgs[i];
66 //PERMS[ perms[i] ] = ( orgs[i] != null ) ? orgs[i] : -1 ;
70 function fetchHighestWorkPermOrgs(session, userId, perms, onload) {
71 var req = new RemoteRequest(
73 'open-ils.actor.user.has_work_perm_at.batch',
76 req.setCompleteCallback(function(r){
77 onload(OILS_WORK_PERMS = r.getResultObject());
82 return OILS_WORK_PERMS = req.getResultObject();
88 Finds the lowest relevent org unit between a context org unit and a set of
89 permission orgs. This defines the sphere of influence for a given action
90 on a specific set of data. if the context org shares no common nodes with
91 the set of permission orgs, null is returned.
92 returns the orgUnit object
94 function findReleventRootOrg(permOrgList, contextOrgId) {
95 var contextOrgNode = findOrgUnit(contextOrgId);
96 for(var i = 0; i < permOrgList.length; i++) {
97 var permOrg = findOrgUnit(permOrgList[i]);
98 if(orgIsMine(permOrg, contextOrgNode)) {
99 // perm org is equal to or a parent of the context org, so the context org is the highest
100 return contextOrgNode;
101 } else if(orgIsMine(contextOrgNode, permOrg)) {
102 // perm org is a child if the context org, so permOrg is the highest org
110 /* offset is the depth of the highest org
111 in the tree we're building
114 /* XXX Moved to opac_utils.js */
117 function buildOrgSel(selector, org, offset) {
118 insertSelectorVal( selector, -1,
119 org.name(), org.id(), null, findOrgDepth(org) - offset );
120 for( var c in org.children() )
121 buildOrgSel( selector, org.children()[c], offset);
125 /** removes all child nodes in 'tbody' that have the attribute 'key' defined */
126 function cleanTbody(tbody, key) {
127 for( var c = 0; c < tbody.childNodes.length; c++ ) {
128 var child = tbody.childNodes[c];
129 if(child && child.getAttribute(key)) tbody.removeChild(child);
134 /** Inserts a row into a specified place in a table
135 * tbody is the table body
136 * row is the context row after which the new row is to be inserted
137 * newRow is the new row to insert
139 function insRow( tbody, row, newRow ) {
140 if(row.nextSibling) tbody.insertBefore( newRow, row.nextSibling );
141 else{ tbody.appendChild(newRow); }
145 /** Checks to see if a given node should be enabled
146 * A node should be enabled if the itemOrg is lower in the
147 * org tree than my permissions allow editing
148 * I.e. I can edit the context item because it's "below" me
150 function checkDisabled( node, itemOrg, perm ) {
151 var itemDepth = findOrgDepth(itemOrg);
152 var mydepth = findOrgDepth(PERMS[perm]);
153 if( mydepth != -1 && mydepth <= itemDepth ) node.disabled = false;
157 * If the item-related org unit (owner, etc.) is one of or
158 * or a child of any of the perm-orgs related to the
159 * provided permission, enable the requested node
161 function checkPermOrgDisabled(node, itemOrg, perm) {
162 var org_list = OILS_WORK_PERMS[perm];
163 if(org_list.length > 0) {
164 for(var i = 0; i < org_list.length; i++) {
165 var highPermOrg = findOrgUnit(org_list[i]);
166 if(orgIsMine(highPermOrg, findOrgUnit(itemOrg)))
167 node.disabled = false;
173 function fetchOrgUnit(id, callback) {
175 if(ORG_CACHE[id]) return ORG_CACHE[id];
176 var req = new Request(FETCH_ORG_UNIT, SESSION, id);
181 var org = r.getResultObject();
190 var org = req.result();