public function saveNewUser(\model\User $newUser) { $newUser->getUserName(); $file = fopen("data/" . $newUser->getUserName() . ".txt", "w"); fwrite($file, $newUser->getPassword()); fclose($file); }
public function addUserToDatabase(\model\User $user) { //Checking if username already exists if (isset($this->users[$user->getUserName()])) { throw new \Exception("Username already exists"); } else { if (isset($this->users[$user->getPassword()])) { throw new \Exception("Password already exists"); } else { //saving to "database with username as index key" $key = $user->getUserName(); $this->users[$key] = $user->getPassword(); } } }
/** * @param User $user * @return bool * @throws \EmtpyPasswordException * @throws \EmtpyUsernameException */ public function authenticateUser(\model\User $user) { $username = $user->getUserName(); $password = $user->getPassword(); if (empty($username)) { throw new \common\EmtpyUsernameException(); } if (empty($password)) { throw new \common\EmtpyPasswordException(); } if (file_exists("data/" . $username . ".txt")) { $handle = fopen("data/" . $username . ".txt", "r"); $contents = fread($handle, filesize("data/" . $username . ".txt")); fclose($handle); if ($contents === $password) { $this->setUserLoggedInSession(); return true; } else { return false; } } else { return false; } }