From 605c8b892a4a27ec9f7359847bf5b2c114005a14 Mon Sep 17 00:00:00 2001 From: Thomas Miceli <27960254+thomiceli@users.noreply.github.com> Date: Mon, 23 Sep 2024 16:55:57 +0200 Subject: [PATCH] Add/Remove admins (#337) --- docs/administration/manage-admins.md | 11 +++++++++++ internal/cli/admin.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 docs/administration/manage-admins.md diff --git a/docs/administration/manage-admins.md b/docs/administration/manage-admins.md new file mode 100644 index 00000000..ae9ee268 --- /dev/null +++ b/docs/administration/manage-admins.md @@ -0,0 +1,11 @@ +# Manage admins + +You can add and remove Opengist admins from the CLI. + +```bash +./opengist admin toggle-admin +``` +```bash +$ ./opengist admin toggle-admin thomas +User thomas admin set to true +``` \ No newline at end of file diff --git a/internal/cli/admin.go b/internal/cli/admin.go index c0fd533b..5abac413 100644 --- a/internal/cli/admin.go +++ b/internal/cli/admin.go @@ -12,6 +12,7 @@ var CmdAdmin = cli.Command{ Usage: "Admin commands", Subcommands: []*cli.Command{ &CmdAdminResetPassword, + &CmdAdminToggleAdmin, }, } @@ -48,3 +49,30 @@ var CmdAdminResetPassword = cli.Command{ return nil }, } + +var CmdAdminToggleAdmin = cli.Command{ + Name: "toggle-admin", + Usage: "Toggle the admin status for a given user", + ArgsUsage: "[username]", + Action: func(ctx *cli.Context) error { + initialize(ctx) + if ctx.NArg() < 1 { + return fmt.Errorf("username is required") + } + username := ctx.Args().Get(0) + + user, err := db.GetUserByUsername(username) + if err != nil { + fmt.Printf("Cannot get user %s: %s\n", username, err) + return err + } + + user.IsAdmin = !user.IsAdmin + if err = user.Update(); err != nil { + fmt.Printf("Cannot update user %s: %s\n", username, err) + } + + fmt.Printf("User %s admin set to %t\n", username, user.IsAdmin) + return nil + }, +}