diff --git a/db/models.go b/db/models.go index 5f65786..e8df93c 100644 --- a/db/models.go +++ b/db/models.go @@ -30,6 +30,7 @@ type Subnet struct { IPv4Net string IPv6Net string LocationID uint + Comment string } type Location struct { diff --git a/db/setup.go b/db/setup.go index 1182d03..2430acc 100644 --- a/db/setup.go +++ b/db/setup.go @@ -31,7 +31,7 @@ func SetupDB() { } else { log.Println("Admin PW is:", adminPW) } - adminPWHash, err := utils.RandomString(32) + adminPWHash, err := utils.HashPassword(adminPW) if err != nil { log.Fatalf("Could not generate admin PW: %v", err) } else { @@ -49,4 +49,21 @@ func SetupDB() { if result.Error != nil { log.Println(err) } + result = nil + result = conn.Create(&Location{ + Name: "Example", + Comment: "This is an example Location", + Subnets: []Subnet{ + { + Name: "VLAN1337", + DisplayName: "Example Subnet", + IPv4Net: "1.3.3.7/32", + IPv6Net: "dead:beef:ca1f/64", + }, + }, + }) + if result.Error != nil { + log.Println(err) + } + } diff --git a/db/subnets.go b/db/subnets.go index c434c42..1a133de 100644 --- a/db/subnets.go +++ b/db/subnets.go @@ -18,6 +18,14 @@ func (subnet *Subnet) Delete() error { } return nil +} + +func (subnet *Subnet) Get() *Subnet { + if err := conn.First(subnet).Error; err != nil { + return nil + } + return subnet + } func CountSubnets() int { var count int64 @@ -29,3 +37,21 @@ func CountSubnets() int { return int(count) } + +func (subnet *Subnet) Location() Location { + var location Location + + conn.First(&location, subnet.LocationID) + return location +} + +func SubnetsList(filter *Subnet, limit int, offset int, order string) []Subnet { + var subnets []Subnet + if order == "" { + order = "id desc" + } + + conn.Where(filter).Limit(limit).Offset(offset).Order(order).Find(&subnets) + + return subnets +} diff --git a/utils/passwordMatch.go b/utils/passwordMatch.go index 431dd80..8c4e14a 100644 --- a/utils/passwordMatch.go +++ b/utils/passwordMatch.go @@ -21,5 +21,9 @@ func DoPasswordsMatch(hashedPassword, currPassword string) bool { err := bcrypt.CompareHashAndPassword( []byte(hashedPassword), []byte(currPassword)) - return err == nil + if err == nil { + return true + } else { + return false + } } diff --git a/web/auth/hashPassword.go b/web/auth/hashPassword.go index da3f013..4ccce31 100644 --- a/web/auth/hashPassword.go +++ b/web/auth/hashPassword.go @@ -1,6 +1,8 @@ package auth import ( + "log" + "github.com/gin-contrib/sessions" "git.jmbit.de/jmb/goipam/db" @@ -31,7 +33,10 @@ func CheckPassword(username string, password string, session sessions.Session) e if err != nil { return err } + log.Println("Logged in ", user.Name) + return nil } - return nil + log.Println("Login error for ", username) + return &utils.CustomError{Message: "Passwords do not match"} } diff --git a/web/templates/index.templ b/web/templates/index.templ index 79fbdc5..5f72ba2 100644 --- a/web/templates/index.templ +++ b/web/templates/index.templ @@ -1,6 +1,7 @@ package templates import "fmt" +import "git.jmbit.de/jmb/goipam/utils" templ Index(metaContent utils.MetaContent, counters IndexCounts, err error) { @wrapBase(metaContent, "GoIPAM", err) { @@ -15,28 +16,28 @@ templ Index(metaContent utils.MetaContent, counters IndexCounts, err error) { templ indexStats(counters IndexCounts) {
Subnets
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("