How to reach me 📭
arsmn / fiber-casbin Goto Github PK
View Code? Open in Web Editor NEWCasbin middleware for Fiber
License: MIT License
Casbin middleware for Fiber
License: MIT License
Hello
Take the following code for example
app.Delete("/blog/:id",
authz.RequiresPermissions([]string{"blog:create", "blog:delete"}, fibercasbin.AtLeastOne),
func(c *fiber.Ctx) error {
// your handler
},
)
I may want to set up the permission such that I would love to actually check if the user has the role to delete the blog post with id "123". Is that something you plan to support?
Hey...
func IndexTest(ctx *fiber.Ctx) error {
return ctx.Status(http.StatusOK).JSON("hello world!")
}
func IndexDemo1(ctx *fiber.Ctx) error {
enforcer := config.Enforcer
if ok, _ := enforcer.AddPolicy("admin", "/v1/test", "GET"); !ok {
return ctx.Status(http.StatusOK).JSON("Policy exist")
}
return ctx.Status(http.StatusOK).JSON("add success")
}
func IndexDemo2(ctx *fiber.Ctx) error {
enforcer := config.Enforcer
if ok, _ := enforcer.RemovePolicy("admin", "/v1/test", "GET"); !ok {
return ctx.Status(http.StatusOK).JSON("Policy no exist")
}
return ctx.Status(http.StatusOK).JSON("delete success")
}
And the CasbinMiddleware code like this:
func Casbin() *fibercasbin.CasbinMiddleware {
db := config.DB
adapter, _ := gormadapter.NewAdapterByDBWithCustomTable(db, &models.CasbinRule{})
authz := fibercasbin.New(fibercasbin.Config{
ModelFilePath: "config/rbac_model.conf",
PolicyAdapter: adapter,
Unauthorized: func(c *fiber.Ctx) error {
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{"message": "Unauthorized...", "status_code": 401})
},
Forbidden: func(c *fiber.Ctx) error {
return c.Status(http.StatusNotFound).JSON(fiber.Map{"message": "Forbidden ...", "status_code": 404})
},
Lookup: func(c *fiber.Ctx) string {
// get subject from BasicAuth, JWT, Cookie etc in real world
return "admin"
},
})
//adapter.LoadPolicy() // model Model
return authz
}
test the method Demo1 or Demo2 , the CasbinMiddleware not immediate effect。
v.Get("/test", authz.RoutePermission(), IndexTest)
Can your help?
Howdy there 👋 Friendly heads up that it looks like the function to perform a lookup within the fibercasbin
config was changed to actually use the field Lookup
rather than SubLookupFn
. In case anyone else runs into the same issue, wanted to give the heads up to update that line in the docs. 😄
README.md
SubLookupFn: func(c *fiber.Ctx) string {
// fetch authenticated user subject
}
Fixed reference
Lookup: func(c *fiber.Ctx) string {
// fetch authenticated user subject
}
is there a way to reload permissions?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.