mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-18 03:38:01 +02:00
fix: use consistent GetUser family functions (#37553)
fixes adding collaborative owners in Actions settings when the user or organization name contains capital letters. Fixes #37548 --------- Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
parent
19f01691d5
commit
2200ed7499
@ -1051,13 +1051,13 @@ func GetPossibleUserByIDs(ctx context.Context, ids []int64) ([]*User, error) {
|
|||||||
return users, nil
|
return users, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserByName returns user by given name.
|
func getUserByNameWithTypes(ctx context.Context, name string, types ...UserType) (*User, error) {
|
||||||
func GetUserByName(ctx context.Context, name string) (*User, error) {
|
u := &User{}
|
||||||
if len(name) == 0 {
|
sess := db.GetEngine(ctx).Where(builder.Eq{"lower_name": strings.ToLower(name)})
|
||||||
return nil, ErrUserNotExist{Name: name}
|
if len(types) > 0 {
|
||||||
|
sess.In("`type`", types)
|
||||||
}
|
}
|
||||||
u := &User{LowerName: strings.ToLower(name), Type: UserTypeIndividual}
|
has, err := sess.Get(u)
|
||||||
has, err := db.GetEngine(ctx).Get(u)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if !has {
|
} else if !has {
|
||||||
@ -1066,6 +1066,15 @@ func GetUserByName(ctx context.Context, name string) (*User, error) {
|
|||||||
return u, nil
|
return u, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetUserByName returns the user object by given name, any user type.
|
||||||
|
func GetUserByName(ctx context.Context, name string) (*User, error) {
|
||||||
|
return getUserByNameWithTypes(ctx, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetIndividualUserByName(ctx context.Context, name string) (*User, error) {
|
||||||
|
return getUserByNameWithTypes(ctx, name, UserTypeIndividual)
|
||||||
|
}
|
||||||
|
|
||||||
// GetUserEmailsByNames returns a list of e-mails corresponds to names of users
|
// GetUserEmailsByNames returns a list of e-mails corresponds to names of users
|
||||||
// that have their email notifications set to enabled or onmention.
|
// that have their email notifications set to enabled or onmention.
|
||||||
func GetUserEmailsByNames(ctx context.Context, names []string) []string {
|
func GetUserEmailsByNames(ctx context.Context, names []string) []string {
|
||||||
@ -1108,19 +1117,6 @@ func GetMailableUsersByIDs(ctx context.Context, ids []int64, isMention bool) ([]
|
|||||||
Find(&ous)
|
Find(&ous)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserNameByID returns username for the id
|
|
||||||
func GetUserNameByID(ctx context.Context, id int64) (string, error) {
|
|
||||||
var name string
|
|
||||||
has, err := db.GetEngine(ctx).Table("user").Where("id = ?", id).Cols("name").Get(&name)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if has {
|
|
||||||
return name, nil
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetUserIDsByNames returns a slice of ids corresponds to names.
|
// GetUserIDsByNames returns a slice of ids corresponds to names.
|
||||||
func GetUserIDsByNames(ctx context.Context, names []string, ignoreNonExistent bool) ([]int64, error) {
|
func GetUserIDsByNames(ctx context.Context, names []string, ignoreNonExistent bool) ([]int64, error) {
|
||||||
ids := make([]int64, 0, len(names))
|
ids := make([]int64, 0, len(names))
|
||||||
@ -1321,13 +1317,14 @@ func GetUserByEmail(ctx context.Context, email string) (*User, error) {
|
|||||||
if id != 0 {
|
if id != 0 {
|
||||||
return GetUserByID(ctx, id)
|
return GetUserByID(ctx, id)
|
||||||
}
|
}
|
||||||
return GetUserByName(ctx, name)
|
return GetIndividualUserByName(ctx, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, ErrUserNotExist{Name: email}
|
return nil, ErrUserNotExist{Name: email}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetIndividualUser(ctx context.Context, user *User) (bool, error) {
|
func GetIndividualUser(ctx context.Context, user *User) (bool, error) {
|
||||||
|
// FIXME: the design is wrong, empty User fields won't apply, this function should be removed in the future
|
||||||
has, err := db.GetEngine(ctx).Get(user)
|
has, err := db.GetEngine(ctx).Get(user)
|
||||||
if has && user.Type != UserTypeIndividual {
|
if has && user.Type != UserTypeIndividual {
|
||||||
has = false
|
has = false
|
||||||
@ -1492,27 +1489,3 @@ func DisabledFeaturesWithLoginType(user *User) *container.Set[string] {
|
|||||||
}
|
}
|
||||||
return &setting.Admin.UserDisabledFeatures
|
return &setting.Admin.UserDisabledFeatures
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserOrOrgIDByName returns the id for a user or an org by name
|
|
||||||
func GetUserOrOrgIDByName(ctx context.Context, name string) (int64, error) {
|
|
||||||
var id int64
|
|
||||||
has, err := db.GetEngine(ctx).Table("user").Where("name = ?", name).Cols("id").Get(&id)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
} else if !has {
|
|
||||||
return 0, fmt.Errorf("user or org with name %s: %w", name, util.ErrNotExist)
|
|
||||||
}
|
|
||||||
return id, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetUserOrOrgByName returns the user or org by name
|
|
||||||
func GetUserOrOrgByName(ctx context.Context, name string) (*User, error) {
|
|
||||||
var u User
|
|
||||||
has, err := db.GetEngine(ctx).Where("lower_name = ?", strings.ToLower(name)).Get(&u)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else if !has {
|
|
||||||
return nil, ErrUserNotExist{Name: name}
|
|
||||||
}
|
|
||||||
return &u, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@ -129,7 +129,7 @@ func GetHeadOwnerAndRepo(ctx context.Context, baseRepo *repo_model.Repository, c
|
|||||||
if compareReq.HeadOwner == baseRepo.Owner.Name {
|
if compareReq.HeadOwner == baseRepo.Owner.Name {
|
||||||
headOwner = baseRepo.Owner
|
headOwner = baseRepo.Owner
|
||||||
} else {
|
} else {
|
||||||
headOwner, err = user_model.GetUserOrOrgByName(ctx, compareReq.HeadOwner)
|
headOwner, err = user_model.GetUserByName(ctx, compareReq.HeadOwner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,6 @@ package setting
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/actions"
|
"code.gitea.io/gitea/models/actions"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
@ -94,15 +93,12 @@ func ActionsUnitPost(ctx *context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AddCollaborativeOwner(ctx *context.Context) {
|
func AddCollaborativeOwner(ctx *context.Context) {
|
||||||
name := strings.ToLower(ctx.FormString("collaborative_owner"))
|
collUser, err := user_model.GetUserByName(ctx, ctx.FormString("collaborative_owner"))
|
||||||
|
|
||||||
ownerID, err := user_model.GetUserOrOrgIDByName(ctx, name)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, util.ErrNotExist) {
|
if errors.Is(err, util.ErrNotExist) {
|
||||||
ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
|
ctx.JSONError(ctx.Tr("form.user_not_exist"))
|
||||||
ctx.JSONErrorNotFound()
|
|
||||||
} else {
|
} else {
|
||||||
ctx.ServerError("GetUserOrOrgIDByName", err)
|
ctx.ServerError("GetUserByName", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -113,7 +109,7 @@ func AddCollaborativeOwner(ctx *context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
actionsCfg := actionsUnit.ActionsConfig()
|
actionsCfg := actionsUnit.ActionsConfig()
|
||||||
actionsCfg.AddCollaborativeOwner(ownerID)
|
actionsCfg.AddCollaborativeOwner(collUser.ID)
|
||||||
if err := repo_model.UpdateRepoUnitConfig(ctx, actionsUnit); err != nil {
|
if err := repo_model.UpdateRepoUnitConfig(ctx, actionsUnit); err != nil {
|
||||||
ctx.ServerError("UpdateRepoUnitConfig", err)
|
ctx.ServerError("UpdateRepoUnitConfig", err)
|
||||||
return
|
return
|
||||||
|
|||||||
@ -6,6 +6,7 @@ package migrations
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -989,12 +990,15 @@ func (g *GiteaLocalUploader) remapUser(ctx context.Context, source user_model.Ex
|
|||||||
func (g *GiteaLocalUploader) remapLocalUser(ctx context.Context, source user_model.ExternalUserMigrated) (int64, error) {
|
func (g *GiteaLocalUploader) remapLocalUser(ctx context.Context, source user_model.ExternalUserMigrated) (int64, error) {
|
||||||
userid, ok := g.userMap[source.GetExternalID()]
|
userid, ok := g.userMap[source.GetExternalID()]
|
||||||
if !ok {
|
if !ok {
|
||||||
name, err := user_model.GetUserNameByID(ctx, source.GetExternalID())
|
user, err := user_model.GetUserByID(ctx, source.GetExternalID())
|
||||||
if err != nil {
|
if errors.Is(err, util.ErrNotExist) {
|
||||||
|
g.userMap[source.GetExternalID()] = userid
|
||||||
|
return 0, nil
|
||||||
|
} else if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
// let's not reuse an ID when the user was deleted or has a different user name
|
// let's not reuse an ID when the user was deleted or has a different username
|
||||||
if name != source.GetExternalName() {
|
if !util.AsciiEqualFold(user.Name, source.GetExternalName()) {
|
||||||
userid = 0
|
userid = 0
|
||||||
} else {
|
} else {
|
||||||
userid = source.GetExternalID()
|
userid = source.GetExternalID()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user