ch some
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

This commit is contained in:
2026-09-04 19:33:26 +05:00
parent 28509eb4ca
commit d1a6fcaadc
102 changed files with 2089 additions and 1957 deletions

View File

@@ -9,6 +9,14 @@ import (
"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"
@@ -43,6 +51,10 @@ func IsOwnScope(c *gin.Context) bool {
return GuardFrom(c) == guardWeb
}
func IsAdminGuard(c *gin.Context) bool {
return GuardFrom(c) == guardAdmin
}
func SkipRecordGuard(c *gin.Context) bool {
return !HasBranchScope(c) && !IsOwnScope(c)
}
@@ -94,6 +106,40 @@ func GuardBranch(c *gin.Context, branchID int64) bool {
return false
}
// GuardNewBranchAssignment guards POST /branch-users: a branch-scoped caller
// may only assign a user who is unbranched or already in one of their own
// branches. Without this, an operator holding users.manage could pull
// another branch's operator into their own branch — the destination-branch
// check alone doesn't catch that, since it never looks at the target's
// current branch.
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)
if service == nil {
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
return false
}
currentBranches, err := service.BranchIDs(c.Request.Context(), targetUserID)
if err != nil {
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
return false
}
for _, branchID := range currentBranches {
if !AllowsBranch(c, branchID) {
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
return false
}
}
return true
}
func GuardUser(c *gin.Context, userID int64) bool {
if IsOwnScope(c) {
id := userID
@@ -139,7 +185,8 @@ func UserInBranchSQL(userExpr string, placeholder int) string {
OR EXISTS (SELECT 1 FROM card_rekwizit_orders cro WHERE cro.user_id = %s AND cro.branch_id = ANY($%d))
OR EXISTS (SELECT 1 FROM visa_master_payment_orders vmo WHERE vmo.user_id = %s AND vmo.branch_id = ANY($%d))
OR EXISTS (SELECT 1 FROM loan_paid_off_letters lpl WHERE lpl.user_id = %s AND lpl.branch_id = ANY($%d))
)`, userExpr, placeholder, userExpr, placeholder, userExpr, placeholder, userExpr, placeholder, userExpr, placeholder, userExpr, placeholder, userExpr, placeholder)
OR EXISTS (SELECT 1 FROM card_balances cb WHERE cb.created_by = %s AND cb.branch_id = ANY($%d))
)`, userExpr, placeholder, userExpr, placeholder, userExpr, placeholder, userExpr, placeholder, userExpr, placeholder, userExpr, placeholder, userExpr, placeholder, userExpr, placeholder)
}
func containsInt64(ids []int64, want int64) bool {