This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<feat> finlly add casbin to our server .
Signed-off-by: JackyCZJ <chenzj@esixnetwork.net>
- Loading branch information
JackyCZJ
committed
Mar 2, 2020
1 parent
dee5040
commit e660055
Showing
10 changed files
with
185 additions
and
29 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
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,81 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/casbin/casbin/v2" | ||
"github.com/labstack/echo/v4" | ||
"github.com/labstack/echo/v4/middleware" | ||
) | ||
|
||
type ( | ||
// Config defines the config for CasbinAuth middleware. | ||
Config struct { | ||
// Skipper defines a function to skip middleware. | ||
Skipper middleware.Skipper | ||
|
||
// Enforcer CasbinAuth main rule. | ||
// Required. | ||
Enforcer *casbin.Enforcer | ||
} | ||
) | ||
|
||
var ( | ||
// DefaultConfig is the default CasbinAuth middleware config. | ||
DefaultConfig = Config{ | ||
Skipper: middleware.DefaultSkipper, | ||
} | ||
) | ||
|
||
func MiddlewareWithConfig(config Config) echo.MiddlewareFunc { | ||
// Defaults | ||
if config.Skipper == nil { | ||
config.Skipper = DefaultConfig.Skipper | ||
} | ||
|
||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
if config.Skipper(c) { | ||
return next(c) | ||
} | ||
|
||
if pass, err := config.CheckPermission(c); err == nil && pass { | ||
return next(c) | ||
} else if err != nil { | ||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) | ||
} | ||
|
||
return echo.ErrForbidden | ||
} | ||
} | ||
} | ||
|
||
func Middleware(ce *casbin.Enforcer) echo.MiddlewareFunc { | ||
c := DefaultConfig | ||
c.Enforcer = ce | ||
return MiddlewareWithConfig(c) | ||
} | ||
|
||
func (a *Config) CheckPermission(e echo.Context) (bool, error) { | ||
user := e.Get("user_id") | ||
role := e.Get("role") | ||
if user == nil && role == nil { | ||
user = "guest" | ||
_, err := a.Enforcer.AddRoleForUser(user.(string), "0") | ||
if err != nil { | ||
return false, err | ||
} | ||
} | ||
role = strconv.Itoa(role.(int)) | ||
|
||
_, err := a.Enforcer.AddRoleForUser(user.(string), role.(string)) | ||
if err != nil { | ||
return false, err | ||
} | ||
method := e.Request().Method | ||
path := e.Request().URL.Path | ||
fmt.Println(user.(string), method, path) | ||
return a.Enforcer.Enforce(role, path, method) | ||
} |
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
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,14 @@ | ||
[request_definition] | ||
r = sub, obj, act | ||
|
||
[policy_definition] | ||
p = sub, obj, act | ||
|
||
[role_definition] | ||
g = _, _ | ||
|
||
[policy_effect] | ||
e = some(where (p.eft == allow)) | ||
|
||
[matchers] | ||
m = g(r.sub, p.sub) && keyMatch(r.obj, p.obj) && (r.act == p.act || p.act == "*") |
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,32 @@ | ||
p , 3 , /*, (GET|POST|PUT|DELETE) | ||
|
||
p , 0 , /user/login , POST | ||
p , 0 , /user/register , POST | ||
|
||
p , 1 , /user/logout , POST | ||
p , 2 , /user/logout , POST | ||
|
||
p , 1 , /user/setting/* , (GET|POST|PUT|DELETE) | ||
p , 2 , /user/setting/* , (GET|POST|PUT|DELETE) | ||
|
||
|
||
p, 0 , /api/v1/Goods/* , GET | ||
p, 1 , /api/v1/Goods/* , GET | ||
p, 2 , /api/v1/Goods/* , (GET|POST|PUT|DELETE) | ||
|
||
p , 1 , /api/v1/Order/:id , (GET|POST|PUT|DELETE) | ||
p , 1 , /api/v1/Order , (POST|PUT) | ||
p , 2 , /api/v1/Order/:id/* , (GET|POST|PUT|DELETE) | ||
|
||
|
||
p, 0 , /api/v1/Shop/* , GET | ||
p, 1 , /api/v1/Shop/* , GET | ||
p, 2 , /api/v1/Shop/* , (GET|POST|PUT|DELETE) | ||
p, 2 , /api/v1/Shop/setting/* , (GET|POST|PUT|DELETE) | ||
|
||
|
||
p, 0 , /api/v1/Cart , (GET|POST|PUT|DELETE) | ||
p, 1 , /api/v1/Cart , (GET|POST|PUT|DELETE) | ||
p, 2 , /api/v1/Cart , (GET|POST|PUT|DELETE) | ||
|
||
|
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
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
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
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
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