chore: build
This commit is contained in:
@@ -14,6 +14,7 @@ func (User) TableName() string { return "users" }
|
||||
type UserStore interface {
|
||||
ByUsername(username string) (User, bool)
|
||||
Create(username, passwordHash string, role Role) (User, error)
|
||||
Delete(id string) error
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
@@ -21,8 +22,15 @@ type Handler struct {
|
||||
sessions session.Manager
|
||||
iss *Issuer
|
||||
secure bool // cookie Secure (activé en prod/HTTPS)
|
||||
// cleanup : nettoyage lancé en tâche de fond après la suppression d'un
|
||||
// compte (démos, sites, données liées au pseudo). Voir SetAccountCleanup.
|
||||
cleanup func(username string)
|
||||
}
|
||||
|
||||
// SetAccountCleanup branche le nettoyage des ressources d'un compte supprimé
|
||||
// (le paquet auth ne connaît ni les démos ni les projets).
|
||||
func (h *Handler) SetAccountCleanup(fn func(username string)) { h.cleanup = fn }
|
||||
|
||||
func NewHandler(users UserStore, sessions session.Manager, iss *Issuer, secure bool) *Handler {
|
||||
return &Handler{users: users, sessions: sessions, iss: iss, secure: secure}
|
||||
}
|
||||
@@ -178,6 +186,49 @@ func (h *Handler) Logout(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"status": "déconnecté"})
|
||||
}
|
||||
|
||||
type deleteAccountRequest struct {
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
// DeleteAccount : DELETE /profile/account — le client supprime lui-même son
|
||||
// compte, après avoir confirmé son mot de passe. Le compte et la session sont
|
||||
// supprimés immédiatement ; ses démos, plateformes et sites sont détruits en
|
||||
// tâche de fond (la destruction des namespaces peut prendre du temps).
|
||||
func (h *Handler) DeleteAccount(c *gin.Context) {
|
||||
var req deleteAccountRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "mot de passe requis"})
|
||||
return
|
||||
}
|
||||
p := PrincipalFrom(c)
|
||||
if p == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
||||
return
|
||||
}
|
||||
user, found := h.users.ByUsername(p.Username)
|
||||
if !found || user.ID != p.UserID {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
||||
return
|
||||
}
|
||||
ok, err := VerifyPassword(req.Password, user.PasswordHash)
|
||||
if err != nil || !ok {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "mot de passe incorrect"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.users.Delete(user.ID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "erreur serveur"})
|
||||
return
|
||||
}
|
||||
_ = h.sessions.Delete(c.Request.Context(), p.SessionID)
|
||||
h.setSessionCookie(c, "", -1)
|
||||
|
||||
if h.cleanup != nil {
|
||||
go h.cleanup(user.Username)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"status": "compte supprimé"})
|
||||
}
|
||||
|
||||
func (h *Handler) setSessionCookie(c *gin.Context, value string, maxAge int) {
|
||||
http.SetCookie(c.Writer, &http.Cookie{
|
||||
Name: session.CookieName,
|
||||
|
||||
Reference in New Issue
Block a user