improve checks

This commit is contained in:
2023-04-03 08:33:31 +10:00
parent b45e276df5
commit 748f4251e1
9 changed files with 139 additions and 43 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/jmoiron/sqlx"
"github.com/joho/godotenv"
"golang.org/x/crypto/bcrypt"
_ "modernc.org/sqlite"
)
@@ -124,7 +125,17 @@ func CreateTables() {
}
rowCount, _ = CheckCount("users")
if rowCount == 0 {
if _, err = db.Exec("INSERT INTO users VALUES(1, 1, 'Administrator', '$2a$10$k1qldm.bWqZsQWrKPdahR.Pfz5LxkMUka2.8INEeSD7euzkiznIR.');"); err != nil {
// Check if there was an initial password defined in the .env file
initialPassword := os.Getenv("INITIAL_PASSWORD")
if initialPassword == "" {
initialPassword = "password"
} else if initialPassword[:4] == "$2a$" {
fmt.Printf("CreateTables inital admin password is already a hash")
} else {
cryptText, _ := bcrypt.GenerateFromPassword([]byte(initialPassword), bcrypt.DefaultCost)
initialPassword = string(cryptText)
}
if _, err = db.Exec("INSERT INTO users VALUES(1, 1, 'Administrator', ?);", initialPassword); err != nil {
fmt.Printf("Error adding initial admin role : '%s'", err)
os.Exit(1)
}
@@ -223,7 +234,7 @@ func CheckColumnExists(table string, column string) (bool, error) {
for rows.Next() {
// cols is an []interface{} of all of the column results
cols, _ := rows.SliceScan()
fmt.Printf("CheckColumnExists Value is '%v'\n", cols[0].(int64))
fmt.Printf("CheckColumnExists Value is '%v' for table '%s' and column '%s'\n", cols[0].(int64), table, column)
count = cols[0].(int64)
if count == 1 {