diff options
Diffstat (limited to 'go-src/user.go')
| -rw-r--r-- | go-src/user.go | 88 |
1 files changed, 51 insertions, 37 deletions
diff --git a/go-src/user.go b/go-src/user.go index 1456340..78501c7 100644 --- a/go-src/user.go +++ b/go-src/user.go @@ -14,6 +14,55 @@ type User struct { Name string } +type UserProp int + +const ( + passwordProp UserProp = iota + 1 + nameProp +) + +func (u User) ValidatePassword(password string) bool { + // TODO: Implement hashing + f, _ := os.ReadFile(folderPaths.FileAtUsersFolder(u.Username)) + passHash := bytes.Split(f, []byte("\n"))[1] + return string(passHash) == password +} + +func (u User) updateProp(currentPassword string, newValue string, userProp UserProp) bool { + if !u.ValidatePassword(currentPassword) { + return false + } + var fileContents []byte + + switch userProp { + case passwordProp: + fileContents = []byte(u.Username + "\n" + newValue + "\n" + u.Name) + case nameProp: + fileContents = []byte(u.Username + "\n" + currentPassword + "\n" + newValue) + default: + return false + } + + os.WriteFile( + folderPaths.FileAtUsersFolder(u.Username), + fileContents, + 0644) + return true +} + +func (u User) UpdatePassword(oldPassword string, newPassword string) bool { + return u.updateProp(oldPassword, newPassword, passwordProp) +} + +func (u User) UpdateName(password string, newName string) bool { + return u.updateProp(password, newName, nameProp) +} + +func logoutUser() { + loggedInUser.Username = "" + loggedInUser.Name = "" +} + func createUser(data []string) { // TODO: Password hashing f, _ := os.Create(folderPaths.FileAtUsersFolder(data[0])) @@ -22,50 +71,15 @@ func createUser(data []string) { } func logInUser(username string, password string) bool { - if validatePassword(username, password) { - loggedInUser = getUser(username) + if cu := getUser(username); cu.ValidatePassword(password) { + loggedInUser = cu return true } return false } -func logoutUser() { - loggedInUser.Username = "" - loggedInUser.Name = "" -} - func getUser(username string) User { f, _ := os.ReadFile(folderPaths.FileAtUsersFolder(username)) values := bytes.Split(f, []byte("\n")) return User{string(values[0]), string(values[2])} } - -func validatePassword(username string, password string) bool { - // TODO: Implement hashing - f, _ := os.ReadFile(folderPaths.FileAtUsersFolder(username)) - passHash := bytes.Split(f, []byte("\n"))[1] - return string(passHash) == password -} - -func updatePassword(oldPassword string, newPassword string) bool { - if !validatePassword(loggedInUser.Username, oldPassword) { - return false - } - os.WriteFile( - folderPaths.FileAtUsersFolder(loggedInUser.Username), - []byte(loggedInUser.Username+"\n"+newPassword+"\n"+loggedInUser.Name), - 0644) - return true -} - -func updateName(password string, newName string) bool { - if !validatePassword(loggedInUser.Username, password) { - return false - } - os.WriteFile( - folderPaths.FileAtUsersFolder(loggedInUser.Username), - []byte(loggedInUser.Username+"\n"+password+"\n"+newName), - 0644) - loggedInUser.Name = newName - return true -} |
