Added ability to add user
This commit is contained in:
		@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										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)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user