Skip to content

Commit 4516fe2

Browse files
Backport recent changes to legacy go
Signed-off-by: Lukasz Gryglicki <lgryglicki@cncf.io> Assisted by [OpenAI](https://platform.openai.com/) Assisted by [GitHub Copilot](https://github.com/features/copilot)
1 parent f053d26 commit 4516fe2

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

cla-backend-go/v2/sign/service.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,7 +2061,7 @@ func (s *service) populateUserDetails(ctx context.Context, signatureReferenceTyp
20612061
if userModel.Username != "" {
20622062
userSignDetails.userSignatureName = userModel.Username
20632063
}
2064-
if userEmail := getUserEmail(userModel, preferredEmail, allowLFEmail); userEmail != "" {
2064+
if userEmail := getUserEmail(userModel, preferredEmail, latestSignature.SignatureReturnURLType, allowLFEmail); userEmail != "" {
20652065
userSignDetails.userSignatureEmail = userEmail
20662066
}
20672067
}
@@ -2162,11 +2162,15 @@ func getTabsFromDocument(document *v1Models.ClaGroupDocument, documentID string,
21622162
}
21632163

21642164
// helper function to get user email
2165-
func getUserEmail(user *v1Models.User, preferredEmail string, allowLFEmail bool) string {
2165+
func getUserEmail(user *v1Models.User, preferredEmail string, providerType string, allowLFEmail bool) string {
21662166
if user == nil {
21672167
return ""
21682168
}
21692169
if preferredEmail != "" {
2170+
if strings.EqualFold(providerType, "github") || strings.EqualFold(providerType, "gitlab") {
2171+
return preferredEmail
2172+
}
2173+
21702174
if utils.StringInSlice(preferredEmail, user.Emails) || (allowLFEmail && user.LfEmail == strfmt.Email(preferredEmail)) {
21712175
return preferredEmail
21722176
}
@@ -2244,7 +2248,7 @@ func (s *service) createDefaultIndividualValues(user *v1Models.User, preferredEm
22442248
}
22452249
}
22462250

2247-
if userEmail := getUserEmail(user, preferredEmail, allowLFEmail); userEmail != "" {
2251+
if userEmail := getUserEmail(user, preferredEmail, "", allowLFEmail); userEmail != "" {
22482252
defaultValues["email"] = userEmail
22492253
}
22502254

cla-backend-legacy/internal/api/github_oauth.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,13 @@ func (h *Handlers) githubGetOrCreateUser(ctx context.Context, sess middleware.Se
265265

266266
now := time.Now().UTC()
267267
if userItem != nil {
268-
// Update existing user: set github id, username, emails.
268+
// Update existing user: set github id, username, display name and emails
269269
if githubLogin != "" {
270270
userItem["user_github_username"] = githubLogin
271271
}
272+
if githubName != "" {
273+
userItem["user_name"] = githubName
274+
}
272275
userItem["user_emails"] = emails
273276
userItem["user_github_id"] = githubID
274277
userItem["date_modified"] = formatPynamoDateTimeUTC(now)

cla-backend-legacy/internal/api/handlers.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,31 @@ func (h *Handlers) putAuditEventBestEffort(ctx context.Context, in auditEventInp
713713
_ = h.events.PutItem(ctx, item)
714714
}
715715

716+
func (h *Handlers) refreshStoredUserName(ctx context.Context, item map[string]types.AttributeValue, authUser *auth.AuthUser) map[string]types.AttributeValue {
717+
if h == nil || h.users == nil || item == nil || authUser == nil {
718+
return item
719+
}
720+
721+
userName := strings.TrimSpace(authUser.Name)
722+
if userName == "" || getAttrString(item, "user_name") == userName {
723+
return item
724+
}
725+
726+
updated := make(map[string]types.AttributeValue, len(item)+1)
727+
for k, v := range item {
728+
updated[k] = v
729+
}
730+
updated["user_name"] = &types.AttributeValueMemberS{Value: userName}
731+
updated["date_modified"] = &types.AttributeValueMemberS{Value: formatPynamoDateTimeUTC(time.Now().UTC())}
732+
733+
if err := h.users.PutItem(ctx, updated); err != nil {
734+
logging.Warnf("refreshStoredUserName: unable to update user_id=%s lf_username=%s: %v", getAttrString(item, "user_id"), authUser.Username, err)
735+
return item
736+
}
737+
738+
return updated
739+
}
740+
716741
func (h *Handlers) getOrCreateUser(ctx context.Context, authUser *auth.AuthUser) (map[string]types.AttributeValue, bool, error) {
717742
if authUser == nil {
718743
return nil, false, fmt.Errorf("missing auth user")
@@ -723,7 +748,7 @@ func (h *Handlers) getOrCreateUser(ctx context.Context, authUser *auth.AuthUser)
723748
return nil, false, err
724749
}
725750
if len(items) > 0 {
726-
return items[0], false, nil
751+
return h.refreshStoredUserName(ctx, items[0], authUser), false, nil
727752
}
728753

729754
now := time.Now().UTC()
@@ -1276,6 +1301,11 @@ func (h *Handlers) GetHealthV2(w http.ResponseWriter, r *http.Request) {
12761301
// Calls: cla.controllers.user.get_user
12771302

12781303
func (h *Handlers) GetUserV2(w http.ResponseWriter, r *http.Request) {
1304+
_, authErrResp, err := h.authValidator.Authenticate(r.Header)
1305+
if err != nil {
1306+
respond.JSON(w, http.StatusUnauthorized, authErrResp)
1307+
return
1308+
}
12791309
if h.users == nil {
12801310
respond.JSON(w, http.StatusInternalServerError, "users store not configured")
12811311
return

0 commit comments

Comments
 (0)