mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 15:53:42 +08:00 
			
		
		
		
	fix: check user status when validating token (#23)
This commit is contained in:
		@@ -98,6 +98,16 @@ func TokenAuth() func(c *gin.Context) {
 | 
			
		||||
			c.Abort()
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		if !model.IsUserEnabled(token.UserId) {
 | 
			
		||||
			c.JSON(http.StatusOK, gin.H{
 | 
			
		||||
				"error": gin.H{
 | 
			
		||||
					"message": "用户已被封禁",
 | 
			
		||||
					"type":    "one_api_error",
 | 
			
		||||
				},
 | 
			
		||||
			})
 | 
			
		||||
			c.Abort()
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		c.Set("id", token.UserId)
 | 
			
		||||
		c.Set("token_id", token.Id)
 | 
			
		||||
		c.Set("unlimited_times", token.UnlimitedTimes)
 | 
			
		||||
 
 | 
			
		||||
@@ -195,6 +195,19 @@ func IsAdmin(userId int) bool {
 | 
			
		||||
	return user.Role >= common.RoleAdminUser
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func IsUserEnabled(userId int) bool {
 | 
			
		||||
	if userId == 0 {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
	var user User
 | 
			
		||||
	err := DB.Where("id = ?", userId).Select("status").Find(&user).Error
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		common.SysError("No such user " + err.Error())
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
	return user.Status == common.UserStatusEnabled
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ValidateAccessToken(token string) (user *User) {
 | 
			
		||||
	if token == "" {
 | 
			
		||||
		return nil
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user