diff --git a/cmd/data/db.go b/cmd/data/db.go index 7bd7392..53fb868 100644 --- a/cmd/data/db.go +++ b/cmd/data/db.go @@ -35,16 +35,6 @@ func OpenDB(dbName string) (*DB, 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) if err != nil { return fmt.Errorf("error creating password hash: %v", err) diff --git a/cmd/data/helpers.go b/cmd/data/helpers.go index 39e8572..bc4b3f9 100644 --- a/cmd/data/helpers.go +++ b/cmd/data/helpers.go @@ -50,24 +50,3 @@ func getCredentials() (string, string, error) { 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 -} diff --git a/cmd/ui/helpers.go b/cmd/ui/helpers.go new file mode 100644 index 0000000..b88ffbe --- /dev/null +++ b/cmd/ui/helpers.go @@ -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 + } +} diff --git a/cmd/ui/htmlStructs.go b/cmd/ui/htmlStructs.go new file mode 100644 index 0000000..5b1faa2 --- /dev/null +++ b/cmd/ui/htmlStructs.go @@ -0,0 +1 @@ +package ui diff --git a/cmd/ui/ui.go b/cmd/ui/ui.go index 6a1681a..6cd7ef1 100644 --- a/cmd/ui/ui.go +++ b/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) } } + +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) + } +} diff --git a/web/templates/add-user.html b/web/templates/add-user.html new file mode 100644 index 0000000..b238bd7 --- /dev/null +++ b/web/templates/add-user.html @@ -0,0 +1,19 @@ +{{define "page-content"}} +
+ + + + + + + + + + + + + + + +
+{{end}}