244 lines
5.2 KiB
Go
244 lines
5.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/dusiburg/kotosoft-id/internal/models"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/dusiburg/kotosoft-id/internal/services"
|
|
)
|
|
|
|
type PositionsHandler struct {
|
|
svc *services.Service
|
|
}
|
|
|
|
func NewPositionsHandler(svc *services.Service) *PositionsHandler {
|
|
return &PositionsHandler{svc: svc}
|
|
}
|
|
|
|
func localeFromRequest(c *gin.Context) string {
|
|
if l := c.GetHeader("X-Locale"); l != "" {
|
|
return l
|
|
}
|
|
if l := c.GetHeader("Accept-Language"); l != "" {
|
|
return l
|
|
}
|
|
return "en"
|
|
}
|
|
|
|
func userIDFromContext(c *gin.Context) (uuid.UUID, bool) {
|
|
v, ok := c.Get("user_id")
|
|
if !ok {
|
|
return uuid.Nil, false
|
|
}
|
|
id, ok := v.(uuid.UUID)
|
|
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 {
|
|
c.Error(models.ErrUnauthorized)
|
|
return
|
|
}
|
|
|
|
positions, err := h.svc.GetUserPositions(c.Request.Context(), userID, localeFromRequest(c))
|
|
if err != nil {
|
|
c.Error(models.ErrInternal)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, positions)
|
|
}
|
|
|
|
func (h *PositionsHandler) ListPositions(c *gin.Context) {
|
|
if !isAdminFromContext(c) {
|
|
c.Error(models.ErrForbidden)
|
|
return
|
|
}
|
|
|
|
positions, err := h.svc.GetAllPositions(c.Request.Context(), localeFromRequest(c))
|
|
if err != nil {
|
|
c.Error(models.ErrInternal)
|
|
return
|
|
}
|
|
|
|
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) UpsertPosition(c *gin.Context) {
|
|
if !isAdminFromContext(c) {
|
|
c.Error(models.ErrForbidden)
|
|
return
|
|
}
|
|
|
|
var req upsertPositionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.Error(models.ErrBadRequest)
|
|
return
|
|
}
|
|
|
|
if err := h.svc.UpsertPositionForLocale(c.Request.Context(), req.Code, localeFromRequest(c), req.Name, req.Description); err != nil {
|
|
c.Error(models.ErrInternal)
|
|
return
|
|
}
|
|
|
|
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)
|
|
}
|