casbin / casbin.js

An authorization library that supports access control models like ACL, RBAC, ABAC in Frontend Javascript
https://casbin.org/docs/en/frontend
Apache License 2.0
160 stars 37 forks source link

Error in casbin.js advanced usage. #273

Closed cwgitadmin closed 1 year ago

cwgitadmin commented 1 year ago

Screenshot from 2023-06-06 08-30-29

casbin-bot commented 1 year ago

@nodece @Zxilly @Shivansh-yadav13

cwgitadmin commented 1 year ago

API Code

exports.getUserPermissions = async function (req, res) {
  try {
    const subject = req.query.subject;
    var e = await enforcer();
    var permissions = await casbin.casbinJsGetPermissionForUser(e, subject);
    res.send({data:permissions});
  } catch (error) {
    console.log("getPermissionsByRoleId:", error);
    res.status(400).json(error.message);
  }
};
cwgitadmin commented 1 year ago

Frontend Code

 const authorizer = new Authorizer(
    "auto", // mode
    {
      endpoint: `http://localhost:3001/roles/user-permissions`,
      requestHeaders: {
        method: ["GET"],
      }
    }
  );

  // Set your visitor.
  // Casbin.js will automatically sync the permission with your backend Casbin service.
  authorizer.setUser("74");

  // Evaluate the permission
  const result = await authorizer.can("read", "/dashboard/*");
  console.log("result", result);
cwgitadmin commented 1 year ago

Please help me to fix this error.

hsluoyz commented 1 year ago

@cwgitadmin hi, why close?

cwgitadmin commented 1 year ago

I got solution for above mentioned issue

vasanth123 commented 12 months ago

I got solution for above mentioned issue

pls provide the solution here

vasanth123 commented 12 months ago

I am facing same issue

Dias1c commented 7 months ago

Why closed? @vasanth123 did you fix this issue? Please share your solution here :)

Dias1c commented 5 months ago

I have solved this problem. The problem was in order of request.

When you calling method can of authorizer class, it alwas enforces your request in own order:

enforcer.enforce(this.user, domain, object, action)

And if you have own request order, authorizer.can will throws error.

So I created own library that solves this problem, you can write your request in order which you want. https://github.com/Dias1c/casbin-js/tree/main

hsluoyz commented 5 months ago

@Dias1c can you make PR to merge this to this repo?