Skip to content

Commit

Permalink
Add/Remove admins (thomiceli#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomiceli authored Sep 23, 2024
1 parent fa8217e commit 605c8b8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/administration/manage-admins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Manage admins

You can add and remove Opengist admins from the CLI.

```bash
./opengist admin toggle-admin <username>
```
```bash
$ ./opengist admin toggle-admin thomas
User thomas admin set to true
```
28 changes: 28 additions & 0 deletions internal/cli/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var CmdAdmin = cli.Command{
Usage: "Admin commands",
Subcommands: []*cli.Command{
&CmdAdminResetPassword,
&CmdAdminToggleAdmin,
},
}

Expand Down Expand Up @@ -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
},
}

0 comments on commit 605c8b8

Please sign in to comment.