commit 0bd1d46d168a7c3764e3ec9c31cc1922564b62fc
parent f65dcd524c025e2fb7a0b7c3f408f19bd125422c
Author: Felicitus <felicitus@felicitus.org>
Date: Thu, 16 Feb 2012 07:16:59 +0100
Refactored login process to use the SessionManager
Diffstat:
4 files changed, 193 insertions(+), 125 deletions(-)
diff --git a/src/frontend/js/Components/Auth/LoginDialog.js b/src/frontend/js/Components/Auth/LoginDialog.js
@@ -0,0 +1,82 @@
+/**
+ * Defines the login dialog
+ */
+Ext.define('PartKeepr.LoginDialog', {
+ extend: 'Ext.Window',
+ /* Various style settings */
+ title: i18n("PartKeepr: Login"),
+
+ width: 400,
+ height: 125,
+
+ modal: true,
+ resizable: false,
+
+ layout: 'anchor',
+
+ bodyStyle: 'padding: 5px;',
+
+ /**
+ * Initializes the login dialog component
+ */
+ initComponent: function () {
+
+ this.loginField = Ext.ComponentMgr.create({
+ xtype: 'textfield',
+ value: "",
+ fieldLabel: i18n("Username"),
+ anchor: '100%'
+ });
+
+ this.passwordField = Ext.ComponentMgr.create({
+ xtype: 'textfield',
+ inputType: "password",
+ value: "",
+ fieldLabel: i18n("Password"),
+ anchor: '100%'
+ });
+
+ Ext.apply(this, {
+ // Login when "enter" was hit
+ keys: [{
+ key: Ext.EventObject.ENTER,
+ handler: this.login,
+ scope: this
+ }],
+ items: [
+ this.loginField,
+ this.passwordField
+ ],
+ dockedItems: [{
+ xtype: 'toolbar',
+ enableOverflow: true,
+ dock: 'bottom',
+ ui: 'footer',
+ pack: 'start',
+ defaults: {minWidth: 100},
+ items: [
+ {
+ text: i18n("Connect"),
+ icon: 'resources/silkicons/connect.png',
+ handler: Ext.bind(this.login, this)
+ },{
+ text: i18n("Close"),
+ handler: Ext.bind(this.close, this),
+ icon: 'resources/silkicons/cancel.png'
+ }]
+ }]
+ });
+
+ this.callParent(arguments);
+
+ // Focus the login field on show
+ this.on("show", function () { this.loginField.focus(); }, this);
+ },
+ /**
+ * Fires the "login" event
+ */
+ login: function () {
+ this.fireEvent("login", this.loginField.getValue(), this.passwordField.getValue());
+ }
+
+});+
\ No newline at end of file
diff --git a/src/frontend/js/Components/Session/SessionManager.js b/src/frontend/js/Components/Session/SessionManager.js
@@ -0,0 +1,87 @@
+/**
+ * Represents a session against the PartKeepr Server.
+ */
+Ext.define("PartKeepr.Session", {
+ extend: 'Ext.util.Observable',
+
+ /**
+ * Holds the current session ID, or null if no session is active
+ *
+ * @var string
+ */
+ session: null,
+
+ /**
+ * Holds an instance of the login dialog, or "null" if no login dialog is active.
+ *
+ * @var object
+ */
+ loginDialog: null,
+
+
+ /**
+ * Constructs a new SessionManager.
+ *
+ * @param config Optional: Specifies a configuration object
+ */
+ constructor: function(config){
+ this.addEvents({
+ "login": true
+ });
+
+ this.callParent(arguments);
+ },
+ /**
+ * Creates and shows the login dialog, as well as setting up any event handlers.
+ */
+ login: function (username, password) {
+ this.loginDialog = Ext.create("PartKeepr.LoginDialog");
+
+ if (username && password) {
+ this.onLogin(username, password);
+ } else {
+ this.loginDialog.on("login", this.onLogin, this);
+ this.loginDialog.show();
+ }
+ },
+ /**
+ * Removes the current session.
+ */
+ logout: function () {
+ this.session = null;
+ },
+ /**
+ * Callback from the login dialog when the "login" button was clicked.
+ *
+ * @param username The username, as entered in the login dialog
+ * @param password The password, as entered
+ */
+ onLogin: function (username, password) {
+ var k = new PartKeepr.ServiceCall("Auth", "login");
+ k.setParameter("username", username);
+ k.setParameter("password", md5(password));
+
+ k.enableAnonymous();
+ k.setHandler(Ext.bind(this.onAfterLogin, this));
+ k.doCall();
+ },
+ /**
+ * Callback when the service call is complete.
+ *
+ * @param response The session ID
+ */
+ onAfterLogin: function (response) {
+ this.session = response.sessionid;
+ this.loginDialog.destroy();
+
+ this.fireEvent("login");
+ },
+ /**
+ * Returns the current session
+ *
+ * @returns the session, or null if no session is available
+ */
+ getSession: function () {
+ return this.session;
+ }
+});+
\ No newline at end of file
diff --git a/src/frontend/js/Dialogs/Auth/LoginDialog.js b/src/frontend/js/Dialogs/Auth/LoginDialog.js
@@ -1,115 +0,0 @@
-Ext.define('PartKeepr.LoginDialog', {
- extend: 'Ext.Window',
- title: i18n("PartKeepr: Login"),
- loginField: null,
-
- width: 400,
- height: 125,
-
- modal: true,
- resizable: false,
-
- layout: 'anchor',
-
- initComponent: function () {
-
- this.loginField = Ext.ComponentMgr.create({
- xtype: 'textfield',
- value: "",
- fieldLabel: i18n("Username"),
- anchor: '100%'
- });
-
- this.passwordField = Ext.ComponentMgr.create({
- xtype: 'textfield',
- inputType: "password",
- value: "",
- fieldLabel: i18n("Password"),
- anchor: '100%'
- });
-
- Ext.apply(this, {
-
- keys: [{
- key: Ext.EventObject.ENTER,
- handler: this.login,
- scope: this
- }],
- bodyStyle: 'padding: 5px;',
- items: [
- this.loginField,
- this.passwordField
- ],
- dockedItems: [{
- xtype: 'toolbar',
- enableOverflow: true,
- dock: 'bottom',
- ui: 'footer',
- pack: 'start',
- defaults: {minWidth: 100},
- items: [
- {
- text: i18n("Connect"),
- icon: 'resources/silkicons/connect.png',
- handler: Ext.bind(this.login, this)
- },{
- text: i18n("Close"),
- handler: Ext.bind(this.close, this),
- icon: 'resources/silkicons/cancel.png'
- }]
- }]
- });
-
- this.callParent(arguments);
-
- this.on("show", function () { this.loginField.focus(); }, this);
-
- if (PartKeepr.autoLoginUsername) {
- this.loginField.setValue(PartKeepr.autoLoginUsername);
- this.passwordField.setValue(PartKeepr.autoLoginPassword);
- this.login();
-
- PartKeepr.autoLoginUsername = null;
- }
- },
- login: function () {
- var call = new PartKeepr.ServiceCall("Auth", "login");
-
- call.setParameter("username", this.loginField.getValue());
- call.setParameter("password", md5(this.passwordField.getValue()));
- call.setLoadMessage(i18n("Logging in..."));
- call.enableAnonymous();
- call.setHandler(Ext.bind(this.onLogin, this));
- call.doCall();
- },
- /**
- * Callback after the login call was completed.
- *
- * @param obj The response object from the server
- */
- onLogin: function (obj) {
- // Set session + username
- PartKeepr.getApplication().setSession(obj.sessionid);
- PartKeepr.getApplication().setUsername(obj.username);
-
- // @todo Disable the "edit users" menu somehow else
- if (!obj.admin) {
- Ext.getCmp("edit-users").hide();
- } else {
- Ext.getCmp("edit-users").show();
- }
-
- // Set the admin flag
- PartKeepr.getApplication().setAdmin(obj.admin);
-
- // Call the "login" method, which initializes the system with the user
- PartKeepr.getApplication().login();
-
- // Write out a logging message
- PartKeepr.log(i18n("Logged in as")+" "+obj.username);
-
- // Close the window
- this.close();
-
- }
-});-
\ No newline at end of file
diff --git a/src/frontend/js/PartKeepr.js b/src/frontend/js/PartKeepr.js
@@ -21,13 +21,18 @@ Ext.application({
PartKeepr.setAvailableImageFormats(window.availableImageFormats);
// If auto login is wanted (for e.g. demo systems), put it in here
+
+
+ this.sessionManager = new PartKeepr.Session();
+ this.sessionManager.on("login", this.onLogin, this);
+
if (window.autoLoginUsername) {
- PartKeepr.setAutoLogin(window.autoLoginUsername,window.autoLoginPassword);
+ this.sessionManager.login(window.autoLoginUsername, window.autoLoginPassword);
+ } else {
+ this.sessionManager.login();
}
- new PartKeepr.LoginDialog().show();
-
Ext.fly(document.body).on('contextmenu', this.onContextMenu, this);
},
onContextMenu: function (e, target) {
@@ -37,7 +42,7 @@ Ext.application({
* Handles the login function. Initializes the part manager window,
* enables the menu bar and creates the stores+loads them.
*/
- login: function () {
+ onLogin: function () {
this.createGlobalStores();
this.reloadStores();
@@ -58,6 +63,8 @@ Ext.application({
this.displayTipWindowTask = new Ext.util.DelayedTask(this.displayTipOfTheDayWindow, this);
this.displayTipWindowTask.delay(100);
+ this.setSession(this.getSessionManager().getSession());
+
},
/**
* Displays the tip of the day window.
@@ -109,6 +116,14 @@ Ext.application({
alert(i18n("The following cronjobs aren't running:")+"\n\n"+data.data.inactiveCronjobs.join("\n"));
}
},
+ /**
+ * Returns the session manager
+ *
+ * @returns SessionManager
+ */
+ getSessionManager: function () {
+ return this.sessionManager;
+ },
/*
* Checks for unacknowledged system notices
*
@@ -137,7 +152,7 @@ Ext.application({
logout: function () {
this.menuBar.disable();
this.centerPanel.removeAll(true);
- this.setSession(null);
+ this.getSessionManager.logout();
},
createGlobalStores: function () {
this.footprintStore = Ext.create("Ext.data.Store",
@@ -317,7 +332,7 @@ Ext.application({
*
*/
reloadStores: function () {
- if (this.getSession()) {
+ if (this.getSessionManager().getSession()) {
this.footprintStore.load();
this.manufacturerStore.load();
this.distributorStore.load();
@@ -419,11 +434,9 @@ Ext.application({
return this.statusBar;
},
getSession: function () {
- return this.session;
+ return this.getSessionManager().getSession();
},
setSession: function (session) {
- this.session = session;
-
if (session) {
this.getStatusbar().getConnectionButton().setConnected();
} else {