How we can use USER ROLE in jQuery?

问题内容:

I am using the functions below for showing project info in popup which is working fine.The 2nd function is used to delete only the Document files for that particular project.I want to disable this function for Users with role “Manager”.

function openShowProjectModal(id) {
    $.post("/get-project-info", {ID: id}, function (res) {
        var project = JSON.parse(res);
        $("#job-name-show").text(project.JobName);
        $("#job-number-show").text(project.JobNumber);
        $("#location-show").text(project.Location);

        var files = "";
        for (var i = 0; i < project.Docs.length; i++) {
            var filename =  project.Docs[i].Document;
            var id = project.Docs[i].ID;


            // I want to disable the Delete function or to hide this Trash icon.

            files += i+1 + ". " +('<a href="/lib/document/'+filename +'">'+filename+'</a>' ) +"   " + '<a href="#" onclick=deleteDoc('+ id +')><i  class="fa fa-trash-o"    aria-hidden="true"></i> </a> <br/>';

            $("#project-files").html(files);

        }

    });
}


function deleteDoc(id) {
    if(confirm("Are you sure to delete?")){
        $.post("/remove-doc",
        {
            ID :id
        }, function (res) {
            if (res == "removed") {
                window.location.href = '/projects';

            } else {
                console.log("not removed");
            }
        }); 
    }
}

问题评论:

3  
You must use backend, for user access restriction
3  
you will need to check the role at the server side due to security issues. After the role check the server should respond to the client which will decide to display the remove button or not
    
you mean I should create Delete option in PHP instead of jquery?
– pankaj
1 hour ago

答案:

答案1:

I do not know where do you store roles. Maybe something like this will work:

function deleteDoc(id) {
if(user.role === "Manager") return;
  if(confirm("Are you sure to delete?")){
  $.post("/remove-doc",
   {
     ID :id
    }, function (res) {
     if (res == "removed") {
      window.location.href = '/projects';
     } else {
        console.log("not removed");
     }
 }); 
}

答案评论:

    
Here is code to find my user role .$userInfo[‘Role’]…but how it will come to know user role is manager here ..if (user.role === “Manager”) return;
– pankaj
1 hour ago
    
$userInfo[‘Role’] -> is php ? A raw solution is to instantiate a global user var in your root window via php (between <script></script> tag) and retrieve with javascript

原文地址:

https://stackoverflow.com/questions/47755160/how-we-can-use-user-role-in-jquery

添加评论