You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-08-06 06:02:40 +03:00
Use dynamic filters on compatibility SSO logins
This commit is contained in:
@@ -28,6 +28,7 @@ use url::Url;
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
filter::{Filter, StatementExt},
|
||||||
iden::{CompatSessions, CompatSsoLogins},
|
iden::{CompatSessions, CompatSsoLogins},
|
||||||
pagination::QueryBuilderExt,
|
pagination::QueryBuilderExt,
|
||||||
tracing::ExecuteExt,
|
tracing::ExecuteExt,
|
||||||
@@ -98,6 +99,42 @@ impl TryFrom<CompatSsoLoginLookup> for CompatSsoLogin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Filter for CompatSsoLoginFilter<'_> {
|
||||||
|
fn generate_condition(&self, _has_joins: bool) -> impl sea_query::IntoCondition {
|
||||||
|
sea_query::Condition::all()
|
||||||
|
.add_option(self.user().map(|user| {
|
||||||
|
Expr::exists(
|
||||||
|
Query::select()
|
||||||
|
.expr(Expr::cust("1"))
|
||||||
|
.from(CompatSessions::Table)
|
||||||
|
.and_where(
|
||||||
|
Expr::col((CompatSessions::Table, CompatSessions::UserId))
|
||||||
|
.eq(Uuid::from(user.id)),
|
||||||
|
)
|
||||||
|
.and_where(
|
||||||
|
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::CompatSessionId))
|
||||||
|
.equals((CompatSessions::Table, CompatSessions::CompatSessionId)),
|
||||||
|
)
|
||||||
|
.take(),
|
||||||
|
)
|
||||||
|
}))
|
||||||
|
.add_option(self.state().map(|state| {
|
||||||
|
if state.is_exchanged() {
|
||||||
|
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::ExchangedAt)).is_not_null()
|
||||||
|
} else if state.is_fulfilled() {
|
||||||
|
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::FulfilledAt))
|
||||||
|
.is_not_null()
|
||||||
|
.and(
|
||||||
|
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::ExchangedAt))
|
||||||
|
.is_null(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::FulfilledAt)).is_null()
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<'c> CompatSsoLoginRepository for PgCompatSsoLoginRepository<'c> {
|
impl<'c> CompatSsoLoginRepository for PgCompatSsoLoginRepository<'c> {
|
||||||
type Error = DatabaseError;
|
type Error = DatabaseError;
|
||||||
@@ -384,36 +421,7 @@ impl<'c> CompatSsoLoginRepository for PgCompatSsoLoginRepository<'c> {
|
|||||||
CompatSsoLoginLookupIden::ExchangedAt,
|
CompatSsoLoginLookupIden::ExchangedAt,
|
||||||
)
|
)
|
||||||
.from(CompatSsoLogins::Table)
|
.from(CompatSsoLogins::Table)
|
||||||
.and_where_option(filter.user().map(|user| {
|
.apply_filter(filter)
|
||||||
Expr::exists(
|
|
||||||
Query::select()
|
|
||||||
.expr(Expr::cust("1"))
|
|
||||||
.from(CompatSessions::Table)
|
|
||||||
.and_where(
|
|
||||||
Expr::col((CompatSessions::Table, CompatSessions::UserId))
|
|
||||||
.eq(Uuid::from(user.id)),
|
|
||||||
)
|
|
||||||
.and_where(
|
|
||||||
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::CompatSessionId))
|
|
||||||
.equals((CompatSessions::Table, CompatSessions::CompatSessionId)),
|
|
||||||
)
|
|
||||||
.take(),
|
|
||||||
)
|
|
||||||
}))
|
|
||||||
.and_where_option(filter.state().map(|state| {
|
|
||||||
if state.is_exchanged() {
|
|
||||||
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::ExchangedAt)).is_not_null()
|
|
||||||
} else if state.is_fulfilled() {
|
|
||||||
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::FulfilledAt))
|
|
||||||
.is_not_null()
|
|
||||||
.and(
|
|
||||||
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::ExchangedAt))
|
|
||||||
.is_null(),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::FulfilledAt)).is_null()
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
.generate_pagination(
|
.generate_pagination(
|
||||||
(CompatSsoLogins::Table, CompatSsoLogins::CompatSsoLoginId),
|
(CompatSsoLogins::Table, CompatSsoLogins::CompatSsoLoginId),
|
||||||
pagination,
|
pagination,
|
||||||
@@ -442,36 +450,7 @@ impl<'c> CompatSsoLoginRepository for PgCompatSsoLoginRepository<'c> {
|
|||||||
let (sql, arguments) = Query::select()
|
let (sql, arguments) = Query::select()
|
||||||
.expr(Expr::col((CompatSsoLogins::Table, CompatSsoLogins::CompatSsoLoginId)).count())
|
.expr(Expr::col((CompatSsoLogins::Table, CompatSsoLogins::CompatSsoLoginId)).count())
|
||||||
.from(CompatSsoLogins::Table)
|
.from(CompatSsoLogins::Table)
|
||||||
.and_where_option(filter.user().map(|user| {
|
.apply_filter(filter)
|
||||||
Expr::exists(
|
|
||||||
Query::select()
|
|
||||||
.expr(Expr::cust("1"))
|
|
||||||
.from(CompatSessions::Table)
|
|
||||||
.and_where(
|
|
||||||
Expr::col((CompatSessions::Table, CompatSessions::UserId))
|
|
||||||
.eq(Uuid::from(user.id)),
|
|
||||||
)
|
|
||||||
.and_where(
|
|
||||||
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::CompatSessionId))
|
|
||||||
.equals((CompatSessions::Table, CompatSessions::CompatSessionId)),
|
|
||||||
)
|
|
||||||
.take(),
|
|
||||||
)
|
|
||||||
}))
|
|
||||||
.and_where_option(filter.state().map(|state| {
|
|
||||||
if state.is_exchanged() {
|
|
||||||
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::ExchangedAt)).is_not_null()
|
|
||||||
} else if state.is_fulfilled() {
|
|
||||||
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::FulfilledAt))
|
|
||||||
.is_not_null()
|
|
||||||
.and(
|
|
||||||
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::ExchangedAt))
|
|
||||||
.is_null(),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
Expr::col((CompatSsoLogins::Table, CompatSsoLogins::FulfilledAt)).is_null()
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
.build_sqlx(PostgresQueryBuilder);
|
.build_sqlx(PostgresQueryBuilder);
|
||||||
|
|
||||||
let count: i64 = sqlx::query_scalar_with(&sql, arguments)
|
let count: i64 = sqlx::query_scalar_with(&sql, arguments)
|
||||||
|
Reference in New Issue
Block a user