user.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2014 The Gogs 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 (
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "github.com/dchest/scrypt"
  11. )
  12. // User types.
  13. const (
  14. UT_INDIVIDUAL = iota + 1
  15. UT_ORGANIZATION
  16. )
  17. // Login types.
  18. const (
  19. LT_PLAIN = iota + 1
  20. LT_LDAP
  21. )
  22. // A User represents the object of individual and member of organization.
  23. type User struct {
  24. Id int64
  25. LowerName string `xorm:"unique not null"`
  26. Name string `xorm:"unique not null"`
  27. Email string `xorm:"unique not null"`
  28. Passwd string `xorm:"not null"`
  29. LoginType int
  30. Type int
  31. NumFollowers int
  32. NumFollowings int
  33. NumStars int
  34. NumRepos int
  35. Avatar string `xorm:"varchar(2048) not null"`
  36. Created time.Time `xorm:"created"`
  37. Updated time.Time `xorm:"updated"`
  38. }
  39. // A Follow represents
  40. type Follow struct {
  41. Id int64
  42. UserId int64 `xorm:"unique(s)"`
  43. FollowId int64 `xorm:"unique(s)"`
  44. Created time.Time `xorm:"created"`
  45. }
  46. // Operation types of repository.
  47. const (
  48. OP_CREATE_REPO = iota + 1
  49. OP_DELETE_REPO
  50. OP_STAR_REPO
  51. OP_FOLLOW_REPO
  52. OP_COMMIT_REPO
  53. OP_PULL_REQUEST
  54. )
  55. // A Action represents
  56. type Action struct {
  57. Id int64
  58. UserId int64
  59. OpType int
  60. RepoId int64
  61. Content string
  62. Created time.Time `xorm:"created"`
  63. }
  64. var (
  65. ErrUserAlreadyExist = errors.New("User already exist")
  66. ErrUserNotExist = errors.New("User does not exist")
  67. )
  68. // IsUserExist checks if given user name exist,
  69. // the user name should be noncased unique.
  70. func IsUserExist(name string) (bool, error) {
  71. return orm.Get(&User{LowerName: strings.ToLower(name)})
  72. }
  73. // validateUser checks if user exist.
  74. func validateUser(name string) error {
  75. isExist, err := IsUserExist(name)
  76. if err != nil {
  77. return err
  78. } else if isExist {
  79. return ErrUserAlreadyExist
  80. }
  81. return nil
  82. }
  83. // RegisterUser creates record of a new user.
  84. func RegisterUser(user *User) (err error) {
  85. if err = validateUser(user.Name); err != nil {
  86. return err
  87. }
  88. _, err = orm.Insert(user)
  89. return err
  90. }
  91. // UpdateUser updates user's information.
  92. func UpdateUser(user *User) (err error) {
  93. _, err = orm.Id(user.Id).Update(user)
  94. return err
  95. }
  96. // DeleteUser completely deletes everything of the user.
  97. func DeleteUser(user *User) error {
  98. // TODO: check if has ownership of any repository.
  99. _, err := orm.Delete(user)
  100. // TODO: delete and update follower information.
  101. return err
  102. }
  103. // EncodePasswd encodes password to safe format.
  104. func (user *User) EncodePasswd(pass string) error {
  105. newPasswd, err := scrypt.Key([]byte(user.Passwd), []byte("!#@FDEWREWR&*("), 16384, 8, 1, 64)
  106. user.Passwd = fmt.Sprintf("%x", newPasswd)
  107. return err
  108. }
  109. // LoginUserPlain validates user by raw user name and password.
  110. func LoginUserPlain(name, passwd string) (*User, error) {
  111. user := User{Name: name}
  112. if err := user.EncodePasswd(passwd); err != nil {
  113. return nil, err
  114. }
  115. has, err := orm.Get(&user)
  116. if !has {
  117. err = ErrUserNotExist
  118. }
  119. if err != nil {
  120. return nil, err
  121. }
  122. return &user, nil
  123. }
  124. // FollowUser marks someone be another's follower.
  125. func FollowUser(userId int64, followId int64) error {
  126. session := orm.NewSession()
  127. defer session.Close()
  128. session.Begin()
  129. _, err := session.Insert(&Follow{UserId: userId, FollowId: followId})
  130. if err != nil {
  131. session.Rollback()
  132. return err
  133. }
  134. _, err = session.Exec("update user set num_followers = num_followers + 1 where id = ?", followId)
  135. if err != nil {
  136. session.Rollback()
  137. return err
  138. }
  139. _, err = session.Exec("update user set num_followings = num_followings + 1 where id = ?", userId)
  140. if err != nil {
  141. session.Rollback()
  142. return err
  143. }
  144. return session.Commit()
  145. }
  146. // UnFollowUser unmarks someone be another's follower.
  147. func UnFollowUser(userId int64, unFollowId int64) error {
  148. session := orm.NewSession()
  149. defer session.Close()
  150. session.Begin()
  151. _, err := session.Delete(&Follow{UserId: userId, FollowId: unFollowId})
  152. if err != nil {
  153. session.Rollback()
  154. return err
  155. }
  156. _, err = session.Exec("update user set num_followers = num_followers - 1 where id = ?", unFollowId)
  157. if err != nil {
  158. session.Rollback()
  159. return err
  160. }
  161. _, err = session.Exec("update user set num_followings = num_followings - 1 where id = ?", userId)
  162. if err != nil {
  163. session.Rollback()
  164. return err
  165. }
  166. return session.Commit()
  167. }