add user's display-name to peer view (#525) (#534)
Some checks failed
Docker / Build and Push (push) Has been cancelled
Docker / release (push) Has been cancelled
github-pages / deploy (push) Has been cancelled

This commit is contained in:
h44z
2025-09-21 13:02:12 +02:00
committed by GitHub
parent 80693400be
commit 61bf349813
5 changed files with 58 additions and 15 deletions

View File

@@ -1,12 +1,14 @@
package domain
import (
"errors"
"fmt"
"net"
"strings"
"time"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"gorm.io/gorm"
"github.com/h44z/wg-portal/internal"
"github.com/h44z/wg-portal/internal/config"
@@ -44,6 +46,7 @@ type Peer struct {
DisplayName string // a nice display name/ description for the peer
Identifier PeerIdentifier `gorm:"primaryKey;column:identifier"` // peer unique identifier
UserIdentifier UserIdentifier `gorm:"index;column:user_identifier"` // the owner
User *User `gorm:"-"` // the owner user object; loaded automatically after fetch
InterfaceIdentifier InterfaceIdentifier `gorm:"index;column:interface_identifier"` // the interface id
Disabled *time.Time `gorm:"column:disabled"` // if this field is set, the peer is disabled
DisabledReason string // the reason why the peer has been disabled
@@ -348,3 +351,26 @@ type PeerCreationRequest struct {
UserIdentifiers []string
Prefix string
}
// AfterFind is a GORM hook that automatically loads the associated User object
// based on the UserIdentifier field. If the identifier is empty or no user is
// found, the User field is set to nil.
func (p *Peer) AfterFind(tx *gorm.DB) error {
if p == nil {
return nil
}
if p.UserIdentifier == "" {
p.User = nil
return nil
}
var u User
if err := tx.Where("identifier = ?", p.UserIdentifier).First(&u).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
p.User = nil
return nil
}
return err
}
p.User = &u
return nil
}