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

No files matched your search

+26 -12
View File
@@ -8,6 +8,7 @@ import (
"fmt"
"strings"
"online-back/internal/identity/rbac"
"online-back/internal/identity/user"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
@@ -98,7 +99,13 @@ func (r *repository) ListStaff(ctx context.Context, filter user.ListFilter) (use
}
func buildStaffWhere(filter user.ListFilter) (whereClause string, args []interface{}) {
conditions := []string{`EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id)`}
var conditions []string
if len(filter.BranchIds) > 0 {
args = append(args, filter.BranchIds)
conditions = append(conditions, fmt.Sprintf(`EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id AND bu.branch_id = ANY($%d))`, len(args)))
} else {
conditions = append(conditions, `EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id)`)
}
conditions, args = appendNameSearch(conditions, args, filter.Search)
return " WHERE " + strings.Join(conditions, " AND "), args
}
@@ -145,17 +152,22 @@ func buildFilteredListQuery(filter user.ListFilter, roles ...string) (fromClause
var conditions []string
conditions, args = appendNameSearch(conditions, args, filter.Search)
if len(roles) == 1 {
args = append(args, roles[0])
n := len(args)
conditions = append(conditions, fmt.Sprintf(`r.code = $%d`, n))
} else if len(roles) > 0 {
if len(roles) > 0 {
placeholders := make([]string, len(roles))
for i, role := range roles {
args = append(args, role)
placeholders[i] = fmt.Sprintf("$%d", len(args))
}
conditions = append(conditions, fmt.Sprintf(`r.code IN (%s)`, strings.Join(placeholders, ", ")))
if len(roles) == 1 {
conditions = append(conditions, fmt.Sprintf(`r.code = %s`, placeholders[0]))
} else {
conditions = append(conditions, fmt.Sprintf(`r.code IN (%s)`, strings.Join(placeholders, ", ")))
}
}
if len(filter.BranchIds) > 0 {
args = append(args, filter.BranchIds)
conditions = append(conditions, rbac.UserInBranchSQL("u.id", len(args)))
}
if len(conditions) == 0 {
@@ -187,11 +199,13 @@ func likePattern(term string) string {
func (r *repository) GetByRole(ctx context.Context, role string, filter user.ListFilter) (user.ListUserDTO, error) {
fromClause, whereClause, args := buildFilteredListQuery(filter, role)
excludeStaff := `NOT EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id)`
if whereClause == "" {
whereClause = " WHERE " + excludeStaff
} else {
whereClause += " AND " + excludeStaff
if len(filter.BranchIds) == 0 {
excludeStaff := `NOT EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id)`
if whereClause == "" {
whereClause = " WHERE " + excludeStaff
} else {
whereClause += " AND " + excludeStaff
}
}
return r.paginateUsers(ctx, filter, fromClause, whereClause, args)
}
+4 -3
View File
@@ -57,9 +57,10 @@ type UpdateUserDTO struct {
}
type ListFilter struct {
Search string
Page int
Limit int
Search string
BranchIds []int64
Page int
Limit int
}
type ListUserDTO struct {
+14
View File
@@ -13,6 +13,7 @@ import (
"online-back/internal/appresult"
"online-back/internal/config"
"online-back/internal/identity/rbac"
"online-back/pkg/logging"
minioUpload "online-back/pkg/minio"
@@ -180,6 +181,9 @@ func (h *handler) getByID(c *gin.Context) {
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
return
}
if !rbac.GuardUser(c, item.Id) {
return
}
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
}
@@ -234,6 +238,10 @@ func (h *handler) update(c *gin.Context) {
return
}
if rbac.HasBranchScope(c) && !rbac.GuardUser(c, id) {
return
}
var dto UpdateUserDTO
if err := c.ShouldBindJSON(&dto); err != nil {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
@@ -273,6 +281,10 @@ func (h *handler) delete(c *gin.Context) {
return
}
if rbac.HasBranchScope(c) && !rbac.GuardUser(c, id) {
return
}
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
return
@@ -327,6 +339,8 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
filter.Search = raw
}
rbac.BindBranchIDs(c, &filter.BranchIds)
return filter, true
}