176 lines
4.6 KiB
Go
176 lines
4.6 KiB
Go
package profile
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/omnex/control-plane/api/internal/auth"
|
|
)
|
|
|
|
type Handler struct {
|
|
store Store
|
|
}
|
|
|
|
func NewHandler(store Store) *Handler {
|
|
return &Handler{store: store}
|
|
}
|
|
|
|
type newUsername struct {
|
|
Username string `json:"username" binding:"required,min=2,max=120"`
|
|
}
|
|
|
|
type newPassword struct {
|
|
Password string `json:"password" binding:"required,min=8"`
|
|
}
|
|
|
|
type newTelegram struct {
|
|
Telegram string `json:"telegram" binding:"required,min=3,max=64"`
|
|
}
|
|
|
|
type alertSettingsRequest struct {
|
|
DiscordWebhookURL string `json:"discord_webhook_url" binding:"omitempty,max=500,url"`
|
|
TelegramBotToken string `json:"telegram_bot_token" binding:"omitempty,max=200"`
|
|
TelegramChatID string `json:"telegram_chat_id" binding:"omitempty,max=64"`
|
|
}
|
|
|
|
type alertSettingsResponse struct {
|
|
DiscordWebhookURL string `json:"discord_webhook_url"`
|
|
TelegramBotToken string `json:"telegram_bot_token"`
|
|
TelegramChatID string `json:"telegram_chat_id"`
|
|
}
|
|
|
|
func toAlertSettingsResponse(user auth.User) alertSettingsResponse {
|
|
deref := func(s *string) string {
|
|
if s == nil {
|
|
return ""
|
|
}
|
|
return *s
|
|
}
|
|
return alertSettingsResponse{
|
|
DiscordWebhookURL: deref(user.AlertDiscordWebhookURL),
|
|
TelegramBotToken: deref(user.AlertTelegramBotToken),
|
|
TelegramChatID: deref(user.AlertTelegramChatID),
|
|
}
|
|
}
|
|
|
|
func (h *Handler) UpdateUsernameById(c *gin.Context) {
|
|
var u newUsername
|
|
|
|
if err := c.ShouldBindJSON(&u); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
|
return
|
|
}
|
|
|
|
p := auth.PrincipalFrom(c)
|
|
if p == nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
|
return
|
|
}
|
|
|
|
user, err := h.store.UpdateUsername(p.UserID, u.Username)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "erreur serveur"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, user)
|
|
}
|
|
|
|
func (h *Handler) UpdatePasswordById(c *gin.Context) {
|
|
var u newPassword
|
|
if err := c.ShouldBindJSON(&u); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
|
return
|
|
}
|
|
|
|
p := auth.PrincipalFrom(c)
|
|
if p == nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
|
return
|
|
}
|
|
|
|
hash, err := auth.HashPassword(u.Password)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "erreur serveur"})
|
|
return
|
|
}
|
|
|
|
user, err := h.store.UpdatePassword(p.UserID, hash)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, user)
|
|
}
|
|
|
|
func (h *Handler) GetTelegramById(c *gin.Context) {
|
|
p := auth.PrincipalFrom(c)
|
|
if p == nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
|
return
|
|
}
|
|
user, err := h.store.GetTelegram(p.UserID)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"telegram": user.Telegram})
|
|
}
|
|
|
|
func (h *Handler) SetTelegramById(c *gin.Context) {
|
|
var t newTelegram
|
|
if err := c.ShouldBindJSON(&t); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
|
return
|
|
}
|
|
p := auth.PrincipalFrom(c)
|
|
if p == nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
|
return
|
|
}
|
|
user, err := h.store.SetTelegram(p.UserID, t.Telegram)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "erreur serveur"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"telegram": user.Telegram})
|
|
}
|
|
|
|
// GetAlerts : réglages d'alerte monitoring (webhook Discord / bot Telegram)
|
|
// de l'admin authentifié — chacun configure les siens, rien de partagé.
|
|
func (h *Handler) GetAlerts(c *gin.Context) {
|
|
p := auth.PrincipalFrom(c)
|
|
if p == nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
|
return
|
|
}
|
|
user, err := h.store.GetAlertSettings(p.UserID)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, toAlertSettingsResponse(user))
|
|
}
|
|
|
|
// SetAlerts : enregistre les réglages d'alerte de l'admin authentifié. Un
|
|
// champ vide désactive le canal correspondant.
|
|
func (h *Handler) SetAlerts(c *gin.Context) {
|
|
var req alertSettingsRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
|
return
|
|
}
|
|
p := auth.PrincipalFrom(c)
|
|
if p == nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
|
return
|
|
}
|
|
user, err := h.store.SetAlertSettings(p.UserID, req.DiscordWebhookURL, req.TelegramBotToken, req.TelegramChatID)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "erreur serveur"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, toAlertSettingsResponse(user))
|
|
}
|