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