0

The title may be a bit non-descriptive, so let me explain what I want to do.

I have a lot of JavaScript functions that I want to be able to "give access to" based on a user's permissions to do certain things on my system. As an example:

I have a JS function to add an employee:

function doNewEmployee() {
    var x;
    var objTemp=new Object();
    var myElements = new Array ("username","password1", "password2", "emp_type",
 "name", "surname", "personal_contact", "work_contact", "location_dd",
 "home_address", "access_level", "id_num");
    for (x=0;x<myElements.length;x++){
        objTemp[''+myElements[x]+'']=document.getElementById(myElements[x]).value;
    }
    asyncProxy.addNewEmployee(objTemp);
}

This sends all the parameters through AJAX to my PHP

Now, what I want to be able to do is check whether the user has permissions to execute this function and only include it in the final JS file sent client-side if this is true.

Is this possible? I was thinking about just creating a load of if statements, echoing out the javascript functions in the index.php file, but this would entail having all my JS inline, which isn't what I want.

I guess at the end of the day I want a dynamically built JS file?

Any help would be greatly appreciated!

4
  • I guess at the end of the day I want a dynamically built JS file — Looks like you do! Commented May 11, 2013 at 20:57
  • you could also just dynamically echo in is includes based on user login. Commented May 11, 2013 at 21:12
  • 1
    If the problem is that you don't know how to generate a js file dynamically, take a look here: How can I generate Dynamic Javascript?. Commented May 11, 2013 at 21:16
  • Please, also note that you should not rely on "not including the functions" for security. It is very easy to bypass this security meassur, so server-side checks must be in place as well. Commented May 12, 2013 at 12:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.