Some checks failed
Build and Release / Create Release (push) Successful in 0s
Trigger Vault Plugin Rebuild / Trigger Vault Rebuild (push) Successful in 0s
Build and Release / Integration Tests (PostgreSQL) (push) Successful in 2m48s
Build and Release / Lint (push) Failing after 5m2s
Build and Release / Build Binaries (amd64, windows, windows-latest) (push) Has been skipped
Build and Release / Build Binaries (amd64, darwin, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (amd64, linux, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (arm64, darwin, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (arm64, linux, linux-latest) (push) Has been skipped
Build and Release / Unit Tests (push) Successful in 5m37s
Go's semantic import versioning requires v2+ modules to include the major version in the module path. This enables using proper version tags (v3.x.x) instead of pseudo-versions. Updated module path: code.gitcaddy.com/server/v3
85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
user_model "code.gitcaddy.com/server/v3/models/user"
|
|
"code.gitcaddy.com/server/v3/modules/setting"
|
|
"code.gitcaddy.com/server/v3/modules/storage"
|
|
user_service "code.gitcaddy.com/server/v3/services/user"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
func microcmdUserDelete() *cli.Command {
|
|
return &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(ctx context.Context, c *cli.Command) error {
|
|
if !c.IsSet("id") && !c.IsSet("username") && !c.IsSet("email") {
|
|
return errors.New("You must provide the id, username or email of a user to delete")
|
|
}
|
|
|
|
if !setting.IsInTesting {
|
|
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"))
|
|
}
|