DEVELOPMENT ENVIRONMENT

~liljamo/felu

c22516aad2f0ffa1decb6e1a6ae1221d84e7ce8f — Jonni Liljamo 1 year, 1 month ago 712c650
feat: more breaking changes by chaning the initial schema
4 files changed, 18 insertions(+), 18 deletions(-)

M db/migrations.go
M db/users.go
M handlers/login.go
M middlewares/admin.go
M db/migrations.go => db/migrations.go +5 -5
@@ 66,19 66,19 @@ func migrations() []string {
			schema_version INTEGER
		)`, migrationsTable),
		fmt.Sprintf(`CREATE TABLE users (
			id INTEGER NOT NULL PRIMARY KEY,
			ulid TEXT NOT NULL,
			id TEXT NOT NULL PRIMARY KEY,
			email TEXT NOT NULL,
			pwd TEXT NOT NULL,
			is_admin INTEGER DEFAULT FALSE NOT NULL,
			UNIQUE(ulid, email)
			UNIQUE(id, email)
		)`),
		fmt.Sprintf(`CREATE TABLE domains (
			id TEXT NOT NULL PRIMARY KEY,
			apikey TEXT NOT NULL,
			owner INTEGER NOT NULL,
			ddns_domain TEXT,
			owner TEXT NOT NULL,
			ddns_domain TEXT NOT NULL,
			a_record TEXT,
			UNIQUE(id, ddns_domain),
			FOREIGN KEY(owner) REFERENCES users(id)
		)`),
	}

M db/users.go => db/users.go +11 -11
@@ 23,7 23,7 @@ func CreateUser(email string, pwd string) error {

	ulid := ulid.Make().String()

	_, err = DBConn.Exec(`INSERT INTO users(ulid, email, pwd) VALUES ($1, $2, $3)`,
	_, err = DBConn.Exec(`INSERT INTO users(id, email, pwd) VALUES ($1, $2, $3)`,
		ulid, email, string(encoded))
	if err != nil {
		return err


@@ 41,7 41,7 @@ func CreateAdmin(email string, pwd string) error {

	ulid := ulid.Make().String()

	_, err = DBConn.Exec(`INSERT INTO users(ulid, email, pwd, is_admin) VALUES ($1, $2, $3, TRUE)`,
	_, err = DBConn.Exec(`INSERT INTO users(id, email, pwd, is_admin) VALUES ($1, $2, $3, TRUE)`,
		ulid, email, string(encoded))
	if err != nil {
		return err


@@ 51,7 51,7 @@ func CreateAdmin(email string, pwd string) error {
}

type User struct {
	Ulid    string
	Id    string
	Email   string
	IsAdmin bool
}


@@ 59,8 59,8 @@ type User struct {
func FetchUserWithCreds(email string, pwd string) (*User, error) {
	user := User{ Email: email }
	var encodedPwd string
	err := DBConn.QueryRow(`SELECT ulid, pwd FROM users WHERE email = $1`,
		email).Scan(&user.Ulid, &encodedPwd)
	err := DBConn.QueryRow(`SELECT id, pwd FROM users WHERE email = $1`,
		email).Scan(&user.Id, &encodedPwd)
	if err == sql.ErrNoRows {
		return nil, errors.New("User not found")
	}


@@ 79,10 79,10 @@ func FetchUserWithCreds(email string, pwd string) (*User, error) {
	return &user, nil
}

func FetchUserWithUlid(ulid string) (*User, error) {
	user := User{ Ulid: ulid }
	err := DBConn.QueryRow(`SELECT email, is_admin FROM users WHERE ulid = $1`,
		ulid).Scan(&user.Email, &user.IsAdmin)
func FetchUserWithId(id string) (*User, error) {
	user := User{ Id: id }
	err := DBConn.QueryRow(`SELECT email, is_admin FROM users WHERE id = $1`,
		id).Scan(&user.Email, &user.IsAdmin)
	if err == sql.ErrNoRows {
		return nil, errors.New("User not found")
	}


@@ 94,7 94,7 @@ func FetchUserWithUlid(ulid string) (*User, error) {
}

func FetchAllUsers() ([]User, error) {
	rows, err := DBConn.Query(`SELECT ulid, email, is_admin FROM users`)
	rows, err := DBConn.Query(`SELECT id, email, is_admin FROM users`)
	if err != nil {
		return nil, err
	}


@@ 103,7 103,7 @@ func FetchAllUsers() ([]User, error) {
	var users []User
	for rows.Next() {
		var user User
		err = rows.Scan(&user.Ulid, &user.Email, &user.IsAdmin)
		err = rows.Scan(&user.Id, &user.Email, &user.IsAdmin)
		if err != nil {
			return nil, err
		}

M handlers/login.go => handlers/login.go +1 -1
@@ 34,7 34,7 @@ func AuthLogin(sm *scs.SessionManager, c *gin.Context) {
		return
	}

	sm.Put(c.Request.Context(), "user_id", user.Ulid)
	sm.Put(c.Request.Context(), "user_id", user.Id)

	c.Header("HX-Redirect", "/manage")
	//c.Redirect(http.StatusFound, "/manage")

M middlewares/admin.go => middlewares/admin.go +1 -1
@@ 17,7 17,7 @@ func AdminOnly() gin.HandlerFunc {
	return func(c *gin.Context) {
		user_id, exists := c.Get("user_id")
		if exists {
			user, err := db.FetchUserWithUlid(user_id.(string))
			user, err := db.FetchUserWithId(user_id.(string))
			if err == nil {
				if user.IsAdmin {
					c.Next()