Skip to content

Commit

Permalink
Update to Casbin 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Aug 3, 2019
1 parent 749041b commit b2abe43
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 7 additions & 1 deletion authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ func (a *BasicAuthorizer) CheckPermission(r *http.Request) bool {
user := a.GetUserName(r)
method := r.Method
path := r.URL.Path
return a.enforcer.Enforce(user, path, method)

allowed, err := a.enforcer.Enforce(user, path, method)
if err != nil {
panic(err)
}

return allowed
}

// RequirePermission returns the 403 Forbidden to the client
Expand Down
8 changes: 5 additions & 3 deletions authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func testAuthzRequest(t *testing.T, router *gin.Engine, user string, path string

func TestBasic(t *testing.T) {
router := gin.New()
router.Use(NewAuthorizer(casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")))
e, _ := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")
router.Use(NewAuthorizer(e))
router.Any("/*anypath", func(c *gin.Context) {
c.Status(200)
})
Expand All @@ -39,7 +40,8 @@ func TestBasic(t *testing.T) {

func TestPathWildcard(t *testing.T) {
router := gin.New()
router.Use(NewAuthorizer(casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")))
e, _ := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")
router.Use(NewAuthorizer(e))
router.Any("/*anypath", func(c *gin.Context) {
c.Status(200)
})
Expand All @@ -61,7 +63,7 @@ func TestPathWildcard(t *testing.T) {

func TestRBAC(t *testing.T) {
router := gin.New()
e := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")
e, _ := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")
router.Use(NewAuthorizer(e))
router.Any("/*anypath", func(c *gin.Context) {
c.Status(200)
Expand Down

0 comments on commit b2abe43

Please sign in to comment.