-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docs from gofiber/contrib@b9547c4
- Loading branch information
1 parent
8b21172
commit 4a8f60e
Showing
115 changed files
with
21,552 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
### Casbin | ||
Casbin middleware for Fiber | ||
|
||
### Install | ||
``` | ||
go get -u github.com/gofiber/fiber/v2 | ||
go get -u github.com/gofiber/contrib/casbin | ||
``` | ||
choose an adapter from [here](https://casbin.org/docs/en/adapters) | ||
``` | ||
go get -u github.com/casbin/xorm-adapter | ||
``` | ||
|
||
### Signature | ||
```go | ||
casbin.New(config ...casbin.Config) *casbin.Middleware | ||
``` | ||
|
||
### Config | ||
| Property | Type | Description | Default | | ||
| :--- | :--- | :--- | :--- | | ||
| ModelFilePath | `string` | Model file path | `"./model.conf"` | | ||
| PolicyAdapter | `persist.Adapter` | Database adapter for policies | `./policy.csv` | | ||
| Enforcer | `*casbin.Enforcer` | Custom casbin enforcer | `Middleware generated enforcer using ModelFilePath & PolicyAdapter` | | ||
| Lookup | `func(*fiber.Ctx) string` | Look up for current subject | `""` | | ||
| Unauthorized | `func(*fiber.Ctx) error` | Response body for unauthorized responses | `Unauthorized` | | ||
| Forbidden | `func(*fiber.Ctx) error` | Response body for forbidden responses | `Forbidden` | | ||
|
||
### Examples | ||
- [Gorm Adapter](https://github.com/svcg/-fiber_casbin_demo) | ||
- [File Adapter](https://github.com/gofiber/contrib/casbin/tree/master/example) | ||
|
||
### CustomPermission | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/contrib/casbin" | ||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/casbin/xorm-adapter/v2" | ||
) | ||
|
||
func main() { | ||
app := fiber.New() | ||
|
||
authz := casbin.New(casbin.Config{ | ||
ModelFilePath: "path/to/rbac_model.conf", | ||
PolicyAdapter: xormadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/"), | ||
Lookup: func(c *fiber.Ctx) string { | ||
// fetch authenticated user subject | ||
}, | ||
}) | ||
|
||
app.Post("/blog", | ||
authz.RequiresPermissions([]string{"blog:create"}, casbin.WithValidationRule(casbin.MatchAllRule)), | ||
func(c *fiber.Ctx) error { | ||
// your handler | ||
}, | ||
) | ||
|
||
app.Delete("/blog/:id", | ||
authz.RequiresPermissions([]string{"blog:create", "blog:delete"}, casbin.WithValidationRule(casbin.AtLeastOneRule)), | ||
func(c *fiber.Ctx) error { | ||
// your handler | ||
}, | ||
) | ||
|
||
app.Listen(":8080") | ||
} | ||
``` | ||
|
||
### RoutePermission | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/contrib/casbin" | ||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/casbin/xorm-adapter/v2" | ||
) | ||
|
||
func main() { | ||
app := fiber.New() | ||
|
||
authz := casbin.New(casbin.Config{ | ||
ModelFilePath: "path/to/rbac_model.conf", | ||
PolicyAdapter: xormadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/"), | ||
Lookup: func(c *fiber.Ctx) string { | ||
// fetch authenticated user subject | ||
}, | ||
}) | ||
|
||
// check permission with Method and Path | ||
app.Post("/blog", | ||
authz.RoutePermission(), | ||
func(c *fiber.Ctx) error { | ||
// your handler | ||
}, | ||
) | ||
|
||
app.Listen(":8080") | ||
} | ||
``` | ||
|
||
### RoleAuthorization | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/contrib/casbin" | ||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/casbin/xorm-adapter/v2" | ||
) | ||
|
||
func main() { | ||
app := fiber.New() | ||
|
||
authz := casbin.New(casbin.Config{ | ||
ModelFilePath: "path/to/rbac_model.conf", | ||
PolicyAdapter: xormadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/"), | ||
Lookup: func(c *fiber.Ctx) string { | ||
// fetch authenticated user subject | ||
}, | ||
}) | ||
|
||
app.Put("/blog/:id", | ||
authz.RequiresRoles([]string{"admin"}), | ||
func(c *fiber.Ctx) error { | ||
// your handler | ||
}, | ||
) | ||
|
||
app.Listen(":8080") | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Don't Send a PR to Update Docs Files | ||
|
||
We are no longer accepting doc files changes in the **gofiber/docs** repo. Please submit your pull requests to https://github.com/gofiber/fiber instead. Any PR that edits documentations won't be accepted in this repository. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Website | ||
|
||
This website is built using [Docusaurus 2](https://docusaurus.io/), a modern static website generator. | ||
|
||
### Installation | ||
|
||
``` | ||
$ yarn | ||
``` | ||
|
||
### Local Development | ||
|
||
``` | ||
$ yarn start | ||
``` | ||
|
||
This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server. | ||
|
||
### Build | ||
|
||
``` | ||
$ yarn build | ||
``` | ||
|
||
This command generates static content into the `build` directory and can be served using any static contents hosting service. | ||
|
||
### Deployment | ||
|
||
Using SSH: | ||
|
||
``` | ||
$ USE_SSH=true yarn deploy | ||
``` | ||
|
||
Not using SSH: | ||
|
||
``` | ||
$ GIT_USER=<Your GitHub username> yarn deploy | ||
``` | ||
|
||
If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. |
Oops, something went wrong.