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.

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