5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-22 16:20:26 +00:00

Refactor webhook permission checks

This commit is contained in:
Qais Patankar 2020-03-17 16:18:50 +00:00 committed by Wim
parent b5dc4353fb
commit 79f764c7a8

View File

@ -124,25 +124,27 @@ func (b *Bdiscord) Connect() error {
b.Log.Debugf("found channel %#v", channel) b.Log.Debugf("found channel %#v", channel)
} }
} else { } else {
b.canEditWebhooks = true manageWebhooks := discordgo.PermissionManageWebhooks
var channelsDenied []string
for _, info := range b.Channels { for _, info := range b.Channels {
id := b.getChannelID(info.Name) // note(qaisjp): this readlocks channelsMutex id := b.getChannelID(info.Name) // note(qaisjp): this readlocks channelsMutex
b.Log.Debugf("Verifying PermissionManageWebhooks for %s with ID %s", info.ID, id) b.Log.Debugf("Verifying PermissionManageWebhooks for %s with ID %s", info.ID, id)
perms, permsErr := b.c.UserChannelPermissions(userinfo.ID, id)
manageWebhooks := discordgo.PermissionManageWebhooks perms, permsErr := b.c.UserChannelPermissions(userinfo.ID, id)
if permsErr != nil { if permsErr != nil {
b.Log.Warnf("Can't manage webhooks in channel \"%s\", because: %s", info.Name, permsErr.Error()) b.Log.Warnf("Failed to check PermissionManageWebhooks in channel \"%s\": %s", info.Name, permsErr.Error())
b.canEditWebhooks = false } else if perms&manageWebhooks == manageWebhooks {
} else if perms&manageWebhooks != manageWebhooks { continue
b.Log.Warnf("Can't manage webhooks in channel \"%s\"", info.Name)
b.canEditWebhooks = false
} }
channelsDenied = append(channelsDenied, fmt.Sprintf("%#v", info.Name))
} }
b.canEditWebhooks = len(channelsDenied) == 0
if b.canEditWebhooks { if b.canEditWebhooks {
b.Log.Info("Can manage webhooks; will edit channel for global webhook on send") b.Log.Info("Can manage webhooks; will edit channel for global webhook on send")
} else { } else {
b.Log.Warn("Can't manage webhooks; won't edit channel for global webhook on send") b.Log.Warn("Can't manage webhooks; won't edit channel for global webhook on send")
b.Log.Warn("Can't manage webhooks in channels: ", strings.Join(channelsDenied, ", "))
} }
} }
b.channelsMutex.RUnlock() b.channelsMutex.RUnlock()