From c8d01029eaef8bf1f5ca2fbbccc64aa61a906ca7 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Fri, 9 Oct 2020 12:53:50 -0400 Subject: [PATCH] admin: enforce admin status of users --- bot/bot.go | 28 ---------------------------- bot/mock.go | 2 +- plugins/admin/admin.go | 17 +++++++++++------ plugins/admin/admin_test.go | 9 +++++++-- 4 files changed, 19 insertions(+), 37 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 5670967..c11e01f 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -230,34 +230,6 @@ func (b *bot) CheckAdmin(nick string) bool { 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 func (b *bot) RegisterFilter(name string, f func(string) string) { b.filters[name] = f diff --git a/bot/mock.go b/bot/mock.go index 679a687..162b5fd 100644 --- a/bot/mock.go +++ b/bot/mock.go @@ -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) 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) { mb.Reactions = append(mb.Reactions, reaction) diff --git a/plugins/admin/admin.go b/plugins/admin/admin.go index 20369bc..87db42e 100644 --- a/plugins/admin/admin.go +++ b/plugins/admin/admin.go @@ -88,12 +88,6 @@ func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Messag 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" { dur := time.Duration(p.cfg.GetInt("quietDuration", 5)) * time.Minute 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 } + 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) { submatches := addBlacklist.FindStringSubmatch(message.Body) plugin := submatches[1] diff --git a/plugins/admin/admin_test.go b/plugins/admin/admin_test.go index 917b5b9..92a337e 100644 --- a/plugins/admin/admin_test.go +++ b/plugins/admin/admin_test.go @@ -1,10 +1,11 @@ package admin import ( - "github.com/velour/catbase/plugins/cli" "strings" "testing" + "github.com/velour/catbase/plugins/cli" + "github.com/stretchr/testify/assert" "github.com/velour/catbase/bot" "github.com/velour/catbase/bot/msg" @@ -20,6 +21,10 @@ func setup(t *testing.T) (*AdminPlugin, *bot.MockBot) { mb = bot.NewMockBot() a = New(mb) mb.DB().MustExec(`delete from config`) + err := mb.Config().Set("admins", "tester") + if err != nil { + t.FailNow() + } return a, mb } @@ -30,7 +35,7 @@ func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) { } c := cli.CliPlugin{} return &c, bot.Message, msg.Message{ - User: &user.User{Name: "tester"}, + User: &user.User{Name: "admin"}, Channel: "test", Body: payload, Command: isCmd,