Bind permissions to roles and scope operator access by branch.
Replace permission_user with permission_role, enforce RBAC on admin routes, and limit operators to their assigned branches. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
427d5b4e07
commit
1fd0abbcb2
57 files changed
+1281
-657
No files matched your search
@@ -0,0 +1,104 @@
|
||||
package rbac
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
contextBranchIDs = "branchIds"
|
||||
contextService = "rbacService"
|
||||
)
|
||||
|
||||
func BranchIDsFrom(c *gin.Context) []int64 {
|
||||
raw, ok := c.Get(contextBranchIDs)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
ids, _ := raw.([]int64)
|
||||
return ids
|
||||
}
|
||||
|
||||
func HasBranchScope(c *gin.Context) bool {
|
||||
return len(BranchIDsFrom(c)) > 0
|
||||
}
|
||||
|
||||
func AllowsBranch(c *gin.Context, branchID int64) bool {
|
||||
ids := BranchIDsFrom(c)
|
||||
if len(ids) == 0 {
|
||||
return true
|
||||
}
|
||||
return containsInt64(ids, branchID)
|
||||
}
|
||||
|
||||
func BindBranchIDs(c *gin.Context, dest *[]int64) {
|
||||
ids := BranchIDsFrom(c)
|
||||
if len(ids) == 0 {
|
||||
return
|
||||
}
|
||||
*dest = ids
|
||||
}
|
||||
|
||||
func GuardBranch(c *gin.Context, branchID int64) bool {
|
||||
if AllowsBranch(c, branchID) {
|
||||
return true
|
||||
}
|
||||
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
return false
|
||||
}
|
||||
|
||||
func GuardUser(c *gin.Context, userID int64) bool {
|
||||
ids := BranchIDsFrom(c)
|
||||
if len(ids) == 0 {
|
||||
return true
|
||||
}
|
||||
raw, ok := c.Get(contextService)
|
||||
if !ok {
|
||||
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
return false
|
||||
}
|
||||
service, _ := raw.(*Service)
|
||||
if service == nil {
|
||||
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
return false
|
||||
}
|
||||
visible, err := service.UserVisibleInBranches(c.Request.Context(), userID, ids)
|
||||
if err != nil || !visible {
|
||||
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func AppendBranchIDs(conditions []string, args []interface{}, column string, ids []int64) ([]string, []interface{}) {
|
||||
if len(ids) == 0 {
|
||||
return conditions, args
|
||||
}
|
||||
args = append(args, ids)
|
||||
conditions = append(conditions, fmt.Sprintf("%s = ANY($%d)", column, len(args)))
|
||||
return conditions, args
|
||||
}
|
||||
|
||||
func UserInBranchSQL(userExpr string, placeholder int) string {
|
||||
return fmt.Sprintf(`(
|
||||
EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = %s AND bu.branch_id = ANY($%d))
|
||||
OR EXISTS (SELECT 1 FROM card_orders co WHERE co.user_id = %s AND co.branch_id = ANY($%d))
|
||||
OR EXISTS (SELECT 1 FROM loan_orders lo WHERE lo.user_id = %s AND lo.branch_id = ANY($%d))
|
||||
OR EXISTS (SELECT 1 FROM card_pin_orders cpo WHERE cpo.user_id = %s AND cpo.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)
|
||||
}
|
||||
|
||||
func containsInt64(ids []int64, want int64) bool {
|
||||
for _, id := range ids {
|
||||
if id == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in new issue
Block a user