Search federatedUser instead of loginName

This commit is contained in:
Michael Jerger 2024-02-08 11:09:55 +01:00
parent e44be72251
commit 66bc7a9a0c
4 changed files with 46 additions and 52 deletions

View file

@ -32,7 +32,6 @@ func GetFederationHost(ctx context.Context, ID int64) (*FederationHost, error) {
func FindFederationHostByFqdn(ctx context.Context, fqdn string) (*FederationHost, error) {
host := new(FederationHost)
// TODO: use parameter with toLower
has, err := db.GetEngine(ctx).Where("host_fqdn=?", strings.ToLower(fqdn)).Get(host)
if err != nil {
return nil, err

View file

@ -811,6 +811,17 @@ func ValidateUser(u *User, cols ...string) error {
return nil
}
func (user User) Validate() []string {
var result []string
if err := ValidateUser(&user); err != nil {
result = append(result, err.Error())
}
if err := ValidateEmail(user.Email); err != nil {
result = append(result, err.Error())
}
return result
}
// UpdateUserCols update user according special columns
func UpdateUserCols(ctx context.Context, u *User, cols ...string) error {
if err := ValidateUser(u, cols...); err != nil {

View file

@ -22,3 +22,29 @@ func CreateFederationUser(ctx context.Context, user *FederatedUser) error {
_, err := db.GetEngine(ctx).Insert(user)
return err
}
func FindFederatedUser(ctx context.Context, externalID string,
federationHostID int64) (*User, *FederatedUser, error) {
federatedUser := new(FederatedUser)
user := new(User)
has, err := db.GetEngine(ctx).Where("external_id=? and federation_host_id=?", externalID, federationHostID).Get(federatedUser)
if err != nil {
return nil, nil, err
} else if !has {
return nil, nil, nil
}
has, err = db.GetEngine(ctx).ID(federatedUser.UserID).Get(user)
if err != nil {
return nil, nil, err
} else if !has {
return nil, nil, fmt.Errorf("User %v for federated user is missing.", federatedUser.UserID)
}
if res, err := validation.IsValid(*user); !res {
return nil, nil, fmt.Errorf("FederatedUser is not valid: %v", err)
}
if res, err := validation.IsValid(*federatedUser); !res {
return nil, nil, fmt.Errorf("FederatedUser is not valid: %v", err)
}
return user, federatedUser, nil
}

View file

@ -8,7 +8,6 @@ import (
"net/http"
"strings"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/forgefed"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
@ -16,7 +15,6 @@ import (
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/validation"
"code.gitea.io/gitea/modules/web"
@ -74,8 +72,6 @@ func RepositoryInbox(ctx *context.APIContext) {
// "204":
// "$ref": "#/responses/empty"
var user *user_model.User
repository := ctx.Repo.Repository
log.Info("RepositoryInbox: repo: %v", repository)
@ -137,37 +133,21 @@ func RepositoryInbox(ctx *context.APIContext) {
log.Info("RepositoryInbox: remoteStargazer: %v", actorAsLoginID)
// Check if user already exists
// TODO: search for federation user instead
// users, _, err := SearchFederatedUser(actorID.ID, federationHost.ID)
users, err := SearchUsersByLoginName(actorAsLoginID)
user, _, err := user_model.FindFederatedUser(ctx, actorID.ID, federationHost.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "RepositoryInbox: Searching for user failed", err)
return
}
log.Info("RepositoryInbox: local found users: %v", len(users))
switch len(users) {
case 0:
{
user, err = createUserFromAP(ctx, actorID, federationHost.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError,
"RepositoryInbox: Creating federated user failed", err)
return
}
log.Info("RepositoryInbox: created user from ap: %v", user)
}
case 1:
{
user = users[0]
log.Info("RepositoryInbox: found user: %v", user)
}
default:
{
ctx.Error(http.StatusInternalServerError, "RepositoryInbox",
fmt.Errorf(" more than one matches for federated users"))
if user != nil {
log.Info("RepositoryInbox: found user: %v", user)
} else {
user, err = createUserFromAP(ctx, actorID, federationHost.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError,
"RepositoryInbox: Creating federated user failed", err)
return
}
log.Info("RepositoryInbox: created user from ap: %v", user)
}
// execute the activity if the repo was not stared already
@ -189,28 +169,6 @@ func RepositoryInbox(ctx *context.APIContext) {
ctx.Status(http.StatusNoContent)
}
// TODO: Move this to model.user.search ? or to model.user.externalLoginUser ?
func SearchUsersByLoginName(loginName string) ([]*user_model.User, error) {
actionsUser := user_model.NewActionsUser()
actionsUser.IsAdmin = true
options := &user_model.SearchUserOptions{
LoginName: loginName,
Actor: actionsUser,
Type: user_model.UserTypeRemoteUser,
OrderBy: db.SearchOrderByAlphabetically,
ListOptions: db.ListOptions{PageSize: 1},
IsActive: util.OptionalBoolFalse,
IncludeReserved: true,
}
users, _, err := user_model.SearchUsers(db.DefaultContext, options)
if err != nil {
return []*user_model.User{}, fmt.Errorf("search failed: %v", err)
}
return users, nil
}
func createFederationHost(ctx *context.APIContext, actorID forgefed.ActorID) (forgefed.FederationHost, error) {
actionsUser := user_model.NewActionsUser()
client, err := api.NewClient(ctx, actionsUser, "no idea where to get key material.")