Passwort ändern eingefügt

This commit is contained in:
Matthias Grief
2024-12-02 14:58:07 +01:00
parent f4601e1840
commit 5e87a8654c
3 changed files with 53 additions and 3 deletions

View File

@@ -36,6 +36,10 @@ class User
*/
public static function createUser(string $username, string $password): User|false
{
if(self::getFromUsername($username) !== false) {
return false;
}
if(strlen($username) > 100) {
return false;
}
@@ -141,6 +145,54 @@ class User
}
fclose($file);
unset($this->username);
unset($this->passwordHash);
return true;
}
/**
* Ändert das Passwort des Accounts
* @param string $oldPassword altes Passwort
* @param string $newPassword Neues Passwort
* @return bool true, wenn erfolgreich geändert, sonst false
*/
public function changePassword(string $oldPassword, string $newPassword): bool
{
if(!$this->isPasswordCorrect($oldPassword)) {
return false;
}
if(!$this->logout()) {
return false;
}
$file = fopen(self::$userdataDirectory . self::$userdataFile, "c+");
if(!$file) {
return false;
}
$this->passwordHash = password_hash($newPassword, PASSWORD_ARGON2I);
$lastLine = ftell($file);
while (($data = fgetcsv($file, 300)) !== false) {
if (count($data) != 2) {
} else if ($data[0] !== $this->username) {
} else {
$data[1] = $this->passwordHash;
fseek($file, $lastLine);
fputcsv($file, $data);
break;
}
$lastLine = ftell($file);
}
fclose($file);
return true;
}
@@ -191,7 +243,6 @@ class User
if(time() - $_SESSION["login_time"] > 86400 * 5) {
session_unset();
session_destroy();
return false;
}
@@ -211,7 +262,6 @@ class User
}
session_unset();
session_destroy();
return true;
}