Added ability to add user
This commit is contained in:
parent
2e08600814
commit
8ef6b6472d
@ -35,16 +35,6 @@ func OpenDB(dbName string) (*DB, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool) error {
|
func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool) error {
|
||||||
userString, stringLen, ok := checkUserStrings(user, first, last)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("error: %v is longer than %v characters", userString, stringLen)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !permissionsOK(writer, editor, admin) {
|
|
||||||
return fmt.Errorf("error: permissions must be mutually exclusive: writer = %v, editor = %v, admin = %v",
|
|
||||||
writer, editor, admin)
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
||||||
|
@ -50,24 +50,3 @@ func getCredentials() (string, string, error) {
|
|||||||
|
|
||||||
return user, pass, nil
|
return user, pass, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkUserStrings(user, first, last string) (string, int, bool) {
|
|
||||||
userLen := 15
|
|
||||||
nameLen := 50
|
|
||||||
|
|
||||||
if len(user) > userLen {
|
|
||||||
return user, userLen, false
|
|
||||||
} else if len(first) > nameLen {
|
|
||||||
return first, nameLen, false
|
|
||||||
} else if len(last) > nameLen {
|
|
||||||
return last, nameLen, false
|
|
||||||
} else {
|
|
||||||
return "", 0, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func permissionsOK(writer, editor, admin bool) bool {
|
|
||||||
return writer && !editor && !admin ||
|
|
||||||
!writer && editor && !admin ||
|
|
||||||
!writer && !editor && admin
|
|
||||||
}
|
|
||||||
|
16
cmd/ui/helpers.go
Normal file
16
cmd/ui/helpers.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package ui
|
||||||
|
|
||||||
|
func checkUserStrings(user, first, last string) (string, int, bool) {
|
||||||
|
userLen := 15
|
||||||
|
nameLen := 50
|
||||||
|
|
||||||
|
if len(user) > userLen {
|
||||||
|
return user, userLen, false
|
||||||
|
} else if len(first) > nameLen {
|
||||||
|
return first, nameLen, false
|
||||||
|
} else if len(last) > nameLen {
|
||||||
|
return last, nameLen, false
|
||||||
|
} else {
|
||||||
|
return "", 0, true
|
||||||
|
}
|
||||||
|
}
|
1
cmd/ui/htmlStructs.go
Normal file
1
cmd/ui/htmlStructs.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package ui
|
40
cmd/ui/ui.go
40
cmd/ui/ui.go
@ -45,3 +45,43 @@ func HandleFinishedEdit(f *feed.Feed) http.HandlerFunc {
|
|||||||
// template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "html-result", rssItem)
|
// template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "html-result", rssItem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HandleAddUser(db *data.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var writer, editor, admin bool
|
||||||
|
|
||||||
|
user := r.PostFormValue("username")
|
||||||
|
pass := r.PostFormValue("password")
|
||||||
|
pass2 := r.PostFormValue("password2")
|
||||||
|
first := r.PostFormValue("first-name")
|
||||||
|
last := r.PostFormValue("last-name")
|
||||||
|
role := r.PostFormValue("role")
|
||||||
|
|
||||||
|
_, _, ok := checkUserStrings(user, first, last)
|
||||||
|
if !ok {
|
||||||
|
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
|
||||||
|
}
|
||||||
|
if pass != pass2 {
|
||||||
|
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
|
||||||
|
}
|
||||||
|
switch 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:
|
||||||
|
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
db.AddUser(user, pass, first, last, writer, editor, admin)
|
||||||
|
template.Must(template.ParseFiles("web/templates/editor.html")).Execute(w, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
19
web/templates/add-user.html
Normal file
19
web/templates/add-user.html
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{{define "page-content"}}
|
||||||
|
<form>
|
||||||
|
<input name="username" placeholder="Benutzername" type="text" />
|
||||||
|
<input name="password" placeholder="Passwort" type="password" />
|
||||||
|
<input name="password2" placeholder="Passwort wiederholen" type="password" />
|
||||||
|
|
||||||
|
<input name="first-name" placeholder="Vorname" type="text" />
|
||||||
|
<input name="last-name" placeholder="Nachname" type="text" />
|
||||||
|
|
||||||
|
<label for="writer">Schreiber</label>
|
||||||
|
<input id="writer" name="role" type="radio" value="writer" />
|
||||||
|
<label for="editor">Redakteur</label>
|
||||||
|
<input id="editor" name="role" type="radio" value="editor" />
|
||||||
|
<label for="admin">Admin</label>
|
||||||
|
<input id="admin" name="role" type="radio" value="admin" />
|
||||||
|
|
||||||
|
<input type="submit" value="Anlegen" hx-post="/add-user/" hx-target="#page-content" />
|
||||||
|
</form>
|
||||||
|
{{end}}
|
Loading…
x
Reference in New Issue
Block a user