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.

237 lines
5.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. // An 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. func GetUserById(id int64) (*User, error) {
  143. user := new(User)
  144. has, err := orm.Id(id).Get(user)
  145. if err != nil {
  146. return nil, err
  147. }
  148. if !has {
  149. return nil, ErrUserNotExist
  150. }
  151. return user, nil
  152. }
  153. // LoginUserPlain validates user by raw user name and password.
  154. func LoginUserPlain(name, passwd string) (*User, error) {
  155. user := User{Name: name, Passwd: passwd}
  156. if err := user.EncodePasswd(); err != nil {
  157. return nil, err
  158. }
  159. has, err := orm.Get(&user)
  160. if !has {
  161. err = ErrUserNotExist
  162. }
  163. if err != nil {
  164. return nil, err
  165. }
  166. return &user, nil
  167. }
  168. // FollowUser marks someone be another's follower.
  169. func FollowUser(userId int64, followId int64) error {
  170. session := orm.NewSession()
  171. defer session.Close()
  172. session.Begin()
  173. _, err := session.Insert(&Follow{UserId: userId, FollowId: followId})
  174. if err != nil {
  175. session.Rollback()
  176. return err
  177. }
  178. _, err = session.Exec("update user set num_followers = num_followers + 1 where id = ?", followId)
  179. if err != nil {
  180. session.Rollback()
  181. return err
  182. }
  183. _, err = session.Exec("update user set num_followings = num_followings + 1 where id = ?", userId)
  184. if err != nil {
  185. session.Rollback()
  186. return err
  187. }
  188. return session.Commit()
  189. }
  190. // UnFollowUser unmarks someone be another's follower.
  191. func UnFollowUser(userId int64, unFollowId int64) error {
  192. session := orm.NewSession()
  193. defer session.Close()
  194. session.Begin()
  195. _, err := session.Delete(&Follow{UserId: userId, FollowId: unFollowId})
  196. if err != nil {
  197. session.Rollback()
  198. return err
  199. }
  200. _, err = session.Exec("update user set num_followers = num_followers - 1 where id = ?", unFollowId)
  201. if err != nil {
  202. session.Rollback()
  203. return err
  204. }
  205. _, err = session.Exec("update user set num_followings = num_followings - 1 where id = ?", userId)
  206. if err != nil {
  207. session.Rollback()
  208. return err
  209. }
  210. return session.Commit()
  211. }