7 function UserSession() {
8 this.cookie = new cookieObject("ses", 1, "/opac/", "ils_ses");
9 this.connected = false;
10 globalUserSession = this;
14 UserSession.prototype.destroy = function() {
15 debug("Removing user session");
16 this.connected = false;
17 this.session_id = null;
23 UserSession.prototype.persist = function() {
25 this.cookie = new cookieObject("ses", 1, "/opac/", "ils_ses");
27 if(!this.session_id) return;
30 this.cookie.put("ils_ses", this.session_id);
32 debug("Persisting session with session " +
33 this.session_id + " and uname " + this.username );
36 debug("Persisted session " + this.cookie.fields[UserSession.SES]);
41 UserSession.prototype.verifySession = function(ses) {
43 debug("Verifying session...");
45 debug("Session key passed in [" + ses + "], verifying...");
48 this.session_id = ses;
50 this.session_id = this.cookie.fields[UserSession.SES];
52 if(this.session_id && this.userObject && this.username && this.connected) {
57 debug("Retrieveing user info for session " + this.session_id);
59 /* user is returning to the page with a session key */
60 var request = new RemoteRequest("open-ils.auth",
61 "open-ils.auth.session.retrieve", this.session_id );
66 var user = request.getResultObject();
69 if( typeof user == 'object' && user._isfieldmapper) {
71 debug("User retrieved, setting up user info");
72 this.username = user.usrname();
73 this.userObject = user;
74 this.connected = true;
79 debug("User session " + this.session_id + " is no longer valid");
85 debug("No session cookie found");
92 UserSession.instance = function() {
93 if( globalUserSession )
94 return globalUserSession;
95 return new UserSession();
98 UserSession.prototype.setSessionId = function( id ) {
99 debug("User session id " + id );
100 this.session_id = id;
103 UserSession.prototype.getSessionId = function() {
104 return this.session_id;
107 UserSession.prototype.login = function( username, password ) {
109 if(!username || !password) { return false; }
110 this.username = username;
112 var init_request = new RemoteRequest( 'open-ils.auth',
113 'open-ils.auth.authenticate.init', username );
115 init_request.send(true);
116 var seed = init_request.getResultObject();
118 if( ! seed || seed == '0') {
119 /* XXX should be an exception */
120 alert( "Error Communicating with Authentication Server" );
124 var auth_request = new RemoteRequest( 'open-ils.auth',
125 'open-ils.auth.authenticate.complete', username,
126 hex_md5(seed + hex_md5(password)), "opac");
128 auth_request.send(true);
129 var auth_result = auth_request.getResultObject();
131 if(auth_result == '0') { return false; }
133 this.setSessionId(auth_result);
135 this.connected = true;
144 /* grab this users org unit */
145 /* if new_org_id is provided, it is used instead of the home_ou
147 UserSession.prototype.grabOrgUnit = function(org) {
148 var session = this.getSessionId();
151 "No session ID for user in grabOrgUnit()");
154 debug("Retrieving this users object");
156 var request = new RemoteRequest(
158 "open-ils.auth.session.retrieve",
161 this.userObject = request.getResultObject();
163 if(org) this.orgUnit = org;
164 else this.orgUnit = findOrgUnit(this.userObject.home_ou());
166 if(!paramObj.__depth && this.orgUnit)
167 globalSelectedDepth = findOrgDepth(this.orgUnit.ou_type());
168 if(!paramObj.__location && this.orgUnit)
169 globalPage.updateSelectedLocation(this.orgUnit);
171 globalPage.updateCurrentLocation(this.orgUnit);
179 UserSession.prototype.updatePassword = function(currentPassword, password) {
180 if(!password || !currentPassword) return null;
182 var request = new RemoteRequest(
184 "open-ils.actor.user.password.update",
192 try { resp = request.getResultObject(); }
194 if(instanceOf(E, ex))
202 this.password = password;
203 this.userObject.passwd(password);
211 UserSession.prototype.updateUsername = function(username) {
212 if(!username) return null;
213 var request = new RemoteRequest(
215 "open-ils.actor.user.username.update",
219 var resp = request.getResultObject();
221 this.username = username;
222 this.userObject.usrname(username);
228 UserSession.prototype.updateEmail = function(email) {
229 if(!email) return null;
230 var request = new RemoteRequest(
232 "open-ils.actor.user.email.update",
236 var resp = request.getResultObject();
238 this.userObject.email(email);
245 UserSession.prototype.fleshMe = function(force) {
246 if(this.fleshed && !force) return;
248 var req = new RemoteRequest(
250 "open-ils.actor.user.fleshed.retrieve",
251 this.session_id, this.userObject.id());
254 this.userObject = req.getResultObject();
255 this.username = this.userObject.usrname();