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 + }, +}