Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

admin_user_change_password.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. "code.gitea.io/gitea/modules/auth/password"
  9. "code.gitea.io/gitea/modules/optional"
  10. "code.gitea.io/gitea/modules/setting"
  11. user_service "code.gitea.io/gitea/services/user"
  12. "github.com/urfave/cli/v2"
  13. )
  14. var microcmdUserChangePassword = &cli.Command{
  15. Name: "change-password",
  16. Usage: "Change a user's password",
  17. Action: runChangePassword,
  18. Flags: []cli.Flag{
  19. &cli.StringFlag{
  20. Name: "username",
  21. Aliases: []string{"u"},
  22. Value: "",
  23. Usage: "The user to change password for",
  24. },
  25. &cli.StringFlag{
  26. Name: "password",
  27. Aliases: []string{"p"},
  28. Value: "",
  29. Usage: "New password to set for user",
  30. },
  31. &cli.BoolFlag{
  32. Name: "must-change-password",
  33. Usage: "User must change password (can be disabled by --must-change-password=false)",
  34. Value: true,
  35. },
  36. },
  37. }
  38. func runChangePassword(c *cli.Context) error {
  39. if err := argsSet(c, "username", "password"); err != nil {
  40. return err
  41. }
  42. ctx, cancel := installSignals()
  43. defer cancel()
  44. if err := initDB(ctx); err != nil {
  45. return err
  46. }
  47. user, err := user_model.GetUserByName(ctx, c.String("username"))
  48. if err != nil {
  49. return err
  50. }
  51. opts := &user_service.UpdateAuthOptions{
  52. Password: optional.Some(c.String("password")),
  53. MustChangePassword: optional.Some(c.Bool("must-change-password")),
  54. }
  55. if err := user_service.UpdateAuth(ctx, user, opts); err != nil {
  56. switch {
  57. case errors.Is(err, password.ErrMinLength):
  58. return fmt.Errorf("password is not long enough, needs to be at least %d characters", setting.MinPasswordLength)
  59. case errors.Is(err, password.ErrComplexity):
  60. return errors.New("password does not meet complexity requirements")
  61. case errors.Is(err, password.ErrIsPwned):
  62. return errors.New("the password is in a list of stolen passwords previously exposed in public data breaches, please try again with a different password, to see more details: https://haveibeenpwned.com/Passwords")
  63. default:
  64. return err
  65. }
  66. }
  67. fmt.Printf("%s's password has been successfully updated!\n", user.Name)
  68. return nil
  69. }