changed role permission
This commit is contained in:
1 parent
f0ecf23879
commit
323e131216
74 files changed
+2915
-4713
No files matched your search
@@ -8,6 +8,8 @@ import (
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/identity/rbac/perm"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
@@ -27,7 +29,7 @@ func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler
|
||||
}
|
||||
|
||||
func (h *handler) Register(router *gin.RouterGroup) {
|
||||
group := router.Group(baseURL, appresult.JwtTokenCheck())
|
||||
group := rbac.Group(router, baseURL, rbac.RequireStaff(perm.Roles))
|
||||
group.GET("", h.list)
|
||||
group.GET("/:id", h.getByID)
|
||||
group.POST("", h.create)
|
||||
@@ -41,7 +43,7 @@ func (h *handler) Register(router *gin.RouterGroup) {
|
||||
// @Tags role
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param role_name query string false "Filter by name"
|
||||
// @Param role_name query string false "Filter by name or code"
|
||||
// @Param page query int false "Page number" default(1)
|
||||
// @Param limit query int false "Items per page" default(10)
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
@@ -100,7 +102,7 @@ func (h *handler) getByID(c *gin.Context) {
|
||||
|
||||
// create godoc
|
||||
// @Summary Create role
|
||||
// @Description Creates a new role
|
||||
// @Description Creates a role with a scope (own, branch, all) and its permission codes. The caller cannot create a role wider than their own scope or holding permissions they lack.
|
||||
// @Tags role
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -108,6 +110,8 @@ func (h *handler) getByID(c *gin.Context) {
|
||||
// @Param body body CreateRoleDTO true "Role payload"
|
||||
// @Success 201 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 403 {object} appresult.AppError
|
||||
// @Failure 409 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /roles [post]
|
||||
func (h *handler) create(c *gin.Context) {
|
||||
@@ -116,8 +120,19 @@ func (h *handler) create(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
dto.Code = strings.TrimSpace(dto.Code)
|
||||
dto.Permissions = dedupe(dto.Permissions)
|
||||
|
||||
next := rbac.RoleInfo{Code: dto.Code, Scope: dto.Scope, Perms: dto.Permissions}
|
||||
if !h.authorize(c, nil, next) {
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.Create(c.Request.Context(), dto)
|
||||
if errors.Is(err, ErrDuplicateCode) {
|
||||
c.JSON(http.StatusConflict, appresult.ErrDuplicate)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
h.logger.Errorf("create role failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
@@ -129,7 +144,7 @@ func (h *handler) create(c *gin.Context) {
|
||||
|
||||
// update godoc
|
||||
// @Summary Update role
|
||||
// @Description Updates an existing role
|
||||
// @Description Replaces the role's name, scope and whole permission set. System roles keep their scope; the caller must be able to grant the role both before and after the change.
|
||||
// @Tags role
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -138,6 +153,7 @@ func (h *handler) create(c *gin.Context) {
|
||||
// @Param body body UpdateRoleDTO true "Role payload"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 403 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /roles/{id} [put]
|
||||
@@ -153,6 +169,17 @@ func (h *handler) update(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
dto.Permissions = dedupe(dto.Permissions)
|
||||
|
||||
existing, ok := h.load(c, id)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
current := existing.Info()
|
||||
next := rbac.RoleInfo{ID: id, Code: existing.Code, Scope: dto.Scope, IsSystem: existing.IsSystem, Perms: dto.Permissions}
|
||||
if !h.authorize(c, ¤t, next) {
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.Update(c.Request.Context(), id, dto)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
@@ -170,14 +197,16 @@ func (h *handler) update(c *gin.Context) {
|
||||
|
||||
// delete godoc
|
||||
// @Summary Delete role
|
||||
// @Description Deletes a role
|
||||
// @Description Deletes a role. System roles and roles still assigned to users cannot be deleted.
|
||||
// @Tags role
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Role ID"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 403 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 409 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /roles/{id} [delete]
|
||||
func (h *handler) delete(c *gin.Context) {
|
||||
@@ -187,7 +216,24 @@ func (h *handler) delete(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
||||
existing, ok := h.load(c, id)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if existing.IsSystem {
|
||||
c.JSON(http.StatusConflict, appresult.ErrSystemRole)
|
||||
return
|
||||
}
|
||||
if err := rbac.PrincipalFrom(c).CanGrant(existing.Info()); err != nil {
|
||||
h.logger.Warnf("role delete denied: %v", err)
|
||||
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, ErrInUse) {
|
||||
c.JSON(http.StatusConflict, appresult.ErrRoleInUse)
|
||||
return
|
||||
} else if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
} else if err != nil {
|
||||
@@ -199,6 +245,49 @@ func (h *handler) delete(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData("role removed"))
|
||||
}
|
||||
|
||||
func (h *handler) load(c *gin.Context, id int64) (RoleDTO, bool) {
|
||||
item, err := h.repository.GetByID(c.Request.Context(), id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return RoleDTO{}, false
|
||||
}
|
||||
if err != nil {
|
||||
h.logger.Errorf("get role failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return RoleDTO{}, false
|
||||
}
|
||||
return item, true
|
||||
}
|
||||
|
||||
func (h *handler) authorize(c *gin.Context, current *rbac.RoleInfo, next rbac.RoleInfo) bool {
|
||||
err := rbac.PrincipalFrom(c).CanSaveRole(current, next)
|
||||
switch {
|
||||
case err == nil:
|
||||
return true
|
||||
case errors.Is(err, rbac.ErrUnknownPerm), errors.Is(err, rbac.ErrInvalidScope):
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
case errors.Is(err, rbac.ErrSystemRole):
|
||||
c.JSON(http.StatusConflict, appresult.ErrSystemRole)
|
||||
default:
|
||||
h.logger.Warnf("role save denied: %v", err)
|
||||
c.JSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func dedupe(codes []perm.Code) []perm.Code {
|
||||
seen := make(map[perm.Code]struct{}, len(codes))
|
||||
out := make([]perm.Code, 0, len(codes))
|
||||
for _, code := range codes {
|
||||
if _, ok := seen[code]; ok {
|
||||
continue
|
||||
}
|
||||
seen[code] = struct{}{}
|
||||
out = append(out, code)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter := ListFilter{
|
||||
Page: shared.DefaultPage,
|
||||
|
||||
Reference in new issue
Block a user