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

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package cmd
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  
    10  	user_model "code.gitea.io/gitea/models/user"
    11  
    12  	"github.com/urfave/cli/v2"
    13  )
    14  
    15  var microcmdUserMustChangePassword = &cli.Command{
    16  	Name:   "must-change-password",
    17  	Usage:  "Set the must change password flag for the provided users or all users",
    18  	Action: runMustChangePassword,
    19  	Flags: []cli.Flag{
    20  		&cli.BoolFlag{
    21  			Name:    "all",
    22  			Aliases: []string{"A"},
    23  			Usage:   "All users must change password, except those explicitly excluded with --exclude",
    24  		},
    25  		&cli.StringSliceFlag{
    26  			Name:    "exclude",
    27  			Aliases: []string{"e"},
    28  			Usage:   "Do not change the must-change-password flag for these users",
    29  		},
    30  		&cli.BoolFlag{
    31  			Name:  "unset",
    32  			Usage: "Instead of setting the must-change-password flag, unset it",
    33  		},
    34  	},
    35  }
    36  
    37  func runMustChangePassword(c *cli.Context) error {
    38  	ctx, cancel := installSignals()
    39  	defer cancel()
    40  
    41  	if c.NArg() == 0 && !c.IsSet("all") {
    42  		return errors.New("either usernames or --all must be provided")
    43  	}
    44  
    45  	mustChangePassword := !c.Bool("unset")
    46  	all := c.Bool("all")
    47  	exclude := c.StringSlice("exclude")
    48  
    49  	if err := initDB(ctx); err != nil {
    50  		return err
    51  	}
    52  
    53  	n, err := user_model.SetMustChangePassword(ctx, all, mustChangePassword, c.Args().Slice(), exclude)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	fmt.Printf("Updated %d users setting MustChangePassword to %t\n", n, mustChangePassword)
    59  	return nil
    60  }