improve checks
This commit is contained in:
@@ -44,6 +44,7 @@ func (s *Secret) SaveSecret() (*Secret, error) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Returns all matching secrets, up to caller to determine how to deal with multiple results
|
||||
func GetSecrets(s *Secret) ([]Secret, error) {
|
||||
var err error
|
||||
var rows *sqlx.Rows
|
||||
@@ -52,6 +53,7 @@ func GetSecrets(s *Secret) ([]Secret, error) {
|
||||
fmt.Printf("GetSecret querying values '%v'\n", s)
|
||||
|
||||
// Determine whether to query for a specific device or a category of devices
|
||||
// Prefer querying device name than category
|
||||
if s.DeviceName != "" {
|
||||
rows, err = db.Queryx("SELECT * FROM secrets WHERE DeviceName LIKE ? AND RoleId = ?", s.DeviceName, s.RoleId)
|
||||
} else if s.DeviceCategory != "" {
|
||||
@@ -62,8 +64,6 @@ func GetSecrets(s *Secret) ([]Secret, error) {
|
||||
return secretResults, err
|
||||
}
|
||||
|
||||
// TODO - do we want to generate an error if the query returns more than one result?
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("GetSecret error executing sql record : '%s'\n", err)
|
||||
return secretResults, err
|
||||
|
@@ -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 {
|
||||
|
@@ -12,7 +12,7 @@ type User struct {
|
||||
UserId int `db:"UserId"`
|
||||
RoleId int `db:"RoleId"`
|
||||
UserName string `db:"UserName"`
|
||||
Password string `db:"Password"`
|
||||
Password string `db:"Password" json:"-"`
|
||||
}
|
||||
|
||||
type UserRole struct {
|
||||
@@ -87,7 +87,7 @@ func GetUserByID(uid uint) (User, error) {
|
||||
var u User
|
||||
|
||||
// Query database for matching user object
|
||||
err := db.QueryRowx("SELECT * FROM users INNER JOIN roles ON users.RoleId = roles.RoleId WHERE UserId=?", uid).StructScan(&u)
|
||||
err := db.QueryRowx("SELECT * FROM users WHERE UserId=?", uid).StructScan(&u)
|
||||
if err != nil {
|
||||
return u, errors.New("user not found")
|
||||
}
|
||||
@@ -96,12 +96,25 @@ func GetUserByID(uid uint) (User, error) {
|
||||
return u, errors.New("User not found!")
|
||||
}
|
||||
*/
|
||||
u.PrepareGive()
|
||||
//u.PrepareGive()
|
||||
|
||||
return u, nil
|
||||
|
||||
}
|
||||
|
||||
func GetUserByName(username string) (User, error) {
|
||||
|
||||
var u User
|
||||
|
||||
// Query database for matching user object
|
||||
err := db.QueryRowx("SELECT * FROM users WHERE UserName=?", username).StructScan(&u)
|
||||
if err != nil {
|
||||
return u, errors.New("user not found")
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func GetUserRoleByID(uid uint) (UserRole, error) {
|
||||
|
||||
var ur UserRole
|
||||
@@ -118,6 +131,8 @@ func GetUserRoleByID(uid uint) (UserRole, error) {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
func (u *User) PrepareGive() {
|
||||
u.Password = ""
|
||||
}
|
||||
*/
|
||||
|
Reference in New Issue
Block a user