Add expires filter to personal sessions list

This commit is contained in:
Olivier 'reivilibre
2025-10-21 10:10:14 +01:00
parent 2bf837257c
commit a8adab1301
3 changed files with 46 additions and 0 deletions

View File

@@ -75,6 +75,10 @@ pub struct FilterParams {
/// Filter by access token expiry date /// Filter by access token expiry date
#[serde(rename = "filter[expires_after]")] #[serde(rename = "filter[expires_after]")]
expires_after: Option<DateTime<Utc>>, expires_after: Option<DateTime<Utc>>,
/// Filter by whether the access token has an expiry time
#[serde(rename = "filter[expires]")]
expires: Option<bool>,
} }
impl std::fmt::Display for FilterParams { impl std::fmt::Display for FilterParams {
@@ -113,6 +117,10 @@ impl std::fmt::Display for FilterParams {
)?; )?;
sep = '&'; sep = '&';
} }
if let Some(expires) = self.expires {
write!(f, "{sep}filter[expires]={}", expires)?;
sep = '&';
}
let _ = sep; let _ = sep;
Ok(()) Ok(())
@@ -270,6 +278,12 @@ pub async fn handler(
filter filter
}; };
let filter = if let Some(expires) = params.expires {
filter.with_expires(expires)
} else {
filter
};
let response = match include_count { let response = match include_count {
IncludeCount::True => { IncludeCount::True => {
let page = repo.personal_session().list(filter, pagination).await?; let page = repo.personal_session().list(filter, pagination).await?;
@@ -514,6 +528,11 @@ mod tests {
&["01FSHN9AG0YQYAR04VCYTHJ8SK", "01FSPT2RG08Y11Y5BM4VZ4CN8K"], &["01FSHN9AG0YQYAR04VCYTHJ8SK", "01FSPT2RG08Y11Y5BM4VZ4CN8K"],
), ),
("filter[status]=revoked", &["01FSM7P1G0VBGAMK9D9QMGQ5MY"]), ("filter[status]=revoked", &["01FSM7P1G0VBGAMK9D9QMGQ5MY"]),
(
"filter[expires]=true",
&["01FSHN9AG0YQYAR04VCYTHJ8SK", "01FSPT2RG08Y11Y5BM4VZ4CN8K"],
),
("filter[expires]=false", &["01FSM7P1G0VBGAMK9D9QMGQ5MY"]),
]; ];
for (filter, expected_ids) in filters_and_expected { for (filter, expected_ids) in filters_and_expected {

View File

@@ -567,5 +567,15 @@ impl Filter for PersonalSessionFilter<'_> {
Expr::col((PersonalAccessTokens::Table, PersonalAccessTokens::ExpiresAt)) Expr::col((PersonalAccessTokens::Table, PersonalAccessTokens::ExpiresAt))
.gt(expires_after) .gt(expires_after)
})) }))
.add_option(self.expires().map(|expires| {
let column =
Expr::col((PersonalAccessTokens::Table, PersonalAccessTokens::ExpiresAt));
if expires {
column.is_not_null()
} else {
column.is_null()
}
}))
} }
} }

View File

@@ -156,6 +156,7 @@ pub struct PersonalSessionFilter<'a> {
last_active_after: Option<DateTime<Utc>>, last_active_after: Option<DateTime<Utc>>,
expires_before: Option<DateTime<Utc>>, expires_before: Option<DateTime<Utc>>,
expires_after: Option<DateTime<Utc>>, expires_after: Option<DateTime<Utc>>,
expires: Option<bool>,
} }
/// Filter for what state a personal session is in. /// Filter for what state a personal session is in.
@@ -332,4 +333,20 @@ impl<'a> PersonalSessionFilter<'a> {
pub fn expires_after(&self) -> Option<DateTime<Utc>> { pub fn expires_after(&self) -> Option<DateTime<Utc>> {
self.expires_after self.expires_after
} }
/// Only return sessions whose access tokens have, or don't have,
/// an expiry time set
#[must_use]
pub fn with_expires(mut self, expires: bool) -> Self {
self.expires = Some(expires);
self
}
/// Get the expires filter
///
/// Returns [`None`] if no expires filter was set
#[must_use]
pub fn expires(&self) -> Option<bool> {
self.expires
}
} }