A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

52 lines
1.5 KiB

  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import "strings"
  6. // UserRedirect represents that a user name should be redirected to another
  7. type UserRedirect struct {
  8. ID int64 `xorm:"pk autoincr"`
  9. LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
  10. RedirectUserID int64 // userID to redirect to
  11. }
  12. // LookupUserRedirect look up userID if a user has a redirect name
  13. func LookupUserRedirect(userName string) (int64, error) {
  14. userName = strings.ToLower(userName)
  15. redirect := &UserRedirect{LowerName: userName}
  16. if has, err := x.Get(redirect); err != nil {
  17. return 0, err
  18. } else if !has {
  19. return 0, ErrUserRedirectNotExist{Name: userName}
  20. }
  21. return redirect.RedirectUserID, nil
  22. }
  23. // newUserRedirect create a new user redirect
  24. func newUserRedirect(e Engine, ID int64, oldUserName, newUserName string) error {
  25. oldUserName = strings.ToLower(oldUserName)
  26. newUserName = strings.ToLower(newUserName)
  27. if err := deleteUserRedirect(e, newUserName); err != nil {
  28. return err
  29. }
  30. if _, err := e.Insert(&UserRedirect{
  31. LowerName: oldUserName,
  32. RedirectUserID: ID,
  33. }); err != nil {
  34. return err
  35. }
  36. return nil
  37. }
  38. // deleteUserRedirect delete any redirect from the specified user name to
  39. // anything else
  40. func deleteUserRedirect(e Engine, userName string) error {
  41. userName = strings.ToLower(userName)
  42. _, err := e.Delete(&UserRedirect{LowerName: userName})
  43. return err
  44. }