changed role permission
This commit is contained in:
1 parent
f0ecf23879
commit
323e131216
74 files changed
+2915
-4713
No files matched your search
@@ -5,61 +5,69 @@ import (
|
||||
"net/http"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/identity/rbac/perm"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// The three valid guard names. Use these constants when validating
|
||||
// the guardName field in role/permission DTOs.
|
||||
const (
|
||||
GuardWeb = guardWeb
|
||||
GuardOperator = guardOperator
|
||||
GuardAdmin = guardAdmin
|
||||
)
|
||||
|
||||
const (
|
||||
contextBranchIDs = "branchIds"
|
||||
contextService = "rbacService"
|
||||
)
|
||||
|
||||
func BranchIDsFrom(c *gin.Context) []int64 {
|
||||
raw, ok := c.Get(contextBranchIDs)
|
||||
// PrincipalFrom returns the caller loaded by Guard, or nil on public routes.
|
||||
func PrincipalFrom(c *gin.Context) *Principal {
|
||||
raw, ok := c.Get(contextPrincipal)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
ids, _ := raw.([]int64)
|
||||
return ids
|
||||
p, _ := raw.(*Principal)
|
||||
return p
|
||||
}
|
||||
|
||||
func serviceFrom(c *gin.Context) Authorizer {
|
||||
raw, ok := c.Get(contextService)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
service, _ := raw.(Authorizer)
|
||||
return service
|
||||
}
|
||||
|
||||
// Can reports whether the caller holds code (always true for superadmin).
|
||||
func Can(c *gin.Context, code perm.Code) bool {
|
||||
return PrincipalFrom(c).Can(code)
|
||||
}
|
||||
|
||||
// BranchIDsFrom returns the caller's branches when their scope is branch,
|
||||
// nil otherwise (all and own scopes are not narrowed by branch).
|
||||
func BranchIDsFrom(c *gin.Context) []int64 {
|
||||
p := PrincipalFrom(c)
|
||||
if p == nil || p.Scope != ScopeBranch {
|
||||
return nil
|
||||
}
|
||||
return p.BranchIDs
|
||||
}
|
||||
|
||||
func HasBranchScope(c *gin.Context) bool {
|
||||
return len(BranchIDsFrom(c)) > 0
|
||||
}
|
||||
|
||||
func RoleFrom(c *gin.Context) string {
|
||||
raw, _ := c.Get(contextRole)
|
||||
role, _ := raw.(string)
|
||||
return role
|
||||
}
|
||||
|
||||
func GuardFrom(c *gin.Context) string {
|
||||
raw, _ := c.Get(contextGuard)
|
||||
guard, _ := raw.(string)
|
||||
return guard
|
||||
}
|
||||
|
||||
func IsOwnScope(c *gin.Context) bool {
|
||||
return GuardFrom(c) == guardWeb
|
||||
p := PrincipalFrom(c)
|
||||
return p != nil && p.Scope == ScopeOwn
|
||||
}
|
||||
|
||||
func IsAdminGuard(c *gin.Context) bool {
|
||||
return GuardFrom(c) == guardAdmin
|
||||
func HasAllScope(c *gin.Context) bool {
|
||||
p := PrincipalFrom(c)
|
||||
return p != nil && p.Scope == ScopeAll
|
||||
}
|
||||
|
||||
// SkipRecordGuard: all-scope callers see every record, so handlers can skip
|
||||
// the extra owner/branch lookup.
|
||||
func SkipRecordGuard(c *gin.Context) bool {
|
||||
return !HasBranchScope(c) && !IsOwnScope(c)
|
||||
return HasAllScope(c)
|
||||
}
|
||||
|
||||
func CurrentUserID(c *gin.Context) (int64, bool) {
|
||||
if p := PrincipalFrom(c); p != nil {
|
||||
return p.UserID, true
|
||||
}
|
||||
return parseContextUserID(c)
|
||||
}
|
||||
|
||||
@@ -116,12 +124,7 @@ func GuardNewBranchAssignment(c *gin.Context, targetUserID int64) bool {
|
||||
if !HasBranchScope(c) {
|
||||
return true
|
||||
}
|
||||
raw, ok := c.Get(contextService)
|
||||
if !ok {
|
||||
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
return false
|
||||
}
|
||||
service, _ := raw.(Authorizer)
|
||||
service := serviceFrom(c)
|
||||
if service == nil {
|
||||
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
return false
|
||||
@@ -149,12 +152,7 @@ func GuardUser(c *gin.Context, userID int64) bool {
|
||||
if len(ids) == 0 {
|
||||
return true
|
||||
}
|
||||
raw, ok := c.Get(contextService)
|
||||
if !ok {
|
||||
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
return false
|
||||
}
|
||||
service, _ := raw.(Authorizer)
|
||||
service := serviceFrom(c)
|
||||
if service == nil {
|
||||
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
return false
|
||||
|
||||
Reference in new issue
Block a user