user.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. user.LowerName = strings.ToLower(user.Name)
  89. // TODO: generate Avatar address.
  90. user.Created = time.Now()
  91. user.Updated = time.Now()
  92. _, err = orm.Insert(user)
  93. return err
  94. }
  95. // UpdateUser updates user's information.
  96. func UpdateUser(user *User) (err error) {
  97. _, err = orm.Id(user.Id).Update(user)
  98. return err
  99. }
  100. // DeleteUser completely deletes everything of the user.
  101. func DeleteUser(user *User) error {
  102. // TODO: check if has ownership of any repository.
  103. _, err := orm.Delete(user)
  104. // TODO: delete and update follower information.
  105. return err
  106. }
  107. // EncodePasswd encodes password to safe format.
  108. func (user *User) EncodePasswd(pass string) error {
  109. newPasswd, err := scrypt.Key([]byte(user.Passwd), []byte("!#@FDEWREWR&*("), 16384, 8, 1, 64)
  110. user.Passwd = fmt.Sprintf("%x", newPasswd)
  111. return err
  112. }
  113. // LoginUserPlain validates user by raw user name and password.
  114. func LoginUserPlain(name, passwd string) (*User, error) {
  115. user := User{Name: name}
  116. if err := user.EncodePasswd(passwd); err != nil {
  117. return nil, err
  118. }
  119. has, err := orm.Get(&user)
  120. if !has {
  121. err = ErrUserNotExist
  122. }
  123. if err != nil {
  124. return nil, err
  125. }
  126. return &user, nil
  127. }
  128. // FollowUser marks someone be another's follower.
  129. func FollowUser(userId int64, followId int64) error {
  130. session := orm.NewSession()
  131. defer session.Close()
  132. session.Begin()
  133. _, err := session.Insert(&Follow{UserId: userId, FollowId: followId})
  134. if err != nil {
  135. session.Rollback()
  136. return err
  137. }
  138. _, err = session.Exec("update user set num_followers = num_followers + 1 where id = ?", followId)
  139. if err != nil {
  140. session.Rollback()
  141. return err
  142. }
  143. _, err = session.Exec("update user set num_followings = num_followings + 1 where id = ?", userId)
  144. if err != nil {
  145. session.Rollback()
  146. return err
  147. }
  148. return session.Commit()
  149. }
  150. // UnFollowUser unmarks someone be another's follower.
  151. func UnFollowUser(userId int64, unFollowId int64) error {
  152. session := orm.NewSession()
  153. defer session.Close()
  154. session.Begin()
  155. _, err := session.Delete(&Follow{UserId: userId, FollowId: unFollowId})
  156. if err != nil {
  157. session.Rollback()
  158. return err
  159. }
  160. _, err = session.Exec("update user set num_followers = num_followers - 1 where id = ?", unFollowId)
  161. if err != nil {
  162. session.Rollback()
  163. return err
  164. }
  165. _, err = session.Exec("update user set num_followings = num_followings - 1 where id = ?", userId)
  166. if err != nil {
  167. session.Rollback()
  168. return err
  169. }
  170. return session.Commit()
  171. }