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.

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