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:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user