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

63 line
1.6KB

  1. // Copyright 2019 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 externalaccount
  5. import (
  6. "strings"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/structs"
  9. "github.com/markbates/goth"
  10. )
  11. // LinkAccountToUser link the gothUser to the user
  12. func LinkAccountToUser(user *models.User, gothUser goth.User) error {
  13. loginSource, err := models.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
  14. if err != nil {
  15. return err
  16. }
  17. externalLoginUser := &models.ExternalLoginUser{
  18. ExternalID: gothUser.UserID,
  19. UserID: user.ID,
  20. LoginSourceID: loginSource.ID,
  21. RawData: gothUser.RawData,
  22. Provider: gothUser.Provider,
  23. Email: gothUser.Email,
  24. Name: gothUser.Name,
  25. FirstName: gothUser.FirstName,
  26. LastName: gothUser.LastName,
  27. NickName: gothUser.NickName,
  28. Description: gothUser.Description,
  29. AvatarURL: gothUser.AvatarURL,
  30. Location: gothUser.Location,
  31. AccessToken: gothUser.AccessToken,
  32. AccessTokenSecret: gothUser.AccessTokenSecret,
  33. RefreshToken: gothUser.RefreshToken,
  34. ExpiresAt: gothUser.ExpiresAt,
  35. }
  36. if err := models.LinkExternalToUser(user, externalLoginUser); err != nil {
  37. return err
  38. }
  39. externalID := externalLoginUser.ExternalID
  40. var tp structs.GitServiceType
  41. for _, s := range structs.SupportedFullGitService {
  42. if strings.EqualFold(s.Name(), gothUser.Provider) {
  43. tp = s
  44. break
  45. }
  46. }
  47. if tp.Name() != "" {
  48. return models.UpdateMigrationsByType(tp, externalID, user.ID)
  49. }
  50. return nil
  51. }
上海开阖软件有限公司 沪ICP备12045867号-1