fixes
This commit is contained in:
@@ -37,15 +37,6 @@ 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 {
|
||||
@@ -63,11 +54,6 @@ func (h *PositionsHandler) GetMyPositions(c *gin.Context) {
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -84,11 +70,6 @@ type upsertPositionRequest struct {
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -103,16 +84,38 @@ func (h *PositionsHandler) UpsertPosition(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) UpdatePosition(c *gin.Context) {
|
||||
var req struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description *string `json:"description"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Error(models.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.UpsertPositionForLocale(c.Request.Context(), c.Param("code"), localeFromRequest(c), req.Name, req.Description); err != nil {
|
||||
c.Error(models.ErrInternal)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) DeletePosition(c *gin.Context) {
|
||||
if err := h.svc.DeletePosition(c.Request.Context(), c.Param("code")); err != nil {
|
||||
c.Error(err)
|
||||
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)
|
||||
@@ -128,12 +131,7 @@ func (h *PositionsHandler) AddPermissionToPosition(c *gin.Context) {
|
||||
}
|
||||
|
||||
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 {
|
||||
if err := h.svc.RemovePermissionFromPosition(c.Request.Context(), c.Param("code"), c.Param("permissionCode")); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
@@ -142,11 +140,6 @@ func (h *PositionsHandler) RemovePermissionFromPosition(c *gin.Context) {
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -157,11 +150,6 @@ func (h *PositionsHandler) ListPermissions(c *gin.Context) {
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -189,11 +177,6 @@ func (h *PositionsHandler) AssignPosition(c *gin.Context) {
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -208,6 +191,50 @@ func (h *PositionsHandler) RevokePosition(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) CreatePermission(c *gin.Context) {
|
||||
var req struct {
|
||||
Code string `json:"code" binding:"required"`
|
||||
Description *string `json:"description"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Error(models.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.CreatePermission(c.Request.Context(), req.Code, req.Description); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) UpdatePermission(c *gin.Context) {
|
||||
var req struct {
|
||||
Description *string `json:"description"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Error(models.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.UpdatePermission(c.Request.Context(), c.Param("code"), req.Description); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *PositionsHandler) DeletePermission(c *gin.Context) {
|
||||
if err := h.svc.DeletePermission(c.Request.Context(), 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"`
|
||||
|
||||
@@ -22,6 +22,7 @@ func NewRouter(
|
||||
limiter *services.RateLimiter,
|
||||
) *gin.Engine {
|
||||
r := gin.Default()
|
||||
r.Use(middleware.LoggingMiddleware())
|
||||
r.Use(errorMiddleware())
|
||||
|
||||
o := r.Group("/oauth")
|
||||
@@ -57,15 +58,19 @@ func NewRouter(
|
||||
me.DELETE("/consent/:clientId", authMiddleware(svc, repo, "profile:consent:delete"), consents.RevokeAppAccess)
|
||||
me.GET("/positions", authMiddleware(svc, repo, "profile:read"), positions.GetMyPositions)
|
||||
|
||||
admin := api.Group("/admin", authMiddleware(svc, repo))
|
||||
admin := api.Group("/admin", authMiddleware(svc, repo), middleware.AdminOnly())
|
||||
{
|
||||
admin.GET("/permissions", positions.ListPermissions)
|
||||
admin.POST("/permissions", positions.CreatePermission)
|
||||
admin.PUT("/permissions/:code", positions.UpdatePermission)
|
||||
admin.DELETE("/permissions/:code", positions.DeletePermission)
|
||||
|
||||
admin.GET("/positions", positions.ListPositions)
|
||||
admin.POST("/positions", positions.UpsertPosition)
|
||||
|
||||
admin.PUT("/positions/:code", positions.UpdatePosition)
|
||||
admin.DELETE("/positions/:code", positions.DeletePosition)
|
||||
admin.POST("/positions/:code/permissions", positions.AddPermissionToPosition)
|
||||
admin.DELETE("/positions/:code/permissions/:permCode", positions.RemovePermissionFromPosition)
|
||||
admin.DELETE("/positions/:code/permissions/:permissionCode", positions.RemovePermissionFromPosition)
|
||||
|
||||
admin.POST("/users/:userId/positions", positions.AssignPosition)
|
||||
admin.DELETE("/users/:userId/positions/:code", positions.RevokePosition)
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/dusiburg/kotosoft-id/internal/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func AdminOnly() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
isAdmin, exists := c.Get("is_admin")
|
||||
if !exists || !isAdmin.(bool) {
|
||||
c.Error(models.ErrForbidden.WithMessage("Admin role required"))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type RequestLogger struct {
|
||||
RequestID string `json:"request_id"`
|
||||
Method string `json:"method"`
|
||||
Path string `json:"path"`
|
||||
Status int `json:"status"`
|
||||
Duration time.Duration `json:"duration_ms"`
|
||||
UserID string `json:"user_id,omitempty"`
|
||||
ClientIP string `json:"client_ip"`
|
||||
UserAgent string `json:"user_agent"`
|
||||
Errors []string `json:"errors,omitempty"`
|
||||
Extra map[string]any `json:"extra,omitempty"`
|
||||
}
|
||||
|
||||
func LoggingMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
requestID := uuid.New().String()
|
||||
|
||||
c.Set("request_id", requestID)
|
||||
|
||||
c.Next()
|
||||
|
||||
duration := time.Since(start)
|
||||
|
||||
userID, _ := c.Get("user_id")
|
||||
userIDStr := ""
|
||||
if uid, ok := userID.(uuid.UUID); ok {
|
||||
userIDStr = uid.String()
|
||||
}
|
||||
|
||||
var errors []string
|
||||
for _, err := range c.Errors {
|
||||
errors = append(errors, err.Error())
|
||||
}
|
||||
|
||||
entry := RequestLogger{
|
||||
RequestID: requestID,
|
||||
Method: c.Request.Method,
|
||||
Path: c.Request.URL.Path,
|
||||
Status: c.Writer.Status(),
|
||||
Duration: duration,
|
||||
UserID: userIDStr,
|
||||
ClientIP: c.ClientIP(),
|
||||
UserAgent: c.Request.UserAgent(),
|
||||
Errors: errors,
|
||||
}
|
||||
|
||||
if logData, err := json.Marshal(entry); err == nil {
|
||||
log.Println(string(logData))
|
||||
} else {
|
||||
log.Printf("failed to marshal log entry: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func RequestIDFromContext(c *gin.Context) string {
|
||||
if rid, exists := c.Get("request_id"); exists {
|
||||
if ridStr, ok := rid.(string); ok {
|
||||
return ridStr
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -39,6 +39,26 @@ func (r *PositionRepository) GetAllPermissions(ctx context.Context) ([]models.Pe
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func (r *PositionRepository) CreatePermission(ctx context.Context, code string, description *string) error {
|
||||
_, err := r.db.Exec(ctx, `
|
||||
INSERT INTO permissions (code, description)
|
||||
VALUES ($1, $2)
|
||||
`, code, description)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PositionRepository) UpdatePermission(ctx context.Context, code string, description *string) error {
|
||||
_, err := r.db.Exec(ctx, `
|
||||
UPDATE permissions SET description = $2 WHERE code = $1
|
||||
`, code, description)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PositionRepository) DeletePermission(ctx context.Context, code string) error {
|
||||
_, err := r.db.Exec(ctx, `DELETE FROM permissions WHERE code = $1`, code)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetPermissionByCode(ctx context.Context, code string) (*models.Permission, error) {
|
||||
var p models.Permission
|
||||
err := r.db.QueryRow(ctx, `
|
||||
@@ -208,6 +228,11 @@ func (r *PositionRepository) UpsertPositionForLocale(ctx context.Context, code,
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (r *PositionRepository) DeletePositionByCode(ctx context.Context, code string) error {
|
||||
_, err := r.db.Exec(ctx, `DELETE FROM positions WHERE code = $1`, code)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PositionRepository) AssignPosition(ctx context.Context, userID, positionID uuid.UUID, assignedBy *uuid.UUID, validFrom interface{}, validTo *interface{}) error {
|
||||
_, err := r.db.Exec(ctx, `
|
||||
INSERT INTO user_positions (user_id, position_id, assigned_by, valid_from, valid_to)
|
||||
|
||||
@@ -61,6 +61,10 @@ func (s *Service) UpsertPositionForLocale(ctx context.Context, code, locale, nam
|
||||
return s.positions.UpsertPositionForLocale(ctx, code, locale, name, description)
|
||||
}
|
||||
|
||||
func (s *Service) DeletePosition(ctx context.Context, code string) error {
|
||||
return s.positions.DeletePositionByCode(ctx, code)
|
||||
}
|
||||
|
||||
func (s *Service) AssignPosition(ctx context.Context, adminID, targetUserID uuid.UUID, input models.AssignPositionInput) error {
|
||||
position, err := s.positions.GetPositionByCode(ctx, input.PositionCode)
|
||||
if err != nil {
|
||||
@@ -84,6 +88,18 @@ func (s *Service) RevokePosition(ctx context.Context, targetUserID uuid.UUID, po
|
||||
return s.positions.RevokePosition(ctx, targetUserID, position.ID)
|
||||
}
|
||||
|
||||
func (s *Service) CreatePermission(ctx context.Context, code string, description *string) error {
|
||||
return s.positions.CreatePermission(ctx, code, description)
|
||||
}
|
||||
|
||||
func (s *Service) UpdatePermission(ctx context.Context, code string, description *string) error {
|
||||
return s.positions.UpdatePermission(ctx, code, description)
|
||||
}
|
||||
|
||||
func (s *Service) DeletePermission(ctx context.Context, code string) error {
|
||||
return s.positions.DeletePermission(ctx, code)
|
||||
}
|
||||
|
||||
func (s *Service) UserHasPermission(ctx context.Context, userID uuid.UUID, permissionCode string) (bool, error) {
|
||||
allowed, _, err := s.positions.UserHasPermission(ctx, userID, permissionCode)
|
||||
return allowed, err
|
||||
|
||||
@@ -63,6 +63,11 @@ type PositionRepo interface {
|
||||
|
||||
UserHasPermission(ctx context.Context, userID uuid.UUID, permissionCode string) (bool, string, error)
|
||||
GetUserPermissions(ctx context.Context, userID uuid.UUID) ([]models.Permission, error)
|
||||
|
||||
CreatePermission(ctx context.Context, code string, description *string) error
|
||||
UpdatePermission(ctx context.Context, code string, description *string) error
|
||||
DeletePositionByCode(ctx context.Context, code string) error
|
||||
DeletePermission(ctx context.Context, code string) error
|
||||
}
|
||||
|
||||
type TokenConfig struct {
|
||||
|
||||
Reference in New Issue
Block a user