Add single-user handling (#9)
- Disallow log on for single-user mode user - Improve CLI header display
This commit is contained in:
@@ -47,13 +47,17 @@ switch ($argv[1]) {
|
||||
printfn('Missing parameters: delete-user requires e-mail address');
|
||||
exit(-1);
|
||||
}
|
||||
delete_user();
|
||||
delete_user($argv[2]);
|
||||
break;
|
||||
case 'migrate-single-user':
|
||||
printfn('TODO: single-user migration');
|
||||
if ($argc < 4) {
|
||||
printfn('Missing parameters: migrate-single-user requires e-mail and password');
|
||||
exit(-1);
|
||||
}
|
||||
migrate_single_user();
|
||||
break;
|
||||
case 'remove-single-user':
|
||||
printfn('TODO: single-user removal');
|
||||
delete_user(Security::SINGLE_USER_EMAIL);
|
||||
break;
|
||||
default:
|
||||
printfn('Unrecognized option "%s"', $argv[1]);
|
||||
@@ -70,14 +74,8 @@ function add_user(): void {
|
||||
|
||||
try {
|
||||
// Ensure there is not already a user with this e-mail address
|
||||
$existsQuery = $db->prepare('SELECT COUNT(*) FROM frc_user WHERE email = :email');
|
||||
$existsQuery->bindValue(':email', $argv[2]);
|
||||
$existsResult = $existsQuery->execute();
|
||||
if (!$existsResult) {
|
||||
printfn('SQLite error: %s', $db->lastErrorMsg());
|
||||
return;
|
||||
}
|
||||
if ($existsResult->fetchArray(SQLITE3_NUM)[0] != 0) {
|
||||
$user = Security::findUserByEmail($argv[2], $db);
|
||||
if ($user) {
|
||||
printfn('A user with e-mail address "%s" already exists', $argv[2]);
|
||||
return;
|
||||
}
|
||||
@@ -100,14 +98,8 @@ function set_password(): void {
|
||||
|
||||
try {
|
||||
// Ensure this user exists
|
||||
$existsQuery = $db->prepare('SELECT COUNT(*) FROM frc_user WHERE email = :email');
|
||||
$existsQuery->bindValue(':email', $argv[2]);
|
||||
$existsResult = $existsQuery->execute();
|
||||
if (!$existsResult) {
|
||||
printfn('SQLite error: %s', $db->lastErrorMsg());
|
||||
return;
|
||||
}
|
||||
if ($existsResult->fetchArray(SQLITE3_NUM)[0] == 0) {
|
||||
$user = Security::findUserByEmail($argv[2], $db);
|
||||
if (!$user) {
|
||||
printfn('No user exists with e-mail address "%s"', $argv[2]);
|
||||
return;
|
||||
}
|
||||
@@ -122,29 +114,25 @@ function set_password(): void {
|
||||
|
||||
/**
|
||||
* Delete a user
|
||||
*
|
||||
* @param string $email The e-mail address of the user to be deleted
|
||||
*/
|
||||
function delete_user(): void {
|
||||
global $argv;
|
||||
function delete_user(string $email): void {
|
||||
|
||||
$db = Data::getConnection();
|
||||
|
||||
try {
|
||||
$displayUser = $email == Security::SINGLE_USER_EMAIL ? 'single-user mode user' : "user \"$email\"";
|
||||
|
||||
// Get the ID for the provided e-mail address
|
||||
$idQuery = $db->prepare('SELECT id FROM frc_user WHERE email = :email');
|
||||
$idQuery->bindValue(':email', $argv[2]);
|
||||
$idResult = $idQuery->execute();
|
||||
if (!$idResult) {
|
||||
printfn('SQLite error: %s', $db->lastErrorMsg());
|
||||
return;
|
||||
}
|
||||
$id = $idResult->fetchArray(SQLITE3_NUM);
|
||||
if (!$id) {
|
||||
printfn('No user exists with e-mail address "%s"', $argv[2]);
|
||||
$user = Security::findUserByEmail($email, $db);
|
||||
if (!$user) {
|
||||
printfn('No %s exists', $displayUser);
|
||||
return;
|
||||
}
|
||||
|
||||
$feedCountQuery = $db->prepare('SELECT COUNT(*) FROM feed WHERE user_id = :user');
|
||||
$feedCountQuery->bindValue(':user', $id[0]);
|
||||
$feedCountQuery->bindValue(':user', $user['id']);
|
||||
$feedCountResult = $feedCountQuery->execute();
|
||||
if (!$feedCountResult) {
|
||||
printfn('SQLite error: %s', $db->lastErrorMsg());
|
||||
@@ -152,25 +140,52 @@ function delete_user(): void {
|
||||
}
|
||||
$feedCount = $feedCountResult->fetchArray(SQLITE3_NUM);
|
||||
|
||||
$proceed = readline("Delete user \"$argv[2]\" and their $feedCount[0] feed(s)? (y/N)" . PHP_EOL);
|
||||
$proceed = readline("Delete the $displayUser and their $feedCount[0] feed(s)? (y/N)" . PHP_EOL);
|
||||
if (!$proceed || !str_starts_with(strtolower($proceed), 'y')) {
|
||||
printfn('Deletion canceled');
|
||||
return;
|
||||
}
|
||||
|
||||
$itemDelete = $db->prepare('DELETE FROM item WHERE feed_id IN (SELECT id FROM feed WHERE user_id = :user)');
|
||||
$itemDelete->bindValue(':user', $id[0]);
|
||||
$itemDelete->bindValue(':user', $user['id']);
|
||||
$itemDelete->execute();
|
||||
|
||||
$feedDelete = $db->prepare('DELETE FROM feed WHERE user_id = :user');
|
||||
$feedDelete->bindValue(':user', $id[0]);
|
||||
$feedDelete->bindValue(':user', $user['id']);
|
||||
$feedDelete->execute();
|
||||
|
||||
$userDelete = $db->prepare('DELETE FROM frc_user WHERE id = :user');
|
||||
$userDelete->bindValue(':user', $id[0]);
|
||||
$userDelete->bindValue(':user', $user['id']);
|
||||
$userDelete->execute();
|
||||
|
||||
printfn('User "%s" deleted successfully', $argv[2]);
|
||||
printfn(strtoupper(substr($displayUser, 0, 1)) . substr($displayUser, 1) . ' deleted successfully');
|
||||
} 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 {
|
||||
$single = Security::findUserByEmail(Security::SINGLE_USER_EMAIL, $db);
|
||||
if (!$single) {
|
||||
printfn('There is no single-user mode user to be migrated');
|
||||
return;
|
||||
}
|
||||
|
||||
$migrateQuery = $db->prepare('UPDATE frc_user SET email = :email, password = :password WHERE id = :id');
|
||||
$migrateQuery->bindValue(':email', $argv[2]);
|
||||
$migrateQuery->bindValue(':password', password_hash($argv[3], Security::PW_ALGORITHM));
|
||||
$migrateQuery->bindValue(':id', $single['id']);
|
||||
$migrateQuery->execute();
|
||||
|
||||
printfn('The single user has been moved to "%s", with password "%s"', $argv[2], $argv[3]);
|
||||
} finally {
|
||||
$db->close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user