admin role checking
This commit is contained in:
parent
0a3fc1f089
commit
3e21f25dfd
@ -31,15 +31,12 @@ func SettingsHandler(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check the bot is in the guild
|
// Check the bot is in the guild
|
||||||
_, isInGuild := cache.Instance.GetGuild(guildId, false)
|
guild, isInGuild := cache.Instance.GetGuild(guildId, false)
|
||||||
if !isInGuild {
|
if !isInGuild {
|
||||||
ctx.Redirect(302, fmt.Sprintf("https://invite.ticketsbot.net/?guild_id=%s&disable_guild_select=true&response_type=code&scope=bot%%20identify&redirect_uri=%s", guildIdStr, config.Conf.Server.BaseUrl))
|
ctx.Redirect(302, fmt.Sprintf("https://invite.ticketsbot.net/?guild_id=%s&disable_guild_select=true&response_type=code&scope=bot%%20identify&redirect_uri=%s", guildIdStr, config.Conf.Server.BaseUrl))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get object for selected guild
|
|
||||||
guild, _ := cache.Instance.GetGuild(guildId, false)
|
|
||||||
|
|
||||||
// Verify the user has permissions to be here
|
// Verify the user has permissions to be here
|
||||||
isAdmin := make(chan bool)
|
isAdmin := make(chan bool)
|
||||||
go utils.IsAdmin(guild, userId, isAdmin)
|
go utils.IsAdmin(guild, userId, isAdmin)
|
||||||
|
1
go.mod
1
go.mod
@ -21,4 +21,5 @@ require (
|
|||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62
|
github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62
|
||||||
github.com/rxdn/gdl v0.0.0-20200417164852-76b2d3c847c1
|
github.com/rxdn/gdl v0.0.0-20200417164852-76b2d3c847c1
|
||||||
|
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a // indirect
|
||||||
)
|
)
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
"github.com/TicketsBot/GoPanel/config"
|
"github.com/TicketsBot/GoPanel/config"
|
||||||
"github.com/TicketsBot/GoPanel/database/table"
|
"github.com/TicketsBot/GoPanel/database/table"
|
||||||
"github.com/TicketsBot/GoPanel/rpc/cache"
|
"github.com/TicketsBot/GoPanel/rpc/cache"
|
||||||
"github.com/TicketsBot/GoPanel/rpc/ratelimit"
|
"github.com/TicketsBot/GoPanel/rpc/ratelimit"
|
||||||
|
"github.com/apex/log"
|
||||||
"github.com/rxdn/gdl/objects/guild"
|
"github.com/rxdn/gdl/objects/guild"
|
||||||
"github.com/rxdn/gdl/permission"
|
"github.com/rxdn/gdl/permission"
|
||||||
"github.com/rxdn/gdl/rest"
|
"github.com/rxdn/gdl/rest"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsAdmin(g guild.Guild, userId uint64, res chan bool) {
|
func IsAdmin(g guild.Guild, userId uint64, res chan bool) {
|
||||||
@ -24,29 +29,28 @@ func IsAdmin(g guild.Guild, userId uint64, res chan bool) {
|
|||||||
res <- true
|
res <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
if permission.HasPermissionRaw(g.Permissions, permission.Administrator) {
|
userRoles, _ := getRoles(g.Id, userId)
|
||||||
|
|
||||||
|
// check if user has administrator permission
|
||||||
|
if hasAdministratorPermission(g.Id, userRoles) {
|
||||||
res <- true
|
res <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
adminRolesChan := make(chan []uint64)
|
adminRolesChan := make(chan []uint64)
|
||||||
go table.GetAdminRoles(g.Id, adminRolesChan)
|
go table.GetAdminRoles(g.Id, adminRolesChan)
|
||||||
adminRoles := <- adminRolesChan
|
adminRoles := <-adminRolesChan
|
||||||
|
|
||||||
userRoles, found := getRoles(g.Id, userId)
|
hasTicketAdminRole := false
|
||||||
|
|
||||||
hasAdminRole := false
|
|
||||||
if found {
|
|
||||||
for _, userRole := range userRoles {
|
for _, userRole := range userRoles {
|
||||||
for _, adminRole := range adminRoles {
|
for _, adminRole := range adminRoles {
|
||||||
if userRole == adminRole {
|
if userRole == adminRole {
|
||||||
hasAdminRole = true
|
hasTicketAdminRole = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if hasAdminRole {
|
if hasTicketAdminRole {
|
||||||
res <- true
|
res <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,3 +72,57 @@ func getRoles(guildId, userId uint64) ([]uint64, bool) {
|
|||||||
|
|
||||||
return member.Roles, true
|
return member.Roles, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hasAdministratorPermission(guildId uint64, roles []uint64) bool {
|
||||||
|
var lock sync.Mutex
|
||||||
|
var hasAdministrator bool
|
||||||
|
|
||||||
|
group, _ := errgroup.WithContext(context.Background())
|
||||||
|
for _, roleId := range roles {
|
||||||
|
group.Go(func() error {
|
||||||
|
roleHasAdmin, err := roleHasAdministrator(guildId, roleId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if roleHasAdmin {
|
||||||
|
lock.Lock()
|
||||||
|
hasAdministrator = true
|
||||||
|
lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := group.Wait(); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasAdministrator
|
||||||
|
}
|
||||||
|
|
||||||
|
func roleHasAdministrator(guildId, roleId uint64) (bool, error) {
|
||||||
|
role, found := cache.Instance.GetRole(roleId)
|
||||||
|
if !found {
|
||||||
|
roles, err := rest.GetGuildRoles(config.Conf.Bot.Token, ratelimit.Ratelimiter, guildId)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err.Error())
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
go cache.Instance.StoreRoles(roles, guildId)
|
||||||
|
for _, r := range roles {
|
||||||
|
if r.Id == roleId {
|
||||||
|
role = r
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
return false, errors.New("role does not exist")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return permission.HasPermissionRaw(role.Permissions, permission.Administrator), nil
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user