mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
Fixes #9282 Adds a new admin panel category for federation related administration. Includes views for: - Instance Federation Configuration - List of Federation Hosts - (Per-Instance) List of Federated Users Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11115 Reviewed-by: elle <0xllx0@noreply.codeberg.org> Reviewed-by: Panagiotis "Ivory" Vasilopoulos <git@n0toose.net> Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Florian Pallas <mail@fpallas.com> Co-committed-by: Florian Pallas <mail@fpallas.com>
106 lines
2.6 KiB
Go
106 lines
2.6 KiB
Go
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package forgefed
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"forgejo.org/models/db"
|
|
"forgejo.org/modules/log"
|
|
"forgejo.org/modules/validation"
|
|
)
|
|
|
|
func init() {
|
|
db.RegisterModel(new(FederationHost))
|
|
}
|
|
|
|
func CountFederationHosts(ctx context.Context) (int64, error) {
|
|
return db.GetEngine(ctx).Count(FederationHost{})
|
|
}
|
|
|
|
func FindFederationHosts(ctx context.Context, opts db.ListOptions) (hosts []*FederationHost, err error) {
|
|
sess := db.GetEngine(ctx)
|
|
|
|
if opts.PageSize > 0 {
|
|
sess = db.SetSessionPagination(sess, &opts)
|
|
}
|
|
|
|
err = sess.Find(&hosts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, host := range hosts {
|
|
if res, err := validation.IsValid(host); !res {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return hosts, nil
|
|
}
|
|
|
|
func GetFederationHost(ctx context.Context, ID int64) (*FederationHost, error) {
|
|
log.Trace("GetFederationHost: %v", ID)
|
|
host := new(FederationHost)
|
|
has, err := db.GetEngine(ctx).Where("id=?", ID).Get(host)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if !has {
|
|
return nil, fmt.Errorf("FederationInfo record %v does not exist", ID)
|
|
}
|
|
if res, err := validation.IsValid(host); !res {
|
|
return nil, err
|
|
}
|
|
log.Trace("GetFederationHost: %v, got host %v", ID, host)
|
|
return host, nil
|
|
}
|
|
|
|
func findFederationHostFromDB(ctx context.Context, searchKey, searchValue string) (*FederationHost, error) {
|
|
host := new(FederationHost)
|
|
has, err := db.GetEngine(ctx).Where(searchKey, searchValue).Get(host)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if !has {
|
|
return nil, nil
|
|
}
|
|
if res, err := validation.IsValid(host); !res {
|
|
return nil, err
|
|
}
|
|
return host, nil
|
|
}
|
|
|
|
func FindFederationHostByFqdnAndPort(ctx context.Context, fqdn string, port uint16) (*FederationHost, error) {
|
|
host := new(FederationHost)
|
|
has, err := db.GetEngine(ctx).Where("host_fqdn=? AND host_port=?", fqdn, port).Get(host)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if !has {
|
|
return nil, nil
|
|
}
|
|
if res, err := validation.IsValid(host); !res {
|
|
return nil, err
|
|
}
|
|
return host, nil
|
|
}
|
|
|
|
func FindFederationHostByKeyID(ctx context.Context, keyID string) (*FederationHost, error) {
|
|
return findFederationHostFromDB(ctx, "key_id=?", keyID)
|
|
}
|
|
|
|
func CreateFederationHost(ctx context.Context, host *FederationHost) error {
|
|
if res, err := validation.IsValid(host); !res {
|
|
return err
|
|
}
|
|
_, err := db.GetEngine(ctx).Insert(host)
|
|
return err
|
|
}
|
|
|
|
func UpdateFederationHost(ctx context.Context, host *FederationHost) error {
|
|
if res, err := validation.IsValid(host); !res {
|
|
return err
|
|
}
|
|
_, err := db.GetEngine(ctx).ID(host.ID).Update(host)
|
|
return err
|
|
}
|