Skip to content

Commit 917f4d1

Browse files
committed
Allow filtering OAuth sessions with any/no user
1 parent 423681d commit 917f4d1

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

crates/storage-pg/src/oauth2/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ mod tests {
525525
let pagination = Pagination::first(10);
526526

527527
// First, list all the sessions
528-
let filter = OAuth2SessionFilter::new();
528+
let filter = OAuth2SessionFilter::new().for_any_user();
529529
let list = repo
530530
.oauth2_session()
531531
.list(filter, pagination)

crates/storage-pg/src/oauth2/session.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ impl Filter for OAuth2SessionFilter<'_> {
125125
let scope: Vec<String> = scope.iter().map(|s| s.as_str().to_owned()).collect();
126126
Expr::col((OAuth2Sessions::Table, OAuth2Sessions::ScopeList)).contains(scope)
127127
}))
128+
.add_option(self.any_user().map(|any_user| {
129+
if any_user {
130+
Expr::col((OAuth2Sessions::Table, OAuth2Sessions::UserId)).is_not_null()
131+
} else {
132+
Expr::col((OAuth2Sessions::Table, OAuth2Sessions::UserId)).is_null()
133+
}
134+
}))
128135
.add_option(self.last_active_after().map(|last_active_after| {
129136
Expr::col((OAuth2Sessions::Table, OAuth2Sessions::LastActiveAt))
130137
.gt(last_active_after)

crates/storage/src/oauth2/session.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ impl OAuth2SessionState {
3535
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
3636
pub struct OAuth2SessionFilter<'a> {
3737
user: Option<&'a User>,
38+
any_user: Option<bool>,
3839
browser_session: Option<&'a BrowserSession>,
3940
device: Option<&'a Device>,
4041
client: Option<&'a Client>,
@@ -66,6 +67,28 @@ impl<'a> OAuth2SessionFilter<'a> {
6667
self.user
6768
}
6869

70+
/// List sessions which belong to any user
71+
#[must_use]
72+
pub fn for_any_user(mut self) -> Self {
73+
self.any_user = Some(true);
74+
self
75+
}
76+
77+
/// List sessions which belong to no user
78+
#[must_use]
79+
pub fn for_no_user(mut self) -> Self {
80+
self.any_user = Some(false);
81+
self
82+
}
83+
84+
/// Get the 'any user' filter
85+
///
86+
/// Returns [`None`] if no 'any user' filter was set
87+
#[must_use]
88+
pub fn any_user(&self) -> Option<bool> {
89+
self.any_user
90+
}
91+
6992
/// List sessions started by a specific browser session
7093
#[must_use]
7194
pub fn for_browser_session(mut self, browser_session: &'a BrowserSession) -> Self {

0 commit comments

Comments
 (0)