From 6de2701bb34da3ab0e9f9e6038541eecbec1d7e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dachary?= Date: Thu, 20 Jul 2023 14:35:58 +0200 Subject: [PATCH] [F3] introduce UserTypeF3 To avoid conflicts should UserTypeRemoteUser be used differently by Gitea --- models/user/search.go | 7 ++++++- models/user/user.go | 16 ++++++++++++---- services/f3/promote.go | 11 ++++++++--- tests/integration/f3_test.go | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/models/user/search.go b/models/user/search.go index bf35fcd9eb..8e6e93b0f6 100644 --- a/models/user/search.go +++ b/models/user/search.go @@ -39,7 +39,12 @@ type SearchUserOptions struct { } func (opts *SearchUserOptions) toSearchQueryBase() *xorm.Session { - var cond builder.Cond = builder.Eq{"type": opts.Type} + var cond builder.Cond + if opts.Type == UserTypeIndividual { + cond = builder.In("type", UserTypeIndividual, UserTypeF3) + } else { + cond = builder.Eq{"type": opts.Type} + } if len(opts.Keyword) > 0 { lowerKeyword := strings.ToLower(opts.Keyword) keywordCond := builder.Or( diff --git a/models/user/user.go b/models/user/user.go index 4b19eda67b..b1bc4aed12 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -55,6 +55,9 @@ const ( UserTypeRemoteUser ) +// It belongs above but is set explicitly here to avoid conflicts +const UserTypeF3 UserType = 128 + const ( // EmailNotificationsEnabled indicates that the user would like to receive all email notifications except your own EmailNotificationsEnabled = "enabled" @@ -215,7 +218,7 @@ func (u *User) GetEmail() string { // GetAllUsers returns a slice of all individual users found in DB. func GetAllUsers() ([]*User, error) { users := make([]*User, 0) - return users, db.GetEngine(db.DefaultContext).OrderBy("id").Where("type = ?", UserTypeIndividual).Find(&users) + return users, db.GetEngine(db.DefaultContext).OrderBy("id").In("type", UserTypeIndividual, UserTypeF3).Find(&users) } // IsLocal returns true if user login type is LoginPlain. @@ -411,6 +414,10 @@ func (u *User) IsBot() bool { return u.Type == UserTypeBot } +func (u *User) IsF3() bool { + return u.Type == UserTypeF3 +} + // DisplayName returns full name if it's not empty, // returns username otherwise. func (u *User) DisplayName() string { @@ -963,7 +970,8 @@ func GetUserByName(ctx context.Context, name string) (*User, error) { if len(name) == 0 { return nil, ErrUserNotExist{0, name, 0} } - u := &User{LowerName: strings.ToLower(name), Type: UserTypeIndividual} + // adding Type: UserTypeIndividual is a noop because it is zero and discarded + u := &User{LowerName: strings.ToLower(name)} has, err := db.GetEngine(ctx).Get(u) if err != nil { return nil, err @@ -999,7 +1007,7 @@ func GetMaileableUsersByIDs(ctx context.Context, ids []int64, isMention bool) ([ if isMention { return ous, db.GetEngine(ctx). In("id", ids). - Where("`type` = ?", UserTypeIndividual). + In("`type`", UserTypeIndividual, UserTypeF3). And("`prohibit_login` = ?", false). And("`is_active` = ?", true). In("`email_notifications_preference`", EmailNotificationsEnabled, EmailNotificationsOnMention, EmailNotificationsAndYourOwn). @@ -1008,7 +1016,7 @@ func GetMaileableUsersByIDs(ctx context.Context, ids []int64, isMention bool) ([ return ous, db.GetEngine(ctx). In("id", ids). - Where("`type` = ?", UserTypeIndividual). + In("`type`", UserTypeIndividual, UserTypeF3). And("`prohibit_login` = ?", false). And("`is_active` = ?", true). In("`email_notifications_preference`", EmailNotificationsEnabled, EmailNotificationsAndYourOwn). diff --git a/services/f3/promote.go b/services/f3/promote.go index b11ff83a19..5a56e83773 100644 --- a/services/f3/promote.go +++ b/services/f3/promote.go @@ -18,7 +18,7 @@ func getUserByLoginName(ctx context.Context, name string) (*user_model.User, err if len(name) == 0 { return nil, user_model.ErrUserNotExist{Name: name} } - u := &user_model.User{LoginName: name, LoginType: auth_model.F3, Type: user_model.UserTypeRemoteUser} + u := &user_model.User{LoginName: name, LoginType: auth_model.F3, Type: user_model.UserTypeF3} has, err := db.GetEngine(ctx).Get(u) if err != nil { return nil, err @@ -30,12 +30,12 @@ func getUserByLoginName(ctx context.Context, name string) (*user_model.User, err // The user created by F3 has: // -// Type UserTypeRemoteUser +// Type UserTypeF3 // LogingType F3 // LoginName set to the unique identifier of the originating forge // LoginSource set to the F3 source that can be matched against a OAuth2 source // -// If the source from which an authentification happens is OAuth2, a existing +// If the source from which an authentification happens is OAuth2, an existing // F3 user will be promoted to an OAuth2 user provided: // // user.LoginName is the same as goth.UserID (argument loginName) @@ -82,6 +82,11 @@ func getF3UserToPromote(ctx context.Context, source *auth_model.Source, loginNam return nil, err } + if !u.IsF3() { + log.Debug("getF3UserToPromote: user %v is not a managed by F3", u) + return nil, nil + } + if u.Email != "" { log.Debug("getF3UserToPromote: the user email is already set to '%s'", u.Email) return nil, nil diff --git a/tests/integration/f3_test.go b/tests/integration/f3_test.go index b6bafdf850..d7e10af36b 100644 --- a/tests/integration/f3_test.go +++ b/tests/integration/f3_test.go @@ -146,7 +146,7 @@ func TestMaybePromoteF3User(t *testing.T) { gitlabEmail := "gitlabuser@example.com" userBeforeSignIn := &user_model.User{ Name: "gitlabuser", - Type: user_model.UserTypeRemoteUser, + Type: user_model.UserTypeF3, LoginType: auth_model.F3, LoginSource: f3.ID, LoginName: gitlabUserID,