]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/util/UserSession.js
free Cookie library. persists cookies.
[Evergreen.git] / Open-ILS / src / javascript / util / UserSession.js
1 var globalUserSession;
2
3
4 function UserSession() { 
5
6         if(globalUserSession != null) {
7                 return globalUserSession;
8         }
9         this.connected          = false;
10         this.verifySession();
11         this.exp_days           = null;
12         globalUserSession = this; 
13 }
14
15 UserSession.prototype.destroy = function() {
16         debug("Removing user session");
17         this.connected          = false;
18         this.session_id = null;
19         this.username           = null;
20         this.orgUnit            = null;
21         this.cookie.remove();
22 }
23
24 UserSession.prototype.verifySession = function() {
25
26         this.cookie = new cookieObject("ses", 1, "/opac/", "ils_ses", "ils_uname");
27
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);
33                 this.cookie.write();
34                 return true;
35         }
36
37         this.session_id = this.cookie.fields[0];
38         this.username   = this.cookie.fields[1];
39
40         if( this.session_id ) {
41                 debug("Found user session " + this.session_id);
42                 debug("Found user uname " + this.username);
43         }
44
45         if( this.username && this.session_id ) { 
46                 /* we're in the middle of an active session */
47                 this.connected = true;
48
49         } else {
50
51                 if(this.session_id) {
52
53                         debug("Retrieving user information\n");
54
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 );
58
59                         request.send(true);
60                         var user = request.getResultObject();
61
62                         if(user && user[0]) {
63
64                                 debug("Received user object " + js2JSON(user) + "\n");
65                                 this.username = user.usrname();
66
67                         } else {
68                                 this.destroy();
69                                 return;
70                         }
71
72                         if(this.username) {
73
74                                 this.connected = true;
75                                 this.cookie.put("ils_ses", this.session_id);
76                                 this.cookie.put("ils_uname", this.username);
77                                 this.cookie.write();
78
79                         } else {
80
81                                 this.cookie.remove();
82                                 this.session_id = null;
83                                 this.username = null;
84                                 this.connected = false;
85
86                         }
87                 }
88         }
89 }
90
91
92 UserSession.instance = function() {
93         return new UserSession();
94 }
95
96 /* XXX needs to be a callback */
97 function timed_out() {
98                 alert('User Session Timed Out.  \nRedirecting to start page'); 
99                 location.href='/'; 
100 }
101
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;
108 }
109
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 );
115 }
116
117 function destroySessionTimer() {
118         var obj = globalUserSession;
119         if(obj.timeout_id != null) { clearTimeout( obj.timeout_id ); }
120 }
121
122 UserSession.prototype.setSessionId = function( id ) {
123         debug("User session id " + id );
124         this.session_id = id;
125 }
126
127 UserSession.prototype.getSessionId = function() {
128         return this.session_id;
129 }
130
131 UserSession.prototype.login = function( username, password ) {
132
133         if(!username || !password) { return false; }
134         this.username = username;
135
136         var init_request = new RemoteRequest( 'open-ils.auth',
137                       'open-ils.auth.authenticate.init', username );
138
139         init_request.send(true);
140         var seed = init_request.getResultObject();
141
142         if( ! seed || seed == '0') {
143                 /* XXX should be an exception */
144                 alert( "Error Communicating with Authentication Server" );
145                 return null;
146         }
147
148         var auth_request = new RemoteRequest( 'open-ils.auth',
149                         'open-ils.auth.authenticate.complete', username, 
150                         hex_md5(seed + hex_md5(password)));
151
152         auth_request.send(true);
153         var auth_result = auth_request.getResultObject();
154
155         if(auth_result == '0') { return false; }
156
157         this.setSessionId(auth_result);
158
159         this.connected = true;
160
161         this.verifySession();
162
163         return true;
164 }
165
166
167
168 /* grab this users org unit */
169 UserSession.prototype.grabOrgUnit = function() {
170         var session = this.getSessionId();
171         if(!session) {
172                 throw new EXLogic(
173                         "No session ID for user in grabOrgUnit()");
174         }
175
176         debug("Retrieving this users object");
177
178         var request = new RemoteRequest(
179                         "open-ils.auth",
180                         "open-ils.auth.session.retrieve",
181                         this.session_id);
182         request.send(true);
183         this.userObject = request.getResultObject();
184         
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);
190
191         return;
192
193 }
194
195
196
197