gitea源码

user_system.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package user
  4. import (
  5. "strings"
  6. "code.gitea.io/gitea/modules/structs"
  7. )
  8. const (
  9. GhostUserID int64 = -1
  10. GhostUserName = "Ghost"
  11. )
  12. // NewGhostUser creates and returns a fake user for someone has deleted their account.
  13. func NewGhostUser() *User {
  14. return &User{
  15. ID: GhostUserID,
  16. Name: GhostUserName,
  17. LowerName: strings.ToLower(GhostUserName),
  18. }
  19. }
  20. func IsGhostUserName(name string) bool {
  21. return strings.EqualFold(name, GhostUserName)
  22. }
  23. // IsGhost check if user is fake user for a deleted account
  24. func (u *User) IsGhost() bool {
  25. if u == nil {
  26. return false
  27. }
  28. return u.ID == GhostUserID && u.Name == GhostUserName
  29. }
  30. const (
  31. ActionsUserID int64 = -2
  32. ActionsUserName = "gitea-actions"
  33. ActionsUserEmail = "teabot@gitea.io"
  34. )
  35. func IsGiteaActionsUserName(name string) bool {
  36. return strings.EqualFold(name, ActionsUserName)
  37. }
  38. // NewActionsUser creates and returns a fake user for running the actions.
  39. func NewActionsUser() *User {
  40. return &User{
  41. ID: ActionsUserID,
  42. Name: ActionsUserName,
  43. LowerName: ActionsUserName,
  44. IsActive: true,
  45. FullName: "Gitea Actions",
  46. Email: ActionsUserEmail,
  47. KeepEmailPrivate: true,
  48. LoginName: ActionsUserName,
  49. Type: UserTypeBot,
  50. AllowCreateOrganization: true,
  51. Visibility: structs.VisibleTypePublic,
  52. }
  53. }
  54. func (u *User) IsGiteaActions() bool {
  55. return u != nil && u.ID == ActionsUserID
  56. }
  57. func GetSystemUserByName(name string) *User {
  58. if IsGhostUserName(name) {
  59. return NewGhostUser()
  60. }
  61. if IsGiteaActionsUserName(name) {
  62. return NewActionsUser()
  63. }
  64. return nil
  65. }