2019-10-14 11:40:42 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-10-14 11:40:42 +05:30
|
|
|
|
|
|
|
package externalaccount
|
|
|
|
|
|
|
|
import (
|
2023-09-25 18:47:37 +05:30
|
|
|
"context"
|
2024-06-20 18:54:53 +05:30
|
|
|
"strconv"
|
2019-10-14 11:40:42 +05:30
|
|
|
"strings"
|
|
|
|
|
2022-01-02 18:42:35 +05:30
|
|
|
"code.gitea.io/gitea/models/auth"
|
2023-09-09 02:39:23 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-10-14 11:40:42 +05:30
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
|
|
|
|
"github.com/markbates/goth"
|
|
|
|
)
|
|
|
|
|
2023-10-14 14:07:24 +05:30
|
|
|
func toExternalLoginUser(ctx context.Context, user *user_model.User, gothUser goth.User) (*user_model.ExternalLoginUser, error) {
|
|
|
|
authSource, err := auth.GetActiveOAuth2SourceByName(ctx, gothUser.Provider)
|
2019-10-14 11:40:42 +05:30
|
|
|
if err != nil {
|
2021-12-14 14:07:11 +05:30
|
|
|
return nil, err
|
2019-10-14 11:40:42 +05:30
|
|
|
}
|
2021-12-14 14:07:11 +05:30
|
|
|
return &user_model.ExternalLoginUser{
|
2019-10-14 11:40:42 +05:30
|
|
|
ExternalID: gothUser.UserID,
|
|
|
|
UserID: user.ID,
|
2022-01-02 18:42:35 +05:30
|
|
|
LoginSourceID: authSource.ID,
|
2019-10-14 11:40:42 +05:30
|
|
|
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 14:07:11 +05:30
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkAccountToUser link the gothUser to the user
|
2023-09-25 18:47:37 +05:30
|
|
|
func LinkAccountToUser(ctx context.Context, user *user_model.User, gothUser goth.User) error {
|
2023-10-14 14:07:24 +05:30
|
|
|
externalLoginUser, err := toExternalLoginUser(ctx, user, gothUser)
|
2021-12-14 14:07:11 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-14 11:40:42 +05:30
|
|
|
}
|
|
|
|
|
2023-10-14 14:07:24 +05:30
|
|
|
if err := user_model.LinkExternalToUser(ctx, user, externalLoginUser); err != nil {
|
2019-10-14 11:40:42 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-17 07:36:28 +05:30
|
|
|
externalID := externalLoginUser.ExternalID
|
2019-10-14 11:40:42 +05:30
|
|
|
|
|
|
|
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 18:47:37 +05:30
|
|
|
return UpdateMigrationsByType(ctx, tp, externalID, user.ID)
|
2019-10-14 11:40:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-14 14:07:11 +05:30
|
|
|
|
|
|
|
// UpdateExternalUser updates external user's information
|
2023-10-14 14:07:24 +05:30
|
|
|
func UpdateExternalUser(ctx context.Context, user *user_model.User, gothUser goth.User) error {
|
|
|
|
externalLoginUser, err := toExternalLoginUser(ctx, user, gothUser)
|
2021-12-14 14:07:11 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-14 14:07:24 +05:30
|
|
|
return user_model.UpdateExternalUserByExternalID(ctx, externalLoginUser)
|
2021-12-14 14:07:11 +05:30
|
|
|
}
|
2023-09-09 02:39:23 +05:30
|
|
|
|
|
|
|
// UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID
|
2023-09-25 18:47:37 +05:30
|
|
|
func UpdateMigrationsByType(ctx context.Context, tp structs.GitServiceType, externalUserID string, userID int64) error {
|
2024-06-20 18:54:53 +05:30
|
|
|
// Skip update if externalUserID is not a valid numeric ID or exceeds int64
|
|
|
|
if _, err := strconv.ParseInt(externalUserID, 10, 64); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-29 17:42:54 +05:30
|
|
|
if err := issues_model.UpdateIssuesMigrationsByType(ctx, tp, externalUserID, userID); err != nil {
|
2023-09-09 02:39:23 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-29 17:42:54 +05:30
|
|
|
if err := issues_model.UpdateCommentsMigrationsByType(ctx, tp, externalUserID, userID); err != nil {
|
2023-09-09 02:39:23 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-25 18:47:37 +05:30
|
|
|
if err := repo_model.UpdateReleasesMigrationsByType(ctx, tp, externalUserID, userID); err != nil {
|
2023-09-09 02:39:23 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-29 17:42:54 +05:30
|
|
|
if err := issues_model.UpdateReactionsMigrationsByType(ctx, tp, externalUserID, userID); err != nil {
|
2023-09-09 02:39:23 +05:30
|
|
|
return err
|
|
|
|
}
|
2023-09-29 17:42:54 +05:30
|
|
|
return issues_model.UpdateReviewsMigrationsByType(ctx, tp, externalUserID, userID)
|
2023-09-09 02:39:23 +05:30
|
|
|
}
|