fix label of --id in admin delete user (#14005)

* fix label of --id in admin delete user

This pr fixes the label descriptor of `gitea admin delete user`
but also adds a `--username` option.

Fix #13995

Signed-off-by: Andrew Thornton <art27@cantab.net>

* fix-spacing

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Add delete email support

Signed-off-by: Andrew Thornton <art27@cantab.net>

Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
zeripath 2020-12-17 07:57:32 +00:00 committed by GitHub
parent c57e1f2653
commit 5e09de174c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 7 deletions

View file

@ -10,6 +10,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"strings"
"text/tabwriter" "text/tabwriter"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -126,8 +127,21 @@ var (
microcmdUserDelete = cli.Command{ microcmdUserDelete = cli.Command{
Name: "delete", Name: "delete",
Usage: "Delete specific user", Usage: "Delete specific user by id, name or email",
Flags: []cli.Flag{idFlag}, Flags: []cli.Flag{
cli.Int64Flag{
Name: "id",
Usage: "ID of user of the user to delete",
},
cli.StringFlag{
Name: "username,u",
Usage: "Username of the user to delete",
},
cli.StringFlag{
Name: "email,e",
Usage: "Email of the user to delete",
},
},
Action: runDeleteUser, Action: runDeleteUser,
} }
@ -463,18 +477,33 @@ func runListUsers(c *cli.Context) error {
} }
func runDeleteUser(c *cli.Context) error { func runDeleteUser(c *cli.Context) error {
if !c.IsSet("id") { if !c.IsSet("id") && !c.IsSet("username") && !c.IsSet("email") {
return fmt.Errorf("--id flag is missing") return fmt.Errorf("You must provide the id, username or email of a user to delete")
} }
if err := initDB(); err != nil { if err := initDB(); err != nil {
return err return err
} }
user, err := models.GetUserByID(c.Int64("id")) var err error
var user *models.User
if c.IsSet("email") {
user, err = models.GetUserByEmail(c.String("email"))
} else if c.IsSet("username") {
user, err = models.GetUserByName(c.String("username"))
} else {
user, err = models.GetUserByID(c.Int64("id"))
}
if err != nil { if err != nil {
return err return err
} }
if c.IsSet("username") && user.LowerName != strings.ToLower(strings.TrimSpace(c.String("username"))) {
return fmt.Errorf("The user %s who has email %s does not match the provided username %s", user.Name, c.String("email"), c.String("username"))
}
if c.IsSet("id") && user.ID != c.Int64("id") {
return fmt.Errorf("The user %s does not match the provided id %d", user.Name, c.Int64("id"))
}
return models.DeleteUser(user) return models.DeleteUser(user)
} }

View file

@ -69,7 +69,10 @@ Admin operations:
- `gitea admin user list` - `gitea admin user list`
- `delete`: - `delete`:
- Options: - Options:
- `--id`: ID of user to be deleted. Required. - `--email`: Email of the user to be deleted.
- `--username`: Username of user to be deleted.
- `--id`: ID of user to be deleted.
- One of `--id`, `--username` or `--email` is required. If more than one is provided then all have to match.
- Examples: - Examples:
- `gitea admin user delete --id 1` - `gitea admin user delete --id 1`
- `create`: - Options: - `--name value`: Username. Required. As of gitea 1.9.0, use the `--username` flag instead. - `--username value`: Username. Required. New in gitea 1.9.0. - `--password value`: Password. Required. - `--email value`: Email. Required. - `--admin`: If provided, this makes the user an admin. Optional. - `--access-token`: If provided, an access token will be created for the user. Optional. (default: false). - `--must-change-password`: If provided, the created user will be required to choose a newer password after - `create`: - Options: - `--name value`: Username. Required. As of gitea 1.9.0, use the `--username` flag instead. - `--username value`: Username. Required. New in gitea 1.9.0. - `--password value`: Password. Required. - `--email value`: Email. Required. - `--admin`: If provided, this makes the user an admin. Optional. - `--access-token`: If provided, an access token will be created for the user. Optional. (default: false). - `--must-change-password`: If provided, the created user will be required to choose a newer password after