Skip to content

Commit 5e09de1

Browse files
zeripath6543techknowlogick
authored
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 <[email protected]> * fix-spacing Signed-off-by: Andrew Thornton <[email protected]> * Add delete email support Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: 6543 <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent c57e1f2 commit 5e09de1

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

cmd/admin.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"errors"
1111
"fmt"
1212
"os"
13+
"strings"
1314
"text/tabwriter"
1415

1516
"code.gitea.io/gitea/models"
@@ -125,9 +126,22 @@ var (
125126
}
126127

127128
microcmdUserDelete = cli.Command{
128-
Name: "delete",
129-
Usage: "Delete specific user",
130-
Flags: []cli.Flag{idFlag},
129+
Name: "delete",
130+
Usage: "Delete specific user by id, name or email",
131+
Flags: []cli.Flag{
132+
cli.Int64Flag{
133+
Name: "id",
134+
Usage: "ID of user of the user to delete",
135+
},
136+
cli.StringFlag{
137+
Name: "username,u",
138+
Usage: "Username of the user to delete",
139+
},
140+
cli.StringFlag{
141+
Name: "email,e",
142+
Usage: "Email of the user to delete",
143+
},
144+
},
131145
Action: runDeleteUser,
132146
}
133147

@@ -463,18 +477,33 @@ func runListUsers(c *cli.Context) error {
463477
}
464478

465479
func runDeleteUser(c *cli.Context) error {
466-
if !c.IsSet("id") {
467-
return fmt.Errorf("--id flag is missing")
480+
if !c.IsSet("id") && !c.IsSet("username") && !c.IsSet("email") {
481+
return fmt.Errorf("You must provide the id, username or email of a user to delete")
468482
}
469483

470484
if err := initDB(); err != nil {
471485
return err
472486
}
473487

474-
user, err := models.GetUserByID(c.Int64("id"))
488+
var err error
489+
var user *models.User
490+
if c.IsSet("email") {
491+
user, err = models.GetUserByEmail(c.String("email"))
492+
} else if c.IsSet("username") {
493+
user, err = models.GetUserByName(c.String("username"))
494+
} else {
495+
user, err = models.GetUserByID(c.Int64("id"))
496+
}
475497
if err != nil {
476498
return err
477499
}
500+
if c.IsSet("username") && user.LowerName != strings.ToLower(strings.TrimSpace(c.String("username"))) {
501+
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"))
502+
}
503+
504+
if c.IsSet("id") && user.ID != c.Int64("id") {
505+
return fmt.Errorf("The user %s does not match the provided id %d", user.Name, c.Int64("id"))
506+
}
478507

479508
return models.DeleteUser(user)
480509
}

docs/content/doc/usage/command-line.en-us.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ Admin operations:
6969
- `gitea admin user list`
7070
- `delete`:
7171
- Options:
72-
- `--id`: ID of user to be deleted. Required.
72+
- `--email`: Email of the user to be deleted.
73+
- `--username`: Username of user to be deleted.
74+
- `--id`: ID of user to be deleted.
75+
- One of `--id`, `--username` or `--email` is required. If more than one is provided then all have to match.
7376
- Examples:
7477
- `gitea admin user delete --id 1`
7578
- `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

0 commit comments

Comments
 (0)