2019-10-14 02:10:42 -04:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-10-14 02:10:42 -04:00
|
|
|
|
|
|
|
package externalaccount
|
|
|
|
|
|
|
|
import (
|
2023-09-25 09:17:37 -04:00
|
|
|
"context"
|
2019-10-14 02:10:42 -04:00
|
|
|
"strings"
|
|
|
|
|
2022-01-02 08:12:35 -05:00
|
|
|
"code.gitea.io/gitea/models/auth"
|
2023-09-08 17:09:23 -04:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-10-14 02:10:42 -04:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
|
|
|
|
"github.com/markbates/goth"
|
|
|
|
)
|
|
|
|
|
2021-12-14 03:37:11 -05:00
|
|
|
func toExternalLoginUser(user *user_model.User, gothUser goth.User) (*user_model.ExternalLoginUser, error) {
|
2022-01-02 08:12:35 -05:00
|
|
|
authSource, err := auth.GetActiveOAuth2SourceByName(gothUser.Provider)
|
2019-10-14 02:10:42 -04:00
|
|
|
if err != nil {
|
2021-12-14 03:37:11 -05:00
|
|
|
return nil, err
|
2019-10-14 02:10:42 -04:00
|
|
|
}
|
2021-12-14 03:37:11 -05:00
|
|
|
return &user_model.ExternalLoginUser{
|
2019-10-14 02:10:42 -04:00
|
|
|
ExternalID: gothUser.UserID,
|
|
|
|
UserID: user.ID,
|
2022-01-02 08:12:35 -05:00
|
|
|
LoginSourceID: authSource.ID,
|
2019-10-14 02:10:42 -04:00
|
|
|
RawData: gothUser.RawData,
|
|
|
|
Provider: gothUser.Provider,
|
|
|
|
Email: gothUser.Email,
|
|
|
|
Name: gothUser.Name,
|
|
|
|
FirstName: gothUser.FirstName,
|
|
|
|
LastName: gothUser.LastName,
|
|
|
|
NickName: gothUser.NickName,
|
|
|
|
Description: gothUser.Description,
|
|
|
|
AvatarURL: gothUser.AvatarURL,
|
|
|
|
Location: gothUser.Location,
|
|
|
|
AccessToken: gothUser.AccessToken,
|
|
|
|
AccessTokenSecret: gothUser.AccessTokenSecret,
|
|
|
|
RefreshToken: gothUser.RefreshToken,
|
|
|
|
ExpiresAt: gothUser.ExpiresAt,
|
2021-12-14 03:37:11 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkAccountToUser link the gothUser to the user
|
2023-09-25 09:17:37 -04:00
|
|
|
func LinkAccountToUser(ctx context.Context, user *user_model.User, gothUser goth.User) error {
|
2021-12-14 03:37:11 -05:00
|
|
|
externalLoginUser, err := toExternalLoginUser(user, gothUser)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-14 02:10:42 -04:00
|
|
|
}
|
|
|
|
|
2021-11-28 09:11:58 -05:00
|
|
|
if err := user_model.LinkExternalToUser(user, externalLoginUser); err != nil {
|
2019-10-14 02:10:42 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-16 22:06:28 -04:00
|
|
|
externalID := externalLoginUser.ExternalID
|
2019-10-14 02:10:42 -04:00
|
|
|
|
|
|
|
var tp structs.GitServiceType
|
|
|
|
for _, s := range structs.SupportedFullGitService {
|
|
|
|
if strings.EqualFold(s.Name(), gothUser.Provider) {
|
|
|
|
tp = s
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tp.Name() != "" {
|
2023-09-25 09:17:37 -04:00
|
|
|
return UpdateMigrationsByType(ctx, tp, externalID, user.ID)
|
2019-10-14 02:10:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-14 03:37:11 -05:00
|
|
|
|
|
|
|
// UpdateExternalUser updates external user's information
|
|
|
|
func UpdateExternalUser(user *user_model.User, gothUser goth.User) error {
|
|
|
|
externalLoginUser, err := toExternalLoginUser(user, gothUser)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user_model.UpdateExternalUserByExternalID(externalLoginUser)
|
|
|
|
}
|
2023-09-08 17:09:23 -04:00
|
|
|
|
|
|
|
// UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID
|
2023-09-25 09:17:37 -04:00
|
|
|
func UpdateMigrationsByType(ctx context.Context, tp structs.GitServiceType, externalUserID string, userID int64) error {
|
2023-09-08 17:09:23 -04:00
|
|
|
if err := issues_model.UpdateIssuesMigrationsByType(tp, externalUserID, userID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := issues_model.UpdateCommentsMigrationsByType(tp, externalUserID, userID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-25 09:17:37 -04:00
|
|
|
if err := repo_model.UpdateReleasesMigrationsByType(ctx, tp, externalUserID, userID); err != nil {
|
2023-09-08 17:09:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := issues_model.UpdateReactionsMigrationsByType(tp, externalUserID, userID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return issues_model.UpdateReviewsMigrationsByType(tp, externalUserID, userID)
|
|
|
|
}
|