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")