code.gitea.io/gitea@v1.21.7/cmd/admin_user_list.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package cmd
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"text/tabwriter"
    10  
    11  	user_model "code.gitea.io/gitea/models/user"
    12  
    13  	"github.com/urfave/cli/v2"
    14  )
    15  
    16  var microcmdUserList = &cli.Command{
    17  	Name:   "list",
    18  	Usage:  "List users",
    19  	Action: runListUsers,
    20  	Flags: []cli.Flag{
    21  		&cli.BoolFlag{
    22  			Name:  "admin",
    23  			Usage: "List only admin users",
    24  		},
    25  	},
    26  }
    27  
    28  func runListUsers(c *cli.Context) error {
    29  	ctx, cancel := installSignals()
    30  	defer cancel()
    31  
    32  	if err := initDB(ctx); err != nil {
    33  		return err
    34  	}
    35  
    36  	users, err := user_model.GetAllUsers(ctx)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	w := tabwriter.NewWriter(os.Stdout, 5, 0, 1, ' ', 0)
    42  
    43  	if c.IsSet("admin") {
    44  		fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\n")
    45  		for _, u := range users {
    46  			if u.IsAdmin {
    47  				fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", u.ID, u.Name, u.Email, u.IsActive)
    48  			}
    49  		}
    50  	} else {
    51  		twofa := user_model.UserList(users).GetTwoFaStatus(ctx)
    52  		fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\tIsAdmin\t2FA\n")
    53  		for _, u := range users {
    54  			fmt.Fprintf(w, "%d\t%s\t%s\t%t\t%t\t%t\n", u.ID, u.Name, u.Email, u.IsActive, u.IsAdmin, twofa[u.ID])
    55  		}
    56  	}
    57  
    58  	w.Flush()
    59  	return nil
    60  }