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

{ fmt.Sprint(counters.SubnetCount) }

-
+
-
+

Addresses

{ fmt.Sprint(counters.AddressCount) }

-
+
-
+

Devices

{ fmt.Sprint(counters.DeviceCount) }

-
+
-
+

Locations

{ fmt.Sprint(counters.LocationCount) }

-
+
} diff --git a/web/templates/index_templ.go b/web/templates/index_templ.go index 00207cc..e117034 100644 --- a/web/templates/index_templ.go +++ b/web/templates/index_templ.go @@ -11,6 +11,7 @@ import "io" import "bytes" import "fmt" +import "git.jmbit.de/jmb/goipam/utils" func Index(metaContent utils.MetaContent, counters IndexCounts, err error) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { @@ -72,59 +73,59 @@ func indexStats(counters IndexCounts) templ.Component { templ_7745c5c3_Var3 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Subnets

") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/web/templates/login.templ b/web/templates/login.templ index aadb1d0..f723c4c 100644 --- a/web/templates/login.templ +++ b/web/templates/login.templ @@ -1,5 +1,7 @@ package templates +import "git.jmbit.de/jmb/goipam/utils" + templ Login(metaContent utils.MetaContent, title string, err error) { @wrapBase(metaContent, title, err) {
diff --git a/web/templates/login_templ.go b/web/templates/login_templ.go index 9abe278..5cdf69f 100644 --- a/web/templates/login_templ.go +++ b/web/templates/login_templ.go @@ -10,6 +10,8 @@ import "context" import "io" import "bytes" +import "git.jmbit.de/jmb/goipam/utils" + func Login(metaContent utils.MetaContent, title string, err error) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) diff --git a/web/templates/mainComponents.templ b/web/templates/mainComponents.templ index 785458b..3ed5f7a 100644 --- a/web/templates/mainComponents.templ +++ b/web/templates/mainComponents.templ @@ -1,5 +1,7 @@ package templates +import "git.jmbit.de/jmb/goipam/utils" + templ head(title string) { @@ -25,7 +27,7 @@ templ footer(timestamp string) { } templ navbar(loggedIn bool) { -