close(); } } /** * Get the way we will refer to the user against whom action is being taken * * @param string $email The e-mail address of the user * @return string The string to use when displaying results */ function display_user(string $email): string { return $email == Security::SINGLE_USER_EMAIL ? 'single-user mode user' : "user \"$email\""; } /** * Set a user's password */ function set_password(string $email, string $password): void { $db = Data::getConnection(); try { $displayUser = display_user($email); // Ensure this user exists $user = User::findByEmail($email); if (!$user) { printfn('No %s exists', $displayUser); return; } Security::updatePassword($email, $password, $db); $msg = $email == Security::SINGLE_USER_EMAIL && $password == Security::SINGLE_USER_PASSWORD ? 'reset' : sprintf('set to "%s"', $password); printfn('%s password %s successfully', init_cap($displayUser), $msg); } catch (DocumentException $ex) { printfn("$ex"); } finally { $db->close(); } } /** * Delete a user * * @param string $email The e-mail address of the user to be deleted */ function delete_user(string $email): void { $db = Data::getConnection(); try { $displayUser = display_user($email); // Get the user for the provided e-mail address $user = User::findByEmail($email); if (!$user) { printfn('No %s exists', $displayUser); return; } try { $feedCount = Count::byFields(Table::FEED, [Field::EQ('user_id', $user->id)], $db); } catch (DocumentException $ex) { printfn("$ex"); return; } $proceed = readline("Delete the $displayUser and their $feedCount feed(s)? (y/N)" . PHP_EOL); if (!$proceed || !str_starts_with(strtolower($proceed), 'y')) { printfn('Deletion canceled'); return; } try { // TODO: convert query $itemDelete = $db->prepare('DELETE FROM item WHERE feed_id IN (SELECT id FROM feed WHERE user_id = :user)'); $itemDelete->bindValue(':user', $user->id); $itemDelete->execute(); Delete::byFields(Table::FEED, [Field::EQ('user_id', $user->id)], $db); Delete::byId(Table::USER, $user->id, $db); printfn('%s deleted successfully', init_cap($displayUser)); } catch (DocumentException $ex) { printfn("$ex"); } } catch (DocumentException $ex) { printfn("$ex"); } finally { $db->close(); } } /** * Change the single-user mode user to a different e-mail address and password */ function migrate_single_user(): void { global $argv; $db = Data::getConnection(); try { if (!$single = User::findByEmail(Security::SINGLE_USER_EMAIL)) { printfn('There is no single-user mode user to be migrated'); return; } Patch::byId(Table::USER, $single->id, ['email' => $argv[2], 'password' => password_hash($argv[3], Security::PW_ALGORITHM)], $db); printfn('The single user has been moved to "%s", with password "%s"', $argv[2], $argv[3]); } catch (DocumentException $ex) { printfn("$ex"); } finally { $db->close(); } }