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.

must_change_password.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package user
  4. import (
  5. "context"
  6. "strings"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/modules/util"
  9. "xorm.io/builder"
  10. )
  11. func SetMustChangePassword(ctx context.Context, all, mustChangePassword bool, include, exclude []string) (int64, error) {
  12. sliceTrimSpaceDropEmpty := func(input []string) []string {
  13. output := make([]string, 0, len(input))
  14. for _, in := range input {
  15. in = strings.ToLower(strings.TrimSpace(in))
  16. if in == "" {
  17. continue
  18. }
  19. output = append(output, in)
  20. }
  21. return output
  22. }
  23. var cond builder.Cond
  24. // Only include the users where something changes to get an accurate count
  25. cond = builder.Neq{"must_change_password": mustChangePassword}
  26. if !all {
  27. include = sliceTrimSpaceDropEmpty(include)
  28. if len(include) == 0 {
  29. return 0, util.NewSilentWrapErrorf(util.ErrInvalidArgument, "no users to include provided")
  30. }
  31. cond = cond.And(builder.In("lower_name", include))
  32. }
  33. exclude = sliceTrimSpaceDropEmpty(exclude)
  34. if len(exclude) > 0 {
  35. cond = cond.And(builder.NotIn("lower_name", exclude))
  36. }
  37. return db.GetEngine(ctx).Where(cond).MustCols("must_change_password").Update(&User{MustChangePassword: mustChangePassword})
  38. }