user and auth session management class
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Sun, 13 Apr 2008 15:51:04 +0000 (15:51 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Sun, 13 Apr 2008 15:51:04 +0000 (15:51 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@9325 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/web/js/dojo/openils/User.js [new file with mode: 0644]

diff --git a/Open-ILS/web/js/dojo/openils/User.js b/Open-ILS/web/js/dojo/openils/User.js
new file mode 100644 (file)
index 0000000..c547abc
--- /dev/null
@@ -0,0 +1,88 @@
+/* ---------------------------------------------------------------------------
+ * Copyright (C) 2008  Georgia Public Library Service
+ * Bill Erickson <erickson@esilibrary.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * ---------------------------------------------------------------------------
+ */
+
+if(!dojo._hasResource["openils.User"]) {
+
+    dojo._hasResource["openils.User"] = true;
+    dojo.provide("openils.User");
+    dojo.require('openils.Event');
+
+    dojo.declare('openils.User', null, {});
+
+    openils.User.user = null;
+    openils.User.authtoken = null;
+    openils.User.authtime = null;
+
+    var ses = new OpenSRF.ClientSession('open-ils.auth');
+
+    openils.User.getBySession = function(onComplete) {
+        var req = ses.request('open-ils.auth.session.retrieve', openils.User.authtoken);
+        req.oncomplete = function(r) {
+            var user = r.recv().content();
+            openils.User.user = user;
+            if(onComplete)
+                onComplete(user);
+        }
+        req.send();
+    }
+
+    openils.User.getById = function(id, onComplete) {
+        var ases = new OpenSRF.ClientSession('open-ils.actor');
+        var req = ases.request('open-ils.actor.user.retrieve', openils.User.authtoken, id);
+        if(onComplete) {
+            req.oncomplete = function(r) {
+                var user = r.recv().content();
+                onComplete(user);
+            }
+            req.send();
+        } else {
+            req.timeout = 10;
+            req.send();
+            return req.recv().content();
+        }
+    }
+
+
+    /**
+     * Logs in, sets the authtoken/authtime vars, and fetches the logged in user
+     */
+    openils.User.login = function(args, onComplete) {
+        var initReq = ses.request('open-ils.auth.authenticate.init', args.username);
+
+        initReq.oncomplete = function(r) {
+            var seed = r.recv().content(); 
+            alert(seed);
+            var loginInfo = {
+                password : hex_md5(seed + hex_md5(args.passwd)), 
+                type : args.type || 'opac',
+                org : args.location,
+            };
+
+            var authReq = ses.request('open-ils.auth.authenticate.complete', loginInfo);
+            authReq.oncomplete = function(rr) {
+                var data = rr.recv().content();
+                openils.User.authtoken = data.payload.authtoken;
+                openils.User.authtime = data.payload.authtime;
+                openils.User.getBySession(onComplete);
+            }
+            authReq.send();
+        }
+
+        initReq.send();
+    }
+}
+
+