server: use the new into_tuple from sea_orm

This commit is contained in:
Valentin Tolmer 2023-02-10 12:43:49 +01:00 committed by nitnelave
parent 63cbf30dd7
commit d04305433f
2 changed files with 8 additions and 18 deletions

View File

@ -8,7 +8,7 @@ use super::{
}; };
use async_trait::async_trait; use async_trait::async_trait;
use lldap_auth::opaque; use lldap_auth::opaque;
use sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait, FromQueryResult, QuerySelect}; use sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait, QuerySelect};
use secstr::SecUtf8; use secstr::SecUtf8;
use tracing::{debug, instrument}; use tracing::{debug, instrument};
@ -50,18 +50,14 @@ impl SqlBackendHandler {
#[instrument(skip_all, level = "debug", err)] #[instrument(skip_all, level = "debug", err)]
async fn get_password_file_for_user(&self, user_id: UserId) -> Result<Option<Vec<u8>>> { async fn get_password_file_for_user(&self, user_id: UserId) -> Result<Option<Vec<u8>>> {
#[derive(FromQueryResult)]
struct OnlyPasswordHash {
password_hash: Option<Vec<u8>>,
}
// Fetch the previously registered password file from the DB. // Fetch the previously registered password file from the DB.
Ok(model::User::find_by_id(user_id) Ok(model::User::find_by_id(user_id)
.select_only() .select_only()
.column(UserColumn::PasswordHash) .column(UserColumn::PasswordHash)
.into_model::<OnlyPasswordHash>() .into_tuple::<(Option<Vec<u8>>,)>()
.one(&self.sql_pool) .one(&self.sql_pool)
.await? .await?
.and_then(|u| u.password_hash)) .and_then(|u| u.0))
} }
} }

View File

@ -8,8 +8,7 @@ use crate::domain::{
use async_trait::async_trait; use async_trait::async_trait;
use sea_orm::{ use sea_orm::{
sea_query::{Cond, Expr}, sea_query::{Cond, Expr},
ActiveModelTrait, ColumnTrait, EntityTrait, FromQueryResult, IntoActiveModel, QueryFilter, ActiveModelTrait, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter, QuerySelect,
QuerySelect,
}; };
use std::collections::HashSet; use std::collections::HashSet;
use tracing::{debug, instrument}; use tracing::{debug, instrument};
@ -24,11 +23,6 @@ fn gen_random_string(len: usize) -> String {
.collect() .collect()
} }
#[derive(FromQueryResult)]
struct OnlyJwtHash {
jwt_hash: i64,
}
#[async_trait] #[async_trait]
impl TcpBackendHandler for SqlBackendHandler { impl TcpBackendHandler for SqlBackendHandler {
#[instrument(skip_all, level = "debug")] #[instrument(skip_all, level = "debug")]
@ -37,11 +31,11 @@ impl TcpBackendHandler for SqlBackendHandler {
.select_only() .select_only()
.column(JwtStorageColumn::JwtHash) .column(JwtStorageColumn::JwtHash)
.filter(JwtStorageColumn::Blacklisted.eq(true)) .filter(JwtStorageColumn::Blacklisted.eq(true))
.into_model::<OnlyJwtHash>() .into_tuple::<(i64,)>()
.all(&self.sql_pool) .all(&self.sql_pool)
.await? .await?
.into_iter() .into_iter()
.map(|m| m.jwt_hash as u64) .map(|m| m.0 as u64)
.collect::<HashSet<u64>>()) .collect::<HashSet<u64>>())
} }
@ -91,11 +85,11 @@ impl TcpBackendHandler for SqlBackendHandler {
.add(JwtStorageColumn::UserId.eq(user)) .add(JwtStorageColumn::UserId.eq(user))
.add(JwtStorageColumn::Blacklisted.eq(false)), .add(JwtStorageColumn::Blacklisted.eq(false)),
) )
.into_model::<OnlyJwtHash>() .into_tuple::<(i64,)>()
.all(&self.sql_pool) .all(&self.sql_pool)
.await? .await?
.into_iter() .into_iter()
.map(|t| t.jwt_hash as u64) .map(|t| t.0 as u64)
.collect::<HashSet<u64>>(); .collect::<HashSet<u64>>();
model::JwtStorage::update_many() model::JwtStorage::update_many()
.col_expr(JwtStorageColumn::Blacklisted, Expr::value(true)) .col_expr(JwtStorageColumn::Blacklisted, Expr::value(true))