This package provides Role-Based Access Control (RBAC) functionality using Casbin. It allows you to define roles, permissions, and enforce access control in your application.
Create a policy.csv file to define your roles and permissions. Below is an example:
p, superadmin, *, *
p, admin, /api/v1/admin/*, *
p, user, /api/v1/*, *pdefines a policy.- The first column is the role.
- The second column is the resource (e.g., URL path).
- The third column is the action (e.g., HTTP method or
*for all actions).
Use the NewManager function to initialize the RBAC manager with the policy file.
import (
"github.com/rizalgowandy/gdk/pkg/rbac"
)
func main() {
policyFile := "path/to/policy.csv"
rbacManager, err := rbac.NewManager(policyFile)
if err != nil {
panic(err)
}
// Use rbacManager in your application
}The Auth middleware validates JWT tokens and sets the user claims in the context.
import (
"github.com/labstack/echo/v4"
"github.com/rizalgowandy/gdk/pkg/auth"
"github.com/rizalgowandy/gdk/pkg/httpx/echo/middleware"
)
func main() {
e := echo.New()
authOperator := auth.NewOperator("your-secret-key")
e.Use(middleware.Auth(authOperator))
// Define your routes
e.Start(":8080")
}The RBAC middleware enforces role-based access control based on the user's roles and permissions.
import (
"github.com/labstack/echo/v4"
"github.com/rizalgowandy/gdk/pkg/httpx/echo/middleware"
"github.com/rizalgowandy/gdk/pkg/rbac"
)
func main() {
e := echo.New()
rbacManager, _ := rbac.NewManager("path/to/policy.csv")
authOperator := auth.NewOperator("your-secret-key")
e.Use(middleware.Auth(authOperator))
e.Use(middleware.RBAC(rbacManager, authOperator))
// Define your routes
e.GET("/api/v1/admin/dashboard", func(c echo.Context) error {
return c.JSON(200, map[string]string{"message": "Welcome Admin!"})
})
e.Start(":8080")
}- Ensure the
policy.csvfile is accessible and contains the correct permissions. - The
Authmiddleware must be used before theRBACmiddleware to ensure user claims are available for RBAC checks.