Skip to content

Commit 51ef628

Browse files
committed
feat: add emergency logout job for users
1 parent 8e179f4 commit 51ef628

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/cli/mod.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@ use clap::{Parser, Subcommand};
44

55
#[derive(Debug, Parser)]
66
#[command(version, about, long_about = None)]
7-
pub struct Args {
7+
struct Args {
88
#[command(subcommand)]
99
command: Option<Commands>,
1010
}
1111

1212
#[derive(Subcommand, Debug)]
13-
pub enum Commands {
13+
enum Commands {
1414
/// Run an internal job
1515
#[command(subcommand)]
1616
Job(JobCommand),
1717
}
1818

1919
#[derive(Debug, Subcommand)]
20-
pub enum JobCommand {
20+
enum JobCommand {
2121
/// Cleans up mod_downloads from more than 30 days ago
2222
CleanupDownloads,
2323
/// Cleans up auth and refresh tokens that are expired
2424
CleanupTokens,
25+
/// Emergency logout for a developer
26+
LogoutDeveloper {
27+
/// Username of the developer
28+
username: String,
29+
},
2530
/// Runs migrations
2631
Migrate,
2732
}
@@ -44,6 +49,12 @@ pub async fn maybe_cli(data: &AppData) -> anyhow::Result<bool> {
4449

4550
Ok(true)
4651
}
52+
JobCommand::LogoutDeveloper { username } => {
53+
let mut conn = data.db().acquire().await?;
54+
jobs::logout_user::logout_user(&username, &mut *conn).await?;
55+
56+
Ok(true)
57+
}
4758
JobCommand::CleanupTokens => {
4859
let mut conn = data.db().acquire().await?;
4960
jobs::token_cleanup::token_cleanup(&mut *conn).await?;

src/jobs/logout_user.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use crate::database::repository::{auth_tokens, developers, refresh_tokens};
2+
use crate::types::api::ApiError;
3+
use sqlx::PgConnection;
4+
5+
pub async fn logout_user(username: &str, conn: &mut PgConnection) -> Result<(), ApiError> {
6+
let dev = developers::get_one_by_username(username, conn)
7+
.await?
8+
.ok_or(ApiError::NotFound("Developer not found".into()))?;
9+
10+
auth_tokens::remove_developer_tokens(dev.id, conn).await?;
11+
refresh_tokens::remove_developer_tokens(dev.id, conn).await?;
12+
13+
Ok(())
14+
}

src/jobs/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod cleanup_downloads;
2+
pub mod logout_user;
23
pub mod migrate;
34
pub mod token_cleanup;

0 commit comments

Comments
 (0)