Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 [Feature]: Casbin: Allow users to use other types of Enforcers #715

Open
3 tasks done
achmadafriza opened this issue Aug 15, 2023 · 1 comment
Open
3 tasks done
Labels

Comments

@achmadafriza
Copy link

Feature Description

Currently, Casbin supports multiple types of Enforcer:

  • Enforcer
  • SyncedEnforcer
  • CachedEnforcer
  • SyncedCachedEnforcer
  • DistributedEnforcer

This doesn't deviate from the core functionalities of the Enforcer, which is described in the interface IEnforcer. The types of enforcers acts as a decorator to the actual Enforcer. As such, users should be able to use the middleware with other types of Enforcer.

Additional Context (optional)

No response

Code Snippet (optional)

package main

import (
	c "github.com/casbin/casbin/v2"
	"github.com/gofiber/contrib/casbin"
	"github.com/gofiber/fiber/v2"
)

func main() {
	enforcer, _ := c.NewEnforcer("path/to/basic_model.conf", "path/to/basic_policy.csv")
	//enforcer, _ := c.NewSyncedEnforcer("path/to/basic_model.conf", "path/to/basic_policy.csv") // This fails, but it should work.
	return casbin.New(casbin.Config{
		Enforcer: enforcer,
		Lookup: func(c *fiber.Ctx) string {
			claims, ok := c.UserContext().Value(model.AuthContextKey).(*model.AuthClaims)
			if !ok {
				logger.Error(nil, "failed to get auth claims")
				return ""
			}
			return claims.Role
		},
		Unauthorized: func(c *fiber.Ctx) error {
			return model.Response(c, http.StatusUnauthorized)
		},
		Forbidden: func(c *fiber.Ctx) error {
			return model.Response(c, http.StatusForbidden)
		},
	})
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my suggestion prior to opening this one.
  • I understand that improperly formatted feature requests may be closed without explanation.
@achmadafriza achmadafriza added the ✏️ Feature New feature or request label Aug 15, 2023
@achmadafriza
Copy link
Author

There's a simple fix which just changes /gofiber/casbin.Config.Enforcer type from casbin.*Enforcer to casbin.IEnforcer. However this does not take into account the behavior of DistributedEnforcer, which have another interface casbin.IDistributedEnforcer.

I could submit a PR for it later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants