### Installing Hertz Casbin Contrib Source: https://github.com/hertz-contrib/casbin/blob/main/README.md This shell command uses the Go module system to download and install the `hertz-contrib/casbin` package. This makes the library available for use as a dependency in your Go projects. ```shell go get github.com/hertz-contrib/casbin ``` -------------------------------- ### Importing Hertz Casbin Contrib Source: https://github.com/hertz-contrib/casbin/blob/main/README.md This Go import statement includes the necessary package to use the Casbin middleware functions and types within your Hertz application code. You need to run the `go get` command first to fetch this package. ```go import "github.com/hertz-contrib/casbin" ``` -------------------------------- ### Implementing Casbin Authorization in Hertz with Sessions Source: https://github.com/hertz-contrib/casbin/blob/main/README.md This comprehensive Go example demonstrates setting up a Hertz server (`server.Default()`) that utilizes both sessions (`hertz-contrib/sessions`) and the `hertz-contrib/casbin` middleware for authorization. It shows how to initialize the session store and Casbin middleware, protect routes with `RequiresPermissions` and `RequiresRoles` (using AND/CUSTOM logic), handle user login to store the subject (user identifier) in the session, and defines a helper function (`subjectFromSession`) to retrieve the subject from the session for Casbin checks. ```go package main import ( "context" "log" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/server" "github.com/hertz-contrib/casbin" "github.com/hertz-contrib/sessions" "github.com/hertz-contrib/sessions/cookie" ) func main() { h := server.Default() // Using sessions and casbin. store := cookie.NewStore([]byte("secret")) h.Use(sessions.New("session", store)) auth, err := casbin.NewCasbinMiddleware("example/config/model.conf", "example/config/policy.csv", subjectFromSession) if err != nil { log.Fatal(err) } h.POST("/login", func(ctx context.Context, c *app.RequestContext) { // Verify username and password. // ... // Store current subject in session session := sessions.Default(c) session.Set("name", "alice") err := session.Save() if err != nil { log.Fatal(err) } c.String(200, "you login successfully") }) h.GET("/book/r", auth.RequiresPermissions("book:read", casbin.WithLogic(casbin.AND)), func(ctx context.Context, c *app.RequestContext) { c.String(200, "you read the book successfully") }) h.GET("/book/rw", auth.RequiresPermissions("book:read book:write", casbin.WithLogic(casbin.AND)), func(ctx context.Context, c *app.RequestContext) { c.String(200, "you read the book failed") }) h.GET("/book/custom/rw", auth.RequiresPermissions("book:read && book:write", casbin.WithLogic(casbin.CUSTOM)), func(ctx context.Context, c *app.RequestContext) { c.String(200, "you read the book failed") }) h.POST("/book/u", auth.RequiresRoles("user", casbin.WithLogic(casbin.AND)), func(ctx context.Context, c *app.RequestContext) { c.String(200, "you posted a book successfully") }) h.POST("/book/ua", auth.RequiresRoles("user admin", casbin.WithLogic(casbin.AND)), func(ctx context.Context, c *app.RequestContext) { c.String(200, "you posted a book failed") }) h.POST("/book/custom/ua", auth.RequiresRoles("user && admin", casbin.WithLogic(casbin.CUSTOM)), func(ctx context.Context, c *app.RequestContext) { c.String(200, "you posted a book failed") }) h.Spin() } ``` -------------------------------- ### Pushing Local Branch to Remote (Git) Source: https://github.com/hertz-contrib/casbin/blob/main/CONTRIBUTING.md This command pushes the specified local branch (`my-fix-branch`) to the remote repository named `origin`. This makes the branch and its commits available on the remote server (e.g., GitHub) so that a pull request can be created. Requires Git installed and a configured remote named 'origin'. ```git git push origin my-fix-branch ``` -------------------------------- ### Creating New Branch for PR (Git) Source: https://github.com/hertz-contrib/casbin/blob/main/CONTRIBUTING.md This command creates a new Git branch named `my-fix-branch` based on the `develop` branch and switches to the new branch. It is typically used to isolate changes for a specific feature or fix before submitting a pull request. Requires Git installed and a local repository cloned. ```git git checkout -b my-fix-branch develop ``` -------------------------------- ### Extracting Subject from Session in Hertz Source: https://github.com/hertz-contrib/casbin/blob/main/README.md This Go function, designed to be passed to `casbin.NewCasbinMiddleware`, retrieves the current user's subject (typically an identifier like username or user ID) from the Hertz request context's session. It specifically looks for a string value associated with the key "name" and returns it if found, otherwise returning an empty string. This function is crucial for the Casbin middleware to identify who is making the request. ```go // subjectFromSession get subject from session. func subjectFromSession(ctx context.Context, c *app.RequestContext) string { // Get subject from session. session := sessions.Default(c) if subject, ok := session.Get("name").(string); !ok { return "" } else { return subject } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.