You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.go 5.6 kB

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