Added new callback to viewmodels, allowing to react to login events

Removed old loginstate.subscribe method and migrated existing code to new mechanism.

This should allow plugins to only request their sensitive data if a user is a) logged in and has b) the necessary rights for a request. The server should still do additional auth checks, but this way no unnecessary request have to be made.
This commit is contained in:
Gina Häußge 2015-04-21 19:38:41 +02:00
parent 3f272b209d
commit 2ce40c4847
5 changed files with 41 additions and 29 deletions

View file

@ -476,6 +476,12 @@ $(function() {
}
});
log.info("... binding done");
_.each(allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onStartupComplete")) {
viewModel.onStartupComplete();
}
});
};
if (!_.has(viewModelMap, "settingsViewModel")) {

View file

@ -323,6 +323,14 @@ $(function() {
self.requestData();
};
self.onUserLoggedIn = function(user) {
self.uploadButton.fileupload("enable");
};
self.onUserLoggedOut = function() {
self.uploadButton.fileupload("disable");
};
self.onStartup = function() {
$(".accordion-toggle[data-target='#files']").click(function() {
var files = $("#files");
@ -347,14 +355,6 @@ $(function() {
//~~ Gcode upload
self.uploadButton = $("#gcode_upload");
self.loginState.subscribe(function(change, data) {
if ("login" == change) {
self.uploadButton.fileupload("enable");
} else {
self.uploadButton.fileupload("disable");
}
});
function gcode_upload_done(e, data) {
var filename = undefined;
var location = undefined;

View file

@ -61,12 +61,10 @@ $(function() {
});
};
self.onDataUpdaterReconnect = function() {
self.requestData();
};
self.onStartup = function() {
self.requestData();
self.onUserLoggedIn = function(user) {
if (user.admin) {
self.requestData();
}
};
}

View file

@ -11,6 +11,8 @@ $(function() {
self.isAdmin = ko.observable(false);
self.isUser = ko.observable(false);
self.allViewModels = undefined;
self.currentUser = ko.observable(undefined);
self.userMenuText = ko.computed(function() {
@ -21,12 +23,6 @@ $(function() {
}
});
self.subscribers = [];
self.subscribe = function(callback) {
if (callback === undefined) return;
self.subscribers.push(callback);
};
self.reloadUser = function() {
if (self.currentUser() == undefined) {
return;
@ -57,7 +53,11 @@ $(function() {
self.currentUser(response);
_.each(self.subscribers, function(callback) { callback("login", response); });
_.each(self.allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onUserLoggedIn")) {
viewModel.onUserLoggedIn(response);
}
});
} else {
self.loggedIn(false);
self.username(undefined);
@ -66,7 +66,11 @@ $(function() {
self.currentUser(undefined);
_.each(self.subscribers, function(callback) { callback("logout", {}); });
_.each(self.allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onUserLoggedOut")) {
viewModel.onUserLoggedOut();
}
});
}
};
@ -116,11 +120,15 @@ $(function() {
}
};
self.onAllBound = function(allViewModels) {
self.allViewModels = allViewModels;
};
self.onDataUpdaterReconnect = function() {
self.requestData();
};
self.onStartup = function() {
self.onStartupComplete = function() {
self.requestData();
};
}

View file

@ -57,12 +57,6 @@ $(function() {
return self.editorPassword() != self.editorRepeatedPassword();
});
self.loginState.subscribe(function(change, data) {
if (change == "login" && data.admin) {
self.requestData();
}
});
self.requestData = function() {
if (!CONFIG_ACCESS_CONTROL) return;
@ -267,6 +261,12 @@ $(function() {
}
}
});
};
self.onUserLoggedIn = function(user) {
if (user.admin) {
self.requestData();
}
}
}