| 24 | | dojo.declare('openils.User', null, {}); |
| 25 | | |
| 26 | | openils.User.user = null; |
| 27 | | openils.User.authtoken = null; |
| 28 | | openils.User.authtime = null; |
| 29 | | |
| 30 | | var ses = new OpenSRF.ClientSession('open-ils.auth'); |
| 31 | | |
| 32 | | openils.User.getBySession = function(onComplete) { |
| 33 | | var req = ses.request('open-ils.auth.session.retrieve', openils.User.authtoken); |
| 34 | | if(onComplete) { |
| | 25 | dojo.declare('openils.User', null, { |
| | 26 | |
| | 27 | user : null, |
| | 28 | username : null, |
| | 29 | passwd : null, |
| | 30 | login_type : 'opac', |
| | 31 | location : null, |
| | 32 | authtoken : null, |
| | 33 | authtime : null, |
| | 34 | |
| | 35 | constructor : function ( kwargs ) { |
| | 36 | this.id = kwargs.id; |
| | 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; |
| | 42 | |
| | 43 | if (this.id && this.authtoken) this.getById(); |
| | 44 | else if (this.authtoken) this.getBySession(); |
| | 45 | else if (kwargs.login) this.login(); |
| | 46 | |
| | 47 | }, |
| | 48 | |
| | 49 | getBySession : function(onComplete) { |
| | 50 | var _u = this; |
| | 51 | var req = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.session.retrieve', _u.authtoken); |
| | 52 | if(onComplete) { |
| | 53 | req.oncomplete = function(r) { |
| | 54 | var user = r.recv().content(); |
| | 55 | _u.user = user; |
| | 56 | if(onComplete) |
| | 57 | onComplete(user); |
| | 58 | } |
| | 59 | req.send(); |
| | 60 | } else { |
| | 61 | req.timeout = 10; |
| | 62 | req.send(); |
| | 63 | return _u.user = req.recv().content(); |
| | 64 | } |
| | 65 | }, |
| | 66 | |
| | 67 | getById : function(id, onComplete) { |
| | 68 | var req = OpenSRF.CachedClientSession('open-ils.actor').request('open-ils.actor.user.retrieve', this.authtoken, id); |
| | 69 | if(onComplete) { |
| | 70 | req.oncomplete = function(r) { |
| | 71 | var user = r.recv().content(); |
| | 72 | onComplete(user); |
| | 73 | } |
| | 74 | req.send(); |
| | 75 | } else { |
| | 76 | req.timeout = 10; |
| | 77 | req.send(); |
| | 78 | return req.recv().content(); |
| | 79 | } |
| | 80 | }, |
| | 81 | |
| | 82 | |
| | 83 | /** |
| | 84 | * Logs in, sets the authtoken/authtime vars, and fetches the logged in user |
| | 85 | */ |
| | 86 | login : function(args, onComplete) { |
| | 87 | var _u = this; |
| | 88 | |
| | 89 | if (!args) args = {}; |
| | 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; |
| | 94 | |
| | 95 | var initReq = OpenSRF.CachedClientSession('open-ils.auth').request('open-ils.auth.authenticate.init', args.username); |
| | 96 | |
| | 97 | initReq.oncomplete = function(r) { |
| | 98 | var seed = r.recv().content(); |
| | 99 | alert(seed); |
| | 100 | var loginInfo = { |
| | 101 | password : hex_md5(seed + hex_md5(args.passwd)), |
| | 102 | type : args.type, |
| | 103 | org : args.location, |
| | 104 | }; |
| | 105 | |
| | 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); |
| | 112 | } |
| | 113 | authReq.send(); |
| | 114 | } |
| | 115 | |
| | 116 | initReq.send(); |
| | 117 | }, |
| | 118 | |
| | 119 | /** |
| | 120 | * Returns a list of the "highest" org units where the user |
| | 121 | * has the given permission. |
| | 122 | */ |
| | 123 | getPermOrgList : function(perm, onload) { |
| | 124 | |
| | 125 | var req = OpenSRF.CachedClientSession('open-ils.actor').request( |
| | 126 | 'open-ils.actor.user.work_perm.highest_org_set', |
| | 127 | this.authtoken, perm); |
| | 128 | |
| 42 | | } else { |
| 43 | | req.timeout = 10; |
| 44 | | req.send(); |
| 45 | | return openils.User.user = req.recv().content(); |
| | 135 | }, |
| | 136 | |
| | 137 | /** |
| | 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 |
| | 142 | * onClick events |
| | 143 | */ |
| | 144 | buildPermOrgTreePicker : function(perm, domId, onClick) { |
| | 145 | |
| | 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({ |
| | 152 | store: store, |
| | 153 | query: {_top:'true'}, |
| | 154 | childrenAttrs: ["children"], |
| | 155 | rootLabel : "Location" /* XXX i18n */ |
| | 156 | }); |
| | 157 | |
| | 158 | var tree = new dijit.Tree({model : model}, dojo.byId(domId)); |
| | 159 | if(onClick) |
| | 160 | dojo.connect(tree, 'onClick', onClick); |
| | 161 | tree.startup() |
| | 162 | } |
| | 163 | |
| | 164 | fieldmapper.standardRequest( |
| | 165 | ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'], |
| | 166 | { params: [this.authtoken, perm], |
| | 167 | oncomplete: buildTreePicker, |
| | 168 | async: true |
| | 169 | } |
| | 170 | ) |
| | 171 | }, |
| | 172 | |
| | 173 | /** |
| | 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. |
| | 178 | */ |
| | 179 | buildPermOrgSelector : function(perm, selector) { |
| | 180 | var _u = this; |
| | 181 | |
| | 182 | dojo.require('dojo.data.ItemFileReadStore'); |
| | 183 | |
| | 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; |
| | 188 | selector.startup(); |
| | 189 | selector.setValue(_u.user.ws_ou()); |
| | 190 | } |
| | 191 | |
| | 192 | fieldmapper.standardRequest( |
| | 193 | ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'], |
| | 194 | { params: [this.authtoken, perm], |
| | 195 | oncomplete: buildTreePicker, |
| | 196 | async: true |
| | 197 | } |
| | 198 | ) |
| 47 | | } |
| 48 | | |
| 49 | | openils.User.getById = function(id, onComplete) { |
| 50 | | var ases = new OpenSRF.ClientSession('open-ils.actor'); |
| 51 | | var req = ases.request('open-ils.actor.user.retrieve', openils.User.authtoken, id); |
| 52 | | if(onComplete) { |
| 53 | | req.oncomplete = function(r) { |
| 54 | | var user = r.recv().content(); |
| 55 | | onComplete(user); |
| 56 | | } |
| 57 | | req.send(); |
| 58 | | } else { |
| 59 | | req.timeout = 10; |
| 60 | | req.send(); |
| 61 | | return req.recv().content(); |
| 62 | | } |
| 63 | | } |
| 64 | | |
| 65 | | |
| 66 | | /** |
| 67 | | * Logs in, sets the authtoken/authtime vars, and fetches the logged in user |
| 68 | | */ |
| 69 | | openils.User.login = function(args, onComplete) { |
| 70 | | var initReq = ses.request('open-ils.auth.authenticate.init', args.username); |
| 71 | | |
| 72 | | initReq.oncomplete = function(r) { |
| 73 | | var seed = r.recv().content(); |
| 74 | | alert(seed); |
| 75 | | var loginInfo = { |
| 76 | | password : hex_md5(seed + hex_md5(args.passwd)), |
| 77 | | type : args.type || 'opac', |
| 78 | | org : args.location, |
| 79 | | }; |
| 80 | | |
| 81 | | var authReq = ses.request('open-ils.auth.authenticate.complete', loginInfo); |
| 82 | | authReq.oncomplete = function(rr) { |
| 83 | | var data = rr.recv().content(); |
| 84 | | openils.User.authtoken = data.payload.authtoken; |
| 85 | | openils.User.authtime = data.payload.authtime; |
| 86 | | openils.User.getBySession(onComplete); |
| 87 | | } |
| 88 | | authReq.send(); |
| 89 | | } |
| 90 | | |
| 91 | | initReq.send(); |
| 92 | | } |
| 93 | | |
| 94 | | /** |
| 95 | | * Returns a list of the "highest" org units where the user |
| 96 | | * has the given permission. |
| 97 | | */ |
| 98 | | openils.User.getPermOrgList = function(perm, onload) { |
| 99 | | |
| 100 | | var ases = new OpenSRF.ClientSession('open-ils.actor'); |
| 101 | | var req = ases.request( |
| 102 | | 'open-ils.actor.user.work_perm.highest_org_set', |
| 103 | | openils.User.authtoken, perm); |
| 104 | | |
| 105 | | req.oncomplete = function(r) { |
| 106 | | org_list = r.recv().content(); |
| 107 | | onload(org_list); |
| 108 | | } |
| 109 | | |
| 110 | | req.send(); |
| 111 | | } |
| 112 | | |
| 113 | | /** |
| 114 | | * Builds a dijit.Tree using the orgs where the user has the requested permission |
| 115 | | * @param perm The permission to check |
| 116 | | * @param domId The DOM node where the tree widget should live |
| 117 | | * @param onClick If defined, this will be connected to the tree widget for |
| 118 | | * onClick events |
| 119 | | */ |
| 120 | | openils.User.buildPermOrgTreePicker = function(perm, domId, onClick) { |
| 121 | | |
| 122 | | function buildTreePicker(r) { |
| 123 | | var orgList = r.recv().content(); |
| 124 | | var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)}); |
| 125 | | var model = new dijit.tree.ForestStoreModel({ |
| 126 | | store: store, |
| 127 | | query: {_top:'true'}, |
| 128 | | childrenAttrs: ["children"], |
| 129 | | rootLabel : "Location" /* XXX i18n */ |
| 130 | | }); |
| 131 | | |
| 132 | | var tree = new dijit.Tree({model : model}, dojo.byId(domId)); |
| 133 | | if(onClick) |
| 134 | | dojo.connect(tree, 'onClick', onClick); |
| 135 | | tree.startup() |
| 136 | | } |
| 137 | | |
| 138 | | fieldmapper.standardRequest( |
| 139 | | ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'], |
| 140 | | { params: [openils.User.authtoken, perm], |
| 141 | | oncomplete: buildTreePicker, |
| 142 | | async: true |
| 143 | | } |
| 144 | | ) |
| 145 | | } |
| 146 | | |
| 147 | | /** |
| 148 | | * Sets the store for an existing openils.widget.OrgUnitFilteringSelect |
| 149 | | * using the orgs where the user has the requested permission. |
| 150 | | * @param perm The permission to check |
| 151 | | * @param selector The pre-created dijit.form.FilteringSelect object. |
| 152 | | */ |
| 153 | | openils.User.buildPermOrgSelector = function(perm, selector) { |
| 154 | | |
| 155 | | function buildTreePicker(r) { |
| 156 | | var orgList = r.recv().content(); |
| 157 | | var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgList)}); |
| 158 | | selector.store = store; |
| 159 | | selector.startup(); |
| 160 | | selector.setValue(openils.User.user.ws_ou()); |
| 161 | | } |
| 162 | | |
| 163 | | fieldmapper.standardRequest( |
| 164 | | ['open-ils.actor', 'open-ils.actor.user.work_perm.org_unit_list'], |
| 165 | | { params: [openils.User.authtoken, perm], |
| 166 | | oncomplete: buildTreePicker, |
| 167 | | async: true |
| 168 | | } |
| 169 | | ) |
| 170 | | } |
| | 200 | |
| | 201 | }); |