選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

user.go 4.7KB

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