php-casbin / laravel-authz

An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.
Apache License 2.0
288 stars 47 forks source link

[Question] Please tell me how to expand the list of functions #54

Closed vvistmn closed 10 months ago

vvistmn commented 1 year ago

HI!

I called the addFunction method of the FunctionMap class in the controller to create the function. I use RequestMiddleware, the FunctionMap::loadFunctionMap() method is called there - which returns a list of commands by default. And there is no my function. Where can I define my function so that it can be seen from the RequestMiddleware? Or is it necessary to override the default classes?

hsluoyz commented 1 year ago

@leeqvip

vvistmn commented 1 year ago

So far, it turned out to be implemented as follows in composer.json add the following line:

"autoload": { "psr-4": { "App\\": "app/" }, "exclude-from-classmap": ["/vendor/casbin/casbin/src/Model/FunctionMap.php"], "files": ["app/Services/Permissions/FunctionMap.php"] },

hsluoyz commented 1 year ago

@vvistmn hi, can you make a PR to fix it?

leeqvip commented 1 year ago

@vvistmn How to add a customized function: https://casbin.org/docs/function#how-to-add-a-customized-function You can use facade static calls in this project:

Enforcer::addFunction("yourfunction", function(...$args){
     // todo ...
});