92 lines
2.8 KiB
Go
92 lines
2.8 KiB
Go
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"
|
|
|
|
type handler struct {
|
|
logger *logging.Logger
|
|
repository Repository
|
|
}
|
|
|
|
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
|
|
return &handler{logger: logger, repository: repository}
|
|
}
|
|
|
|
func (h *handler) Register(router *gin.RouterGroup) {
|
|
group := router.Group(baseURL, appresult.JwtTokenCheck())
|
|
group.POST("/bulk", h.setPermissions)
|
|
group.DELETE("/:id", h.delete)
|
|
}
|
|
|
|
// 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 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/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
|
|
// @Summary Remove permission from role
|
|
// @Description Deletes a permission-role assignment
|
|
// @Tags permission-roles
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Permission-role ID"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /permission-roles/{id} [delete]
|
|
func (h *handler) delete(c *gin.Context) {
|
|
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"))
|
|
}
|