mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-23 14:53:34 +01:00
Search federatedUser instead of loginName
This commit is contained in:
parent
e44be72251
commit
66bc7a9a0c
4 changed files with 46 additions and 52 deletions
|
@ -32,7 +32,6 @@ func GetFederationHost(ctx context.Context, ID int64) (*FederationHost, error) {
|
||||||
|
|
||||||
func FindFederationHostByFqdn(ctx context.Context, fqdn string) (*FederationHost, error) {
|
func FindFederationHostByFqdn(ctx context.Context, fqdn string) (*FederationHost, error) {
|
||||||
host := new(FederationHost)
|
host := new(FederationHost)
|
||||||
// TODO: use parameter with toLower
|
|
||||||
has, err := db.GetEngine(ctx).Where("host_fqdn=?", strings.ToLower(fqdn)).Get(host)
|
has, err := db.GetEngine(ctx).Where("host_fqdn=?", strings.ToLower(fqdn)).Get(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -811,6 +811,17 @@ func ValidateUser(u *User, cols ...string) error {
|
||||||
return nil
|
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
|
// UpdateUserCols update user according special columns
|
||||||
func UpdateUserCols(ctx context.Context, u *User, cols ...string) error {
|
func UpdateUserCols(ctx context.Context, u *User, cols ...string) error {
|
||||||
if err := ValidateUser(u, cols...); err != nil {
|
if err := ValidateUser(u, cols...); err != nil {
|
||||||
|
|
|
@ -22,3 +22,29 @@ func CreateFederationUser(ctx context.Context, user *FederatedUser) error {
|
||||||
_, err := db.GetEngine(ctx).Insert(user)
|
_, err := db.GetEngine(ctx).Insert(user)
|
||||||
return err
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
|
||||||
"code.gitea.io/gitea/models/forgefed"
|
"code.gitea.io/gitea/models/forgefed"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
|
@ -16,7 +15,6 @@ import (
|
||||||
"code.gitea.io/gitea/modules/context"
|
"code.gitea.io/gitea/modules/context"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/util"
|
|
||||||
"code.gitea.io/gitea/modules/validation"
|
"code.gitea.io/gitea/modules/validation"
|
||||||
"code.gitea.io/gitea/modules/web"
|
"code.gitea.io/gitea/modules/web"
|
||||||
|
|
||||||
|
@ -74,8 +72,6 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
// "204":
|
// "204":
|
||||||
// "$ref": "#/responses/empty"
|
// "$ref": "#/responses/empty"
|
||||||
|
|
||||||
var user *user_model.User
|
|
||||||
|
|
||||||
repository := ctx.Repo.Repository
|
repository := ctx.Repo.Repository
|
||||||
log.Info("RepositoryInbox: repo: %v", repository)
|
log.Info("RepositoryInbox: repo: %v", repository)
|
||||||
|
|
||||||
|
@ -137,18 +133,14 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
log.Info("RepositoryInbox: remoteStargazer: %v", actorAsLoginID)
|
log.Info("RepositoryInbox: remoteStargazer: %v", actorAsLoginID)
|
||||||
|
|
||||||
// Check if user already exists
|
// Check if user already exists
|
||||||
// TODO: search for federation user instead
|
user, _, err := user_model.FindFederatedUser(ctx, actorID.ID, federationHost.ID)
|
||||||
// users, _, err := SearchFederatedUser(actorID.ID, federationHost.ID)
|
|
||||||
users, err := SearchUsersByLoginName(actorAsLoginID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError, "RepositoryInbox: Searching for user failed", err)
|
ctx.Error(http.StatusInternalServerError, "RepositoryInbox: Searching for user failed", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Info("RepositoryInbox: local found users: %v", len(users))
|
if user != nil {
|
||||||
|
log.Info("RepositoryInbox: found user: %v", user)
|
||||||
switch len(users) {
|
} else {
|
||||||
case 0:
|
|
||||||
{
|
|
||||||
user, err = createUserFromAP(ctx, actorID, federationHost.ID)
|
user, err = createUserFromAP(ctx, actorID, federationHost.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError,
|
ctx.Error(http.StatusInternalServerError,
|
||||||
|
@ -157,18 +149,6 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
}
|
}
|
||||||
log.Info("RepositoryInbox: created user from ap: %v", user)
|
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"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// execute the activity if the repo was not stared already
|
// execute the activity if the repo was not stared already
|
||||||
alreadyStared := repo_model.IsStaring(ctx, user.ID, repository.ID)
|
alreadyStared := repo_model.IsStaring(ctx, user.ID, repository.ID)
|
||||||
|
@ -189,28 +169,6 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
ctx.Status(http.StatusNoContent)
|
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) {
|
func createFederationHost(ctx *context.APIContext, actorID forgefed.ActorID) (forgefed.FederationHost, error) {
|
||||||
actionsUser := user_model.NewActionsUser()
|
actionsUser := user_model.NewActionsUser()
|
||||||
client, err := api.NewClient(ctx, actionsUser, "no idea where to get key material.")
|
client, err := api.NewClient(ctx, actionsUser, "no idea where to get key material.")
|
||||||
|
|
Loading…
Reference in a new issue