Merge pull request #299 from velour/adminOnly

admin: enforce admin status of users
This commit is contained in:
Chris Sexton 2020-10-09 13:17:25 -04:00 committed by GitHub
commit 30f3c26dd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 37 deletions

View File

@ -230,34 +230,6 @@ func (b *bot) CheckAdmin(nick string) bool {
return false return false
} }
var users = map[string]*user.User{}
func (b *bot) GetUser(nick string) *user.User {
if _, ok := users[nick]; !ok {
users[nick] = &user.User{
Name: nick,
Admin: b.checkAdmin(nick),
}
}
return users[nick]
}
func (b *bot) NewUser(nick string) *user.User {
return &user.User{
Name: nick,
Admin: b.checkAdmin(nick),
}
}
func (b *bot) checkAdmin(nick string) bool {
for _, u := range b.Config().GetArray("Admins", []string{}) {
if nick == u {
return true
}
}
return false
}
// Register a text filter which every outgoing message is passed through // Register a text filter which every outgoing message is passed through
func (b *bot) RegisterFilter(name string, f func(string) string) { func (b *bot) RegisterFilter(name string, f func(string) string) {
b.filters[name] = f b.filters[name] = f

View File

@ -60,7 +60,7 @@ func (mb *MockBot) Receive(c Connector, kind Kind, msg msg.Message, args ...inte
} }
func (mb *MockBot) Filter(msg msg.Message, s string) string { return s } func (mb *MockBot) Filter(msg msg.Message, s string) string { return s }
func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil } func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil }
func (mb *MockBot) CheckAdmin(nick string) bool { return false } func (mb *MockBot) CheckAdmin(nick string) bool { return nick == "admin" }
func (mb *MockBot) react(c Connector, channel, reaction string, message msg.Message) (string, error) { func (mb *MockBot) react(c Connector, channel, reaction string, message msg.Message) (string, error) {
mb.Reactions = append(mb.Reactions, reaction) mb.Reactions = append(mb.Reactions, reaction)

View File

@ -88,12 +88,6 @@ func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Messag
return true return true
} }
if strings.ToLower(body) == "reboot" {
p.bot.Send(conn, bot.Message, message.Channel, "brb")
log.Info().Msgf("Got reboot command")
os.Exit(0)
}
if strings.ToLower(body) == "shut up" { if strings.ToLower(body) == "shut up" {
dur := time.Duration(p.cfg.GetInt("quietDuration", 5)) * time.Minute dur := time.Duration(p.cfg.GetInt("quietDuration", 5)) * time.Minute
log.Info().Msgf("Going to sleep for %v, %v", dur, time.Now().Add(dur)) log.Info().Msgf("Going to sleep for %v, %v", dur, time.Now().Add(dur))
@ -114,6 +108,17 @@ func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Messag
return true return true
} }
if !p.bot.CheckAdmin(message.User.Name) {
log.Debug().Msgf("User %s is not an admin", message.User.Name)
return false
}
if strings.ToLower(body) == "reboot" {
p.bot.Send(conn, bot.Message, message.Channel, "brb")
log.Info().Msgf("Got reboot command")
os.Exit(0)
}
if addBlacklist.MatchString(body) { if addBlacklist.MatchString(body) {
submatches := addBlacklist.FindStringSubmatch(message.Body) submatches := addBlacklist.FindStringSubmatch(message.Body)
plugin := submatches[1] plugin := submatches[1]

View File

@ -1,10 +1,11 @@
package admin package admin
import ( import (
"github.com/velour/catbase/plugins/cli"
"strings" "strings"
"testing" "testing"
"github.com/velour/catbase/plugins/cli"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg" "github.com/velour/catbase/bot/msg"
@ -20,6 +21,10 @@ func setup(t *testing.T) (*AdminPlugin, *bot.MockBot) {
mb = bot.NewMockBot() mb = bot.NewMockBot()
a = New(mb) a = New(mb)
mb.DB().MustExec(`delete from config`) mb.DB().MustExec(`delete from config`)
err := mb.Config().Set("admins", "tester")
if err != nil {
t.FailNow()
}
return a, mb return a, mb
} }
@ -30,7 +35,7 @@ func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
} }
c := cli.CliPlugin{} c := cli.CliPlugin{}
return &c, bot.Message, msg.Message{ return &c, bot.Message, msg.Message{
User: &user.User{Name: "tester"}, User: &user.User{Name: "admin"},
Channel: "test", Channel: "test",
Body: payload, Body: payload,
Command: isCmd, Command: isCmd,