2024-03-27 18:48:48 +01:00
|
|
|
package group
|
|
|
|
|
|
|
|
import "github.com/netbirdio/netbird/management/server/integration_reference"
|
|
|
|
|
|
|
|
const (
|
|
|
|
GroupIssuedAPI = "api"
|
|
|
|
GroupIssuedJWT = "jwt"
|
|
|
|
GroupIssuedIntegration = "integration"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Group of the peers for ACL
|
|
|
|
type Group struct {
|
|
|
|
// ID of the group
|
|
|
|
ID string
|
|
|
|
|
|
|
|
// AccountID is a reference to Account that this object belongs
|
|
|
|
AccountID string `json:"-" gorm:"index"`
|
|
|
|
|
|
|
|
// Name visible in the UI
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Issued defines how this group was created (enum of "api", "integration" or "jwt")
|
|
|
|
Issued string
|
|
|
|
|
|
|
|
// Peers list of the group
|
|
|
|
Peers []string `gorm:"serializer:json"`
|
|
|
|
|
|
|
|
IntegrationReference integration_reference.IntegrationReference `gorm:"embedded;embeddedPrefix:integration_ref_"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventMeta returns activity event meta related to the group
|
|
|
|
func (g *Group) EventMeta() map[string]any {
|
|
|
|
return map[string]any{"name": g.Name}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Group) Copy() *Group {
|
|
|
|
group := &Group{
|
|
|
|
ID: g.ID,
|
|
|
|
Name: g.Name,
|
|
|
|
Issued: g.Issued,
|
|
|
|
Peers: make([]string, len(g.Peers)),
|
|
|
|
IntegrationReference: g.IntegrationReference,
|
|
|
|
}
|
|
|
|
copy(group.Peers, g.Peers)
|
|
|
|
return group
|
|
|
|
}
|
2024-10-23 12:05:02 +02:00
|
|
|
|
|
|
|
// HasPeers checks if the group has any peers.
|
|
|
|
func (g *Group) HasPeers() bool {
|
|
|
|
return len(g.Peers) > 0
|
|
|
|
}
|
2024-11-07 22:59:37 +01:00
|
|
|
|
|
|
|
// IsGroupAll checks if the group is a default "All" group.
|
|
|
|
func (g *Group) IsGroupAll() bool {
|
|
|
|
return g.Name == "All"
|
|
|
|
}
|
2024-11-08 23:14:30 +01:00
|
|
|
|
2024-11-11 20:38:24 +01:00
|
|
|
// AddPeer adds peerID to Peers if not present, returning true if added.
|
2024-11-08 23:14:30 +01:00
|
|
|
func (g *Group) AddPeer(peerID string) bool {
|
|
|
|
if peerID == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, itemID := range g.Peers {
|
|
|
|
if itemID == peerID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Peers = append(g.Peers, peerID)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-11-11 20:38:24 +01:00
|
|
|
// RemovePeer removes peerID from Peers if present, returning true if removed.
|
2024-11-08 23:14:30 +01:00
|
|
|
func (g *Group) RemovePeer(peerID string) bool {
|
|
|
|
for i, itemID := range g.Peers {
|
|
|
|
if itemID == peerID {
|
|
|
|
g.Peers = append(g.Peers[:i], g.Peers[i+1:]...)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|