cpolis/cmd/data/db.go

130 lines
2.8 KiB
Go
Raw Normal View History

package data
import (
"database/sql"
"fmt"
"github.com/go-sql-driver/mysql"
2024-02-22 18:49:51 +01:00
"golang.org/x/crypto/bcrypt"
)
type DB struct {
*sql.DB
}
func OpenDB(dbName string) (*DB, error) {
var err error
db := DB{DB: &sql.DB{}}
cfg := mysql.NewConfig()
cfg.DBName = dbName
cfg.User, cfg.Passwd, err = getCredentials()
if err != nil {
2024-02-22 15:23:29 +01:00
return nil, fmt.Errorf("error reading user credentials for DB: %v", err)
}
db.DB, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil {
2024-02-22 15:23:29 +01:00
return nil, fmt.Errorf("error opening DB: %v", err)
}
if err = db.Ping(); err != nil {
2024-02-22 15:23:29 +01:00
return nil, fmt.Errorf("error pinging DB: %v", err)
}
return &db, nil
}
2024-02-22 18:49:51 +01:00
2024-03-01 12:25:53 +01:00
func (db *DB) AddUser(user User, pass string) error {
hashedPass, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("error creating password hash: %v", err)
}
2024-02-22 18:49:51 +01:00
query := `
INSERT INTO users
2024-03-01 12:25:53 +01:00
(username, password, first_name, last_name, role)
2024-02-22 18:49:51 +01:00
VALUES
2024-03-01 12:25:53 +01:00
(?, ?, ?, ?, ?)
2024-02-22 18:49:51 +01:00
`
2024-03-01 12:25:53 +01:00
_, err = db.Exec(query, user.UserName, string(hashedPass), user.FirstName, user.LastName, user.Role)
2024-02-22 18:49:51 +01:00
if err != nil {
return fmt.Errorf("error inserting user into DB: %v", err)
}
return nil
}
2024-02-22 19:27:41 +01:00
2024-03-01 12:25:53 +01:00
func (db *DB) GetID(userName string) (int64, error) {
2024-02-24 09:54:25 +01:00
var id int64
query := `
SELECT id FROM
users
WHERE
username = ?
`
2024-03-01 12:25:53 +01:00
row := db.QueryRow(query, userName)
2024-02-24 09:54:25 +01:00
if err := row.Scan(&id); err != nil {
return 0, fmt.Errorf("user not in DB: %v", err)
}
return id, nil
}
func (db *DB) CheckPassword(id int64, pass string) error {
var queriedPass string
2024-02-22 19:27:41 +01:00
2024-02-24 09:54:25 +01:00
query := `
2024-02-22 19:27:41 +01:00
SELECT password FROM
users
WHERE
id = ?
`
2024-02-24 09:54:25 +01:00
row := db.QueryRow(query, id)
if err := row.Scan(&queriedPass); err != nil {
2024-02-22 19:27:41 +01:00
return fmt.Errorf("error reading password from DB: %v", err)
}
2024-02-24 09:54:25 +01:00
if err := bcrypt.CompareHashAndPassword([]byte(queriedPass), []byte(pass)); err != nil {
return fmt.Errorf("incorrect password: %v", err)
}
return nil
}
func (db *DB) ChangePassword(id int64, oldPass, newPass string) error {
if err := db.CheckPassword(id, oldPass); err != nil {
2024-02-22 19:27:41 +01:00
return fmt.Errorf("error checking password: %v", err)
}
newHashedPass, err := bcrypt.GenerateFromPassword([]byte(newPass), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("error creating password hash: %v", err)
}
2024-02-24 14:49:29 +01:00
query := `
2024-02-24 09:54:25 +01:00
UPDATE users SET
password = ?
WHERE
id = ?
2024-02-22 19:27:41 +01:00
`
2024-02-24 14:49:29 +01:00
_, err = db.Exec(query, string(newHashedPass), id)
2024-02-22 19:27:41 +01:00
if err != nil {
return fmt.Errorf("error updating password in DB: %v", err)
}
return nil
}
2024-02-24 14:49:29 +01:00
func (db *DB) CountEntries() (int64, error) {
var count int64
query := `SELECT COUNT(*) FROM users`
row := db.QueryRow(query)
if err := row.Scan(&count); err != nil {
return 0, fmt.Errorf("error counting rows in user DB: %v", err)
}
return count, nil
}