mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-15 14:08:21 +00:00
d0dbe52e76
Replace #10912 And there are many new tests to cover the CLI behavior There were some concerns about the "option order in hook scripts" (https://github.com/go-gitea/gitea/pull/10912#issuecomment-1137543314), it's not a problem now. Because the hook script uses `/gitea hook --config=/app.ini pre-receive` format. The "config" is a global option, it can appear anywhere. ---- ## ⚠️ BREAKING ⚠️ This PR does it best to avoid breaking anything. The major changes are: * `gitea` itself won't accept web's options: `--install-port` / `--pid` / `--port` / `--quiet` / `--verbose` .... They are `web` sub-command's options. * Use `./gitea web --pid ....` instead * `./gitea` can still run the `web` sub-command as shorthand, with default options * The sub-command's options must follow the sub-command * Before: `./gitea --sub-opt subcmd` might equal to `./gitea subcmd --sub-opt` (well, might not ...) * After: only `./gitea subcmd --sub-opt` could be used * The global options like `--config` are not affected
81 lines
2 KiB
Go
81 lines
2 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/storage"
|
|
user_service "code.gitea.io/gitea/services/user"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
var microcmdUserDelete = &cli.Command{
|
|
Name: "delete",
|
|
Usage: "Delete specific user by id, name or email",
|
|
Flags: []cli.Flag{
|
|
&cli.Int64Flag{
|
|
Name: "id",
|
|
Usage: "ID of user of the user to delete",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "username",
|
|
Aliases: []string{"u"},
|
|
Usage: "Username of the user to delete",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "email",
|
|
Aliases: []string{"e"},
|
|
Usage: "Email of the user to delete",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "purge",
|
|
Usage: "Purge user, all their repositories, organizations and comments",
|
|
},
|
|
},
|
|
Action: runDeleteUser,
|
|
}
|
|
|
|
func runDeleteUser(c *cli.Context) error {
|
|
if !c.IsSet("id") && !c.IsSet("username") && !c.IsSet("email") {
|
|
return fmt.Errorf("You must provide the id, username or email of a user to delete")
|
|
}
|
|
|
|
ctx, cancel := installSignals()
|
|
defer cancel()
|
|
|
|
if err := initDB(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := storage.Init(); err != nil {
|
|
return err
|
|
}
|
|
|
|
var err error
|
|
var user *user_model.User
|
|
if c.IsSet("email") {
|
|
user, err = user_model.GetUserByEmail(ctx, c.String("email"))
|
|
} else if c.IsSet("username") {
|
|
user, err = user_model.GetUserByName(ctx, c.String("username"))
|
|
} else {
|
|
user, err = user_model.GetUserByID(ctx, c.Int64("id"))
|
|
}
|
|
if err != nil {
|
|
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 user_service.DeleteUser(ctx, user, c.Bool("purge"))
|
|
}
|