You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin_user_must_change_password.go 1.4KB

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