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.

76 lines
1.7 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
  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. )
  8. // OT: Oauth2 Type
  9. const (
  10. OT_GITHUB = iota + 1
  11. OT_GOOGLE
  12. OT_TWITTER
  13. OT_QQ
  14. OT_WEIBO
  15. OT_BITBUCKET
  16. OT_OSCHINA
  17. OT_FACEBOOK
  18. )
  19. var (
  20. ErrOauth2RecordNotExist = errors.New("OAuth2 record does not exist")
  21. ErrOauth2NotAssociated = errors.New("OAuth2 is not associated with user")
  22. )
  23. type Oauth2 struct {
  24. Id int64
  25. Uid int64 `xorm:"unique(s)"` // userId
  26. User *User `xorm:"-"`
  27. Type int `xorm:"unique(s) unique(oauth)"` // twitter,github,google...
  28. Identity string `xorm:"unique(s) unique(oauth)"` // id..
  29. Token string `xorm:"TEXT not null"`
  30. }
  31. func BindUserOauth2(userId, oauthId int64) error {
  32. _, err := orm.Id(oauthId).Update(&Oauth2{Uid: userId})
  33. return err
  34. }
  35. func AddOauth2(oa *Oauth2) error {
  36. _, err := orm.Insert(oa)
  37. return err
  38. }
  39. func GetOauth2(identity string) (oa *Oauth2, err error) {
  40. oa = &Oauth2{Identity: identity}
  41. isExist, err := orm.Get(oa)
  42. if err != nil {
  43. return
  44. } else if !isExist {
  45. return nil, ErrOauth2RecordNotExist
  46. } else if oa.Uid == -1 {
  47. return oa, ErrOauth2NotAssociated
  48. }
  49. oa.User, err = GetUserById(oa.Uid)
  50. return oa, err
  51. }
  52. func GetOauth2ById(id int64) (oa *Oauth2, err error) {
  53. oa = new(Oauth2)
  54. has, err := orm.Id(id).Get(oa)
  55. if err != nil {
  56. return nil, err
  57. } else if !has {
  58. return nil, ErrOauth2RecordNotExist
  59. }
  60. return oa, nil
  61. }
  62. // GetOauthByUserId returns list of oauthes that are releated to given user.
  63. func GetOauthByUserId(uid int64) (oas []*Oauth2, err error) {
  64. err = orm.Find(&oas, Oauth2{Uid: uid})
  65. return oas, err
  66. }