2018-09-27 16:51:36 +02:00
|
|
|
package paneldb
|
2018-09-26 15:59:50 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/go-pg/pg"
|
|
|
|
"github.com/pkg/errors"
|
2018-09-27 23:11:07 +02:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2018-09-26 15:59:50 +02:00
|
|
|
)
|
|
|
|
|
2018-09-29 21:48:54 +02:00
|
|
|
// An User represents a user of the application.
|
2018-09-29 12:18:29 +02:00
|
|
|
type User struct {
|
2018-09-29 14:06:50 +02:00
|
|
|
ID string `sql:"uid"`
|
2018-09-29 21:48:54 +02:00
|
|
|
Name string `sql:"username"`
|
2018-09-29 14:06:50 +02:00
|
|
|
Email string `sql:"email"`
|
|
|
|
DateCreated string `sql:"date_created"`
|
|
|
|
PasswordHash string `sql:"password"`
|
2018-09-29 12:18:29 +02:00
|
|
|
}
|
|
|
|
|
2018-09-29 21:48:54 +02:00
|
|
|
func (u User) String() string {
|
|
|
|
return fmt.Sprintf("{User: %s <%s>}", u.Name, u.Email)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DB represents the application database. It provides high-level methods to get and manipulate objects.
|
2018-09-26 15:59:50 +02:00
|
|
|
type DB struct {
|
|
|
|
PgDB *pg.DB
|
|
|
|
Schema string
|
|
|
|
}
|
|
|
|
|
2018-09-29 21:48:54 +02:00
|
|
|
// Setup will create schemas and tables as appropriate
|
2018-09-26 16:53:06 +02:00
|
|
|
func (db *DB) Setup() error {
|
2018-09-26 15:59:50 +02:00
|
|
|
_, err := db.PgDB.Exec(`CREATE SCHEMA IF NOT EXISTS users`)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Error creating schema")
|
|
|
|
}
|
2018-09-26 16:53:06 +02:00
|
|
|
fmt.Println("schema created")
|
2018-09-26 15:59:50 +02:00
|
|
|
|
|
|
|
_, err = db.PgDB.Exec(`CREATE TABLE IF NOT EXISTS users.users (
|
|
|
|
uid UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
|
|
|
username varchar(64) UNIQUE NOT NULL,
|
|
|
|
email varchar(100) UNIQUE NOT NULL,
|
2018-09-29 12:18:29 +02:00
|
|
|
date_created TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
2018-09-26 15:59:50 +02:00
|
|
|
password varchar(256) NOT NULL
|
|
|
|
)`)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Error creating users table")
|
|
|
|
}
|
2018-09-26 16:53:06 +02:00
|
|
|
fmt.Println("table created")
|
2018-09-26 15:59:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-29 21:48:54 +02:00
|
|
|
// UserAdd will add a user to the database
|
2018-09-27 23:11:07 +02:00
|
|
|
func (db *DB) UserAdd(username, email, pwd string) error {
|
|
|
|
stmt, err := db.PgDB.Prepare("INSERT INTO users.users (username, email, password) VALUES ($1, $2, $3)")
|
2018-09-26 15:59:50 +02:00
|
|
|
if err != nil {
|
2018-09-28 00:55:12 +02:00
|
|
|
return errors.Wrap(err, "Error preparing insert statement")
|
2018-09-26 15:59:50 +02:00
|
|
|
}
|
2018-09-27 23:11:07 +02:00
|
|
|
pwhash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Error deriving password (with bcrypt)")
|
|
|
|
}
|
|
|
|
_, err = stmt.Exec(username, email, string(pwhash))
|
2018-09-26 15:59:50 +02:00
|
|
|
if err != nil {
|
2018-09-28 00:55:12 +02:00
|
|
|
return errors.Wrapf(err, "Error adding user `%s`", username)
|
2018-09-26 15:59:50 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-29 12:18:29 +02:00
|
|
|
|
2018-09-29 21:48:54 +02:00
|
|
|
// GetUsers returns an array of Users
|
2018-09-29 14:32:42 +02:00
|
|
|
func (db *DB) GetUsers() ([]User, error) {
|
2018-09-29 12:18:29 +02:00
|
|
|
var users []User
|
|
|
|
_, err := db.PgDB.Query(&users, "SELECT * FROM users.users")
|
|
|
|
if err != nil {
|
|
|
|
return users, errors.Wrap(err, "User enumeration failed")
|
|
|
|
}
|
|
|
|
return users, err
|
|
|
|
}
|
2018-09-29 21:48:54 +02:00
|
|
|
|
|
|
|
// GetUserByName Search for exact username
|
|
|
|
func (db *DB) GetUserByName(username string) (User, error) {
|
|
|
|
var user User
|
|
|
|
_, err := db.PgDB.QueryOne(&user, `SELECT * FROM users.users WHERE username = ?`, username)
|
|
|
|
if err != nil {
|
|
|
|
return user, errors.Wrap(err, "User fetch failed")
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserByEmail searches for exact email
|
|
|
|
func (db *DB) GetUserByEmail(email string) (User, error) {
|
|
|
|
var user User
|
|
|
|
_, err := db.PgDB.QueryOne(&user, "SELECT * FROM users.users WHERE email = ?", email)
|
|
|
|
if err != nil {
|
|
|
|
return user, errors.Wrap(err, "User fetch failed")
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserByID search for exact uid
|
|
|
|
func (db *DB) GetUserByID(uid string) (User, error) {
|
|
|
|
var user User
|
|
|
|
_, err := db.PgDB.QueryOne(&user, "SELECT * FROM users.users WHERE uid=$1", uid)
|
|
|
|
if err != nil {
|
|
|
|
return user, errors.Wrap(err, "User fetch failed")
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|