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.

user.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/dchest/scrypt"
  8. ) // user type
  9. const (
  10. Individual = iota + 1
  11. Organization
  12. )
  13. // login type
  14. const (
  15. Plain = iota + 1
  16. LDAP
  17. )
  18. type User struct {
  19. Id int64
  20. LowerName string `xorm:"unique not null"`
  21. Name string `xorm:"unique not null"`
  22. Email string `xorm:"unique not null"`
  23. Passwd string `xorm:"not null"`
  24. LoginType int
  25. Type int
  26. NumFollowers int
  27. NumFollowings int
  28. NumStars int
  29. NumRepos int
  30. Avatar string `xorm:"varchar(2048) not null"`
  31. Created time.Time `xorm:"created"`
  32. Updated time.Time `xorm:"updated"`
  33. }
  34. type Follow struct {
  35. Id int64
  36. UserId int64 `xorm:"unique(s)"`
  37. FollowId int64 `xorm:"unique(s)"`
  38. Created time.Time `xorm:"created"`
  39. }
  40. const (
  41. OpCreateRepo = iota + 1
  42. OpDeleteRepo
  43. OpStarRepo
  44. OpFollowRepo
  45. OpCommitRepo
  46. OpPullRequest
  47. )
  48. type Action struct {
  49. Id int64
  50. UserId int64
  51. OpType int
  52. RepoId int64
  53. Content string
  54. Created time.Time `xorm:"created"`
  55. }
  56. var (
  57. ErrUserNotExist = errors.New("User not exist")
  58. )
  59. // user's name should be noncased unique
  60. func IsUserExist(name string) (bool, error) {
  61. return orm.Get(&User{LowerName: strings.ToLower(name)})
  62. }
  63. func RegisterUser(user *User) error {
  64. _, err := orm.Insert(user)
  65. return err
  66. }
  67. func UpdateUser(user *User) error {
  68. _, err := orm.Id(user.Id).Update(user)
  69. return err
  70. }
  71. func (user *User) EncodePasswd(pass string) error {
  72. newPasswd, err := scrypt.Key([]byte(user.Passwd), []byte("!#@FDEWREWR&*("), 16384, 8, 1, 64)
  73. user.Passwd = fmt.Sprintf("%x", newPasswd)
  74. return err
  75. }
  76. func LoginUserPlain(name, passwd string) (*User, error) {
  77. user := User{Name: name}
  78. err := user.EncodePasswd(passwd)
  79. if err != nil {
  80. return nil, err
  81. }
  82. has, err := orm.Get(&user)
  83. if !has {
  84. err = ErrUserNotExist
  85. }
  86. if err != nil {
  87. return nil, err
  88. }
  89. return &user, nil
  90. }
  91. func FollowUser(userId int64, followId int64) error {
  92. session := orm.NewSession()
  93. defer session.Close()
  94. session.Begin()
  95. _, err := session.Insert(&Follow{UserId: userId, FollowId: followId})
  96. if err != nil {
  97. session.Rollback()
  98. return err
  99. }
  100. _, err = session.Exec("update user set num_followers = num_followers + 1 where id = ?", followId)
  101. if err != nil {
  102. session.Rollback()
  103. return err
  104. }
  105. _, err = session.Exec("update user set num_followings = num_followings + 1 where id = ?", userId)
  106. if err != nil {
  107. session.Rollback()
  108. return err
  109. }
  110. return session.Commit()
  111. }
  112. func UnFollowUser(userId int64, unFollowId int64) error {
  113. session := orm.NewSession()
  114. defer session.Close()
  115. session.Begin()
  116. _, err := session.Delete(&Follow{UserId: userId, FollowId: unFollowId})
  117. if err != nil {
  118. session.Rollback()
  119. return err
  120. }
  121. _, err = session.Exec("update user set num_followers = num_followers - 1 where id = ?", unFollowId)
  122. if err != nil {
  123. session.Rollback()
  124. return err
  125. }
  126. _, err = session.Exec("update user set num_followings = num_followings - 1 where id = ?", userId)
  127. if err != nil {
  128. session.Rollback()
  129. return err
  130. }
  131. return session.Commit()
  132. }