rm -rf permission module
This commit is contained in:
1 parent
c6b9cb7462
commit
848a07324f
10 files changed
+193
-119
No files matched your search
@@ -1,12 +1,16 @@
|
||||
package permissionrole
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
const baseURL = "/permission-roles"
|
||||
@@ -22,24 +26,37 @@ func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler
|
||||
|
||||
func (h *handler) Register(router *gin.RouterGroup) {
|
||||
group := router.Group(baseURL, appresult.JwtTokenCheck())
|
||||
group.POST("", h.create)
|
||||
group.POST("/bulk", h.setPermissions)
|
||||
group.DELETE("/:id", h.delete)
|
||||
}
|
||||
|
||||
// create godoc
|
||||
// @Summary Assign permission to role
|
||||
// @Description Creates a permission-role assignment, or updates the existing pair if already assigned
|
||||
// setPermissions godoc
|
||||
// @Summary Set a role's permissions
|
||||
// @Description Replaces roleId's whole permission set with permissionIds - pairs not listed are removed, missing ones are added, an empty list clears all permissions from the role
|
||||
// @Tags permission-roles
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param body body CreatePermissionRoleDTO true "Permission-role payload"
|
||||
// @Param body body SetRolePermissionsDTO true "Role + the permission IDs it should have"
|
||||
// @Success 201 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /permission-roles [post]
|
||||
func (h *handler) create(c *gin.Context) {
|
||||
shared.CreateAssoc[CreatePermissionRoleDTO, PermissionRoleDTO](c, h.logger, h.repository, "permission role")
|
||||
// @Router /permission-roles/bulk [post]
|
||||
func (h *handler) setPermissions(c *gin.Context) {
|
||||
var dto SetRolePermissionsDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
items, err := h.repository.SetPermissions(c.Request.Context(), dto)
|
||||
if err != nil {
|
||||
h.logger.Errorf("set role permissions failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, appresult.SuccessWithData(items))
|
||||
}
|
||||
|
||||
// delete godoc
|
||||
@@ -55,5 +72,20 @@ func (h *handler) create(c *gin.Context) {
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /permission-roles/{id} [delete]
|
||||
func (h *handler) delete(c *gin.Context) {
|
||||
shared.DeleteAssoc[CreatePermissionRoleDTO, PermissionRoleDTO](c, h.logger, h.repository, "permission role")
|
||||
id, ok := shared.ParseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
} else if err != nil {
|
||||
h.logger.Errorf("delete permission role failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData("permission role removed"))
|
||||
}
|
||||
Reference in new issue
Block a user