[F3] introduce UserTypeF3

To avoid conflicts should UserTypeRemoteUser be used differently by Gitea
This commit is contained in:
Loïc Dachary 2023-07-20 14:35:58 +02:00
parent 4dd5b8b8bd
commit 6de2701bb3
No known key found for this signature in database
GPG key ID: 992D23B392F9E4F2
4 changed files with 27 additions and 9 deletions

View file

@ -39,7 +39,12 @@ type SearchUserOptions struct {
} }
func (opts *SearchUserOptions) toSearchQueryBase() *xorm.Session { 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 { if len(opts.Keyword) > 0 {
lowerKeyword := strings.ToLower(opts.Keyword) lowerKeyword := strings.ToLower(opts.Keyword)
keywordCond := builder.Or( keywordCond := builder.Or(

View file

@ -55,6 +55,9 @@ const (
UserTypeRemoteUser UserTypeRemoteUser
) )
// It belongs above but is set explicitly here to avoid conflicts
const UserTypeF3 UserType = 128
const ( const (
// EmailNotificationsEnabled indicates that the user would like to receive all email notifications except your own // EmailNotificationsEnabled indicates that the user would like to receive all email notifications except your own
EmailNotificationsEnabled = "enabled" EmailNotificationsEnabled = "enabled"
@ -215,7 +218,7 @@ func (u *User) GetEmail() string {
// GetAllUsers returns a slice of all individual users found in DB. // GetAllUsers returns a slice of all individual users found in DB.
func GetAllUsers() ([]*User, error) { func GetAllUsers() ([]*User, error) {
users := make([]*User, 0) 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. // IsLocal returns true if user login type is LoginPlain.
@ -411,6 +414,10 @@ func (u *User) IsBot() bool {
return u.Type == UserTypeBot return u.Type == UserTypeBot
} }
func (u *User) IsF3() bool {
return u.Type == UserTypeF3
}
// DisplayName returns full name if it's not empty, // DisplayName returns full name if it's not empty,
// returns username otherwise. // returns username otherwise.
func (u *User) DisplayName() string { func (u *User) DisplayName() string {
@ -963,7 +970,8 @@ func GetUserByName(ctx context.Context, name string) (*User, error) {
if len(name) == 0 { if len(name) == 0 {
return nil, ErrUserNotExist{0, 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) has, err := db.GetEngine(ctx).Get(u)
if err != nil { if err != nil {
return nil, err return nil, err
@ -999,7 +1007,7 @@ func GetMaileableUsersByIDs(ctx context.Context, ids []int64, isMention bool) ([
if isMention { if isMention {
return ous, db.GetEngine(ctx). return ous, db.GetEngine(ctx).
In("id", ids). In("id", ids).
Where("`type` = ?", UserTypeIndividual). In("`type`", UserTypeIndividual, UserTypeF3).
And("`prohibit_login` = ?", false). And("`prohibit_login` = ?", false).
And("`is_active` = ?", true). And("`is_active` = ?", true).
In("`email_notifications_preference`", EmailNotificationsEnabled, EmailNotificationsOnMention, EmailNotificationsAndYourOwn). 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). return ous, db.GetEngine(ctx).
In("id", ids). In("id", ids).
Where("`type` = ?", UserTypeIndividual). In("`type`", UserTypeIndividual, UserTypeF3).
And("`prohibit_login` = ?", false). And("`prohibit_login` = ?", false).
And("`is_active` = ?", true). And("`is_active` = ?", true).
In("`email_notifications_preference`", EmailNotificationsEnabled, EmailNotificationsAndYourOwn). In("`email_notifications_preference`", EmailNotificationsEnabled, EmailNotificationsAndYourOwn).

View file

@ -18,7 +18,7 @@ func getUserByLoginName(ctx context.Context, name string) (*user_model.User, err
if len(name) == 0 { if len(name) == 0 {
return nil, user_model.ErrUserNotExist{Name: name} 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) has, err := db.GetEngine(ctx).Get(u)
if err != nil { if err != nil {
return nil, err return nil, err
@ -30,12 +30,12 @@ func getUserByLoginName(ctx context.Context, name string) (*user_model.User, err
// The user created by F3 has: // The user created by F3 has:
// //
// Type UserTypeRemoteUser // Type UserTypeF3
// LogingType F3 // LogingType F3
// LoginName set to the unique identifier of the originating forge // LoginName set to the unique identifier of the originating forge
// LoginSource set to the F3 source that can be matched against a OAuth2 source // 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: // F3 user will be promoted to an OAuth2 user provided:
// //
// user.LoginName is the same as goth.UserID (argument loginName) // 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 return nil, err
} }
if !u.IsF3() {
log.Debug("getF3UserToPromote: user %v is not a managed by F3", u)
return nil, nil
}
if u.Email != "" { if u.Email != "" {
log.Debug("getF3UserToPromote: the user email is already set to '%s'", u.Email) log.Debug("getF3UserToPromote: the user email is already set to '%s'", u.Email)
return nil, nil return nil, nil

View file

@ -146,7 +146,7 @@ func TestMaybePromoteF3User(t *testing.T) {
gitlabEmail := "gitlabuser@example.com" gitlabEmail := "gitlabuser@example.com"
userBeforeSignIn := &user_model.User{ userBeforeSignIn := &user_model.User{
Name: "gitlabuser", Name: "gitlabuser",
Type: user_model.UserTypeRemoteUser, Type: user_model.UserTypeF3,
LoginType: auth_model.F3, LoginType: auth_model.F3,
LoginSource: f3.ID, LoginSource: f3.ID,
LoginName: gitlabUserID, LoginName: gitlabUserID,