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
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"online-back/internal/cards/cardorder"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
@@ -110,7 +111,9 @@ func buildListQuery(filter cardorder.ListFilter) (whereClause string, args []int
|
||||
conditions = append(conditions, fmt.Sprintf("status = $%d", len(args)))
|
||||
}
|
||||
|
||||
if filter.BranchId != nil {
|
||||
if len(filter.BranchIds) > 0 {
|
||||
conditions, args = rbac.AppendBranchIDs(conditions, args, "branch_id", filter.BranchIds)
|
||||
} else if filter.BranchId != nil {
|
||||
args = append(args, *filter.BranchId)
|
||||
conditions = append(conditions, fmt.Sprintf("branch_id = $%d", len(args)))
|
||||
}
|
||||
|
||||
@@ -212,6 +212,7 @@ type UpdateCardOrderDTO struct {
|
||||
type ListFilter struct {
|
||||
Status *string
|
||||
BranchId *int64
|
||||
BranchIds []int64
|
||||
UserId *int64
|
||||
CardStateId *int64
|
||||
CardTypeId *int64
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -98,6 +99,9 @@ func (h *handler) getByID(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
if !rbac.GuardBranch(c, item.BranchId) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
||||
}
|
||||
@@ -153,6 +157,10 @@ func (h *handler) update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !h.guardBranchByID(c, id) {
|
||||
return
|
||||
}
|
||||
|
||||
var dto UpdateCardOrderDTO
|
||||
if err := c.ShouldBindBodyWithJSON(&dto); err != nil {
|
||||
h.logger.Errorf("update card order bind failed: %v", err)
|
||||
@@ -193,6 +201,10 @@ func (h *handler) delete(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !h.guardBranchByID(c, id) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
@@ -276,9 +288,26 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter.Region = raw
|
||||
}
|
||||
|
||||
rbac.BindBranchIDs(c, &filter.BranchIds)
|
||||
return filter, true
|
||||
}
|
||||
|
||||
func (h *handler) guardBranchByID(c *gin.Context, id int64) bool {
|
||||
if !rbac.HasBranchScope(c) {
|
||||
return true
|
||||
}
|
||||
item, err := h.repository.GetByID(c.Request.Context(), id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return false
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return false
|
||||
}
|
||||
return rbac.GuardBranch(c, item.BranchId)
|
||||
}
|
||||
|
||||
func queryValue(c *gin.Context, keys ...string) string {
|
||||
for _, key := range keys {
|
||||
if raw := strings.TrimSpace(c.Query(key)); raw != "" && raw != "undefined" && raw != "null" {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"online-back/internal/cards/cardpinorders"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
@@ -77,7 +78,9 @@ func buildListQuery(filter cardpinorders.ListFilter) (whereClause string, args [
|
||||
conditions = append(conditions, fmt.Sprintf("status = $%d", len(args)))
|
||||
}
|
||||
|
||||
if filter.BranchId != nil {
|
||||
if len(filter.BranchIds) > 0 {
|
||||
conditions, args = rbac.AppendBranchIDs(conditions, args, "branch_id", filter.BranchIds)
|
||||
} else if filter.BranchId != nil {
|
||||
args = append(args, *filter.BranchId)
|
||||
conditions = append(conditions, fmt.Sprintf("branch_id = $%d", len(args)))
|
||||
}
|
||||
|
||||
@@ -74,6 +74,7 @@ type UpdateCardPinOrderDTO struct {
|
||||
type ListFilter struct {
|
||||
Status *string
|
||||
BranchId *int64
|
||||
BranchIds []int64
|
||||
UserId *int64
|
||||
CardTypeId *int64
|
||||
Region string
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -97,6 +98,9 @@ func (h *handler) getByID(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
if !rbac.GuardBranch(c, item.BranchId) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
||||
}
|
||||
@@ -151,6 +155,10 @@ func (h *handler) update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !h.guardBranchByID(c, id) {
|
||||
return
|
||||
}
|
||||
|
||||
var dto UpdateCardPinOrderDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
@@ -190,6 +198,10 @@ func (h *handler) delete(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !h.guardBranchByID(c, id) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
@@ -270,5 +282,22 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter.Region = raw
|
||||
}
|
||||
|
||||
rbac.BindBranchIDs(c, &filter.BranchIds)
|
||||
return filter, true
|
||||
}
|
||||
|
||||
func (h *handler) guardBranchByID(c *gin.Context, id int64) bool {
|
||||
if !rbac.HasBranchScope(c) {
|
||||
return true
|
||||
}
|
||||
item, err := h.repository.GetByID(c.Request.Context(), id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return false
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return false
|
||||
}
|
||||
return rbac.GuardBranch(c, item.BranchId)
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"online-back/internal/cards/cards"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
@@ -68,6 +69,11 @@ func buildListQuery(filter cards.ListFilter) (whereClause string, args []interfa
|
||||
conditions = append(conditions, fmt.Sprintf("user_id = $%d", len(args)))
|
||||
}
|
||||
|
||||
if len(filter.BranchIds) > 0 {
|
||||
args = append(args, filter.BranchIds)
|
||||
conditions = append(conditions, rbac.UserInBranchSQL("user_id", len(args)))
|
||||
}
|
||||
|
||||
if filter.Type != nil {
|
||||
args = append(args, *filter.Type)
|
||||
conditions = append(conditions, fmt.Sprintf("type = $%d", len(args)))
|
||||
|
||||
@@ -33,10 +33,11 @@ type UpdateCardDTO struct {
|
||||
}
|
||||
|
||||
type ListFilter struct {
|
||||
UserId *int64
|
||||
Type *string
|
||||
Page int
|
||||
Limit int
|
||||
UserId *int64
|
||||
BranchIds []int64
|
||||
Type *string
|
||||
Page int
|
||||
Limit int
|
||||
}
|
||||
|
||||
type ListCardDTO struct {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -94,6 +95,13 @@ func (h *handler) getByID(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
if item.UserId != nil && !rbac.GuardUser(c, *item.UserId) {
|
||||
return
|
||||
}
|
||||
if item.UserId == nil && rbac.HasBranchScope(c) {
|
||||
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
||||
}
|
||||
@@ -148,6 +156,10 @@ func (h *handler) update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !h.guardCardByID(c, id) {
|
||||
return
|
||||
}
|
||||
|
||||
var dto UpdateCardDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
@@ -187,6 +199,10 @@ func (h *handler) delete(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !h.guardCardByID(c, id) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
@@ -247,5 +263,26 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter.Limit = limit
|
||||
}
|
||||
|
||||
rbac.BindBranchIDs(c, &filter.BranchIds)
|
||||
return filter, true
|
||||
}
|
||||
|
||||
func (h *handler) guardCardByID(c *gin.Context, id int64) bool {
|
||||
if !rbac.HasBranchScope(c) {
|
||||
return true
|
||||
}
|
||||
item, err := h.repository.GetByID(c.Request.Context(), id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return false
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return false
|
||||
}
|
||||
if item.UserId == nil {
|
||||
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
return false
|
||||
}
|
||||
return rbac.GuardUser(c, *item.UserId)
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"online-back/internal/cards/visamaster"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
@@ -159,7 +160,9 @@ func buildListQuery(filter visamaster.ListFilter) (whereClause string, args []in
|
||||
conditions = append(conditions, fmt.Sprintf("o.status = $%d", len(args)))
|
||||
}
|
||||
|
||||
if filter.BranchId != nil {
|
||||
if len(filter.BranchIds) > 0 {
|
||||
conditions, args = rbac.AppendBranchIDs(conditions, args, "o.branch_id", filter.BranchIds)
|
||||
} else if filter.BranchId != nil {
|
||||
args = append(args, *filter.BranchId)
|
||||
conditions = append(conditions, fmt.Sprintf("o.branch_id = $%d", len(args)))
|
||||
}
|
||||
|
||||
@@ -116,13 +116,14 @@ type UpdateVisaMasterPaymentOrderDTO struct {
|
||||
}
|
||||
|
||||
type ListFilter struct {
|
||||
Name string
|
||||
Status *string
|
||||
BranchId *int64
|
||||
UserId *int64
|
||||
Region string
|
||||
Page int
|
||||
Limit int
|
||||
Name string
|
||||
Status *string
|
||||
BranchId *int64
|
||||
BranchIds []int64
|
||||
UserId *int64
|
||||
Region string
|
||||
Page int
|
||||
Limit int
|
||||
}
|
||||
|
||||
type ListVisaMasterPaymentOrderDTO struct {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -97,6 +98,9 @@ func (h *handler) getByID(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
if !rbac.GuardBranch(c, item.BranchId) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
||||
}
|
||||
@@ -151,6 +155,10 @@ func (h *handler) update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !h.guardBranchByID(c, id) {
|
||||
return
|
||||
}
|
||||
|
||||
var dto UpdateVisaMasterPaymentOrderDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
@@ -190,6 +198,10 @@ func (h *handler) delete(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !h.guardBranchByID(c, id) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
@@ -266,5 +278,22 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter.UserId = &id
|
||||
}
|
||||
|
||||
rbac.BindBranchIDs(c, &filter.BranchIds)
|
||||
return filter, true
|
||||
}
|
||||
|
||||
func (h *handler) guardBranchByID(c *gin.Context, id int64) bool {
|
||||
if !rbac.HasBranchScope(c) {
|
||||
return true
|
||||
}
|
||||
item, err := h.repository.GetByID(c.Request.Context(), id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return false
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return false
|
||||
}
|
||||
return rbac.GuardBranch(c, item.BranchId)
|
||||
}
|
||||
Reference in new issue
Block a user