]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/util/UserSession.js
removing old opac images and css
[Evergreen.git] / Open-ILS / src / javascript / util / UserSession.js
1 var globalUserSession;
2
3 /* cookie fields */
4 UserSession.SES = 0;
5
6
7 function UserSession() { 
8         this.cookie = new cookieObject("ses", 1, "/opac/", "ils_ses");
9         this.connected          = false;
10         globalUserSession = this; 
11         this.fleshed            = false;
12 }
13
14 UserSession.prototype.destroy = function() {
15         debug("Removing user session");
16         this.connected          = false;
17         this.session_id = null;
18         this.username           = null;
19         this.orgUnit            = null;
20         this.cookie.remove();
21 }
22
23 UserSession.prototype.persist = function() {
24
25         this.cookie = new cookieObject("ses", 1, "/opac/", "ils_ses");
26
27         if(!this.session_id) return;
28
29         if( this.session_id )
30                 this.cookie.put("ils_ses", this.session_id);
31
32         debug("Persisting session with session " + 
33                 this.session_id + " and uname " + this.username );
34
35         this.cookie.write();
36         debug("Persisted session " + this.cookie.fields[UserSession.SES]);
37 }
38
39
40
41 UserSession.prototype.verifySession = function(ses) {
42
43         debug("Verifying session...");
44         if(ses)
45                 debug("Session key passed in [" + ses + "], verifying...");
46
47         if(ses != null)
48                 this.session_id = ses;
49         else
50                 this.session_id = this.cookie.fields[UserSession.SES];
51
52         if(this.session_id && this.userObject && this.username && this.connected) {
53                 return true;
54         }
55
56         if(this.session_id) {
57                 debug("Retrieveing user info for session " + this.session_id);
58
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 );
62
63                 debug("1");
64                 request.send(true);
65                 debug("2");
66                 var user = request.getResultObject();
67                 debug("3");
68
69                 if( typeof user == 'object' && user._isfieldmapper) {
70
71                         debug("User retrieved, setting up user info");
72                         this.username = user.usrname();
73                         this.userObject = user;
74                         this.connected = true;
75                         this.persist();
76                         return true;
77
78                 } else {
79                         debug("User session " + this.session_id + " is no longer valid");
80                         this.destroy();
81                         return false;
82                 }
83
84         } else {
85                 debug("No session cookie found");
86                 this.destroy();
87                 return false;
88         }
89 }
90
91
92 UserSession.instance = function() {
93         if( globalUserSession )
94                 return globalUserSession;
95         return new UserSession();
96 }
97
98 UserSession.prototype.setSessionId = function( id ) {
99         debug("User session id " + id );
100         this.session_id = id;
101 }
102
103 UserSession.prototype.getSessionId = function() {
104         return this.session_id;
105 }
106
107 UserSession.prototype.login = function( username, password ) {
108
109         if(!username || !password) { return false; }
110         this.username = username;
111
112         var init_request = new RemoteRequest( 'open-ils.auth',
113                       'open-ils.auth.authenticate.init', username );
114
115         init_request.send(true);
116         var seed = init_request.getResultObject();
117
118         if( ! seed || seed == '0') {
119                 /* XXX should be an exception */
120                 alert( "Error Communicating with Authentication Server" );
121                 return null;
122         }
123
124         var auth_request = new RemoteRequest( 'open-ils.auth',
125                         'open-ils.auth.authenticate.complete', username, 
126                         hex_md5(seed + hex_md5(password)), "opac");
127
128         auth_request.send(true);
129         var auth_result = auth_request.getResultObject();
130
131         if(auth_result == '0') { return false; }
132
133         this.setSessionId(auth_result);
134
135         this.connected = true;
136
137         this.persist();
138
139         return true;
140 }
141
142
143
144 /* grab this users org unit */
145 /* if new_org_id is provided, it is used instead of the home_ou 
146         of the user */
147 UserSession.prototype.grabOrgUnit = function(org) {
148         var session = this.getSessionId();
149         if(!session) {
150                 throw new EXLogic(
151                         "No session ID for user in grabOrgUnit()");
152         }
153
154         debug("Retrieving this users object");
155
156         var request = new RemoteRequest(
157                         "open-ils.auth",
158                         "open-ils.auth.session.retrieve",
159                         this.session_id);
160         request.send(true);
161         this.userObject = request.getResultObject();
162         
163         if(org) this.orgUnit = org;
164         else this.orgUnit = findOrgUnit(this.userObject.home_ou());
165
166         if(!paramObj.__depth && this.orgUnit)
167                 globalSelectedDepth = findOrgDepth(this.orgUnit.ou_type());
168         if(!paramObj.__location && this.orgUnit)
169                 globalPage.updateSelectedLocation(this.orgUnit);
170
171         globalPage.updateCurrentLocation(this.orgUnit);
172
173         return;
174 }
175
176
177
178
179 UserSession.prototype.updatePassword = function(currentPassword, password) {
180         if(!password || !currentPassword) return null;
181
182         var request = new RemoteRequest(
183                 "open-ils.actor",
184                 "open-ils.actor.user.password.update",
185                 this.getSessionId(),
186                 password, 
187                 currentPassword );
188
189         request.send(true);
190         var resp;
191
192         try { resp = request.getResultObject(); }
193         catch(E) { 
194                 if(instanceOf(E, ex))
195                         alert(E.err_msg());
196                 else
197                         alert(E);
198                 return false;
199         }
200
201         if(resp) {
202                 this.password = password;
203                 this.userObject.passwd(password);
204                 return true;
205         }
206
207         return false;
208 }
209
210
211 UserSession.prototype.updateUsername = function(username) {
212         if(!username) return null;
213         var request = new RemoteRequest(
214                 "open-ils.actor",
215                 "open-ils.actor.user.username.update",
216                 this.getSessionId(),
217                 username );
218         request.send(true);
219         var resp = request.getResultObject();
220         if(resp) {
221                 this.username = username;
222                 this.userObject.usrname(username);
223                 return true;
224         }
225         return false;
226 }
227
228 UserSession.prototype.updateEmail = function(email) {
229         if(!email) return null;
230         var request = new RemoteRequest(
231                 "open-ils.actor",
232                 "open-ils.actor.user.email.update",
233                 this.getSessionId(),
234                 email );
235         request.send(true);
236         var resp = request.getResultObject();
237         if(resp) {
238                 this.userObject.email(email);
239                 return true;
240         }
241         return false;
242 }
243
244
245 UserSession.prototype.fleshMe = function(force) {
246         if(this.fleshed && !force) return;
247
248         var req = new RemoteRequest(
249                 "open-ils.actor",
250                 "open-ils.actor.user.fleshed.retrieve",
251                 this.session_id, this.userObject.id());
252
253         req.send(true);
254         this.userObject = req.getResultObject();
255         this.username = this.userObject.usrname();
256         this.fleshed = true;
257 }
258
259
260
261
262