Skip to content

Commit

Permalink
馃悰 Fix no server owner after migration (#188)
Browse files Browse the repository at this point in the history
* add CLI command for resetting server owner

* clean up sessions after changing server owner
  • Loading branch information
aaronleopold committed Nov 4, 2023
1 parent c9571a3 commit 846df2b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 18 deletions.
85 changes: 84 additions & 1 deletion crates/cli/src/commands/account.rs
@@ -1,7 +1,7 @@
use std::{thread, time::Duration};

use clap::Subcommand;
use dialoguer::{theme::ColorfulTheme, Password};
use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password};
use stump_core::{
db::create_client,
prisma::{session, user},
Expand Down Expand Up @@ -38,6 +38,8 @@ pub enum Account {
#[clap(long)]
username: String,
},
/// Enter a flow to change the server owner to another account
ResetOwner,
}

pub async fn handle_account_command(
Expand All @@ -51,6 +53,7 @@ pub async fn handle_account_command(
Account::ResetPassword { username } => {
reset_account_password(username, config.password_hash_cost).await
},
Account::ResetOwner => change_server_owner().await,
}
}

Expand Down Expand Up @@ -168,3 +171,83 @@ async fn print_accounts(locked: Option<bool>) -> CliResult<()> {

Ok(())
}

async fn change_server_owner() -> CliResult<()> {
let client = create_client().await;

let all_accounts = client
.user()
.find_many(vec![user::is_locked::equals(false)])
.exec()
.await?;
let current_server_owner = all_accounts
.iter()
.find(|user| user.is_server_owner)
.cloned();

let username = Input::new()
.with_prompt("Enter the username of the account to assign as server owner")
.allow_empty(false)
.validate_with(|input: &String| -> Result<(), &str> {
let existing_user = all_accounts.iter().find(|user| user.username == *input);
if existing_user.is_some() {
Ok(())
} else {
Err("An account with that username does not exist or their account is locked")
}
})
.interact_text()?;

let confirmation = Confirm::new()
.with_prompt("Are you sure you want to continue?")
.interact()?;

if !confirmation {
println!("Exiting...");
return Ok(());
}

let target_user = all_accounts
.into_iter()
.find(|user| user.username == username)
.ok_or(CliError::OperationFailed(
"Failed to reconcile users after validation".to_string(),
))?;

let progress = default_progress_spinner();
if let Some(user) = current_server_owner {
progress.set_message(format!("Removing owner status from {}", user.username));
client
.user()
.update(
user::id::equals(user.id.clone()),
vec![user::is_server_owner::set(false)],
)
.exec()
.await?;
client
.session()
.delete_many(vec![session::user_id::equals(user.id)])
.exec()
.await?;
}

progress.set_message(format!("Setting owner status for {}", target_user.username));
client
.user()
.update(
user::id::equals(target_user.id.clone()),
vec![user::is_server_owner::set(true)],
)
.exec()
.await?;
client
.session()
.delete_many(vec![session::user_id::equals(target_user.id)])
.exec()
.await?;

progress.finish_with_message("Successfully changed the server owner!");

Ok(())
}
15 changes: 0 additions & 15 deletions crates/cli/src/lib.rs
Expand Up @@ -20,18 +20,3 @@ pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,
}

// # start the server
// $ ./server

// # unlock an account
// $ ./server account lock --username <username>

// # freeze an account
// $ ./server account unlock --username <username>

// # list all frozen accounts
// $ ./server account list --locked

// # reset password for a user
// $ ./server account reset-password --username <username>
2 changes: 1 addition & 1 deletion packages/interface/src/AppRouter.tsx
Expand Up @@ -27,7 +27,7 @@ const ServerConnectionErrorScene = lazily(
)
const LoginOrClaimScene = lazily(() => import('./scenes/auth/LoginOrClaimScene.tsx'))

const IS_DEVELOPMENT = import.meta.env.DEV
const IS_DEVELOPMENT = import.meta.env.MODE === 'development'

export function AppRouter() {
const appProps = useAppProps()
Expand Down
5 changes: 4 additions & 1 deletion packages/interface/src/components/ApplicationVersion.tsx
Expand Up @@ -19,6 +19,8 @@ export default function ApplicationVersion() {
}
}, [version])

if (!version) return null

return (
<Link
href={url}
Expand All @@ -28,7 +30,8 @@ export default function ApplicationVersion() {
underline={false}
>
<span>
v{version?.semver} - {version?.rev}
v{version.semver}
{!!version.rev && ` - ${version.rev}`}
</span>
</Link>
)
Expand Down

0 comments on commit 846df2b

Please sign in to comment.