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:
2026-08-24 18:24:43 +05:00
parent 427d5b4e07
commit 1fd0abbcb2
57 changed files with 1281 additions and 657 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"strings"
"online-back/internal/identity/rbac"
"online-back/internal/lending/loanorders"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
@@ -161,7 +162,9 @@ func buildListQuery(filter loanorders.ListFilter) (whereClause string, args []in
conditions = append(conditions, fmt.Sprintf("lo.status = $%d", len(args)))
}
if filter.BranchId != nil {
if len(filter.BranchIds) > 0 {
conditions, args = rbac.AppendBranchIDs(conditions, args, "lo.branch_id", filter.BranchIds)
} else if filter.BranchId != nil {
args = append(args, *filter.BranchId)
conditions = append(conditions, fmt.Sprintf("lo.branch_id = $%d", len(args)))
}

View File

@@ -227,13 +227,14 @@ type UpdateLoanOrderDTO struct {
}
type ListFilter struct {
Status *string
BranchId *int64
UserId *int64
Source string
Region string
Page int
Limit int
Status *string
BranchId *int64
BranchIds []int64
UserId *int64
Source string
Region string
Page int
Limit int
}
type ListLoanOrderDTO struct {

View File

@@ -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"
@@ -126,6 +127,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))
}
@@ -180,6 +184,10 @@ func (h *handler) update(c *gin.Context) {
return
}
if !h.guardBranchByID(c, id) {
return
}
var dto UpdateLoanOrderDTO
if err := c.ShouldBindJSON(&dto); err != nil {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
@@ -219,6 +227,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
@@ -291,5 +303,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)
}

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"strings"
"online-back/internal/identity/rbac"
"online-back/internal/lending/loanpaidletter"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
@@ -137,7 +138,9 @@ func buildListQuery(filter loanpaidletter.ListFilter) (whereClause string, args
conditions = append(conditions, fmt.Sprintf("l.status = $%d", len(args)))
}
if filter.BranchId != nil {
if len(filter.BranchIds) > 0 {
conditions, args = rbac.AppendBranchIDs(conditions, args, "l.branch_id", filter.BranchIds)
} else if filter.BranchId != nil {
args = append(args, *filter.BranchId)
conditions = append(conditions, fmt.Sprintf("l.branch_id = $%d", len(args)))
}

View File

@@ -80,6 +80,7 @@ type ListFilter struct {
Name string
Status *string
BranchId *int64
BranchIds []int64
Region string
Page int
Limit int

View File

@@ -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"
@@ -137,6 +138,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))
}
@@ -191,6 +195,10 @@ func (h *handler) update(c *gin.Context) {
return
}
if !h.guardBranchByID(c, id) {
return
}
var dto UpdateLoanPaidLetterDTO
if err := c.ShouldBindJSON(&dto); err != nil {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
@@ -230,6 +238,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
@@ -302,5 +314,22 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
filter.BranchId = &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)
}

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"strings"
"online-back/internal/identity/rbac"
"online-back/internal/lending/loansrepayment"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
@@ -81,6 +82,11 @@ func buildListQuery(filter loansrepayment.ListFilter) (whereClause string, args
conditions = append(conditions, fmt.Sprintf("l.passport_id ILIKE $%d", len(args)))
}
if len(filter.BranchIds) > 0 {
args = append(args, filter.BranchIds)
conditions = append(conditions, rbac.UserInBranchSQL("l.user_id", len(args)))
}
if len(conditions) == 0 {
return "", args
}

View File

@@ -29,6 +29,7 @@ type UpdateLoanRepaymentDTO struct {
type ListFilter struct {
PassportId string
BranchIds []int64
Page int
Limit int
}

View File

@@ -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"
@@ -93,6 +94,9 @@ func (h *handler) getByID(c *gin.Context) {
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
return
}
if !rbac.GuardUser(c, item.UserId) {
return
}
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
}
@@ -147,6 +151,10 @@ func (h *handler) update(c *gin.Context) {
return
}
if !h.guardUserByID(c, id) {
return
}
var dto UpdateLoanRepaymentDTO
if err := c.ShouldBindJSON(&dto); err != nil {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
@@ -186,6 +194,10 @@ func (h *handler) delete(c *gin.Context) {
return
}
if !h.guardUserByID(c, id) {
return
}
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
return
@@ -238,5 +250,22 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
filter.PassportId = raw
}
rbac.BindBranchIDs(c, &filter.BranchIds)
return filter, true
}
func (h *handler) guardUserByID(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.GuardUser(c, item.UserId)
}