Refactor CheckAccessPermission().

This commit is contained in:
Gucheng Wang 2022-07-13 00:50:32 +08:00
parent 2bca424370
commit 6e70f0fc58
2 changed files with 20 additions and 11 deletions

View File

@ -51,7 +51,7 @@ func tokenToResponse(token *object.Token) *Response {
func (c *ApiController) HandleLoggedIn(application *object.Application, user *object.User, form *RequestForm) (resp *Response) { func (c *ApiController) HandleLoggedIn(application *object.Application, user *object.User, form *RequestForm) (resp *Response) {
userId := user.GetId() userId := user.GetId()
allowed, err := object.CheckPermission(userId, application) allowed, err := object.CheckAccessPermission(userId, application)
if err != nil { if err != nil {
c.ResponseError(err.Error(), nil) c.ResponseError(err.Error(), nil)
return return

View File

@ -231,19 +231,28 @@ func CheckUserPermission(requestUserId, userId string, strict bool) (bool, error
return hasPermission, fmt.Errorf("you don't have the permission to do this") return hasPermission, fmt.Errorf("you don't have the permission to do this")
} }
func CheckPermission(userId string, application *Application) (bool, error) { func CheckAccessPermission(userId string, application *Application) (bool, error) {
permissions := GetPermissions(application.Organization) permissions := GetPermissions(application.Organization)
allow := true allowed := true
var err error var err error
for _, permission := range permissions { for _, permission := range permissions {
if permission.IsEnabled { if !permission.IsEnabled {
for _, resource := range permission.Resources { continue
if resource == application.Name { }
enforcer := getEnforcer(permission)
allow, err = enforcer.Enforce(userId, application.Name, "read") isHit := false
} for _, resource := range permission.Resources {
if application.Name == resource {
isHit = true
break
} }
} }
if isHit {
enforcer := getEnforcer(permission)
allowed, err = enforcer.Enforce(userId, application.Name, "read")
break
}
} }
return allow, err return allowed, err
} }