feat: user auth.
This commit is contained in:
@@ -2,6 +2,7 @@ package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"hr_receiver/config"
|
||||
"hr_receiver/models"
|
||||
"hr_receiver/mqtt"
|
||||
"hr_receiver/util"
|
||||
@@ -82,7 +83,21 @@ func (sc *SystemDebugController) MqttWebSocket(c *gin.Context) {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid token"})
|
||||
return
|
||||
}
|
||||
if claims.Role != models.UserRoleSuperAdmin {
|
||||
|
||||
var user models.User
|
||||
if err := config.DB.First(&user, claims.UserID).Error; err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "user not found"})
|
||||
return
|
||||
}
|
||||
if !user.IsActive {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "user is disabled"})
|
||||
return
|
||||
}
|
||||
if util.IsTokenRevoked(&user, claims) {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "token has been revoked"})
|
||||
return
|
||||
}
|
||||
if user.Role != models.UserRoleSuperAdmin {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "super admin required"})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user