user.go 5.3 KB

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