whitelabel middleware

This commit is contained in:
Dot-Rar 2020-06-13 14:33:48 +01:00
parent 7c1b4193cf
commit f319143e16
4 changed files with 48 additions and 42 deletions

View File

@ -15,25 +15,6 @@ import (
func WhitelabelPost(ctx *gin.Context) { func WhitelabelPost(ctx *gin.Context) {
userId := ctx.Keys["userid"].(uint64) userId := ctx.Keys["userid"].(uint64)
premiumTier := rpc.PremiumClient.GetTierByUser(userId, false)
if premiumTier < premium.Whitelabel {
var isForced bool
for _, forced := range config.Conf.ForceWhitelabel {
if forced == userId {
isForced = true
break
}
}
if !isForced {
ctx.JSON(402, gin.H{
"success": false,
"error": "You must have the whitelabel premium tier",
})
return
}
}
// Get token // Get token
var data map[string]interface{} var data map[string]interface{}
if err := ctx.BindJSON(&data); err != nil { if err := ctx.BindJSON(&data); err != nil {

View File

@ -13,25 +13,6 @@ import (
func WhitelabelStatusPost(ctx *gin.Context) { func WhitelabelStatusPost(ctx *gin.Context) {
userId := ctx.Keys["userid"].(uint64) userId := ctx.Keys["userid"].(uint64)
premiumTier := rpc.PremiumClient.GetTierByUser(userId, false)
if premiumTier < premium.Whitelabel {
var isForced bool
for _, forced := range config.Conf.ForceWhitelabel {
if forced == userId {
isForced = true
break
}
}
if !isForced {
ctx.JSON(402, gin.H{
"success": false,
"error": "You must have the whitelabel premium tier",
})
return
}
}
// Get bot // Get bot
bot, err := database.Client.Whitelabel.GetByUserId(userId) bot, err := database.Client.Whitelabel.GetByUserId(userId)
if err != nil { if err != nil {

View File

@ -0,0 +1,39 @@
package middleware
import (
"fmt"
"github.com/TicketsBot/GoPanel/config"
"github.com/TicketsBot/GoPanel/rpc"
"github.com/TicketsBot/common/premium"
"github.com/gin-gonic/gin"
)
func VerifyWhitelabel(isApi bool) func(ctx *gin.Context) {
return func(ctx *gin.Context) {
userId := ctx.Keys["userid"].(uint64)
if rpc.PremiumClient.GetTierByUser(userId, false) < premium.Whitelabel {
var isForced bool
for _, forced := range config.Conf.ForceWhitelabel {
if forced == userId {
isForced = true
break
}
}
if !isForced {
if isApi {
ctx.AbortWithStatusJSON(402, gin.H{
"success": false,
"error": "You must have the whitelabel premium tier",
})
} else {
ctx.Redirect(302, fmt.Sprintf("%s/premium", config.Conf.Server.MainSite))
ctx.Abort()
}
return
}
}
}
}

View File

@ -113,11 +113,16 @@ func StartServer() {
{ {
userGroup.GET("/guilds", api.GetGuilds) userGroup.GET("/guilds", api.GetGuilds)
userGroup.GET("/whitelabel", api.WhitelabelGet) {
userGroup.GET("/whitelabel/errors", api.WhitelabelGetErrors) whitelabelGroup := userGroup.Group("/whitelabel", middleware.VerifyWhitelabel(false))
whitelabelApiGroup := userGroup.Group("/whitelabel", middleware.VerifyWhitelabel(true))
userGroup.Group("/").Use(createLimiter(10, time.Minute)).POST("/whitelabel", api.WhitelabelPost) whitelabelGroup.GET("/", api.WhitelabelGet)
userGroup.Group("/").Use(createLimiter(1, time.Second * 5)).POST("/whitelabel/status", api.WhitelabelStatusPost) whitelabelApiGroup.GET("/errors", api.WhitelabelGetErrors)
whitelabelApiGroup.Group("/").Use(createLimiter(10, time.Minute)).POST("/", api.WhitelabelPost)
whitelabelApiGroup.Group("/").Use(createLimiter(1, time.Second * 5)).POST("/status", api.WhitelabelStatusPost)
}
} }
if err := router.Run(config.Conf.Server.Host); err != nil { if err := router.Run(config.Conf.Server.Host); err != nil {