Skip to content

Commit

Permalink
feat: add emergency logout job for users
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleeym committed Jan 22, 2025
1 parent 8e179f4 commit 51ef628
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@ use clap::{Parser, Subcommand};

#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]
pub struct Args {
struct Args {
#[command(subcommand)]
command: Option<Commands>,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
enum Commands {
/// Run an internal job
#[command(subcommand)]
Job(JobCommand),
}

#[derive(Debug, Subcommand)]
pub enum JobCommand {
enum JobCommand {
/// Cleans up mod_downloads from more than 30 days ago
CleanupDownloads,
/// Cleans up auth and refresh tokens that are expired
CleanupTokens,
/// Emergency logout for a developer
LogoutDeveloper {
/// Username of the developer
username: String,
},
/// Runs migrations
Migrate,
}
Expand All @@ -44,6 +49,12 @@ pub async fn maybe_cli(data: &AppData) -> anyhow::Result<bool> {

Ok(true)
}
JobCommand::LogoutDeveloper { username } => {
let mut conn = data.db().acquire().await?;
jobs::logout_user::logout_user(&username, &mut *conn).await?;

Ok(true)
}
JobCommand::CleanupTokens => {
let mut conn = data.db().acquire().await?;
jobs::token_cleanup::token_cleanup(&mut *conn).await?;
Expand Down
14 changes: 14 additions & 0 deletions src/jobs/logout_user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::database::repository::{auth_tokens, developers, refresh_tokens};
use crate::types::api::ApiError;
use sqlx::PgConnection;

pub async fn logout_user(username: &str, conn: &mut PgConnection) -> Result<(), ApiError> {
let dev = developers::get_one_by_username(username, conn)
.await?
.ok_or(ApiError::NotFound("Developer not found".into()))?;

auth_tokens::remove_developer_tokens(dev.id, conn).await?;
refresh_tokens::remove_developer_tokens(dev.id, conn).await?;

Ok(())
}
1 change: 1 addition & 0 deletions src/jobs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod cleanup_downloads;
pub mod logout_user;
pub mod migrate;
pub mod token_cleanup;

0 comments on commit 51ef628

Please sign in to comment.