fix
This commit is contained in:
@@ -4,9 +4,10 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/models"
|
||||
"github.com/dusiburg/kotosoft-id/internal/services"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/services"
|
||||
)
|
||||
|
||||
type PositionsHandler struct {
|
||||
@@ -24,7 +25,7 @@ func localeFromRequest(c *gin.Context) string {
|
||||
if l := c.GetHeader("Accept-Language"); l != "" {
|
||||
return l
|
||||
}
|
||||
return "ru"
|
||||
return "en"
|
||||
}
|
||||
|
||||
func userIDFromContext(c *gin.Context) (uuid.UUID, bool) {
|
||||
@@ -36,6 +37,15 @@ func userIDFromContext(c *gin.Context) (uuid.UUID, bool) {
|
||||
return id, ok
|
||||
}
|
||||
|
||||
func isAdminFromContext(c *gin.Context) bool {
|
||||
v, ok := c.Get("is_admin")
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
b, ok := v.(bool)
|
||||
return ok && b
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) GetMyPositions(c *gin.Context) {
|
||||
userID, ok := userIDFromContext(c)
|
||||
if !ok {
|
||||
@@ -52,25 +62,8 @@ func (h *PositionsHandler) GetMyPositions(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, positions)
|
||||
}
|
||||
|
||||
type upsertPositionRequest struct {
|
||||
Code string `json:"code" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description *string `json:"description"`
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) ListPositions(c *gin.Context) {
|
||||
userID, ok := userIDFromContext(c)
|
||||
if !ok {
|
||||
c.Error(models.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
isAdmin, err := h.svc.UserIsPositionsAdmin(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
c.Error(models.ErrInternal)
|
||||
return
|
||||
}
|
||||
if !isAdmin {
|
||||
if !isAdminFromContext(c) {
|
||||
c.Error(models.ErrForbidden)
|
||||
return
|
||||
}
|
||||
@@ -84,19 +77,14 @@ func (h *PositionsHandler) ListPositions(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, positions)
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) UpsertPosition(c *gin.Context) {
|
||||
userID, ok := userIDFromContext(c)
|
||||
if !ok {
|
||||
c.Error(models.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
type upsertPositionRequest struct {
|
||||
Code string `json:"code" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description *string `json:"description"`
|
||||
}
|
||||
|
||||
isAdmin, err := h.svc.UserIsPositionsAdmin(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
c.Error(models.ErrInternal)
|
||||
return
|
||||
}
|
||||
if !isAdmin {
|
||||
func (h *PositionsHandler) UpsertPosition(c *gin.Context) {
|
||||
if !isAdminFromContext(c) {
|
||||
c.Error(models.ErrForbidden)
|
||||
return
|
||||
}
|
||||
@@ -115,3 +103,141 @@ func (h *PositionsHandler) UpsertPosition(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
type permissionCodeRequest struct {
|
||||
PermissionCode string `json:"permission_code" binding:"required"`
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) AddPermissionToPosition(c *gin.Context) {
|
||||
if !isAdminFromContext(c) {
|
||||
c.Error(models.ErrForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
var req permissionCodeRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Error(models.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.AddPermissionToPosition(c.Request.Context(), c.Param("code"), req.PermissionCode); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) RemovePermissionFromPosition(c *gin.Context) {
|
||||
if !isAdminFromContext(c) {
|
||||
c.Error(models.ErrForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.RemovePermissionFromPosition(c.Request.Context(), c.Param("code"), c.Param("permCode")); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) ListPermissions(c *gin.Context) {
|
||||
if !isAdminFromContext(c) {
|
||||
c.Error(models.ErrForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
perms, err := h.svc.GetAllPermissions(c.Request.Context())
|
||||
if err != nil {
|
||||
c.Error(models.ErrInternal)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, perms)
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) AssignPosition(c *gin.Context) {
|
||||
if !isAdminFromContext(c) {
|
||||
c.Error(models.ErrForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
adminID, ok := userIDFromContext(c)
|
||||
if !ok {
|
||||
c.Error(models.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
targetUserID, err := uuid.Parse(c.Param("userId"))
|
||||
if err != nil {
|
||||
c.Error(models.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var input models.AssignPositionInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.Error(models.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.AssignPosition(c.Request.Context(), adminID, targetUserID, input); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) RevokePosition(c *gin.Context) {
|
||||
if !isAdminFromContext(c) {
|
||||
c.Error(models.ErrForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
targetUserID, err := uuid.Parse(c.Param("userId"))
|
||||
if err != nil {
|
||||
c.Error(models.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.RevokePosition(c.Request.Context(), targetUserID, c.Param("code")); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) Introspect(c *gin.Context) {
|
||||
var req struct {
|
||||
Token string `json:"token" form:"token" binding:"required"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing token"})
|
||||
return
|
||||
}
|
||||
|
||||
id, secret, ok := c.Request.BasicAuth()
|
||||
if !ok {
|
||||
c.Header("WWW-Authenticate", `Basic realm="oauth"`)
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid_client"})
|
||||
return
|
||||
}
|
||||
if _, err := h.svc.AuthenticateClient(c.Request.Context(), services.ClientAuth{
|
||||
ClientID: id,
|
||||
ClientSecret: secret,
|
||||
}); err != nil {
|
||||
c.Header("WWW-Authenticate", `Basic realm="oauth"`)
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid_client"})
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.svc.IntrospectToken(c.Request.Context(), req.Token)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "server_error"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user