Added ability to add user

This commit is contained in:
2024-02-24 10:28:12 +01:00
parent 2e08600814
commit 8ef6b6472d
6 changed files with 76 additions and 31 deletions

View File

@ -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)

View File

@ -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
}