@@ -10,15 +10,19 @@ id: casbin
1010
1111Casbin middleware for Fiber.
1212
13- ** Note: Requires Go 1.18 and above**
13+ ** Compatible with Fiber v3.**
14+
15+ ## Go version support
16+
17+ We only support the latest two versions of Go. Visit [ https://go.dev/doc/devel/release ] ( https://go.dev/doc/devel/release ) for more information.
1418
1519## Install
16- ```
17- go get -u github.com/gofiber/fiber/v2
18- go get -u github.com/gofiber/contrib/casbin
20+ ``` sh
21+ go get -u github.com/gofiber/fiber/v3
22+ go get -u github.com/gofiber/contrib/v3/ casbin
1923```
2024choose an adapter from [ here] ( https://casbin.org/docs/adapters )
21- ```
25+ ``` sh
2226go get -u github.com/casbin/xorm-adapter
2327```
2428
@@ -29,27 +33,27 @@ casbin.New(config ...casbin.Config) *casbin.Middleware
2933
3034## Config
3135
32- | Property | Type | Description | Default |
33- | :--------------| :--------------------------| :-----------------------------------------| :-------------------------------------------------------------------- |
34- | ModelFilePath | ` string ` | Model file path | ` "./model.conf" ` |
36+ | Property | Type | Description | Default |
37+ | :--------------| :--------------------------| :-----------------------------------------| :--------------------------------------------------------------|
38+ | ModelFilePath | ` string ` | Model file path | ` "./model.conf" ` |
3539| PolicyAdapter | ` persist.Adapter ` | Database adapter for policies | ` ./policy.csv ` |
3640| Enforcer | ` *casbin.Enforcer ` | Custom casbin enforcer | ` Middleware generated enforcer using ModelFilePath & PolicyAdapter ` |
37- | Lookup | ` func(* fiber.Ctx) string ` | Look up for current subject | ` "" ` |
38- | Unauthorized | ` func(* fiber.Ctx) error ` | Response body for unauthorized responses | ` Unauthorized ` |
39- | Forbidden | ` func(* fiber.Ctx) error ` | Response body for forbidden responses | ` Forbidden ` |
41+ | Lookup | ` func(fiber.Ctx) string ` | Look up for current subject | ` "" ` |
42+ | Unauthorized | ` func(fiber.Ctx) error ` | Response body for unauthorized responses | ` Unauthorized ` |
43+ | Forbidden | ` func(fiber.Ctx) error ` | Response body for forbidden responses | ` Forbidden ` |
4044
4145### Examples
4246- [ Gorm Adapter] ( https://github.com/svcg/-fiber_casbin_demo )
43- - [ File Adapter] ( https://github.com/gofiber/contrib/casbin/ tree/master/example )
47+ - [ File Adapter] ( https://github.com/gofiber/contrib/tree/master/v3/casbin /example )
4448
4549## CustomPermission
4650
4751``` go
4852package main
4953
5054import (
51- " github.com/gofiber/fiber/v2 "
52- " github.com/gofiber/contrib/casbin"
55+ " github.com/gofiber/fiber/v3 "
56+ " github.com/gofiber/contrib/v3/ casbin"
5357 _ " github.com/go-sql-driver/mysql"
5458 " github.com/casbin/xorm-adapter/v2"
5559)
@@ -60,21 +64,21 @@ func main() {
6064 authz := casbin.New (casbin.Config {
6165 ModelFilePath: " path/to/rbac_model.conf" ,
6266 PolicyAdapter: xormadapter.NewAdapter (" mysql" , " root:@tcp(127.0.0.1:3306)/" ),
63- Lookup: func (c * fiber.Ctx ) string {
67+ Lookup: func (c fiber.Ctx ) string {
6468 // fetch authenticated user subject
6569 },
6670 })
6771
6872 app.Post (" /blog" ,
6973 authz.RequiresPermissions ([]string {" blog:create" }, casbin.WithValidationRule (casbin.MatchAllRule )),
70- func (c * fiber.Ctx ) error {
74+ func (c fiber.Ctx ) error {
7175 // your handler
7276 },
7377 )
7478
7579 app.Delete (" /blog/:id" ,
7680 authz.RequiresPermissions ([]string {" blog:create" , " blog:delete" }, casbin.WithValidationRule (casbin.AtLeastOneRule )),
77- func (c * fiber.Ctx ) error {
81+ func (c fiber.Ctx ) error {
7882 // your handler
7983 },
8084 )
@@ -89,8 +93,8 @@ func main() {
8993package main
9094
9195import (
92- " github.com/gofiber/fiber/v2 "
93- " github.com/gofiber/contrib/casbin"
96+ " github.com/gofiber/fiber/v3 "
97+ " github.com/gofiber/contrib/v3/ casbin"
9498 _ " github.com/go-sql-driver/mysql"
9599 " github.com/casbin/xorm-adapter/v2"
96100)
@@ -101,15 +105,15 @@ func main() {
101105 authz := casbin.New (casbin.Config {
102106 ModelFilePath: " path/to/rbac_model.conf" ,
103107 PolicyAdapter: xormadapter.NewAdapter (" mysql" , " root:@tcp(127.0.0.1:3306)/" ),
104- Lookup: func (c * fiber.Ctx ) string {
108+ Lookup: func (c fiber.Ctx ) string {
105109 // fetch authenticated user subject
106110 },
107111 })
108112
109113 // check permission with Method and Path
110114 app.Post (" /blog" ,
111115 authz.RoutePermission (),
112- func (c * fiber.Ctx ) error {
116+ func (c fiber.Ctx ) error {
113117 // your handler
114118 },
115119 )
@@ -124,8 +128,8 @@ func main() {
124128package main
125129
126130import (
127- " github.com/gofiber/fiber/v2 "
128- " github.com/gofiber/contrib/casbin"
131+ " github.com/gofiber/fiber/v3 "
132+ " github.com/gofiber/contrib/v3/ casbin"
129133 _ " github.com/go-sql-driver/mysql"
130134 " github.com/casbin/xorm-adapter/v2"
131135)
@@ -136,14 +140,14 @@ func main() {
136140 authz := casbin.New (casbin.Config {
137141 ModelFilePath: " path/to/rbac_model.conf" ,
138142 PolicyAdapter: xormadapter.NewAdapter (" mysql" , " root:@tcp(127.0.0.1:3306)/" ),
139- Lookup: func (c * fiber.Ctx ) string {
143+ Lookup: func (c fiber.Ctx ) string {
140144 // fetch authenticated user subject
141145 },
142146 })
143147
144148 app.Put (" /blog/:id" ,
145149 authz.RequiresRoles ([]string {" admin" }),
146- func (c * fiber.Ctx ) error {
150+ func (c fiber.Ctx ) error {
147151 // your handler
148152 },
149153 )
0 commit comments