admin handling

This commit is contained in:
Dot-Rar 2020-03-23 23:14:04 +00:00
parent 862de0e657
commit 469c8e0883
17 changed files with 86 additions and 34 deletions

View File

@ -43,7 +43,9 @@ func BlacklistHandler(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page
return
}

View File

@ -44,7 +44,9 @@ func BlacklistRemoveHandler(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page
return
}

View File

@ -52,7 +52,9 @@ func LogsHandler(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page
return
}

View File

@ -46,7 +46,9 @@ func PanelCreateHandler(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page
return
}

View File

@ -50,7 +50,9 @@ func PanelDeleteHandler(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page
return
}

View File

@ -51,7 +51,9 @@ func PanelHandler(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page
return
}

View File

@ -46,7 +46,9 @@ func SendMessage(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page
return
}

View File

@ -43,7 +43,9 @@ func SettingsHandler(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page
return
}

View File

@ -45,7 +45,9 @@ func TicketCloseHandler(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page
return
}

View File

@ -44,7 +44,9 @@ func TicketListHandler(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page
return
}

View File

@ -49,7 +49,9 @@ func TicketViewHandler(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page
return
}

View File

@ -44,7 +44,9 @@ func UpdateSettingsHandler(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page
return
}

View File

@ -47,7 +47,9 @@ func LogViewHandler(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page
return
}

View File

@ -137,7 +137,9 @@ func WebChatWs(ctx *gin.Context) {
}
// Verify the user has permissions to be here
if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildIdParsed, userId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildIdParsed, userId, isAdmin)
if <-isAdmin {
fmt.Println(err.Error())
conn.Close()
return

View File

@ -26,14 +26,15 @@ func IndexHandler(ctx *gin.Context) {
}
adminGuilds := make([]objects.Guild, 0)
adminGuildIds := table.GetAdminGuilds(userId)
for _, guild := range table.GetGuilds(userIdStr) {
guildId, err := strconv.ParseInt(guild.Id, 10, 64)
if err != nil { // I think this happens when a server was deleted? We should just skip though.
continue
}
if guild.Owner || utils.Contains(adminGuildIds, guildId) {
isAdmin := make(chan bool)
go utils.IsAdmin(store, guild, guildId, userId, isAdmin)
if <-isAdmin {
adminGuilds = append(adminGuilds, guild)
}
}

View File

@ -3,7 +3,7 @@ package objects
type Member struct {
User User
Nick string
Roles []string
Roles []int64 `json:"roles,string"`
JoinedAt string
Deaf bool
Mute bool

View File

@ -1,19 +1,21 @@
package utils
import (
"fmt"
"github.com/TicketsBot/GoPanel/config"
"github.com/TicketsBot/GoPanel/database/table"
"github.com/TicketsBot/GoPanel/utils/discord/endpoints/guild"
"github.com/TicketsBot/GoPanel/utils/discord/objects"
"github.com/apex/log"
"github.com/gin-gonic/contrib/sessions"
"github.com/robfig/go-cache"
"strconv"
"time"
)
func IsAdmin(guild objects.Guild, guildId, user int64, res chan bool) {
userIdStr := strconv.Itoa(int(user))
var roleCache = cache.New(time.Minute, time.Minute)
if Contains(config.Conf.Admins, userIdStr) {
func IsAdmin(store sessions.Session, guild objects.Guild, guildId, userId int64, res chan bool) {
if Contains(config.Conf.Admins, strconv.Itoa(int(userId))) {
res <- true
}
@ -21,30 +23,51 @@ func IsAdmin(guild objects.Guild, guildId, user int64, res chan bool) {
res <- true
}
if table.IsAdmin(guildId, user) {
if table.IsAdmin(guildId, userId) {
res <- true
}
if guild.Permissions & 0x8 != 0 {
res <- true
}
userRoles := GetRoles(store, guildId, userId)
adminRolesChan := make(chan []int64)
go table.GetAdminRoles(strconv.Itoa(int(guildId)), adminRolesChan)
adminRoles := <- adminRolesChan
hasAdminRole := false
for _, userRole := range userRoles {
for _, adminRole := range adminRoles {
if userRole == adminRole {
hasAdminRole = true
break
}
}
}
if hasAdminRole {
res <- true
}
res <- false
}
func GetRolesRest(store sessions.Session, guildId, userId int64, res chan *[]int64) {
func GetRoles(store sessions.Session, guildId, userId int64) []int64 {
key := fmt.Sprintf("%d-%d", guildId, userId)
if cached, ok := roleCache.Get(key); ok {
return cached.([]int64)
}
var member objects.Member
endpoint := guild.GetGuildMember(int(guildId), int(userId))
if err, _ := endpoint.Request(store, nil, nil, &member); err != nil {
res <- nil
return nil
}
roles := make([]int64, 0)
for _, role := range member.Roles {
int, err := strconv.ParseInt(role, 10, 64); if err != nil {
log.Error(err.Error())
continue
}
roleCache.Set(key, &member.Roles, time.Minute)
roles = append(roles, int)
}
res <- &roles
return member.Roles
}