gitea源码

admin_user_must_change_password.go 1.5KB

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