Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

admin_user_change_password.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. pwd "code.gitea.io/gitea/modules/auth/password"
  10. "code.gitea.io/gitea/modules/setting"
  11. "github.com/urfave/cli/v2"
  12. )
  13. var microcmdUserChangePassword = &cli.Command{
  14. Name: "change-password",
  15. Usage: "Change a user's password",
  16. Action: runChangePassword,
  17. Flags: []cli.Flag{
  18. &cli.StringFlag{
  19. Name: "username",
  20. Aliases: []string{"u"},
  21. Value: "",
  22. Usage: "The user to change password for",
  23. },
  24. &cli.StringFlag{
  25. Name: "password",
  26. Aliases: []string{"p"},
  27. Value: "",
  28. Usage: "New password to set for user",
  29. },
  30. },
  31. }
  32. func runChangePassword(c *cli.Context) error {
  33. if err := argsSet(c, "username", "password"); err != nil {
  34. return err
  35. }
  36. ctx, cancel := installSignals()
  37. defer cancel()
  38. if err := initDB(ctx); err != nil {
  39. return err
  40. }
  41. if len(c.String("password")) < setting.MinPasswordLength {
  42. return fmt.Errorf("Password is not long enough. Needs to be at least %d", setting.MinPasswordLength)
  43. }
  44. if !pwd.IsComplexEnough(c.String("password")) {
  45. return errors.New("Password does not meet complexity requirements")
  46. }
  47. pwned, err := pwd.IsPwned(context.Background(), c.String("password"))
  48. if err != nil {
  49. return err
  50. }
  51. if pwned {
  52. return errors.New("The password you chose is on a list of stolen passwords previously exposed in public data breaches. Please try again with a different password.\nFor more details, see https://haveibeenpwned.com/Passwords")
  53. }
  54. uname := c.String("username")
  55. user, err := user_model.GetUserByName(ctx, uname)
  56. if err != nil {
  57. return err
  58. }
  59. if err = user.SetPassword(c.String("password")); err != nil {
  60. return err
  61. }
  62. if err = user_model.UpdateUserCols(ctx, user, "passwd", "passwd_hash_algo", "salt"); err != nil {
  63. return err
  64. }
  65. fmt.Printf("%s's password has been successfully updated!\n", user.Name)
  66. return nil
  67. }