本站源代码
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.

125 lines
2.8KB

  1. // Copyright 2017 The Gitea 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. "code.gitea.io/gitea/modules/auth/openid"
  8. "code.gitea.io/gitea/modules/log"
  9. )
  10. var (
  11. // ErrOpenIDNotExist openid is not known
  12. ErrOpenIDNotExist = errors.New("OpenID is unknown")
  13. )
  14. // UserOpenID is the list of all OpenID identities of a user.
  15. type UserOpenID struct {
  16. ID int64 `xorm:"pk autoincr"`
  17. UID int64 `xorm:"INDEX NOT NULL"`
  18. URI string `xorm:"UNIQUE NOT NULL"`
  19. Show bool `xorm:"DEFAULT false"`
  20. }
  21. // GetUserOpenIDs returns all openid addresses that belongs to given user.
  22. func GetUserOpenIDs(uid int64) ([]*UserOpenID, error) {
  23. openids := make([]*UserOpenID, 0, 5)
  24. if err := x.
  25. Where("uid=?", uid).
  26. Asc("id").
  27. Find(&openids); err != nil {
  28. return nil, err
  29. }
  30. return openids, nil
  31. }
  32. func isOpenIDUsed(e Engine, uri string) (bool, error) {
  33. if len(uri) == 0 {
  34. return true, nil
  35. }
  36. return e.Get(&UserOpenID{URI: uri})
  37. }
  38. // IsOpenIDUsed returns true if the openid has been used.
  39. func IsOpenIDUsed(openid string) (bool, error) {
  40. return isOpenIDUsed(x, openid)
  41. }
  42. // NOTE: make sure openid.URI is normalized already
  43. func addUserOpenID(e Engine, openid *UserOpenID) error {
  44. used, err := isOpenIDUsed(e, openid.URI)
  45. if err != nil {
  46. return err
  47. } else if used {
  48. return ErrOpenIDAlreadyUsed{openid.URI}
  49. }
  50. _, err = e.Insert(openid)
  51. return err
  52. }
  53. // AddUserOpenID adds an pre-verified/normalized OpenID URI to given user.
  54. func AddUserOpenID(openid *UserOpenID) error {
  55. return addUserOpenID(x, openid)
  56. }
  57. // DeleteUserOpenID deletes an openid address of given user.
  58. func DeleteUserOpenID(openid *UserOpenID) (err error) {
  59. var deleted int64
  60. // ask to check UID
  61. var address = UserOpenID{
  62. UID: openid.UID,
  63. }
  64. if openid.ID > 0 {
  65. deleted, err = x.ID(openid.ID).Delete(&address)
  66. } else {
  67. deleted, err = x.
  68. Where("openid=?", openid.URI).
  69. Delete(&address)
  70. }
  71. if err != nil {
  72. return err
  73. } else if deleted != 1 {
  74. return ErrOpenIDNotExist
  75. }
  76. return nil
  77. }
  78. // ToggleUserOpenIDVisibility toggles visibility of an openid address of given user.
  79. func ToggleUserOpenIDVisibility(id int64) (err error) {
  80. _, err = x.Exec("update `user_open_id` set `show` = not `show` where `id` = ?", id)
  81. return err
  82. }
  83. // GetUserByOpenID returns the user object by given OpenID if exists.
  84. func GetUserByOpenID(uri string) (*User, error) {
  85. if len(uri) == 0 {
  86. return nil, ErrUserNotExist{0, uri, 0}
  87. }
  88. uri, err := openid.Normalize(uri)
  89. if err != nil {
  90. return nil, err
  91. }
  92. log.Trace("Normalized OpenID URI: " + uri)
  93. // Otherwise, check in openid table
  94. oid := &UserOpenID{URI: uri}
  95. has, err := x.Get(oid)
  96. if err != nil {
  97. return nil, err
  98. }
  99. if has {
  100. return GetUserByID(oid.UID)
  101. }
  102. return nil, ErrUserNotExist{0, uri, 0}
  103. }
上海开阖软件有限公司 沪ICP备12045867号-1