ch some
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

This commit is contained in:
2026-09-04 19:33:26 +05:00
parent 28509eb4ca
commit d1a6fcaadc
102 changed files with 2089 additions and 1957 deletions

View File

@@ -14,6 +14,7 @@ import (
"online-back/internal/appresult"
"online-back/internal/config"
"online-back/internal/identity/rbac"
"online-back/internal/shared"
"online-back/pkg/logging"
minioUpload "online-back/pkg/minio"
@@ -79,6 +80,7 @@ func (h *handler) list(c *gin.Context) {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
rbac.BindBranchIDs(c, &filter.BranchIds)
result, err := h.repository.List(c.Request.Context(), filter)
if err != nil {
@@ -141,6 +143,7 @@ func (h *handler) listStaff(c *gin.Context) {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
rbac.BindBranchIDs(c, &filter.BranchIds)
result, err := h.repository.ListStaff(c.Request.Context(), filter)
if err != nil {
@@ -154,7 +157,7 @@ func (h *handler) listStaff(c *gin.Context) {
// getByID godoc
// @Summary Get user by ID
// @Description Returns a single user with assigned branches and roleId
// @Description Returns a single user with assigned branches, roleId, and effective permissions
// @Tags user
// @Produce json
// @Security BearerAuth
@@ -165,7 +168,7 @@ func (h *handler) listStaff(c *gin.Context) {
// @Failure 500 {object} appresult.AppError
// @Router /users/{id} [get]
func (h *handler) getByID(c *gin.Context) {
id, ok := parseID(c.Param("id"))
id, ok := shared.ParseID(c.Param("id"))
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
@@ -207,6 +210,15 @@ func (h *handler) create(c *gin.Context) {
return
}
if allowed, err := h.roleAssignmentAllowed(c, dto.Role); err != nil {
h.logger.Errorf("resolve role guard failed: %v", err)
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
return
} else if !allowed {
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
return
}
item, err := h.repository.Create(c.Request.Context(), dto)
if err != nil {
h.logger.Errorf("create user failed: %v", err)
@@ -232,7 +244,7 @@ func (h *handler) create(c *gin.Context) {
// @Failure 500 {object} appresult.AppError
// @Router /users/{id} [put]
func (h *handler) update(c *gin.Context) {
id, ok := parseID(c.Param("id"))
id, ok := shared.ParseID(c.Param("id"))
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
@@ -248,6 +260,15 @@ func (h *handler) update(c *gin.Context) {
return
}
if allowed, err := h.roleAssignmentAllowed(c, dto.Role); err != nil {
h.logger.Errorf("resolve role guard failed: %v", err)
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
return
} else if !allowed {
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
return
}
item, err := h.repository.Update(c.Request.Context(), id, dto)
if errors.Is(err, pgx.ErrNoRows) {
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
@@ -275,7 +296,7 @@ func (h *handler) update(c *gin.Context) {
// @Failure 500 {object} appresult.AppError
// @Router /users/{id} [delete]
func (h *handler) delete(c *gin.Context) {
id, ok := parseID(c.Param("id"))
id, ok := shared.ParseID(c.Param("id"))
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
@@ -297,24 +318,31 @@ func (h *handler) delete(c *gin.Context) {
c.JSON(http.StatusOK, appresult.SuccessWithData("user removed"))
}
func parseID(raw string) (int64, bool) {
id, err := strconv.ParseInt(raw, 10, 64)
if err != nil || id <= 0 {
return 0, false
// roleAssignmentAllowed asks "would granting this role let the target run
// requests as guardAdmin?" using the role's real guard_name, the same input
// rbac.Guard() resolves on every request - not a hardcoded list of role
// names, which would drift the moment a new role is added. Without this, an
// operator holding users.manage could set role:"admin" on themselves via
// this same endpoint and escape branch scoping entirely.
func (h *handler) roleAssignmentAllowed(c *gin.Context, role string) (bool, error) {
if rbac.IsAdminGuard(c) {
return true, nil
}
return id, true
code := strings.ToLower(strings.TrimSpace(role))
if code == "" {
code = "user"
}
guardName, err := h.repository.RoleGuardName(c.Request.Context(), code)
if err != nil {
return false, err
}
return rbac.EffectiveGuard(code, guardName) != rbac.GuardAdmin, nil
}
const (
defaultPage = 1
defaultLimit = 10
maxLimit = 100
)
func parseListFilter(c *gin.Context) (ListFilter, bool) {
filter := ListFilter{
Page: defaultPage,
Limit: defaultLimit,
Page: shared.DefaultPage,
Limit: shared.DefaultLimit,
}
if raw := c.Query("page"); raw != "" {
@@ -327,7 +355,7 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
if raw := c.Query("limit"); raw != "" {
limit, err := strconv.Atoi(raw)
if err != nil || limit < 1 || limit > maxLimit {
if err != nil || limit < 1 || limit > shared.MaxLimit {
return ListFilter{}, false
}
filter.Limit = limit
@@ -339,8 +367,6 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
filter.Search = raw
}
rbac.BindBranchIDs(c, &filter.BranchIds)
return filter, true
}