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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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"
  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,A",
  17. Usage: "All users must change password, except those explicitly excluded with --exclude",
  18. },
  19. cli.StringSliceFlag{
  20. Name: "exclude,e",
  21. Usage: "Do not change the must-change-password flag for these users",
  22. },
  23. cli.BoolFlag{
  24. Name: "unset",
  25. Usage: "Instead of setting the must-change-password flag, unset it",
  26. },
  27. },
  28. }
  29. func runMustChangePassword(c *cli.Context) error {
  30. ctx, cancel := installSignals()
  31. defer cancel()
  32. if c.NArg() == 0 && !c.IsSet("all") {
  33. return errors.New("either usernames or --all must be provided")
  34. }
  35. mustChangePassword := !c.Bool("unset")
  36. all := c.Bool("all")
  37. exclude := c.StringSlice("exclude")
  38. if err := initDB(ctx); err != nil {
  39. return err
  40. }
  41. n, err := user_model.SetMustChangePassword(ctx, all, mustChangePassword, c.Args(), exclude)
  42. if err != nil {
  43. return err
  44. }
  45. fmt.Printf("Updated %d users setting MustChangePassword to %t\n", n, mustChangePassword)
  46. return nil
  47. }