From be2825595d12ee6661933c89727feaf6f3a219ce Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 13 Apr 2021 19:31:39 +0200 Subject: [PATCH] Error out on network name conflict Closes: https://todo.sr.ht/~emersion/soju/29 --- user.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/user.go b/user.go index ac3facd..b1fab71 100644 --- a/user.go +++ b/user.go @@ -671,11 +671,24 @@ func (u *user) removeNetwork(network *network) { panic("tried to remove a non-existing network") } +func (u *user) checkNetwork(record *Network) error { + for _, net := range u.networks { + if net.GetName() == record.GetName() && net.ID != record.ID { + return fmt.Errorf("a network with the name %q already exists", record.GetName()) + } + } + return nil +} + func (u *user) createNetwork(record *Network) (*network, error) { if record.ID != 0 { panic("tried creating an already-existing network") } + if err := u.checkNetwork(record); err != nil { + return nil, err + } + network := newNetwork(u, record, nil) err := u.srv.db.StoreNetwork(u.ID, &network.Network) if err != nil { @@ -692,6 +705,10 @@ func (u *user) updateNetwork(record *Network) (*network, error) { panic("tried updating a new network") } + if err := u.checkNetwork(record); err != nil { + return nil, err + } + network := u.getNetworkByID(record.ID) if network == nil { panic("tried updating a non-existing network")