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