-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathbase_handler.go
More file actions
73 lines (61 loc) · 1.94 KB
/
Copy pathbase_handler.go
File metadata and controls
73 lines (61 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Package api provides domain-based REST API handlers
package api
import (
"github.com/gin-gonic/gin"
"github.com/guidewire-oss/fern-platform/pkg/logging"
)
// BaseHandler provides common functionality for all handlers
type BaseHandler struct {
logger *logging.Logger
}
// NewBaseHandler creates a new base handler
func NewBaseHandler(logger *logging.Logger) *BaseHandler {
return &BaseHandler{
logger: logger,
}
}
// respondWithError sends an error response
func (h *BaseHandler) respondWithError(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{"error": message})
}
// respondWithJSON sends a JSON response
func (h *BaseHandler) respondWithJSON(c *gin.Context, code int, payload interface{}) {
c.JSON(code, payload)
}
// getUserID extracts the user ID from the context
func (h *BaseHandler) getUserID(c *gin.Context) string {
userID, _ := c.Get("user_id")
s, _ := userID.(string)
return s
}
// getTeamID extracts the team ID from the context
func (h *BaseHandler) getTeamID(c *gin.Context) string {
teamID, _ := c.Get("team_id")
s, _ := teamID.(string)
return s
}
// getUserRole extracts the user role from the context
func (h *BaseHandler) getUserRole(c *gin.Context) string {
role, _ := c.Get("user_role")
s, _ := role.(string)
return s
}
// isAdmin checks if the user has admin role
func (h *BaseHandler) isAdmin(c *gin.Context) bool {
return h.getUserRole(c) == "admin"
}
// isManager checks if the user has manager role
func (h *BaseHandler) isManager(c *gin.Context) bool {
role := h.getUserRole(c)
return role == "admin" || role == "manager"
}
// getUserEmail extracts the user email from the context
func (h *BaseHandler) getUserEmail(c *gin.Context) string {
email, _ := c.Get("user_email")
s, _ := email.(string)
return s
}
// ErrorResponse sends an error response with the given status code and message
func (h *BaseHandler) ErrorResponse(c *gin.Context, code int, message string) {
h.respondWithError(c, code, message)
}