A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

122 lines
2.8 KiB

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