From 4bd600c65147d0abfd08ef1c5031e64aba2f39fd Mon Sep 17 00:00:00 2001 From: delthas Date: Thu, 19 Jan 2023 18:13:59 +0100 Subject: [PATCH] service: Store the admin capability independently the user We can acquire admin contexts independently of the user. This is mainly for a future commit that will introduce events without a user. --- downstream.go | 1 + service.go | 13 +++++++------ user.go | 1 + 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/downstream.go b/downstream.go index dc87372..f85a153 100644 --- a/downstream.go +++ b/downstream.go @@ -2433,6 +2433,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. nick: dc.nick, network: dc.network, user: dc.user, + admin: dc.user.Admin, print: func(text string) { sendServicePRIVMSG(dc, text) }, diff --git a/service.go b/service.go index 533b7d6..192e4a7 100644 --- a/service.go +++ b/service.go @@ -40,6 +40,7 @@ type serviceContext struct { nick string // optional network *network // optional user *user + admin bool print func(string) } @@ -137,7 +138,7 @@ func handleServiceCommand(ctx *serviceContext, words []string) { ctx.print(fmt.Sprintf(`error: %v (type "help" for a list of commands)`, err)) return } - if cmd.admin && !ctx.user.Admin { + if cmd.admin && !ctx.admin { ctx.print("error: you must be an admin to use this command") return } @@ -145,7 +146,7 @@ func handleServiceCommand(ctx *serviceContext, words []string) { if cmd.handle == nil { if len(cmd.children) > 0 { var l []string - appendServiceCommandSetHelp(cmd.children, words, ctx.user.Admin, &l) + appendServiceCommandSetHelp(cmd.children, words, ctx.admin, &l) ctx.print("available commands: " + strings.Join(l, ", ")) } else { // Pretend the command does not exist if it has neither children nor handler. @@ -365,7 +366,7 @@ func handleServiceHelp(ctx *serviceContext, params []string) error { if len(cmd.children) > 0 { var l []string - appendServiceCommandSetHelp(cmd.children, words, ctx.user.Admin, &l) + appendServiceCommandSetHelp(cmd.children, words, ctx.admin, &l) ctx.print("available commands: " + strings.Join(l, ", ")) } else { text := strings.Join(words, " ") @@ -378,7 +379,7 @@ func handleServiceHelp(ctx *serviceContext, params []string) error { } } else { var l []string - appendServiceCommandSetHelp(serviceCommands, nil, ctx.user.Admin, &l) + appendServiceCommandSetHelp(serviceCommands, nil, ctx.admin, &l) ctx.print("available commands: " + strings.Join(l, ", ")) } return nil @@ -999,7 +1000,7 @@ func handleUserUpdate(ctx *serviceContext, params []string) error { } if username != "" && username != ctx.user.Username { - if !ctx.user.Admin { + if !ctx.admin { return fmt.Errorf("you must be an admin to update other users") } if nick != nil { @@ -1092,7 +1093,7 @@ func handleUserDelete(ctx *serviceContext, params []string) error { self := ctx.user.Username == username - if !ctx.user.Admin && !self { + if !ctx.admin && !self { return fmt.Errorf("only admins may delete other users") } diff --git a/user.go b/user.go index 944bd2e..264660b 100644 --- a/user.go +++ b/user.go @@ -811,6 +811,7 @@ func (u *user) run() { handleServiceCommand(&serviceContext{ Context: ctx, user: u, + admin: u.Admin, print: func(text string) { // Avoid blocking on e.print in case our context is canceled. // This is a no-op right now because we use context.TODO(),