Implemented proper User struct
This commit is contained in:
parent
f34efc95dd
commit
7f2433c30b
@ -34,7 +34,7 @@ func OpenDB(dbName string) (*DB, error) {
|
||||
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)
|
||||
if err != nil {
|
||||
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 := `
|
||||
INSERT INTO users
|
||||
(username, password, first_name, last_name, writer, editor, admin)
|
||||
(username, password, first_name, last_name, role)
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
func (db *DB) GetID(user string) (int64, error) {
|
||||
func (db *DB) GetID(userName string) (int64, error) {
|
||||
var id int64
|
||||
|
||||
query := `
|
||||
@ -63,7 +63,7 @@ func (db *DB) GetID(user string) (int64, error) {
|
||||
WHERE
|
||||
username = ?
|
||||
`
|
||||
row := db.QueryRow(query, user)
|
||||
row := db.QueryRow(query, userName)
|
||||
if err := row.Scan(&id); err != nil {
|
||||
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"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"streifling.com/jason/cpolis/cmd/data"
|
||||
)
|
||||
|
||||
type AddUserData struct {
|
||||
User string
|
||||
First string
|
||||
Last string
|
||||
Role string
|
||||
Msg string
|
||||
data.User
|
||||
Msg string
|
||||
}
|
||||
|
||||
func inputsEmpty(user, pass, pass2, first, last, role string) bool {
|
||||
return len(user) == 0 ||
|
||||
func inputsEmpty(user data.User, pass, pass2 string) bool {
|
||||
return len(user.UserName) == 0 ||
|
||||
len(user.FirstName) == 0 ||
|
||||
len(user.LastName) == 0 ||
|
||||
len(pass) == 0 ||
|
||||
len(pass2) == 0 ||
|
||||
len(first) == 0 ||
|
||||
len(last) == 0 ||
|
||||
len(role) == 0
|
||||
len(pass2) == 0
|
||||
}
|
||||
|
||||
func checkUserStrings(user, first, last string) (string, int, bool) {
|
||||
func checkUserStrings(user data.User) (string, int, bool) {
|
||||
userLen := 15
|
||||
nameLen := 50
|
||||
|
||||
if len(user) > userLen {
|
||||
if len(user.UserName) > userLen {
|
||||
return "Benutzername", userLen, false
|
||||
} else if len(first) > nameLen {
|
||||
} else if len(user.FirstName) > nameLen {
|
||||
return "Vorname", nameLen, false
|
||||
} else if len(last) > nameLen {
|
||||
} else if len(user.LastName) > nameLen {
|
||||
return "Nachname", nameLen, false
|
||||
} else {
|
||||
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 {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
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 {
|
||||
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{
|
||||
User: r.PostFormValue("username"),
|
||||
First: r.PostFormValue("first-name"),
|
||||
Last: r.PostFormValue("last-name"),
|
||||
Role: r.PostFormValue("role"),
|
||||
User: data.User{
|
||||
UserName: r.PostFormValue("username"),
|
||||
FirstName: r.PostFormValue("first-name"),
|
||||
LastName: r.PostFormValue("last-name"),
|
||||
Role: data.Role(role),
|
||||
},
|
||||
}
|
||||
pass := r.PostFormValue("password")
|
||||
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."
|
||||
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
||||
return
|
||||
}
|
||||
userString, stringLen, ok := checkUserStrings(htmlData.User, htmlData.First, htmlData.Last)
|
||||
userString, stringLen, ok := checkUserStrings(htmlData.User)
|
||||
if !ok {
|
||||
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)
|
||||
return
|
||||
}
|
||||
id, _ := db.GetID(htmlData.User)
|
||||
id, _ := db.GetID(htmlData.UserName)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
|
16
main.go
16
main.go
@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@ -37,20 +36,7 @@ func main() {
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/"))))
|
||||
|
||||
numRows, err := db.CountEntries()
|
||||
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("/", ui.HomePage(db))
|
||||
mux.HandleFunc("/rss/", ui.ShowRSS(feed))
|
||||
|
||||
mux.HandleFunc("GET /hub/", ui.ShowHub())
|
||||
|
@ -1,19 +1,19 @@
|
||||
{{define "page-content"}}
|
||||
<h2>Neuer Benutzer</h2>
|
||||
<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="password2" placeholder="Passwort wiederholen" type="password" />
|
||||
|
||||
<input required name="first-name" placeholder="Vorname" type="text" value="{{.First}}" />
|
||||
<input required name="last-name" placeholder="Nachname" type="text" value="{{.Last}}" />
|
||||
<input required name="first-name" placeholder="Vorname" type="text" value="{{.FirstName}}" />
|
||||
<input required name="last-name" placeholder="Nachname" type="text" value="{{.LastName}}" />
|
||||
|
||||
<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>
|
||||
<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>
|
||||
<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" />
|
||||
</form>
|
||||
|
Loading…
x
Reference in New Issue
Block a user