4 function UserSession() {
6 if(globalUserSession != null) {
7 return globalUserSession;
9 this.connected = false;
12 globalUserSession = this;
15 UserSession.prototype.destroy = function() {
16 debug("Removing user session");
17 this.connected = false;
18 this.session_id = null;
24 UserSession.prototype.verifySession = function() {
26 this.cookie = new cookieObject("ses", 1, "/opac/", "ils_ses", "ils_uname");
28 /* if we're already connected */
29 if(this.username && this.session_id) {
30 this.connected = true;
31 this.cookie.put("ils_ses", this.session_id);
32 this.cookie.put("ils_uname", this.username);
37 this.session_id = this.cookie.fields[0];
38 this.username = this.cookie.fields[1];
40 if( this.session_id ) {
41 debug("Found user session " + this.session_id);
42 debug("Found user uname " + this.username);
45 if( this.username && this.session_id ) {
46 /* we're in the middle of an active session */
47 this.connected = true;
53 debug("Retrieving user information\n");
55 /* user is returning to the page with a session key */
56 var request = new RemoteRequest("open-ils.auth",
57 "open-ils.auth.session.retrieve", this.session_id );
60 var user = request.getResultObject();
64 debug("Received user object " + js2JSON(user) + "\n");
65 this.username = user.usrname();
74 this.connected = true;
75 this.cookie.put("ils_ses", this.session_id);
76 this.cookie.put("ils_uname", this.username);
82 this.session_id = null;
84 this.connected = false;
92 UserSession.instance = function() {
93 return new UserSession();
96 /* XXX needs to be a callback */
97 function timed_out() {
98 alert('User Session Timed Out. \nRedirecting to start page');
102 /** Initialize a user session timeout. */
103 function startSessionTimer( timeout_ms ) {
104 var obj = globalUserSession;
105 obj.timeout_ms = timeout_ms;
106 obj.timeout_id = setTimeout( "timed_out()", timeout_ms );
107 window.onmousemove = resetSessionTimer;
110 /** Reset the user session timeout. Useful if the user is active */
111 function resetSessionTimer() {
112 var obj = globalUserSession;
113 if(obj.timeout_id != null) { clearTimeout( obj.timeout_id ); }
114 obj.timeout_id = setTimeout( "timed_out()", obj.timeout_ms );
117 function destroySessionTimer() {
118 var obj = globalUserSession;
119 if(obj.timeout_id != null) { clearTimeout( obj.timeout_id ); }
122 UserSession.prototype.setSessionId = function( id ) {
123 debug("User session id " + id );
124 this.session_id = id;
127 UserSession.prototype.getSessionId = function() {
128 return this.session_id;
131 UserSession.prototype.login = function( username, password ) {
133 if(!username || !password) { return false; }
134 this.username = username;
136 var init_request = new RemoteRequest( 'open-ils.auth',
137 'open-ils.auth.authenticate.init', username );
139 init_request.send(true);
140 var seed = init_request.getResultObject();
142 if( ! seed || seed == '0') {
143 /* XXX should be an exception */
144 alert( "Error Communicating with Authentication Server" );
148 var auth_request = new RemoteRequest( 'open-ils.auth',
149 'open-ils.auth.authenticate.complete', username,
150 hex_md5(seed + hex_md5(password)));
152 auth_request.send(true);
153 var auth_result = auth_request.getResultObject();
155 if(auth_result == '0') { return false; }
157 this.setSessionId(auth_result);
159 this.connected = true;
161 this.verifySession();
168 /* grab this users org unit */
169 UserSession.prototype.grabOrgUnit = function() {
170 var session = this.getSessionId();
173 "No session ID for user in grabOrgUnit()");
176 debug("Retrieving this users object");
178 var request = new RemoteRequest(
180 "open-ils.auth.session.retrieve",
183 this.userObject = request.getResultObject();
185 this.orgUnit = findOrgUnit(this.userObject.home_ou());
186 globalSearchDepth = findOrgDepth(this.orgUnit.ou_type());
187 globalSelectedDepth = findOrgDepth(this.orgUnit.ou_type());
188 globalPage.updateSelectedLocation(this.orgUnit);
189 globalPage.updateCurrentLocation(this.orgUnit);