password)) { if (password_needs_rehash($user->password, self::PW_ALGORITHM)) { Patch::byId(Table::USER, $user->id, ['password' => password_hash($password, self::PW_ALGORITHM)]); } $_SESSION[Key::USER_ID] = $user->id; $_SESSION[Key::USER_EMAIL] = $user->email; frc_redirect($returnTo ?? '/'); } } /** * Log on a user with e-mail address and password * * @param string $email The e-mail address for the user (cannot be the single-user mode user) * @param string $password The password provided by the user * @param string|null $returnTo The URL to which the user should be redirected * @throws DocumentException If any is encountered */ public static function logOnUser(string $email, string $password, ?string $returnTo): void { if (SECURITY_MODEL == self::SINGLE_USER_WITH_PASSWORD) { $dbEmail = self::SINGLE_USER_EMAIL; } else { if ($email == self::SINGLE_USER_EMAIL) { add_error('Invalid credentials; log on unsuccessful'); return; } $dbEmail = $email; } $user = User::findByEmail($dbEmail); if ($user) self::verifyPassword($user, $password, $returnTo); add_error('Invalid credentials; log on unsuccessful'); } /** * Update the password for the given user * * @param string $email The e-mail address of the user whose password should be updated * @param string $password The new password for this user * @throws DocumentException If any is encountered */ public static function updatePassword(string $email, string $password): void { Patch::byFields(Table::USER, [Field::EQ('email', $email)], ['password' => password_hash($password, self::PW_ALGORITHM)]); } /** * Log on the single user * * @throws DocumentException If any is encountered */ private static function logOnSingleUser(): void { $user = User::findByEmail(self::SINGLE_USER_EMAIL); if (!$user) { User::add(self::SINGLE_USER_EMAIL, self::SINGLE_USER_PASSWORD); $user = User::findByEmail(self::SINGLE_USER_EMAIL); } self::verifyPassword($user, self::SINGLE_USER_PASSWORD, $_GET['returnTo']); } /** * Verify that user is logged on * * @param bool $redirectIfAnonymous Whether to redirect the request if there is no user logged on * @throws DocumentException If any is encountered */ public static function verifyUser(bool $redirectIfAnonymous = true): void { if (key_exists(Key::USER_ID, $_SESSION)) return; if (SECURITY_MODEL == self::SINGLE_USER) self::logOnSingleUser(); if (SECURITY_MODEL != self::SINGLE_USER_WITH_PASSWORD && SECURITY_MODEL != self::MULTI_USER) { die('Unrecognized security model (' . SECURITY_MODEL . ')'); } if ($redirectIfAnonymous) { header("Location: /user/log-on?returnTo={$_SERVER['REQUEST_URI']}", true, 307); die(); } } }