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