I have a php-based authorization setup that loads authorization level into a JS variable, and I'd like to disable onclick events based on the authlevel.
I've written/borrowed the following partial that works fine for executing arbitrary functions with their parameters (from other excellent stackoverflow posts). I've tested it with functions passing parameters and it works great.
function checkauth(authlevel, reqauthlevel, callback, message) {
// Handle custom message or lack thereof
message = (typeof message === "undefined") ? "You do not have authorization for this action" : message;
if (authlevel >= reqauthlevel) {
callback()
}
else {
alert(message)
}
}
function partial(func /*, 0..n args */) {
var args = Array.prototype.slice.call(arguments, 1);
return function() {
var allArguments = args.concat(Array.prototype.slice.call(arguments));
return func.apply(this, allArguments);
};
}
When I call it to handle a button event, however, I get the auth message on page load using the code below. Is there a better way to handle events to selectively ignore them? I know I can use php to just not create the buttons, or js to delete them or produce them selectively, but I'd like to have them there as a demo for UI experience without the action results. TIA - c
$(function(){
$('#spdownbutton').click(checkauth(authlevel, 2, function(){
var selectedchannelname=document.getElementById("selectchannel").value
$.ajax({
url: "/wsgiupdatecontrol",
type: "post",
datatype:"json",
data
{'action':'spchange','subaction':'incdown','database':controldatabase,'channelname':selectedchannelname},
success: function(response){
setTimeout(UpdateChannelData,updatetimeout)
}
});
}, stdmessage));
}