Implemented proper User struct
This commit is contained in:
parent
f59321b9c6
commit
4b5929911e
@ -34,7 +34,7 @@ func OpenDB(dbName string) (*DB, error) {
|
|||||||
return &db, nil
|
return &db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool) error {
|
func (db *DB) AddUser(user User, pass string) error {
|
||||||
hashedPass, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
|
hashedPass, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error creating password hash: %v", err)
|
return fmt.Errorf("error creating password hash: %v", err)
|
||||||
@ -42,11 +42,11 @@ func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool
|
|||||||
|
|
||||||
query := `
|
query := `
|
||||||
INSERT INTO users
|
INSERT INTO users
|
||||||
(username, password, first_name, last_name, writer, editor, admin)
|
(username, password, first_name, last_name, role)
|
||||||
VALUES
|
VALUES
|
||||||
(?, ?, ?, ?, ?, ?, ?)
|
(?, ?, ?, ?, ?)
|
||||||
`
|
`
|
||||||
_, err = db.Exec(query, user, string(hashedPass), first, last, writer, editor, admin)
|
_, err = db.Exec(query, user.UserName, string(hashedPass), user.FirstName, user.LastName, user.Role)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error inserting user into DB: %v", err)
|
return fmt.Errorf("error inserting user into DB: %v", err)
|
||||||
}
|
}
|
||||||
@ -54,7 +54,7 @@ func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) GetID(user string) (int64, error) {
|
func (db *DB) GetID(userName string) (int64, error) {
|
||||||
var id int64
|
var id int64
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
@ -63,7 +63,7 @@ func (db *DB) GetID(user string) (int64, error) {
|
|||||||
WHERE
|
WHERE
|
||||||
username = ?
|
username = ?
|
||||||
`
|
`
|
||||||
row := db.QueryRow(query, user)
|
row := db.QueryRow(query, userName)
|
||||||
if err := row.Scan(&id); err != nil {
|
if err := row.Scan(&id); err != nil {
|
||||||
return 0, fmt.Errorf("user not in DB: %v", err)
|
return 0, fmt.Errorf("user not in DB: %v", err)
|
||||||
}
|
}
|
||||||
|
18
cmd/data/user.go
Normal file
18
cmd/data/user.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package data
|
||||||
|
|
||||||
|
const (
|
||||||
|
Admin = iota
|
||||||
|
Editor
|
||||||
|
Writer
|
||||||
|
)
|
||||||
|
|
||||||
|
type Role int
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
RejectedArticles *ArticleList
|
||||||
|
UserName string
|
||||||
|
FirstName string
|
||||||
|
LastName string
|
||||||
|
ID int64
|
||||||
|
Role
|
||||||
|
}
|
@ -5,42 +5,54 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"streifling.com/jason/cpolis/cmd/data"
|
"streifling.com/jason/cpolis/cmd/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AddUserData struct {
|
type AddUserData struct {
|
||||||
User string
|
data.User
|
||||||
First string
|
Msg string
|
||||||
Last string
|
|
||||||
Role string
|
|
||||||
Msg string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func inputsEmpty(user, pass, pass2, first, last, role string) bool {
|
func inputsEmpty(user data.User, pass, pass2 string) bool {
|
||||||
return len(user) == 0 ||
|
return len(user.UserName) == 0 ||
|
||||||
|
len(user.FirstName) == 0 ||
|
||||||
|
len(user.LastName) == 0 ||
|
||||||
len(pass) == 0 ||
|
len(pass) == 0 ||
|
||||||
len(pass2) == 0 ||
|
len(pass2) == 0
|
||||||
len(first) == 0 ||
|
|
||||||
len(last) == 0 ||
|
|
||||||
len(role) == 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkUserStrings(user, first, last string) (string, int, bool) {
|
func checkUserStrings(user data.User) (string, int, bool) {
|
||||||
userLen := 15
|
userLen := 15
|
||||||
nameLen := 50
|
nameLen := 50
|
||||||
|
|
||||||
if len(user) > userLen {
|
if len(user.UserName) > userLen {
|
||||||
return "Benutzername", userLen, false
|
return "Benutzername", userLen, false
|
||||||
} else if len(first) > nameLen {
|
} else if len(user.FirstName) > nameLen {
|
||||||
return "Vorname", nameLen, false
|
return "Vorname", nameLen, false
|
||||||
} else if len(last) > nameLen {
|
} else if len(user.LastName) > nameLen {
|
||||||
return "Nachname", nameLen, false
|
return "Nachname", nameLen, false
|
||||||
} else {
|
} else {
|
||||||
return "", 0, true
|
return "", 0, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HomePage(db *data.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
numRows, err := db.CountEntries()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if numRows == 0 {
|
||||||
|
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/add-user.html")).Execute(w, nil)
|
||||||
|
} else {
|
||||||
|
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/login.html")).Execute(w, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func CreateUser() http.HandlerFunc {
|
func CreateUser() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", nil)
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", nil)
|
||||||
@ -49,31 +61,38 @@ func CreateUser() http.HandlerFunc {
|
|||||||
|
|
||||||
func AddUser(db *data.DB) http.HandlerFunc {
|
func AddUser(db *data.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var writer, editor, admin bool
|
role, err := strconv.Atoi(r.PostFormValue("role"))
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
htmlData := AddUserData{
|
htmlData := AddUserData{
|
||||||
User: r.PostFormValue("username"),
|
User: data.User{
|
||||||
First: r.PostFormValue("first-name"),
|
UserName: r.PostFormValue("username"),
|
||||||
Last: r.PostFormValue("last-name"),
|
FirstName: r.PostFormValue("first-name"),
|
||||||
Role: r.PostFormValue("role"),
|
LastName: r.PostFormValue("last-name"),
|
||||||
|
Role: data.Role(role),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
pass := r.PostFormValue("password")
|
pass := r.PostFormValue("password")
|
||||||
pass2 := r.PostFormValue("password2")
|
pass2 := r.PostFormValue("password2")
|
||||||
|
|
||||||
if inputsEmpty(htmlData.User, pass, pass2, htmlData.First, htmlData.Last, htmlData.Role) {
|
if inputsEmpty(htmlData.User, pass, pass2) {
|
||||||
htmlData.Msg = "Alle Felder müssen ausgefüllt werden."
|
htmlData.Msg = "Alle Felder müssen ausgefüllt werden."
|
||||||
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userString, stringLen, ok := checkUserStrings(htmlData.User, htmlData.First, htmlData.Last)
|
userString, stringLen, ok := checkUserStrings(htmlData.User)
|
||||||
if !ok {
|
if !ok {
|
||||||
htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt.")
|
htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt.")
|
||||||
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
id, _ := db.GetID(htmlData.User)
|
id, _ := db.GetID(htmlData.UserName)
|
||||||
if id != 0 {
|
if id != 0 {
|
||||||
htmlData.Msg = fmt.Sprint(htmlData.User, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.")
|
htmlData.Msg = fmt.Sprint(htmlData.UserName, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.")
|
||||||
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -82,27 +101,8 @@ func AddUser(db *data.DB) http.HandlerFunc {
|
|||||||
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch htmlData.Role {
|
|
||||||
case "writer":
|
|
||||||
writer = true
|
|
||||||
editor = false
|
|
||||||
admin = false
|
|
||||||
case "editor":
|
|
||||||
writer = false
|
|
||||||
editor = true
|
|
||||||
admin = false
|
|
||||||
case "admin":
|
|
||||||
writer = false
|
|
||||||
editor = false
|
|
||||||
admin = true
|
|
||||||
default:
|
|
||||||
log.Println("When setting up", htmlData.User, "the HTML was altered.")
|
|
||||||
htmlData.Msg = "Das HTML der Seite zu verändern ist unzulässig. Dieser Vorfall wurde gespeichert."
|
|
||||||
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := db.AddUser(htmlData.User, pass, htmlData.First, htmlData.Last, writer, editor, admin); err != nil {
|
if err := db.AddUser(htmlData.User, pass); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
16
main.go
16
main.go
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -37,20 +36,7 @@ func main() {
|
|||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/"))))
|
mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/"))))
|
||||||
|
|
||||||
numRows, err := db.CountEntries()
|
mux.HandleFunc("/", ui.HomePage(db))
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
if numRows == 0 {
|
|
||||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/add-user.html")).Execute(w, nil)
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/login.html")).Execute(w, nil)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
mux.HandleFunc("/rss/", ui.ShowRSS(feed))
|
mux.HandleFunc("/rss/", ui.ShowRSS(feed))
|
||||||
|
|
||||||
mux.HandleFunc("GET /hub/", ui.ShowHub())
|
mux.HandleFunc("GET /hub/", ui.ShowHub())
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
{{define "page-content"}}
|
{{define "page-content"}}
|
||||||
<h2>Neuer Benutzer</h2>
|
<h2>Neuer Benutzer</h2>
|
||||||
<form>
|
<form>
|
||||||
<input required name="username" placeholder="Benutzername" type="text" value="{{.User}}" />
|
<input required name="username" placeholder="Benutzername" type="text" value="{{.UserName}}" />
|
||||||
<input required name="password" placeholder="Passwort" type="password" />
|
<input required name="password" placeholder="Passwort" type="password" />
|
||||||
<input required name="password2" placeholder="Passwort wiederholen" type="password" />
|
<input required name="password2" placeholder="Passwort wiederholen" type="password" />
|
||||||
|
|
||||||
<input required name="first-name" placeholder="Vorname" type="text" value="{{.First}}" />
|
<input required name="first-name" placeholder="Vorname" type="text" value="{{.FirstName}}" />
|
||||||
<input required name="last-name" placeholder="Nachname" type="text" value="{{.Last}}" />
|
<input required name="last-name" placeholder="Nachname" type="text" value="{{.LastName}}" />
|
||||||
|
|
||||||
<label for="writer">Schreiber</label>
|
<label for="writer">Schreiber</label>
|
||||||
<input required id="writer" name="role" type="radio" value="writer" {{if eq .Role "writer" }}checked{{end}} />
|
<input required id="writer" name="role" type="radio" value="2" {{if eq .Role "2" }}checked{{end}} />
|
||||||
<label for="editor">Redakteur</label>
|
<label for="editor">Redakteur</label>
|
||||||
<input required id="editor" name="role" type="radio" value="editor" {{if eq .Role "editor" }}checked{{end}} />
|
<input required id="editor" name="role" type="radio" value="1" {{if eq .Role "1" }}checked{{end}} />
|
||||||
<label for="admin">Admin</label>
|
<label for="admin">Admin</label>
|
||||||
<input required id="admin" name="role" type="radio" value="admin" {{if eq .Role "admin" }}checked{{end}} />
|
<input required id="admin" name="role" type="radio" value="0" {{if eq .Role "0" }}checked{{end}} />
|
||||||
|
|
||||||
<input type="submit" value="Anlegen" hx-post="/add-user/" hx-target="#page-content" />
|
<input type="submit" value="Anlegen" hx-post="/add-user/" hx-target="#page-content" />
|
||||||
</form>
|
</form>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user